From: Decebal Suiu Date: Mon, 14 Apr 2014 14:26:19 +0000 (+0300) Subject: rename PluginVersion to Version X-Git-Tag: release-0.8.0~2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=eb24fa2b3257bd6e06fe7b5d32be46a1d3d63cf3;p=pf4j.git rename PluginVersion to Version --- diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java index 286b6ea..c4ffedf 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java @@ -92,7 +92,7 @@ public class DefaultPluginManager implements PluginManager { /** * The system version used for comparisons to the plugin requires attribute. */ - private PluginVersion systemVersion = PluginVersion.ZERO; + private Version systemVersion = Version.ZERO; /** * The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins"). @@ -116,12 +116,12 @@ public class DefaultPluginManager implements PluginManager { } @Override - public void setSystemVersion(PluginVersion version) { + public void setSystemVersion(Version version) { systemVersion = version; } @Override - public PluginVersion getSystemVersion() { + public Version getSystemVersion() { return systemVersion; } @@ -657,8 +657,8 @@ public class DefaultPluginManager implements PluginManager { } protected boolean isPluginValid(PluginWrapper pluginWrapper) { - PluginVersion requires = pluginWrapper.getDescriptor().getRequires(); - PluginVersion system = getSystemVersion(); + Version requires = pluginWrapper.getDescriptor().getRequires(); + Version system = getSystemVersion(); if (system.isZero() || system.atLeast(requires)) { 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 7f802d1..ad5e426 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java @@ -96,7 +96,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder { if (StringUtils.isEmpty(version)) { throw new PluginException("Plugin-Version cannot be empty"); } - pluginDescriptor.setPluginVersion(PluginVersion.createVersion(version)); + pluginDescriptor.setPluginVersion(Version.createVersion(version)); String provider = attrs.getValue("Plugin-Provider"); pluginDescriptor.setProvider(provider); @@ -105,7 +105,7 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder { String requires = attrs.getValue("Plugin-Requires"); if (StringUtils.isNotEmpty(requires)) { - pluginDescriptor.setRequires(PluginVersion.createVersion(requires)); + pluginDescriptor.setRequires(Version.createVersion(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 d63b66a..e572e1a 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDependency.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDependency.java @@ -1,11 +1,11 @@ /* * 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. @@ -18,17 +18,17 @@ package ro.fortsoft.pf4j; public class PluginDependency { private String pluginId; - private PluginVersion pluginVersion; - + 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 = PluginVersion.createVersion(dependency.substring(index + 1)); + this.pluginVersion = Version.createVersion(dependency.substring(index + 1)); */ this.pluginId = dependency; } @@ -37,7 +37,7 @@ public class PluginDependency { return pluginId; } - public PluginVersion getPluginVersion() { + public Version getPluginVersion() { return pluginVersion; } @@ -45,5 +45,5 @@ public class PluginDependency { public String toString() { return "PluginDependency [pluginId=" + pluginId + ", pluginVersion=" + pluginVersion + "]"; } - + } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java index ae07fbb..f456f7c 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java @@ -27,13 +27,13 @@ public class PluginDescriptor { private String pluginId; private String pluginDescription; private String pluginClass; - private PluginVersion version; - private PluginVersion requires; + private Version version; + private Version requires; private String provider; private List dependencies; public PluginDescriptor() { - requires = PluginVersion.ZERO; + requires = Version.ZERO; dependencies = new ArrayList(); } @@ -61,14 +61,14 @@ public class PluginDescriptor { /** * Returns the version of this plugin. */ - public PluginVersion getVersion() { + public Version getVersion() { return version; } /** * Returns the requires of this plugin. */ - public PluginVersion getRequires() { + public Version getRequires() { return requires; } @@ -107,7 +107,7 @@ public class PluginDescriptor { this.pluginClass = pluginClassName; } - void setPluginVersion(PluginVersion version) { + void setPluginVersion(Version version) { this.version = version; } @@ -115,7 +115,7 @@ public class PluginDescriptor { this.provider = provider; } - void setRequires(PluginVersion requires) { + void setRequires(Version 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 81abe7c..ca381d0 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java @@ -149,12 +149,12 @@ public interface PluginManager { * @default 0.0.0 * @param version */ - public void setSystemVersion(PluginVersion version); + public void setSystemVersion(Version version); /** * Returns the system version. * * * @return the system version */ - public PluginVersion getSystemVersion(); + public Version getSystemVersion(); } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginVersion.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginVersion.java deleted file mode 100644 index c5d57fd..0000000 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginVersion.java +++ /dev/null @@ -1,164 +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; - -/** - * Represents the version of a Plugin and allows versions to be compared. - * 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 PluginVersion implements Comparable { - - public static final PluginVersion ZERO = new PluginVersion(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 PluginVersion(int major, int minor, int patch) { - this.major = major; - this.minor = minor; - this.patch = patch; - } - - public PluginVersion(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 PluginVersion 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 PluginVersion(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(PluginVersion 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(PluginVersion v) { - return compareTo(v) <= 0; - } - - public boolean exceeds(PluginVersion v) { - return compareTo(v) > 0; - } - - // for test only - public static void main(String[] args) { - PluginVersion v = PluginVersion.createVersion("1.2.3-SNAPSHOT"); - System.out.println(v.toString()); - PluginVersion v1 = PluginVersion.createVersion("4.1.0"); - System.out.println(v1.toString()); - PluginVersion v2 = PluginVersion.createVersion("4.0.32"); - System.out.println(v2.toString()); - System.out.println(v1.compareTo(v2)); - } - -} diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java index 2c7c707..f009532 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java @@ -1,11 +1,11 @@ /* * 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. @@ -26,15 +26,15 @@ import ro.fortsoft.pf4j.util.StringUtils; /** * Find a plugin descriptor in a properties file (in plugin repository). - * + * * @author Decebal Suiu */ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder { private static final Logger log = LoggerFactory.getLogger(PropertiesPluginDescriptorFinder.class); - + private static final String DEFAULT_PROPERTIES_FILE_NAME = "plugin.properties"; - + private String propertiesFileName; public PropertiesPluginDescriptorFinder() { @@ -44,7 +44,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder public PropertiesPluginDescriptorFinder(String propertiesFileName) { this.propertiesFileName = propertiesFileName; } - + @Override public PluginDescriptor find(File pluginRepository) throws PluginException { File propertiesFile = new File(pluginRepository, propertiesFileName); @@ -57,9 +57,9 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder try { input = new FileInputStream(propertiesFile); } catch (FileNotFoundException e) { - // not happening + // not happening } - + Properties properties = new Properties(); try { properties.load(input); @@ -71,31 +71,31 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder } catch (IOException e) { throw new PluginException(e.getMessage(), e); } - } - + } + PluginDescriptor pluginDescriptor = new PluginDescriptor(); - + // TODO validate !!! String id = properties.getProperty("plugin.id"); if (StringUtils.isEmpty(id)) { throw new PluginException("plugin.id cannot be empty"); } pluginDescriptor.setPluginId(id); - + String clazz = properties.getProperty("plugin.class"); if (StringUtils.isEmpty(clazz)) { throw new PluginException("plugin.class cannot be empty"); } pluginDescriptor.setPluginClass(clazz); - + String version = properties.getProperty("plugin.version"); if (StringUtils.isEmpty(version)) { throw new PluginException("plugin.version cannot be empty"); } - pluginDescriptor.setPluginVersion(PluginVersion.createVersion(version)); - + pluginDescriptor.setPluginVersion(Version.createVersion(version)); + String provider = properties.getProperty("plugin.provider"); - pluginDescriptor.setProvider(provider); + pluginDescriptor.setProvider(provider); String dependencies = properties.getProperty("plugin.dependencies"); pluginDescriptor.setDependencies(dependencies); diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/Version.java b/pf4j/src/main/java/ro/fortsoft/pf4j/Version.java new file mode 100644 index 0000000..842a37f --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/Version.java @@ -0,0 +1,163 @@ +/* + * 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)); + } + +}