Browse Source

plugin descriptor from a properties file

pull/3/head
Decebal Suiu 12 years ago
parent
commit
a8fd70e2b9
  1. 12
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java
  2. 10
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  3. 97
      pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java
  4. 24
      pf4j/src/main/java/ro/fortsoft/pf4j/util/StringUtils.java

12
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));
@ -85,7 +86,4 @@ public class DefaultPluginDescriptorFinder implements PluginDescriptorFinder {
return pluginDescriptor;
}
private boolean isEmpty(String value) {
return (value == null) || value.isEmpty();
}
}

10
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<PluginWrapper> getPlugins() {
return new ArrayList<PluginWrapper>(plugins.values());
}

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

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