From f3fe919089e65e43313f1219291c39fd1b245663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Franco?= Date: Fri, 5 Jun 2015 14:31:31 +0100 Subject: [PATCH] Replace Version with semver lib --- pf4j/pom.xml | 27 +-- .../fortsoft/pf4j/DefaultPluginManager.java | 10 +- .../pf4j/ManifestPluginDescriptorFinder.java | 5 +- .../ro/fortsoft/pf4j/PluginDependency.java | 56 +++--- .../ro/fortsoft/pf4j/PluginDescriptor.java | 18 +- .../java/ro/fortsoft/pf4j/PluginManager.java | 1 + .../PropertiesPluginDescriptorFinder.java | 3 +- .../main/java/ro/fortsoft/pf4j/Version.java | 163 ------------------ 8 files changed, 70 insertions(+), 213 deletions(-) delete mode 100644 pf4j/src/main/java/ro/fortsoft/pf4j/Version.java diff --git a/pf4j/pom.xml b/pf4j/pom.xml index dbedbbb..4f05a19 100644 --- a/pf4j/pom.xml +++ b/pf4j/pom.xml @@ -14,17 +14,17 @@ PF4J Plugin Framework for Java - - - - org.apache.maven.plugins - maven-compiler-plugin - - -proc:none - - - - + + + + org.apache.maven.plugins + maven-compiler-plugin + + -proc:none + + + + @@ -32,6 +32,11 @@ slf4j-api 1.7.5 + + com.github.zafarkhaja + java-semver + 0.9.0 + junit junit diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java index df73ad5..147ec3d 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java @@ -12,6 +12,8 @@ */ package ro.fortsoft.pf4j; +import com.github.zafarkhaja.semver.Version; +import com.github.zafarkhaja.semver.expr.Expression; import java.io.File; import java.io.FileFilter; import java.io.IOException; @@ -86,7 +88,7 @@ public class DefaultPluginManager implements PluginManager { /** * The system version used for comparisons to the plugin requires attribute. */ - private Version systemVersion = Version.ZERO; + private Version systemVersion = Version.forIntegers(0, 0, 0); private PluginFactory pluginFactory; private ExtensionFactory extensionFactory; @@ -587,7 +589,7 @@ public class DefaultPluginManager implements PluginManager { } } - return (version != null) ? Version.createVersion(version) : Version.ZERO; + return (version != null) ? Version.valueOf(version) : Version.forIntegers(0, 0, 0); } /** @@ -641,9 +643,9 @@ public class DefaultPluginManager implements PluginManager { } protected boolean isPluginValid(PluginWrapper pluginWrapper) { - Version requires = pluginWrapper.getDescriptor().getRequires(); + Expression requires = pluginWrapper.getDescriptor().getRequires(); Version system = getSystemVersion(); - if (system.isZero() || system.atLeast(requires)) { + if (requires.interpret(system)) { return true; } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java index ad5e426..3328782 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java @@ -12,6 +12,7 @@ */ package ro.fortsoft.pf4j; +import com.github.zafarkhaja.semver.Version; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -96,7 +97,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder { if (StringUtils.isEmpty(version)) { throw new PluginException("Plugin-Version cannot be empty"); } - pluginDescriptor.setPluginVersion(Version.createVersion(version)); + pluginDescriptor.setPluginVersion(Version.valueOf(version)); String provider = attrs.getValue("Plugin-Provider"); pluginDescriptor.setProvider(provider); @@ -105,7 +106,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder { String requires = attrs.getValue("Plugin-Requires"); if (StringUtils.isNotEmpty(requires)) { - pluginDescriptor.setRequires(Version.createVersion(requires)); + pluginDescriptor.setRequires(requires); } return pluginDescriptor; diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDependency.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDependency.java index e572e1a..2d1887e 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDependency.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDependency.java @@ -12,38 +12,38 @@ */ package ro.fortsoft.pf4j; +import com.github.zafarkhaja.semver.Version; + /** * @author Decebal Suiu */ public class PluginDependency { - private String pluginId; - private Version pluginVersion; - - public PluginDependency(String dependency) { - /* - int index = dependency.indexOf(':'); - if (index == -1) { - throw new IllegalArgumentException("Illegal dependency specifier "+ dependency); - } - - this.pluginId = dependency.substring(0, index); - this.pluginVersion = Version.createVersion(dependency.substring(index + 1)); - */ - this.pluginId = dependency; - } - - public String getPluginId() { - return pluginId; - } - - public Version getPluginVersion() { - return pluginVersion; - } - - @Override - public String toString() { - return "PluginDependency [pluginId=" + pluginId + ", pluginVersion=" + pluginVersion + "]"; - } + private String pluginId; + private String pluginVersionSupport; + + public PluginDependency(String dependency) { + int index = dependency.indexOf('@'); + if (index == -1) { + this.pluginId = dependency; + } else { + + this.pluginId = dependency.substring(0, index); + this.pluginVersionSupport = dependency.substring(index + 1); + } + } + + public String getPluginId() { + return pluginId; + } + + public String getPluginVersionSupport() { + return pluginVersionSupport; + } + + @Override + public String toString() { + return "PluginDependency [pluginId=" + pluginId + ", pluginVersionSupport=" + pluginVersionSupport + "]"; + } } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java index 0721474..f469e7d 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java @@ -12,6 +12,11 @@ */ package ro.fortsoft.pf4j; +import com.github.zafarkhaja.semver.Parser; +import com.github.zafarkhaja.semver.Version; +import static com.github.zafarkhaja.semver.expr.CompositeExpression.Helper.gte; +import com.github.zafarkhaja.semver.expr.Expression; +import com.github.zafarkhaja.semver.expr.ExpressionParser; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -28,12 +33,12 @@ public class PluginDescriptor { private String pluginDescription; private String pluginClass; private Version version; - private Version requires; + private Expression requires; private String provider; private List dependencies; public PluginDescriptor() { - requires = Version.ZERO; + requires = gte("0.0.0"); // Any dependencies = new ArrayList<>(); } @@ -68,7 +73,7 @@ public class PluginDescriptor { /** * Returns the requires of this plugin. */ - public Version getRequires() { + public Expression getRequires() { return requires; } @@ -115,7 +120,12 @@ public class PluginDescriptor { this.provider = provider; } - void setRequires(Version requires) { + void setRequires(String requires) { + Parser parser = ExpressionParser.newInstance(); + this.requires = parser.parse(requires); + } + + void setRequires(Expression requires) { this.requires = requires; } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java index ca381d0..d889169 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java @@ -12,6 +12,7 @@ */ package ro.fortsoft.pf4j; +import com.github.zafarkhaja.semver.Version; import java.io.File; import java.util.List; import java.util.Set; diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java index f009532..609987b 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java @@ -12,6 +12,7 @@ */ package ro.fortsoft.pf4j; +import com.github.zafarkhaja.semver.Version; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -92,7 +93,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder if (StringUtils.isEmpty(version)) { throw new PluginException("plugin.version cannot be empty"); } - pluginDescriptor.setPluginVersion(Version.createVersion(version)); + pluginDescriptor.setPluginVersion(Version.valueOf(version)); String provider = properties.getProperty("plugin.provider"); pluginDescriptor.setProvider(provider); diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/Version.java b/pf4j/src/main/java/ro/fortsoft/pf4j/Version.java deleted file mode 100644 index 842a37f..0000000 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/Version.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright 2012 Decebal Suiu - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with - * the License. You may obtain a copy of the License in the LICENSE file, or at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package ro.fortsoft.pf4j; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import ro.fortsoft.pf4j.util.StringUtils; - -/** - * Version following semantic defined by Semantic Versioning document. - * Version identifiers have four components. - * - * 1. Major version. A non-negative integer. - * 2. Minor version. A non-negative integer. - * 3. Patch version. A non-negative integer. - * 4. Qualifier. A text string. - * - * This class is immutable. - * - * @author Decebal Suiu - */ -public class Version implements Comparable { - - public static final Version ZERO = new Version(0, 0, 0); - - private static final String FORMAT = "(\\d+)\\.(\\d+)(?:\\.)?(\\d*)(\\.|-|\\+)?([0-9A-Za-z-.]*)?"; - private static final Pattern PATTERN = Pattern.compile(FORMAT); - - private int major; - private int minor; - private int patch; - private String separator; - private String qualifier; - - public Version(int major, int minor, int patch) { - this.major = major; - this.minor = minor; - this.patch = patch; - } - - public Version(int major, int minor, int patch, String separator, String qualifier) { - this.major = major; - this.minor = minor; - this.patch = patch; - this.separator = separator; - this.qualifier = qualifier; - } - - public static Version createVersion(String version) { - Matcher matcher = PATTERN.matcher(version); - if (!matcher.matches()) { - throw new IllegalArgumentException("'" + version + "' does not match format '" + FORMAT + "'"); - } - - - - int major = Integer.valueOf(matcher.group(1)); - int minor = Integer.valueOf(matcher.group(2)); - int patch; - String patchMatch = matcher.group(3); - if (StringUtils.isNotEmpty(patchMatch)) { - patch = Integer.valueOf(patchMatch); - } else { - patch = 0; - } - String separator = matcher.group(4); - String qualifier = matcher.group(5); - - return new Version(major, minor, patch, separator, "".equals(qualifier) ? null : qualifier); - } - - public int getMajor() { - return this.major; - } - - public int getMinor() { - return this.minor; - } - - public int getPatch() { - return this.patch; - } - - public String getQualifier() { - return qualifier; - } - - @Override - public String toString() { - StringBuffer sb = new StringBuffer(50); - sb.append(major); - sb.append('.'); - sb.append(minor); - sb.append('.'); - sb.append(patch); - if (separator != null) { - sb.append(separator); - } - if (qualifier != null) { - sb.append(qualifier); - } - - return sb.toString(); - } - - @Override - public int compareTo(Version version) { - if (version.major > major) { - return 1; - } else if (version.major < major) { - return -1; - } - - if (version.minor > minor) { - return 1; - } else if (version.minor < minor) { - return -1; - } - - if (version.patch > patch) { - return 1; - } else if (version.patch < patch) { - return -1; - } - - return 0; - } - - public boolean isZero() { - return compareTo(ZERO) == 0; - } - - public boolean atLeast(Version v) { - return compareTo(v) <= 0; - } - - public boolean exceeds(Version v) { - return compareTo(v) > 0; - } - - // for test only - public static void main(String[] args) { - Version v = Version.createVersion("1.2.3-SNAPSHOT"); - System.out.println(v.toString()); - Version v1 = Version.createVersion("4.1.0"); - System.out.println(v1.toString()); - Version v2 = Version.createVersion("4.0.32"); - System.out.println(v2.toString()); - System.out.println(v1.compareTo(v2)); - } - -}