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 * @throws PluginException if validation fails
*/ */
protected void validatePluginDescriptor(PluginDescriptor descriptor) throws PluginException { 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"); 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"); 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); pluginDescriptor.setPluginId(id);
String description = attributes.getValue("Plugin-Description"); String description = attributes.getValue("Plugin-Description");
if (StringUtils.isEmpty(description)) { if (StringUtils.isNullOrEmpty(description)) {
pluginDescriptor.setPluginDescription(""); pluginDescriptor.setPluginDescription("");
} else { } else {
pluginDescriptor.setPluginDescription(description); pluginDescriptor.setPluginDescription(description);
@ -106,7 +106,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder {
pluginDescriptor.setPluginClass(clazz); pluginDescriptor.setPluginClass(clazz);
String version = attributes.getValue("Plugin-Version"); String version = attributes.getValue("Plugin-Version");
if (StringUtils.isNotEmpty(version)) { if (StringUtils.isNotNullOrEmpty(version)) {
pluginDescriptor.setPluginVersion(version); pluginDescriptor.setPluginVersion(version);
} }
@ -116,7 +116,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder {
pluginDescriptor.setDependencies(dependencies); pluginDescriptor.setDependencies(dependencies);
String requires = attributes.getValue("Plugin-Requires"); String requires = attributes.getValue("Plugin-Requires");
if (StringUtils.isNotEmpty(requires)) { if (StringUtils.isNotNullOrEmpty(requires)) {
pluginDescriptor.setRequires(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); pluginDescriptor.setPluginId(id);
String description = properties.getProperty("plugin.description"); String description = properties.getProperty("plugin.description");
if (StringUtils.isEmpty(description)) { if (StringUtils.isNullOrEmpty(description)) {
pluginDescriptor.setPluginDescription(""); pluginDescriptor.setPluginDescription("");
} else { } else {
pluginDescriptor.setPluginDescription(description); pluginDescriptor.setPluginDescription(description);
@ -112,7 +112,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
pluginDescriptor.setPluginClass(clazz); pluginDescriptor.setPluginClass(clazz);
String version = properties.getProperty("plugin.version"); String version = properties.getProperty("plugin.version");
if (StringUtils.isNotEmpty(version)) { if (StringUtils.isNotNullOrEmpty(version)) {
pluginDescriptor.setPluginVersion(version); pluginDescriptor.setPluginVersion(version);
} }
@ -123,7 +123,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
pluginDescriptor.setDependencies(dependencies); pluginDescriptor.setDependencies(dependencies);
String requires = properties.getProperty("plugin.requires"); String requires = properties.getProperty("plugin.requires");
if (StringUtils.isNotEmpty(requires)) { if (StringUtils.isNotNullOrEmpty(requires)) {
pluginDescriptor.setRequires(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 { public class FileUtils {
private static final String SLASH = "/";
private static final Logger log = LoggerFactory.getLogger(FileUtils.class); private static final Logger log = LoggerFactory.getLogger(FileUtils.class);
public static List<String> readLines(Path path, boolean ignoreComments) throws IOException { 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 { public static Path getPath(Path path, String first, String... more) throws IOException {
URI uri = path.toUri(); URI uri = path.toUri();
if (isJarFile(path)) { if (isJarFile(path)) {
String pathString = path.toString().replace("\\", SLASH); String pathString = path.toString();
if(!pathString.startsWith(SLASH)){ // transformation for Windows OS
pathString = SLASH + pathString; pathString = StringUtils.addStart(pathString.replace("\\", "/"), "/");
}
uri = URI.create("jar:file:" + pathString); 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 class StringUtils {
public static boolean isEmpty(String str) { public static boolean isNullOrEmpty(String str) {
return (str == null) || str.isEmpty(); return (str == null) || str.isEmpty();
} }
public static boolean isNotEmpty(String str) { public static boolean isNotNullOrEmpty(String str) {
return !isEmpty(str); return !isNullOrEmpty(str);
} }
/** /**
@ -37,4 +37,40 @@ public class StringUtils {
return String.format(str, args); 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