private List<String> enabledPlugins;
private List<String> disabledPlugins;
+ /**
+ * The registered {@link PluginStateListener}s.
+ */
+ private List<PluginStateListener> pluginStateListeners;
+
/**
* Cache value for the runtime mode. No need to re-read it because it wont change at
* runtime.
@Override
public String loadPlugin(File pluginArchiveFile) {
- if (pluginArchiveFile == null || !pluginArchiveFile.exists()) {
+ if ((pluginArchiveFile == null) || !pluginArchiveFile.exists()) {
throw new IllegalArgumentException(String.format("Specified plugin %s does not exist!", pluginArchiveFile));
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
- if (pluginDirectory == null || !pluginDirectory.exists()) {
+ if ((pluginDirectory == null) || !pluginDirectory.exists()) {
throw new IllegalArgumentException(String.format("Failed to expand %s", pluginArchiveFile));
}
// TODO uninstalled plugin dependencies?
unresolvedPlugins.remove(pluginWrapper);
resolvedPlugins.add(pluginWrapper);
- extensionFinder.reset();
+
+ firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, null));
+
return pluginWrapper.getDescriptor().getPluginId();
} catch (PluginException e) {
log.error(e.getMessage(), e);
pluginWrapper.getPlugin().start();
pluginWrapper.setPluginState(PluginState.STARTED);
startedPlugins.add(pluginWrapper);
+
+ firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
}
- PluginWrapper pluginWrapper = plugins.get(pluginId);
+ PluginWrapper pluginWrapper = getPlugin(pluginId);
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
- if (PluginState.STARTED == pluginWrapper.getPluginState()) {
+ PluginState pluginState = pluginWrapper.getPluginState();
+ if (PluginState.STARTED == pluginState) {
log.debug("Already started plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
return PluginState.STARTED;
}
- if (PluginState.DISABLED == pluginWrapper.getPluginState()) {
+ if (PluginState.DISABLED == pluginState) {
// automatically enable plugin on manual plugin start
if (!enablePlugin(pluginId)) {
- return pluginWrapper.getPluginState();
+ return pluginState;
}
}
pluginWrapper.getPlugin().start();
pluginWrapper.setPluginState(PluginState.STARTED);
startedPlugins.add(pluginWrapper);
- } catch (PluginException e) {
+
+ firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
+ } catch (PluginException e) {
log.error(e.getMessage(), e);
}
Iterator<PluginWrapper> itr = startedPlugins.iterator();
while (itr.hasNext()) {
PluginWrapper pluginWrapper = itr.next();
- if (PluginState.STARTED == pluginWrapper.getPluginState()) {
+ PluginState pluginState = pluginWrapper.getPluginState();
+ if (PluginState.STARTED == pluginState) {
try {
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
log.info("Stop plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
pluginWrapper.getPlugin().stop();
pluginWrapper.setPluginState(PluginState.STOPPED);
itr.remove();
+
+ firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
}
- PluginWrapper pluginWrapper = plugins.get(pluginId);
+ PluginWrapper pluginWrapper = getPlugin(pluginId);
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
- if (PluginState.STOPPED == pluginWrapper.getPluginState()) {
+ PluginState pluginState = pluginWrapper.getPluginState();
+ if (PluginState.STOPPED == pluginState) {
log.debug("Already stopped plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
return PluginState.STOPPED;
}
// test for disabled plugin
- if (PluginState.DISABLED == pluginWrapper.getPluginState()) {
+ if (PluginState.DISABLED == pluginState) {
// do nothing
- return pluginWrapper.getPluginState();
+ return pluginState;
}
for (PluginDependency dependency : pluginDescriptor.getDependencies()) {
pluginWrapper.getPlugin().stop();
pluginWrapper.setPluginState(PluginState.STOPPED);
startedPlugins.remove(pluginWrapper);
+
+ firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
@Override
public boolean unloadPlugin(String pluginId) {
try {
- PluginState state = stopPlugin(pluginId);
- if (PluginState.STOPPED != state) {
+ PluginState pluginState = stopPlugin(pluginId);
+ if (PluginState.STOPPED != pluginState) {
return false;
}
- PluginWrapper pluginWrapper = plugins.get(pluginId);
+ PluginWrapper pluginWrapper = getPlugin(pluginId);
PluginDescriptor descriptor = pluginWrapper.getDescriptor();
List<PluginDependency> dependencies = descriptor.getDependencies();
for (PluginDependency dependency : dependencies) {
plugins.remove(pluginId);
resolvedPlugins.remove(pluginWrapper);
pathToIdMap.remove(pluginWrapper.getPluginPath());
- extensionFinder.reset();
+
+ firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
// remove the classloader
if (pluginClassLoaders.containsKey(pluginId)) {
}
- PluginWrapper pluginWrapper = plugins.get(pluginId);
+ PluginWrapper pluginWrapper = getPlugin(pluginId);
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
-
- if (PluginState.DISABLED == getPlugin(pluginId).getPluginState()) {
+ PluginState pluginState = pluginWrapper.getPluginState();
+ if (PluginState.DISABLED == pluginState) {
log.debug("Already disabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
return true;
}
if (PluginState.STOPPED == stopPlugin(pluginId)) {
- getPlugin(pluginId).setPluginState(PluginState.DISABLED);
- extensionFinder.reset();
+ pluginWrapper.setPluginState(PluginState.DISABLED);
+
+ firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, PluginState.STOPPED));
if (disabledPlugins.add(pluginId)) {
try {
throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
}
- PluginWrapper pluginWrapper = plugins.get(pluginId);
+ PluginWrapper pluginWrapper = getPlugin(pluginId);
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
-
- if (PluginState.DISABLED != getPlugin(pluginId).getPluginState()) {
+ PluginState pluginState = pluginWrapper.getPluginState();
+ if (PluginState.DISABLED != pluginState) {
log.debug("Plugin plugin '{}:{}' is not disabled", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
return true;
}
return false;
}
- getPlugin(pluginId).setPluginState(PluginState.CREATED);
- extensionFinder.reset();
+ pluginWrapper.setPluginState(PluginState.CREATED);
+
+ firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
log.info("Enabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
if (!plugins.containsKey(pluginId)) {
throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
}
- PluginWrapper pw = getPlugin(pluginId);
- PluginState state = stopPlugin(pluginId);
- if (PluginState.STOPPED != state) {
+ PluginWrapper pluginWrapper = getPlugin(pluginId);
+ PluginState pluginState = stopPlugin(pluginId);
+ if (PluginState.STOPPED != pluginState) {
log.error("Failed to stop plugin {} on delete", pluginId);
return false;
}
return false;
}
- File pluginFolder = new File(pluginsDirectory, pw.getPluginPath());
+ File pluginFolder = new File(pluginsDirectory, pluginWrapper.getPluginPath());
File pluginZip = null;
FileFilter zipFilter = new ZipFileFilter();
File[] zipFiles = pluginsDirectory.listFiles(zipFilter);
if (zipFiles != null) {
// strip prepended / from the plugin path
- String dirName = pw.getPluginPath().substring(1);
+ String dirName = pluginWrapper.getPluginPath().substring(1);
// find the zip file that matches the plugin path
for (File zipFile : zipFiles) {
String name = zipFile.getName().substring(0, zipFile.getName().lastIndexOf('.'));
if (pluginZip != null && pluginZip.exists()) {
FileUtils.delete(pluginZip);
}
+
return true;
}
}
}
log.warn("Failed to find the plugin for {}", clazz);
+
return null;
}
- /**
+ @Override
+ public synchronized void addPluginStateListener(PluginStateListener listener) {
+ pluginStateListeners.add(listener);
+ }
+
+ @Override
+ public synchronized void removePluginStateListener(PluginStateListener listener) {
+ pluginStateListeners.remove(listener);
+ }
+
+ /**
* Add the possibility to override the PluginDescriptorFinder.
* By default if getRuntimeMode() returns RuntimeMode.DEVELOPMENT than a
* PropertiesPluginDescriptorFinder is returned else this method returns
* Add the possibility to override the ExtensionFinder.
*/
protected ExtensionFinder createExtensionFinder() {
- return new DefaultExtensionFinder(this);
+ DefaultExtensionFinder extensionFinder = new DefaultExtensionFinder(this);
+ addPluginStateListener(extensionFinder);
+
+ return extensionFinder;
}
/**
startedPlugins = new ArrayList<PluginWrapper>();
disabledPlugins = new ArrayList<String>();
+ pluginStateListeners = new ArrayList<PluginStateListener>();
+
pluginClasspath = createPluginClasspath();
pluginDescriptorFinder = createPluginDescriptorFinder();
extensionFinder = createExtensionFinder();
}
}
+ private synchronized void firePluginStateEvent(PluginStateEvent event) {
+ for (PluginStateListener listener : pluginStateListeners) {
+ log.debug("Fire '{}' to '{}'", event, listener);
+ listener.pluginStateChanged(event);
+ }
+ }
+
}
/*
* 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.
public PluginLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, File pluginRepository, PluginClasspath pluginClasspath) {
this.pluginRepository = pluginRepository;
- this.pluginClasspath = pluginClasspath;
- ClassLoader parent = getClass().getClassLoader();
- pluginClassLoader = new PluginClassLoader(pluginManager, pluginDescriptor, parent);
- log.debug("Created class loader {}", pluginClassLoader);
+ this.pluginClasspath = pluginClasspath;
+
+ ClassLoader parent = getClass().getClassLoader();
+ pluginClassLoader = new PluginClassLoader(pluginManager, pluginDescriptor, parent);
+ log.debug("Created class loader '{}'", pluginClassLoader);
}
public File getPluginRepository() {
private boolean loadClasses() {
List<String> classesDirectories = pluginClasspath.getClassesDirectories();
-
+
// add each classes directory to plugin class loader
for (String classesDirectory : classesDirectories) {
// make 'classesDirectory' absolute
File file = new File(pluginRepository, classesDirectory).getAbsoluteFile();
-
+
if (file.exists() && file.isDirectory()) {
log.debug("Found '{}' directory", file.getPath());
-
+
try {
pluginClassLoader.addURL(file.toURI().toURL());
log.debug("Added '{}' to the class loader path", file);
*/
private boolean loadJars() {
List<String> libDirectories = pluginClasspath.getLibDirectories();
-
+
// add each jars directory to plugin class loader
for (String libDirectory : libDirectories) {
// make 'libDirectory' absolute
File file = new File(pluginRepository, libDirectory).getAbsoluteFile();
-
+
// collect all jars from current lib directory in jars variable
Vector<File> jars = new Vector<File>();
getJars(jars, file);