*/
private RuntimeMode runtimeMode;
+ /**
+ * The system version used for comparisons to the plugin requires attribute.
+ */
+ private PluginVersion systemVersion = PluginVersion.DEFAULT;
+
/**
* The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins").
*/
initialize();
}
+ @Override
+ public void setSystemVersion(PluginVersion version) {
+ systemVersion = version;
+ }
+
+ @Override
+ public PluginVersion getSystemVersion() {
+ return systemVersion;
+ }
+
@Override
public List<PluginWrapper> getPlugins() {
return new ArrayList<PluginWrapper>(plugins.values());
return true;
}
+ PluginVersion requires = pluginWrapper.getDescriptor().getRequires();
+ PluginVersion system = getSystemVersion();
+ if (!system.isDefault() && !system.atLeast(requires)) {
+ log.warn(String.format("Failed to enable plugin `{}:{}` because it requires a minimum system version of %s",
+ pluginWrapper.getPluginId(),
+ pluginWrapper.getDescriptor().getVersion(),
+ requires));
+ return false;
+ }
+
try {
if (disabledPlugins.remove(pluginId)) {
FileUtils.writeLines(disabledPlugins, new File(pluginsDirectory, "disabled.txt"));
pluginWrapper.setPluginState(PluginState.DISABLED);
}
+ // optionally enforce minimum system version requirements
+ PluginVersion requires = pluginWrapper.getDescriptor().getRequires();
+ PluginVersion system = getSystemVersion();
+ if (!system.isDefault() && !system.atLeast(requires)) {
+ log.warn(String.format("Disabling plugin '%s:%s' because it requires a minimum system version of %s",
+ pluginWrapper.getPluginId(),
+ pluginWrapper.getDescriptor().getVersion(),
+ requires));
+ pluginWrapper.setPluginState(PluginState.DISABLED);
+ disabledPlugins.add(pluginWrapper.getPluginId());
+ }
+
log.debug("Created wrapper '{}' for plugin '{}'", pluginWrapper, pluginPath);
String pluginId = pluginDescriptor.getPluginId();
String dependencies = attrs.getValue("Plugin-Dependencies");
pluginDescriptor.setDependencies(dependencies);
+ String requires = attrs.getValue("Plugin-Requires");
+ if (StringUtils.isNotEmpty(requires)) {
+ pluginDescriptor.setRequires(PluginVersion.createVersion(requires));
+ }
+
return pluginDescriptor;
}
private String pluginDescription;
private String pluginClass;
private PluginVersion version;
+ private PluginVersion requires;
private String provider;
private List<PluginDependency> dependencies;
public PluginDescriptor() {
+ requires = PluginVersion.DEFAULT;
dependencies = new ArrayList<PluginDependency>();
}
return version;
}
+ /**
+ * Returns the requires of this plugin.
+ */
+ public PluginVersion getRequires() {
+ return requires;
+ }
+
/**
* Returns the provider name of this plugin.
*/
this.provider = provider;
}
+ void setRequires(PluginVersion requires) {
+ this.requires = requires;
+ }
+
void setDependencies(String dependencies) {
if (dependencies != null) {
dependencies = dependencies.trim();
public void removePluginStateListener(PluginStateListener listener);
+ /**
+ * Set the system version. This is used to compare against the plugin
+ * requires attribute. The default system version is 0.0.0 which
+ * disables all version checking.
+ *
+ * @default 0.0.0
+ * @param version
+ */
+ public void setSystemVersion(PluginVersion version);
+
+ /**
+ * Returns the system version.
+ *
+ * * @return the system version
+ */
+ public PluginVersion getSystemVersion();
}
/*
* 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 ro.fortsoft.pf4j.util.StringUtils;
-
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 <a href="http://semver.org/">Semantic Versioning</a> document.
*/
public class PluginVersion implements Comparable<PluginVersion> {
+ public static final PluginVersion DEFAULT = 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);
return qualifier;
}
+ @Override
public String toString() {
StringBuffer sb = new StringBuffer(50);
sb.append(major);
return 0;
}
+ public boolean isDefault() {
+ return compareTo(DEFAULT) == 0;
+ }
+
+ public boolean atLeast(PluginVersion v) {
+ return compareTo(v) <= 0;
+ }
+
// for test only
public static void main(String[] args) {
PluginVersion v = PluginVersion.createVersion("1.2.3-SNAPSHOT");