123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824 |
- /*
- * 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.FileFilter;
- import java.io.IOException;
- import java.util.*;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import ro.fortsoft.pf4j.util.*;
-
- /**
- * Default implementation of the PluginManager interface.
- *
- * @author Decebal Suiu
- */
- public class DefaultPluginManager implements PluginManager {
-
- private static final Logger log = LoggerFactory.getLogger(DefaultPluginManager.class);
-
- public static final String DEFAULT_PLUGINS_DIRECTORY = "plugins";
- public static final String DEVELOPMENT_PLUGINS_DIRECTORY = "../plugins";
-
- private File pluginsDirectory;
-
- private ExtensionFinder extensionFinder;
-
- private PluginDescriptorFinder pluginDescriptorFinder;
-
- private PluginClasspath pluginClasspath;
-
- /**
- * A map of plugins this manager is responsible for (the key is the 'pluginId').
- */
- private Map<String, PluginWrapper> plugins;
-
- /**
- * A map of plugin class loaders (he key is the 'pluginId').
- */
- private Map<String, PluginClassLoader> pluginClassLoaders;
-
- /**
- * A relation between 'pluginPath' and 'pluginId'
- */
- private Map<String, String> pathToIdMap;
-
- /**
- * A list with unresolved plugins (unresolved dependency).
- */
- private List<PluginWrapper> unresolvedPlugins;
-
- /**
- * A list with resolved plugins (resolved dependency).
- */
- private List<PluginWrapper> resolvedPlugins;
-
- /**
- * A list with started plugins.
- */
- private List<PluginWrapper> startedPlugins;
-
- /**
- * 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.
- */
- private RuntimeMode runtimeMode;
-
- /**
- * The system version used for comparisons to the plugin requires attribute.
- */
- private Version systemVersion = Version.ZERO;
-
- private PluginFactory pluginFactory;
- private ExtensionFactory extensionFactory;
- private PluginStatusProvider pluginStatusProvider;
-
- /**
- * The plugins repository.
- */
- private PluginRepository pluginRepository;
-
- /**
- * The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins").
- */
- public DefaultPluginManager() {
- this.pluginsDirectory = createPluginsDirectory();
-
- initialize();
- }
-
- /**
- * Constructs DefaultPluginManager which the given plugins directory.
- *
- * @param pluginsDirectory the directory to search for plugins
- */
- public DefaultPluginManager(File pluginsDirectory) {
- this.pluginsDirectory = pluginsDirectory;
-
- initialize();
- }
-
- @Override
- public void setSystemVersion(Version version) {
- systemVersion = version;
- }
-
- @Override
- public Version getSystemVersion() {
- return systemVersion;
- }
-
- @Override
- public List<PluginWrapper> getPlugins() {
- return new ArrayList<PluginWrapper>(plugins.values());
- }
-
- @Override
- public List<PluginWrapper> getPlugins(PluginState pluginState) {
- List<PluginWrapper> plugins= new ArrayList<PluginWrapper>();
- for (PluginWrapper plugin : getPlugins()) {
- if (pluginState.equals(plugin.getPluginState())) {
- plugins.add(plugin);
- }
- }
-
- return plugins;
- }
-
- @Override
- public List<PluginWrapper> getResolvedPlugins() {
- return resolvedPlugins;
- }
-
- @Override
- public List<PluginWrapper> getUnresolvedPlugins() {
- return unresolvedPlugins;
- }
-
- @Override
- public List<PluginWrapper> getStartedPlugins() {
- return startedPlugins;
- }
-
- @Override
- public PluginWrapper getPlugin(String pluginId) {
- return plugins.get(pluginId);
- }
-
- @Override
- public String loadPlugin(File pluginArchiveFile) {
- if ((pluginArchiveFile == null) || !pluginArchiveFile.exists()) {
- throw new IllegalArgumentException(String.format("Specified plugin %s does not exist!", pluginArchiveFile));
- }
-
- log.debug("Loading plugin from '{}'", 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);
-
- firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, null));
-
- return pluginWrapper.getDescriptor().getPluginId();
- } catch (PluginException e) {
- log.error(e.getMessage(), e);
- }
-
- return null;
- }
-
- /**
- * Start all active plugins.
- */
- @Override
- public void startPlugins() {
- for (PluginWrapper pluginWrapper : resolvedPlugins) {
- PluginState pluginState = pluginWrapper.getPluginState();
- if ((PluginState.DISABLED != pluginState) && (PluginState.STARTED != pluginState)) {
- try {
- PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
- log.info("Start plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
- pluginWrapper.getPlugin().start();
- pluginWrapper.setPluginState(PluginState.STARTED);
- startedPlugins.add(pluginWrapper);
-
- firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
- } catch (PluginException e) {
- log.error(e.getMessage(), e);
- }
- }
- }
- }
-
- /**
- * Start the specified plugin and it's dependencies.
- */
- @Override
- public PluginState startPlugin(String pluginId) {
- if (!plugins.containsKey(pluginId)) {
- throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
- }
-
- PluginWrapper pluginWrapper = getPlugin(pluginId);
- PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
- PluginState pluginState = pluginWrapper.getPluginState();
- if (PluginState.STARTED == pluginState) {
- log.debug("Already started plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
- return PluginState.STARTED;
- }
-
- if (PluginState.DISABLED == pluginState) {
- // automatically enable plugin on manual plugin start
- if (!enablePlugin(pluginId)) {
- return pluginState;
- }
- }
-
- for (PluginDependency dependency : pluginDescriptor.getDependencies()) {
- startPlugin(dependency.getPluginId());
- }
-
- try {
- log.info("Start plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
- pluginWrapper.getPlugin().start();
- pluginWrapper.setPluginState(PluginState.STARTED);
- startedPlugins.add(pluginWrapper);
-
- firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
- } catch (PluginException e) {
- log.error(e.getMessage(), e);
- }
-
- return pluginWrapper.getPluginState();
- }
-
- /**
- * Stop all active plugins.
- */
- @Override
- public void stopPlugins() {
- // stop started plugins in reverse order
- Collections.reverse(startedPlugins);
- Iterator<PluginWrapper> itr = startedPlugins.iterator();
- while (itr.hasNext()) {
- PluginWrapper pluginWrapper = itr.next();
- 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);
- }
- }
- }
- }
-
- /**
- * Stop the specified plugin and it's dependencies.
- */
- @Override
- public PluginState stopPlugin(String pluginId) {
- if (!plugins.containsKey(pluginId)) {
- throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
- }
-
- PluginWrapper pluginWrapper = getPlugin(pluginId);
- PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
- 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 == pluginState) {
- // do nothing
- return pluginState;
- }
-
- for (PluginDependency dependency : pluginDescriptor.getDependencies()) {
- stopPlugin(dependency.getPluginId());
- }
-
- try {
- log.info("Stop plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
- pluginWrapper.getPlugin().stop();
- pluginWrapper.setPluginState(PluginState.STOPPED);
- startedPlugins.remove(pluginWrapper);
-
- firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
- } catch (PluginException e) {
- log.error(e.getMessage(), e);
- }
-
- return pluginWrapper.getPluginState();
- }
-
- /**
- * Load plugins.
- */
- @Override
- public void loadPlugins() {
- log.debug("Lookup plugins in '{}'", pluginsDirectory.getAbsolutePath());
- // check for plugins directory
- if (!pluginsDirectory.exists() || !pluginsDirectory.isDirectory()) {
- log.error("No '{}' directory", pluginsDirectory.getAbsolutePath());
- return;
- }
-
- // expand all plugin archives
- List<File> pluginArchives = pluginRepository.getPluginArchives();
- for (File archiveFile : pluginArchives) {
- try {
- expandPluginArchive(archiveFile);
- } catch (IOException e) {
- log.error(e.getMessage(), e);
- }
- }
-
- // check for no plugins
- List<FileFilter> filterList = new ArrayList<FileFilter>();
- filterList.add(new DirectoryFileFilter());
- filterList.add(new NotFileFilter(createHiddenPluginFilter()));
- FileFilter pluginsFilter = new AndFileFilter(filterList);
- File[] directories = pluginsDirectory.listFiles(pluginsFilter);
- if (directories == null) {
- directories = new File[0];
- }
- log.debug("Found {} possible plugins: {}", directories.length, directories);
- if (directories.length == 0) {
- log.info("No plugins");
- return;
- }
-
- // load any plugin from plugins directory
- for (File directory : directories) {
- try {
- loadPluginDirectory(directory);
- } catch (PluginException e) {
- log.error(e.getMessage(), e);
- }
- }
-
- // resolve 'unresolvedPlugins'
- try {
- resolvePlugins();
- } catch (PluginException e) {
- log.error(e.getMessage(), e);
- }
- }
-
- @Override
- public boolean unloadPlugin(String pluginId) {
- try {
- PluginState pluginState = stopPlugin(pluginId);
- if (PluginState.STARTED == pluginState) {
- return false;
- }
-
- PluginWrapper pluginWrapper = getPlugin(pluginId);
- PluginDescriptor descriptor = pluginWrapper.getDescriptor();
- List<PluginDependency> dependencies = descriptor.getDependencies();
- for (PluginDependency dependency : dependencies) {
- if (!unloadPlugin(dependency.getPluginId())) {
- return false;
- }
- }
-
- // remove the plugin
- plugins.remove(pluginId);
- resolvedPlugins.remove(pluginWrapper);
- pathToIdMap.remove(pluginWrapper.getPluginPath());
-
- firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
-
- // remove the classloader
- if (pluginClassLoaders.containsKey(pluginId)) {
- PluginClassLoader classLoader = pluginClassLoaders.remove(pluginId);
- classLoader.dispose();
- }
-
- return true;
- } catch (IllegalArgumentException e) {
- // ignore not found exceptions because this method is recursive
- }
-
- return false;
- }
-
- @Override
- public boolean disablePlugin(String pluginId) {
- if (!plugins.containsKey(pluginId)) {
- throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
- }
-
- PluginWrapper pluginWrapper = getPlugin(pluginId);
- PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
- PluginState pluginState = pluginWrapper.getPluginState();
- if (PluginState.DISABLED == pluginState) {
- log.debug("Already disabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
- return true;
- }
-
- if (PluginState.STOPPED == stopPlugin(pluginId)) {
- pluginWrapper.setPluginState(PluginState.DISABLED);
-
- firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, PluginState.STOPPED));
-
- if (!pluginStatusProvider.disablePlugin(pluginId)) {
- return false;
- }
-
- log.info("Disabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
-
- return true;
- }
-
- return false;
- }
-
- @Override
- public boolean enablePlugin(String pluginId) {
- if (!plugins.containsKey(pluginId)) {
- throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
- }
-
- PluginWrapper pluginWrapper = getPlugin(pluginId);
- if (!isPluginValid(pluginWrapper)) {
- log.warn("Plugin '{}:{}' can not be enabled", pluginWrapper.getPluginId(),
- pluginWrapper.getDescriptor().getVersion());
- return false;
- }
-
- PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
- PluginState pluginState = pluginWrapper.getPluginState();
- if (PluginState.DISABLED != pluginState) {
- log.debug("Plugin '{}:{}' is not disabled", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
- return true;
- }
-
- if (!pluginStatusProvider.enablePlugin(pluginId)) {
- return false;
- }
-
- pluginWrapper.setPluginState(PluginState.CREATED);
-
- firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState));
-
- log.info("Enabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
-
- return true;
- }
-
- @Override
- public boolean deletePlugin(String pluginId) {
- if (!plugins.containsKey(pluginId)) {
- throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
- }
-
- PluginWrapper pluginWrapper = getPlugin(pluginId);
- PluginState pluginState = stopPlugin(pluginId);
- if (PluginState.STARTED == pluginState) {
- log.error("Failed to stop plugin {} on delete", pluginId);
- return false;
- }
-
- if (!unloadPlugin(pluginId)) {
- log.error("Failed to unload plugin {} on delete", pluginId);
- return false;
- }
-
- File pluginFolder = new File(pluginsDirectory, pluginWrapper.getPluginPath());
-
- if (pluginFolder.exists()) {
- FileUtils.delete(pluginFolder);
- }
-
- pluginRepository.deletePluginArchive(pluginWrapper.getPluginPath());
-
- return true;
- }
-
- /**
- * Get plugin class loader for this path.
- */
- @Override
- public PluginClassLoader getPluginClassLoader(String pluginId) {
- return pluginClassLoaders.get(pluginId);
- }
-
- @Override
- public <T> List<T> getExtensions(Class<T> type) {
- List<ExtensionWrapper<T>> extensionsWrapper = extensionFinder.find(type);
- List<T> extensions = new ArrayList<T>(extensionsWrapper.size());
- for (ExtensionWrapper<T> extensionWrapper : extensionsWrapper) {
- extensions.add(extensionWrapper.getExtension());
- }
-
- return extensions;
- }
-
- @Override
- public Set<String> getExtensionClassNames(String pluginId) {
- return extensionFinder.findClassNames(pluginId);
- }
-
- @Override
- public RuntimeMode getRuntimeMode() {
- if (runtimeMode == null) {
- // retrieves the runtime mode from system
- String modeAsString = System.getProperty("pf4j.mode", RuntimeMode.DEPLOYMENT.toString());
- runtimeMode = RuntimeMode.byName(modeAsString);
- }
-
- return runtimeMode;
- }
-
- /**
- * Retrieves the {@link PluginWrapper} that loaded the given class 'clazz'.
- */
- public PluginWrapper whichPlugin(Class<?> clazz) {
- ClassLoader classLoader = clazz.getClassLoader();
- for (PluginWrapper plugin : resolvedPlugins) {
- if (plugin.getPluginClassLoader() == classLoader) {
- return plugin;
- }
- }
- 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);
- }
-
- public Version getVersion() {
- String version = null;
-
- Package pf4jPackage = getClass().getPackage();
- if (pf4jPackage != null) {
- version = pf4jPackage.getImplementationVersion();
- if (version == null) {
- version = pf4jPackage.getSpecificationVersion();
- }
- }
-
- return (version != null) ? Version.createVersion(version) : Version.ZERO;
- }
-
- /**
- * Add the possibility to override the PluginDescriptorFinder.
- * By default if getRuntimeMode() returns RuntimeMode.DEVELOPMENT than a
- * PropertiesPluginDescriptorFinder is returned else this method returns
- * DefaultPluginDescriptorFinder.
- */
- protected PluginDescriptorFinder createPluginDescriptorFinder() {
- if (RuntimeMode.DEVELOPMENT.equals(getRuntimeMode())) {
- return new PropertiesPluginDescriptorFinder();
- }
-
- return new DefaultPluginDescriptorFinder(pluginClasspath);
- }
-
- /**
- * Add the possibility to override the ExtensionFinder.
- */
- protected ExtensionFinder createExtensionFinder() {
- DefaultExtensionFinder extensionFinder = new DefaultExtensionFinder(this, extensionFactory);
- addPluginStateListener(extensionFinder);
-
- return extensionFinder;
- }
-
- /**
- * Add the possibility to override the PluginClassPath.
- * By default if getRuntimeMode() returns RuntimeMode.DEVELOPMENT than a
- * DevelopmentPluginClasspath is returned else this method returns
- * PluginClasspath.
- */
- protected PluginClasspath createPluginClasspath() {
- if (RuntimeMode.DEVELOPMENT.equals(getRuntimeMode())) {
- return new DevelopmentPluginClasspath();
- }
-
- return new PluginClasspath();
- }
-
- protected PluginStatusProvider createPluginStatusProvider() {
- return new DefaultPluginStatusProvider(pluginsDirectory);
- }
-
- protected PluginRepository createPluginRepository() {
- return new DefaultPluginRepository(pluginsDirectory, new ZipFileFilter());
- }
-
- protected boolean isPluginDisabled(String pluginId) {
- return pluginStatusProvider.isPluginDisabled(pluginId);
- }
-
- protected boolean isPluginValid(PluginWrapper pluginWrapper) {
- Version requires = pluginWrapper.getDescriptor().getRequires();
- Version system = getSystemVersion();
- if (system.isZero() || system.atLeast(requires)) {
- return true;
- }
-
- log.warn("Plugin '{}:{}' requires a minimum system version of {}",
- pluginWrapper.getPluginId(),
- pluginWrapper.getDescriptor().getVersion(),
- requires);
-
- return false;
- }
-
- protected FileFilter createHiddenPluginFilter() {
- return new HiddenFilter();
- }
-
- /**
- * Add the possibility to override the plugins directory.
- * If a "pf4j.pluginsDir" system property is defined than this method returns
- * that directory.
- * If getRuntimeMode() returns RuntimeMode.DEVELOPMENT than a
- * DEVELOPMENT_PLUGINS_DIRECTORY ("../plugins") is returned else this method returns
- * DEFAULT_PLUGINS_DIRECTORY ("plugins").
- * @return
- */
- protected File createPluginsDirectory() {
- String pluginsDir = System.getProperty("pf4j.pluginsDir");
- if (pluginsDir == null) {
- if (RuntimeMode.DEVELOPMENT.equals(getRuntimeMode())) {
- pluginsDir = DEVELOPMENT_PLUGINS_DIRECTORY;
- } else {
- pluginsDir = DEFAULT_PLUGINS_DIRECTORY;
- }
- }
-
- return new File(pluginsDir);
- }
-
- /**
- * Add the possibility to override the PluginFactory..
- */
- protected PluginFactory createPluginFactory() {
- return new DefaultPluginFactory();
- }
-
- /**
- * Add the possibility to override the ExtensionFactory.
- */
- protected ExtensionFactory createExtensionFactory() {
- return new DefaultExtensionFactory();
- }
-
- private void initialize() {
- plugins = new HashMap<String, PluginWrapper>();
- pluginClassLoaders = new HashMap<String, PluginClassLoader>();
- pathToIdMap = new HashMap<String, String>();
- unresolvedPlugins = new ArrayList<PluginWrapper>();
- resolvedPlugins = new ArrayList<PluginWrapper>();
- startedPlugins = new ArrayList<PluginWrapper>();
-
- pluginStateListeners = new ArrayList<PluginStateListener>();
-
- log.info("PF4J version {} in '{}' mode", getVersion(), getRuntimeMode());
-
- pluginClasspath = createPluginClasspath();
- pluginFactory = createPluginFactory();
- extensionFactory = createExtensionFactory();
- pluginDescriptorFinder = createPluginDescriptorFinder();
- extensionFinder = createExtensionFinder();
- pluginStatusProvider = createPluginStatusProvider();
- pluginRepository = createPluginRepository();
-
- System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath());
- }
-
- 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 null;
- }
-
- // retrieves the plugin descriptor
- log.debug("Find plugin descriptor '{}'", pluginPath);
- PluginDescriptor pluginDescriptor = pluginDescriptorFinder.find(pluginDirectory);
- log.debug("Descriptor " + pluginDescriptor);
- String pluginClassName = pluginDescriptor.getPluginClass();
- log.debug("Class '{}' for plugin '{}'", pluginClassName, pluginPath);
-
- // load plugin
- log.debug("Loading plugin '{}'", pluginPath);
- PluginLoader pluginLoader = new PluginLoader(this, pluginDescriptor, pluginDirectory, pluginClasspath);
- pluginLoader.load();
- log.debug("Loaded plugin '{}'", pluginPath);
-
- // create the plugin wrapper
- log.debug("Creating wrapper for plugin '{}'", pluginPath);
- PluginWrapper pluginWrapper = new PluginWrapper(pluginDescriptor, pluginPath, pluginLoader.getPluginClassLoader());
- pluginWrapper.setPluginFactory(pluginFactory);
- pluginWrapper.setRuntimeMode(getRuntimeMode());
-
- // test for disabled plugin
- if (isPluginDisabled(pluginDescriptor.getPluginId())) {
- log.info("Plugin '{}' is disabled", pluginPath);
- pluginWrapper.setPluginState(PluginState.DISABLED);
- }
-
- // validate the plugin
- if (!isPluginValid(pluginWrapper)) {
- log.info("Plugin '{}' is disabled", pluginPath);
- pluginWrapper.setPluginState(PluginState.DISABLED);
- }
-
- log.debug("Created wrapper '{}' for plugin '{}'", pluginWrapper, pluginPath);
-
- String pluginId = pluginDescriptor.getPluginId();
-
- // add plugin to the list with plugins
- plugins.put(pluginId, pluginWrapper);
- unresolvedPlugins.add(pluginWrapper);
-
- // add plugin class loader to the list with class loaders
- PluginClassLoader pluginClassLoader = pluginLoader.getPluginClassLoader();
- pluginClassLoaders.put(pluginId, pluginClassLoader);
-
- return pluginWrapper;
- }
-
- private File expandPluginArchive(File pluginArchiveFile) throws IOException {
- String fileName = pluginArchiveFile.getName();
- long pluginArchiveDate = pluginArchiveFile.lastModified();
- String pluginName = fileName.substring(0, fileName.length() - 4);
- File pluginDirectory = new File(pluginsDirectory, pluginName);
- // 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();
-
- // expand '.zip' file
- Unzip unzip = new Unzip();
- unzip.setSource(pluginArchiveFile);
- unzip.setDestination(pluginDirectory);
- unzip.extract();
- }
-
- return pluginDirectory;
- }
-
- private void resolvePlugins() throws PluginException {
- resolveDependencies();
- }
-
- private void resolveDependencies() throws PluginException {
- DependencyResolver dependencyResolver = new DependencyResolver(unresolvedPlugins);
- resolvedPlugins = dependencyResolver.getSortedPlugins();
- for (PluginWrapper pluginWrapper : resolvedPlugins) {
- unresolvedPlugins.remove(pluginWrapper);
- log.info("Plugin '{}' resolved", pluginWrapper.getDescriptor().getPluginId());
- }
- }
-
- private synchronized void firePluginStateEvent(PluginStateEvent event) {
- for (PluginStateListener listener : pluginStateListeners) {
- log.debug("Fire '{}' to '{}'", event, listener);
- listener.pluginStateChanged(event);
- }
- }
-
- }
|