import java.util.jar.Attributes;
import java.util.jar.Manifest;
+import ro.fortsoft.pf4j.util.StringUtils;
+
/**
* Read the plugin descriptor from the manifest file.
*
// 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");
}
// 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));
return pluginDescriptor;
}
-
- private boolean isEmpty(String value) {
- return (value == null) || value.isEmpty();
- }
+
}
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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();
+ }
+
+}