From 6a4064d05cc5d34c51b0a204b9916ca7957a32f4 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Sun, 17 Dec 2017 12:54:25 +0200 Subject: [PATCH] Minor improvement and refactoring --- .../java/org/pf4j/AbstractPluginManager.java | 4 +- .../pf4j/ManifestPluginDescriptorFinder.java | 6 +-- .../PropertiesPluginDescriptorFinder.java | 6 +-- .../main/java/org/pf4j/util/FileUtils.java | 8 ++-- .../main/java/org/pf4j/util/StringUtils.java | 42 +++++++++++++++++-- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java b/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java index ea94432..21df9c0 100644 --- a/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java +++ b/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"); } diff --git a/pf4j/src/main/java/org/pf4j/ManifestPluginDescriptorFinder.java b/pf4j/src/main/java/org/pf4j/ManifestPluginDescriptorFinder.java index 1e09067..e2bac20 100644 --- a/pf4j/src/main/java/org/pf4j/ManifestPluginDescriptorFinder.java +++ b/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); } diff --git a/pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java b/pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java index 9357bc3..80026b9 100644 --- a/pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java +++ b/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); } diff --git a/pf4j/src/main/java/org/pf4j/util/FileUtils.java b/pf4j/src/main/java/org/pf4j/util/FileUtils.java index 0c54e0a..6da79e0 100644 --- a/pf4j/src/main/java/org/pf4j/util/FileUtils.java +++ b/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 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); } diff --git a/pf4j/src/main/java/org/pf4j/util/StringUtils.java b/pf4j/src/main/java/org/pf4j/util/StringUtils.java index 49657d5..04fc4ec 100644 --- a/pf4j/src/main/java/org/pf4j/util/StringUtils.java +++ b/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); } + /** + *

Adds a substring only if the source string does not already start with the substring, + * otherwise returns the source string.

+ *

+ *

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.

+ *

+ *

+     * StringUtils.addStart(null, *)      = *
+     * StringUtils.addStart("", *)        = *
+     * StringUtils.addStart(*, null)      = *
+     * StringUtils.addStart("domain.com", "www.")  = "www.domain.com"
+     * StringUtils.addStart("abc123", "abc")    = "abc123"
+     * 
+ * + * @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; + } + }