]> source.dussan.org Git - pf4j.git/commitdiff
Replace Version with semver lib 44/head
authorMário Franco <mario.ffranco@gmail.com>
Fri, 5 Jun 2015 13:31:31 +0000 (14:31 +0100)
committerMário Franco <mario.ffranco@gmail.com>
Fri, 5 Jun 2015 15:59:17 +0000 (16:59 +0100)
pf4j/pom.xml
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java
pf4j/src/main/java/ro/fortsoft/pf4j/PluginDependency.java
pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java
pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java
pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java
pf4j/src/main/java/ro/fortsoft/pf4j/Version.java [deleted file]

index dbedbbb6f812e93163e6fe08bb5ed832b06f0fcb..4f05a19179a2added280faba86a020836c6fe2a7 100644 (file)
     <name>PF4J</name>
     <description>Plugin Framework for Java</description>
 
-       <build>
-               <plugins>
-                       <plugin>
-                               <groupId>org.apache.maven.plugins</groupId>
-                               <artifactId>maven-compiler-plugin</artifactId>
-                               <configuration>
-                                       <compilerArgument>-proc:none</compilerArgument>
-                               </configuration>
-                       </plugin>
-               </plugins>
-       </build>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <compilerArgument>-proc:none</compilerArgument>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 
     <dependencies>
         <dependency>
             <artifactId>slf4j-api</artifactId>
             <version>1.7.5</version>
         </dependency>
+        <dependency>
+            <groupId>com.github.zafarkhaja</groupId>
+            <artifactId>java-semver</artifactId>
+            <version>0.9.0</version>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
index df73ad57a785cb6e4ee31eb6bc8deb87c46bb9d0..147ec3db9488e8aa5208ac94d8c804e5bab1147a 100644 (file)
@@ -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;
        }
 
index ad5e426df6d61b26a74e3760a36077a080ad03de..33287822817ee1d7d16b4cdef1bcdc3f99744ef7 100644 (file)
@@ -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;
index e572e1a6dba71d9534e98fda9eadd9bb6378240d..2d1887eff4aa7b83984e371a5203e80cfa15e681 100644 (file)
  */
 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 + "]";
+    }
 
 }
index 07214745b6eca4b4c1fdeddb14cbaaca859651f0..f469e7d9b72997664b2b15beb67bb9c05545cb4a 100644 (file)
  */
 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<PluginDependency> 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<Expression> parser = ExpressionParser.newInstance();
+        this.requires = parser.parse(requires);
+    }
+
+    void setRequires(Expression requires) {
         this.requires = requires;
     }
 
index ca381d05b58a0d48e417cf1999db5654c45e5762..d889169450ac4a3b2698fac6c7fe686de841b2a1 100644 (file)
@@ -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;
index f009532a1dfae13e30d62e10e9ae1272138049af..609987ba41ff2db67b5460f3d1a7225ed3856f31 100644 (file)
@@ -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 (file)
index 842a37f..0000000
+++ /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 <a href="http://semver.org/">Semantic Versioning</a> 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<Version> {
-
-       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));
-    }
-
-}