Browse Source

Implement loading a single plugin archive

pull/6/head
James Moger 11 years ago
parent
commit
de6b7df7a6
  1. 5
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java
  2. 52
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  3. 2
      pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionFinder.java
  4. 9
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java

5
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java

@ -46,6 +46,11 @@ public class DefaultExtensionFinder implements ExtensionFinder {
this.extensionFactory = createExtensionFactory(); this.extensionFactory = createExtensionFactory();
} }
@Override
public void reset() {
entries = null;
}
@Override @Override
public <T> List<ExtensionWrapper<T>> find(Class<T> type) { public <T> List<ExtensionWrapper<T>> find(Class<T> type) {
log.debug("Checking extension point '{}'", type.getName()); log.debug("Checking extension point '{}'", type.getName());

52
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java

@ -146,6 +146,36 @@ public class DefaultPluginManager implements PluginManager {
return startedPlugins; return startedPlugins;
} }
@Override
public String loadPlugin(File pluginArchiveFile) {
if (pluginArchiveFile == null || !pluginArchiveFile.exists()) {
throw new IllegalArgumentException(String.format("Specified plugin %s does not exist!", pluginArchiveFile));
}
File pluginDirectory = null;
try {
pluginDirectory = expandPluginArchive(pluginArchiveFile);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
if (pluginDirectory == null || !pluginDirectory.exists()) {
throw new IllegalArgumentException(String.format("Failed to expand %s", pluginArchiveFile));
}
try {
PluginWrapper pluginWrapper = loadPluginDirectory(pluginDirectory);
// TODO uninstalled plugin dependencies?
unresolvedPlugins.remove(pluginWrapper);
resolvedPlugins.add(pluginWrapper);
compoundClassLoader.addLoader(pluginWrapper.getPluginClassLoader());
extensionFinder.reset();
return pluginWrapper.getDescriptor().getPluginId();
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
return null;
}
/** /**
* Start all active plugins. * Start all active plugins.
*/ */
@ -284,7 +314,7 @@ public class DefaultPluginManager implements PluginManager {
// load any plugin from plugins directory // load any plugin from plugins directory
for (File directory : directories) { for (File directory : directories) {
try { try {
loadPlugin(directory); loadPluginDirectory(directory);
} catch (PluginException e) { } catch (PluginException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
@ -319,6 +349,7 @@ public class DefaultPluginManager implements PluginManager {
plugins.remove(pluginId); plugins.remove(pluginId);
resolvedPlugins.remove(pluginWrapper); resolvedPlugins.remove(pluginWrapper);
pathToIdMap.remove(pluginWrapper.getPluginPath()); pathToIdMap.remove(pluginWrapper.getPluginPath());
extensionFinder.reset();
// remove the classloader // remove the classloader
if (pluginClassLoaders.containsKey(pluginId)) { if (pluginClassLoaders.containsKey(pluginId)) {
@ -425,7 +456,7 @@ public class DefaultPluginManager implements PluginManager {
return plugin; return plugin;
} }
} }
log.warn("Failed to find the plugin for {}", clazz);
return null; return null;
} }
@ -527,14 +558,14 @@ public class DefaultPluginManager implements PluginManager {
System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath()); System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath());
} }
private void loadPlugin(File pluginDirectory) throws PluginException { private PluginWrapper loadPluginDirectory(File pluginDirectory) throws PluginException {
// try to load the plugin // try to load the plugin
String pluginName = pluginDirectory.getName(); String pluginName = pluginDirectory.getName();
String pluginPath = "/".concat(pluginName); String pluginPath = "/".concat(pluginName);
// test for plugin duplication // test for plugin duplication
if (plugins.get(pathToIdMap.get(pluginPath)) != null) { if (plugins.get(pathToIdMap.get(pluginPath)) != null) {
return; return null;
} }
// retrieves the plugin descriptor // retrieves the plugin descriptor
@ -547,7 +578,7 @@ public class DefaultPluginManager implements PluginManager {
// test for disabled plugin // test for disabled plugin
if (isPluginDisabled(pluginDescriptor.getPluginId())) { if (isPluginDisabled(pluginDescriptor.getPluginId())) {
log.info("Plugin '{}' is disabled", pluginPath); log.info("Plugin '{}' is disabled", pluginPath);
return; return null;
} }
// load plugin // load plugin
@ -571,9 +602,11 @@ public class DefaultPluginManager implements PluginManager {
// add plugin class loader to the list with class loaders // add plugin class loader to the list with class loaders
PluginClassLoader pluginClassLoader = pluginLoader.getPluginClassLoader(); PluginClassLoader pluginClassLoader = pluginLoader.getPluginClassLoader();
pluginClassLoaders.put(pluginId, pluginClassLoader); pluginClassLoaders.put(pluginId, pluginClassLoader);
return pluginWrapper;
} }
private void expandPluginArchive(File pluginArchiveFile) throws IOException { private File expandPluginArchive(File pluginArchiveFile) throws IOException {
String fileName = pluginArchiveFile.getName(); String fileName = pluginArchiveFile.getName();
long pluginArchiveDate = pluginArchiveFile.lastModified(); long pluginArchiveDate = pluginArchiveFile.lastModified();
String pluginName = fileName.substring(0, fileName.length() - 4); String pluginName = fileName.substring(0, fileName.length() - 4);
@ -581,6 +614,12 @@ public class DefaultPluginManager implements PluginManager {
// check if exists directory or the '.zip' file is "newer" than directory // check if exists directory or the '.zip' file is "newer" than directory
if (!pluginDirectory.exists() || (pluginArchiveDate > pluginDirectory.lastModified())) { if (!pluginDirectory.exists() || (pluginArchiveDate > pluginDirectory.lastModified())) {
log.debug("Expand plugin archive '{}' in '{}'", pluginArchiveFile, pluginDirectory); log.debug("Expand plugin archive '{}' in '{}'", pluginArchiveFile, pluginDirectory);
// do not overwrite an old version, remove it
if (pluginDirectory.exists()) {
FileUtils.delete(pluginDirectory);
}
// create directory for plugin // create directory for plugin
pluginDirectory.mkdirs(); pluginDirectory.mkdirs();
@ -590,6 +629,7 @@ public class DefaultPluginManager implements PluginManager {
unzip.setDestination(pluginDirectory); unzip.setDestination(pluginDirectory);
unzip.extract(); unzip.extract();
} }
return pluginDirectory;
} }
private void resolvePlugins() throws PluginException { private void resolvePlugins() throws PluginException {

2
pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionFinder.java

@ -21,4 +21,6 @@ public interface ExtensionFinder {
public <T> List<ExtensionWrapper<T>> find(Class<T> type); public <T> List<ExtensionWrapper<T>> find(Class<T> type);
public void reset();
} }

9
pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java

@ -12,6 +12,7 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import java.io.File;
import java.util.List; import java.util.List;
/** /**
@ -47,6 +48,14 @@ public interface PluginManager {
*/ */
public void loadPlugins(); public void loadPlugins();
/**
* Load a plugin.
*
* @param pluginArchiveFile
* @return the pluginId of the installed plugin or null
*/
public String loadPlugin(File pluginArchiveFile);
/** /**
* Start all active plugins. * Start all active plugins.
*/ */

Loading…
Cancel
Save