/** * Shortcut */ public String getPluginId() { return getDescriptor().getPluginId(); }
@Override public String loadPlugin(Path pluginPath) { if ((pluginPath == null) || Files.notExists(pluginPath)) { throw new IllegalArgumentException(String.format("Specified plugin %s does not exist!", pluginPath)); } log.debug("Loading plugin from '{}'", pluginPath); try { PluginWrapper pluginWrapper = loadPluginFromPath(pluginPath); // try to resolve the loaded plugin together with other possible plugins that depend on this plugin resolvePlugins(); return pluginWrapper.getDescriptor().getPluginId(); } catch (PluginException e) { log.error(e.getMessage(), e); } return null; }
String pluginClassName = pluginWrapper.getDescriptor().getPluginClass(); log.debug("Create instance for plugin '{}'", pluginClassName);
/** * Check if this plugin is valid (satisfies "requires" param) for a given system version. * * @param pluginWrapper the plugin to check * @return true if plugin satisfies the "requires" or if requires was left blank */ protected boolean isPluginValid(PluginWrapper pluginWrapper) { String requires = pluginWrapper.getDescriptor().getRequires().trim(); if (!isExactVersionAllowed() && requires.matches("^\\d+\\.\\d+\\.\\d+$")) { // If exact versions are not allowed in requires, rewrite to >= expression requires = ">=" + requires; } if (systemVersion.equals("0.0.0") || versionManager.checkVersionConstraint(systemVersion, requires)) { return true; } PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); log.warn("Plugin '{}' requires a minimum system version of {}, and you have {}", getPluginLabel(pluginDescriptor), pluginWrapper.getDescriptor().getRequires(), getSystemVersion()); return false; }
@Override public Map<String, Set<String>> readPluginsStorages() { log.debug("Reading extensions storages from plugins"); Map<String, Set<String>> result = new LinkedHashMap<>(); List<PluginWrapper> plugins = pluginManager.getPlugins(); for (PluginWrapper plugin : plugins) { String pluginId = plugin.getDescriptor().getPluginId(); log.debug("Reading extensions storage from plugin '{}'", pluginId); Set<String> bucket = new HashSet<>(); try { Enumeration<URL> urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(getExtensionsResource()); if (urls.hasMoreElements()) { collectExtensions(urls, bucket); } else { log.debug("Cannot find '{}'", getExtensionsResource()); } debugExtensions(bucket); result.put(pluginId, bucket); } catch (IOException e) { log.error(e.getMessage(), e); } } return result; }
@Override public Map<String, Set<String>> readPluginsStorages() { log.debug("Reading extensions storages from plugins"); Map<String, Set<String>> result = new LinkedHashMap<>(); List<PluginWrapper> plugins = pluginManager.getPlugins(); for (PluginWrapper plugin : plugins) { String pluginId = plugin.getDescriptor().getPluginId(); log.debug("Reading extensions storages for plugin '{}'", pluginId); final Set<String> bucket = new HashSet<>(); try { Enumeration<URL> urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(getExtensionsResource()); if (urls.hasMoreElements()) { collectExtensions(urls, bucket); } else { log.debug("Cannot find '{}'", getExtensionsResource()); } debugExtensions(bucket); result.put(pluginId, bucket); } catch (IOException | URISyntaxException e) { log.error(e.getMessage(), e); } } return result; }
/** * 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 { log.info("Stop plugin '{}'", getPluginLabel(pluginWrapper.getDescriptor())); pluginWrapper.getPlugin().stop(); pluginWrapper.setPluginState(PluginState.STOPPED); itr.remove(); firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState)); } catch (PluginException e) { log.error(e.getMessage(), e); } } } }
/** * Start all active plugins. */ @Override public void startPlugins() { for (PluginWrapper pluginWrapper : resolvedPlugins) { PluginState pluginState = pluginWrapper.getPluginState(); if ((PluginState.DISABLED != pluginState) && (PluginState.STARTED != pluginState)) { try { log.info("Start plugin '{}'", getPluginLabel(pluginWrapper.getDescriptor())); pluginWrapper.getPlugin().start(); pluginWrapper.setPluginState(PluginState.STARTED); startedPlugins.add(pluginWrapper); firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState)); } catch (Exception e) { log.error(e.getMessage(), e); } } } }
log.info("Unload plugin '{}'", getPluginLabel(pluginWrapper.getDescriptor()));
descriptors.add(plugin.getDescriptor()); log.info("Plugin '{}' resolved", getPluginLabel(pluginWrapper.getDescriptor()));
@Override public void pluginStateChanged(PluginStateEvent event) { // TODO optimize (do only for some transitions) // clear cache entries = null; // By default we're assuming, that no checks for extension dependencies are necessary. // // A plugin, that has an optional dependency to other plugins, might lead to unloadable // Java classes (NoClassDefFoundError) at application runtime due to possibly missing // dependencies. Therefore we're enabling the check for optional extensions, if the // started plugin contains at least one optional plugin dependency. if (checkForExtensionDependencies == null && PluginState.STARTED.equals(event.getPluginState())) { for (PluginDependency dependency : event.getPlugin().getDescriptor().getDependencies()) { if (dependency.isOptional()) { log.debug("Enable check for extension dependencies via ASM."); checkForExtensionDependencies = true; break; } } } }
@Override public boolean enablePlugin(String pluginId) { checkPluginId(pluginId); PluginWrapper pluginWrapper = getPlugin(pluginId); if (!isPluginValid(pluginWrapper)) { log.warn("Plugin '{}' can not be enabled", getPluginLabel(pluginWrapper.getDescriptor())); return false; } PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); PluginState pluginState = pluginWrapper.getPluginState(); if (PluginState.DISABLED != pluginState) { log.debug("Plugin '{}' is not disabled", getPluginLabel(pluginDescriptor)); return true; } if (!pluginStatusProvider.enablePlugin(pluginId)) { return false; } pluginWrapper.setPluginState(PluginState.CREATED); firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState)); log.info("Enabled plugin '{}'", getPluginLabel(pluginDescriptor)); return true; }
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); PluginState pluginState = pluginWrapper.getPluginState(); if (PluginState.STOPPED == pluginState) {
@Override public boolean disablePlugin(String pluginId) { checkPluginId(pluginId); PluginWrapper pluginWrapper = getPlugin(pluginId); PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); PluginState pluginState = pluginWrapper.getPluginState(); if (PluginState.DISABLED == pluginState) { log.debug("Already disabled plugin '{}'", getPluginLabel(pluginDescriptor)); 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 '{}'", getPluginLabel(pluginDescriptor)); return true; } return false; }
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); PluginState pluginState = pluginWrapper.getPluginState(); if (PluginState.STARTED == pluginState) {
/** * Shortcut */ public String getPluginId() { return getDescriptor().getPluginId(); }
@Override public String loadPlugin(Path pluginPath) { if ((pluginPath == null) || Files.notExists(pluginPath)) { throw new IllegalArgumentException(String.format("Specified plugin %s does not exist!", pluginPath)); } log.debug("Loading plugin from '{}'", pluginPath); try { PluginWrapper pluginWrapper = loadPluginFromPath(pluginPath); // try to resolve the loaded plugin together with other possible plugins that depend on this plugin resolvePlugins(); return pluginWrapper.getDescriptor().getPluginId(); } catch (PluginException e) { log.error(e.getMessage(), e); } return null; }
/** * show the started plugins */ public void showStartedPlugins() { List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins(); for (PluginWrapper plugin : startedPlugins) { PluginDescriptor descriptor = plugin.getDescriptor(); String pluginId = plugin.getDescriptor().getPluginId(); String msg = String.format( "Extensions added by plugin id:'%s' version:'%s' %s:", pluginId, descriptor.getVersion(), descriptor.getPluginDescription()); if (debug) LOGGER.log(Level.INFO, msg); Set<String> extensionClassNames = pluginManager .getExtensionClassNames(pluginId); for (String extension : extensionClassNames) { msg = " " + extension; if (debug) LOGGER.log(Level.INFO, msg); } } }
/** * Start all active plugins. */ @Override public void startPlugins() { for (PluginWrapper pluginWrapper : resolvedPlugins) { PluginState pluginState = pluginWrapper.getPluginState(); if ((PluginState.DISABLED != pluginState) && (PluginState.STARTED != pluginState)) { try { log.info("Start plugin '{}'", getPluginLabel(pluginWrapper.getDescriptor())); pluginWrapper.getPlugin().start(); pluginWrapper.setPluginState(PluginState.STARTED); startedPlugins.add(pluginWrapper); firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, pluginState)); } catch (Exception e) { log.error(e.getMessage(), e); } } } }
@Override public boolean disablePlugin(String pluginId) { checkPluginId(pluginId); PluginWrapper pluginWrapper = getPlugin(pluginId); PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); PluginState pluginState = pluginWrapper.getPluginState(); if (PluginState.DISABLED == pluginState) { log.debug("Already disabled plugin '{}'", getPluginLabel(pluginDescriptor)); 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 '{}'", getPluginLabel(pluginDescriptor)); return true; } return false; }