Browse Source

Replace Version with semver lib

pull/44/head
Mário Franco 10 years ago
parent
commit
f3fe919089
  1. 27
      pf4j/pom.xml
  2. 10
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  3. 5
      pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java
  4. 56
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginDependency.java
  5. 18
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java
  6. 1
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java
  7. 3
      pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java
  8. 163
      pf4j/src/main/java/ro/fortsoft/pf4j/Version.java

27
pf4j/pom.xml

@ -14,17 +14,17 @@
<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>
@ -32,6 +32,11 @@
<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>

10
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;
}

5
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;

56
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 + "]";
}
}

18
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<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;
}

1
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;

3
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);

163
pf4j/src/main/java/ro/fortsoft/pf4j/Version.java

@ -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));
}
}
Loading…
Cancel
Save