From a8fd70e2b9360bede87da7156760168dfa8a836d Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Tue, 19 Feb 2013 17:06:03 +0200 Subject: [PATCH] plugin descriptor from a properties file --- .../pf4j/DefaultPluginDescriptorFinder.java | 14 ++- .../fortsoft/pf4j/DefaultPluginManager.java | 10 +- .../PropertiesPluginDescriptorFinder.java | 97 +++++++++++++++++++ .../ro/fortsoft/pf4j/util/StringUtils.java | 24 +++++ 4 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java create mode 100644 pf4j/src/main/java/ro/fortsoft/pf4j/util/StringUtils.java diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java index 201ebb1..3e23ff1 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java @@ -19,6 +19,8 @@ import java.io.IOException; import java.util.jar.Attributes; import java.util.jar.Manifest; +import ro.fortsoft.pf4j.util.StringUtils; + /** * Read the plugin descriptor from the manifest file. * @@ -31,7 +33,6 @@ public class DefaultPluginDescriptorFinder implements PluginDescriptorFinder { // TODO it's ok with classes/ ? File manifestFile = new File(pluginRepository, "classes/META-INF/MANIFEST.MF"); if (!manifestFile.exists()) { - // not found a 'plugin.xml' file for this plugin throw new PluginException("Cannot find '" + manifestFile + "' file"); } @@ -60,19 +61,19 @@ public class DefaultPluginDescriptorFinder implements PluginDescriptorFinder { // TODO validate !!! Attributes attrs = manifest.getMainAttributes(); String id = attrs.getValue("Plugin-Id"); - if (isEmpty(id)) { + if (StringUtils.isEmpty(id)) { throw new PluginException("Plugin-Id cannot be empty"); } pluginDescriptor.setPluginId(id); String clazz = attrs.getValue("Plugin-Class"); - if (isEmpty(clazz)) { + if (StringUtils.isEmpty(clazz)) { throw new PluginException("Plugin-Class cannot be empty"); } pluginDescriptor.setPluginClass(clazz); String version = attrs.getValue("Plugin-Version"); - if (isEmpty(version)) { + if (StringUtils.isEmpty(version)) { throw new PluginException("Plugin-Version cannot be empty"); } pluginDescriptor.setPluginVersion(PluginVersion.createVersion(version)); @@ -84,8 +85,5 @@ public class DefaultPluginDescriptorFinder implements PluginDescriptorFinder { return pluginDescriptor; } - - private boolean isEmpty(String value) { - return (value == null) || value.isEmpty(); - } + } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java index 750e334..9dcfcc7 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java @@ -117,7 +117,15 @@ public class DefaultPluginManager implements PluginManager { System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath()); } - @Override + public PluginDescriptorFinder getPluginDescriptorFinder() { + return pluginDescriptorFinder; + } + + public void setPluginDescriptorFinder(PluginDescriptorFinder pluginDescriptorFinder) { + this.pluginDescriptorFinder = pluginDescriptorFinder; + } + + @Override public List getPlugins() { return new ArrayList(plugins.values()); } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java new file mode 100644 index 0000000..4f0e753 --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java @@ -0,0 +1,97 @@ +/* + * 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.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +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 String propertiesFileName; + + public PropertiesPluginDescriptorFinder() { + this("plugin.properties"); + } + + public PropertiesPluginDescriptorFinder(String propertiesFileName) { + this.propertiesFileName = propertiesFileName; + } + + @Override + public PluginDescriptor find(File pluginRepository) throws PluginException { + File propertiesFile = new File(pluginRepository, propertiesFileName); + if (!propertiesFile.exists()) { + throw new PluginException("Cannot find '" + propertiesFile + "' file"); + } + + InputStream input = null; + try { + input = new FileInputStream(propertiesFile); + } catch (FileNotFoundException e) { + // not happening + } + + Properties properties = new Properties(); + try { + properties.load(input); + } catch (IOException e) { + throw new PluginException(e.getMessage(), e); + } finally { + try { + input.close(); + } 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)); + + String provider = properties.getProperty("plugin.provider"); + pluginDescriptor.setProvider(provider); + String dependencies = properties.getProperty("plugin.dependencies"); + pluginDescriptor.setDependencies(dependencies); + + return pluginDescriptor; + } + +} diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/util/StringUtils.java b/pf4j/src/main/java/ro/fortsoft/pf4j/util/StringUtils.java new file mode 100644 index 0000000..c133255 --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/util/StringUtils.java @@ -0,0 +1,24 @@ +/* + * 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.util; + +/** + * @author Decebal Suiu + */ +public class StringUtils { + + public static boolean isEmpty(String str) { + return (str == null) || str.isEmpty(); + } + +}