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. 58
      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();
}
@Override
public void reset() {
entries = null;
}
@Override
public <T> List<ExtensionWrapper<T>> find(Class<T> type) {
log.debug("Checking extension point '{}'", type.getName());

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

@ -146,6 +146,36 @@ public class DefaultPluginManager implements PluginManager {
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.
*/
@ -284,10 +314,10 @@ public class DefaultPluginManager implements PluginManager {
// load any plugin from plugins directory
for (File directory : directories) {
try {
loadPlugin(directory);
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
loadPluginDirectory(directory);
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
}
// resolve 'unresolvedPlugins'
@ -319,6 +349,7 @@ public class DefaultPluginManager implements PluginManager {
plugins.remove(pluginId);
resolvedPlugins.remove(pluginWrapper);
pathToIdMap.remove(pluginWrapper.getPluginPath());
extensionFinder.reset();
// remove the classloader
if (pluginClassLoaders.containsKey(pluginId)) {
@ -425,7 +456,7 @@ public class DefaultPluginManager implements PluginManager {
return plugin;
}
}
log.warn("Failed to find the plugin for {}", clazz);
return null;
}
@ -527,14 +558,14 @@ public class DefaultPluginManager implements PluginManager {
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
String pluginName = pluginDirectory.getName();
String pluginPath = "/".concat(pluginName);
// test for plugin duplication
if (plugins.get(pathToIdMap.get(pluginPath)) != null) {
return;
return null;
}
// retrieves the plugin descriptor
@ -547,7 +578,7 @@ public class DefaultPluginManager implements PluginManager {
// test for disabled plugin
if (isPluginDisabled(pluginDescriptor.getPluginId())) {
log.info("Plugin '{}' is disabled", pluginPath);
return;
return null;
}
// load plugin
@ -571,9 +602,11 @@ public class DefaultPluginManager implements PluginManager {
// add plugin class loader to the list with class loaders
PluginClassLoader pluginClassLoader = pluginLoader.getPluginClassLoader();
pluginClassLoaders.put(pluginId, pluginClassLoader);
return pluginWrapper;
}
private void expandPluginArchive(File pluginArchiveFile) throws IOException {
private File expandPluginArchive(File pluginArchiveFile) throws IOException {
String fileName = pluginArchiveFile.getName();
long pluginArchiveDate = pluginArchiveFile.lastModified();
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
if (!pluginDirectory.exists() || (pluginArchiveDate > pluginDirectory.lastModified())) {
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
pluginDirectory.mkdirs();
@ -590,6 +629,7 @@ public class DefaultPluginManager implements PluginManager {
unzip.setDestination(pluginDirectory);
unzip.extract();
}
return pluginDirectory;
}
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 void reset();
}

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

@ -12,6 +12,7 @@
*/
package ro.fortsoft.pf4j;
import java.io.File;
import java.util.List;
/**
@ -47,6 +48,14 @@ public interface PluginManager {
*/
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.
*/

Loading…
Cancel
Save