Browse Source

Minor improvement and refactoring

pull/191/head
Decebal Suiu 7 years ago
parent
commit
6a4064d05c
  1. 4
      pf4j/src/main/java/org/pf4j/AbstractPluginManager.java
  2. 6
      pf4j/src/main/java/org/pf4j/ManifestPluginDescriptorFinder.java
  3. 6
      pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java
  4. 8
      pf4j/src/main/java/org/pf4j/util/FileUtils.java
  5. 42
      pf4j/src/main/java/org/pf4j/util/StringUtils.java

4
pf4j/src/main/java/org/pf4j/AbstractPluginManager.java

@ -849,11 +849,11 @@ public abstract class AbstractPluginManager implements PluginManager {
* @throws PluginException if validation fails
*/
protected void validatePluginDescriptor(PluginDescriptor descriptor) throws PluginException {
if (StringUtils.isEmpty(descriptor.getPluginId())) {
if (StringUtils.isNullOrEmpty(descriptor.getPluginId())) {
throw new PluginException("Field 'id' cannot be empty");
}
if (StringUtils.isEmpty(descriptor.getPluginClass())) {
if (StringUtils.isNullOrEmpty(descriptor.getPluginClass())) {
throw new PluginException("Field 'class' cannot be empty");
}

6
pf4j/src/main/java/org/pf4j/ManifestPluginDescriptorFinder.java

@ -96,7 +96,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder {
pluginDescriptor.setPluginId(id);
String description = attributes.getValue("Plugin-Description");
if (StringUtils.isEmpty(description)) {
if (StringUtils.isNullOrEmpty(description)) {
pluginDescriptor.setPluginDescription("");
} else {
pluginDescriptor.setPluginDescription(description);
@ -106,7 +106,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder {
pluginDescriptor.setPluginClass(clazz);
String version = attributes.getValue("Plugin-Version");
if (StringUtils.isNotEmpty(version)) {
if (StringUtils.isNotNullOrEmpty(version)) {
pluginDescriptor.setPluginVersion(version);
}
@ -116,7 +116,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder {
pluginDescriptor.setDependencies(dependencies);
String requires = attributes.getValue("Plugin-Requires");
if (StringUtils.isNotEmpty(requires)) {
if (StringUtils.isNotNullOrEmpty(requires)) {
pluginDescriptor.setRequires(requires);
}

6
pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java

@ -102,7 +102,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
pluginDescriptor.setPluginId(id);
String description = properties.getProperty("plugin.description");
if (StringUtils.isEmpty(description)) {
if (StringUtils.isNullOrEmpty(description)) {
pluginDescriptor.setPluginDescription("");
} else {
pluginDescriptor.setPluginDescription(description);
@ -112,7 +112,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
pluginDescriptor.setPluginClass(clazz);
String version = properties.getProperty("plugin.version");
if (StringUtils.isNotEmpty(version)) {
if (StringUtils.isNotNullOrEmpty(version)) {
pluginDescriptor.setPluginVersion(version);
}
@ -123,7 +123,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
pluginDescriptor.setDependencies(dependencies);
String requires = properties.getProperty("plugin.requires");
if (StringUtils.isNotEmpty(requires)) {
if (StringUtils.isNotNullOrEmpty(requires)) {
pluginDescriptor.setRequires(requires);
}

8
pf4j/src/main/java/org/pf4j/util/FileUtils.java

@ -43,7 +43,6 @@ import java.util.List;
*/
public class FileUtils {
private static final String SLASH = "/";
private static final Logger log = LoggerFactory.getLogger(FileUtils.class);
public static List<String> readLines(Path path, boolean ignoreComments) throws IOException {
@ -217,10 +216,9 @@ public class FileUtils {
public static Path getPath(Path path, String first, String... more) throws IOException {
URI uri = path.toUri();
if (isJarFile(path)) {
String pathString = path.toString().replace("\\", SLASH);
if(!pathString.startsWith(SLASH)){
pathString = SLASH + pathString;
}
String pathString = path.toString();
// transformation for Windows OS
pathString = StringUtils.addStart(pathString.replace("\\", "/"), "/");
uri = URI.create("jar:file:" + pathString);
}

42
pf4j/src/main/java/org/pf4j/util/StringUtils.java

@ -20,12 +20,12 @@ package org.pf4j.util;
*/
public class StringUtils {
public static boolean isEmpty(String str) {
public static boolean isNullOrEmpty(String str) {
return (str == null) || str.isEmpty();
}
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
public static boolean isNotNullOrEmpty(String str) {
return !isNullOrEmpty(str);
}
/**
@ -37,4 +37,40 @@ public class StringUtils {
return String.format(str, args);
}
/**
* <p>Adds a substring only if the source string does not already start with the substring,
* otherwise returns the source string.</p>
* <p/>
* <p>A {@code null} source string will return {@code null}.
* An empty ("") source string will return the empty string.
* A {@code null} search string will return the source string.</p>
* <p/>
* <pre>
* StringUtils.addStart(null, *) = *
* StringUtils.addStart("", *) = *
* StringUtils.addStart(*, null) = *
* StringUtils.addStart("domain.com", "www.") = "www.domain.com"
* StringUtils.addStart("abc123", "abc") = "abc123"
* </pre>
*
* @param str the source String to search, may be null
* @param add the String to search for and add, may be null
* @return the substring with the string added if required
*/
public static String addStart(String str, String add) {
if (isNullOrEmpty(add)) {
return str;
}
if (isNullOrEmpty(str)) {
return add;
}
if (!str.startsWith(add)) {
return add + str;
}
return str;
}
}

Loading…
Cancel
Save