123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /*
- * SonarQube
- * Copyright (C) 2009-2018 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.core.platform;
-
- import com.google.common.annotations.VisibleForTesting;
- import com.google.common.base.Strings;
- import java.io.Closeable;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.lang.SystemUtils;
- import org.sonar.api.Plugin;
- import org.sonar.api.utils.log.Loggers;
- import org.sonar.updatecenter.common.Version;
-
- import static java.util.Arrays.asList;
-
- /**
- * Loads the plugin JAR files by creating the appropriate classloaders and by instantiating
- * the entry point classes as defined in manifests. It assumes that JAR files are compatible with current
- * environment (minimal sonarqube version, compatibility between plugins, ...):
- * <ul>
- * <li>server verifies compatibility of JARs before deploying them at startup (see ServerPluginRepository)</li>
- * <li>batch loads only the plugins deployed on server (see BatchPluginRepository)</li>
- * </ul>
- * <p/>
- * Plugins have their own isolated classloader, inheriting only from API classes.
- * Some plugins can extend a "base" plugin, sharing the same classloader.
- * <p/>
- * This class is stateless. It does not keep pointers to classloaders and {@link org.sonar.api.Plugin}.
- */
- public class PluginLoader {
-
- private static final String[] DEFAULT_SHARED_RESOURCES = {"org/sonar/plugins", "com/sonar/plugins", "com/sonarsource/plugins"};
-
- private static final Version COMPATIBILITY_MODE_MAX_VERSION = Version.create("5.2");
-
- private final PluginJarExploder jarExploder;
- private final PluginClassloaderFactory classloaderFactory;
-
- public PluginLoader(PluginJarExploder jarExploder, PluginClassloaderFactory classloaderFactory) {
- this.jarExploder = jarExploder;
- this.classloaderFactory = classloaderFactory;
- }
-
- public Map<String, Plugin> load(Map<String, PluginInfo> infoByKeys) {
- Collection<PluginClassLoaderDef> defs = defineClassloaders(infoByKeys);
- Map<PluginClassLoaderDef, ClassLoader> classloaders = classloaderFactory.create(defs);
- return instantiatePluginClasses(classloaders);
- }
-
- /**
- * Defines the different classloaders to be created. Number of classloaders can be
- * different than number of plugins.
- */
- @VisibleForTesting
- Collection<PluginClassLoaderDef> defineClassloaders(Map<String, PluginInfo> infoByKeys) {
- Map<String, PluginClassLoaderDef> classloadersByBasePlugin = new HashMap<>();
-
- for (PluginInfo info : infoByKeys.values()) {
- String baseKey = basePluginKey(info, infoByKeys);
- PluginClassLoaderDef def = classloadersByBasePlugin.get(baseKey);
- if (def == null) {
- def = new PluginClassLoaderDef(baseKey);
- classloadersByBasePlugin.put(baseKey, def);
- }
- ExplodedPlugin explodedPlugin = jarExploder.explode(info);
- def.addFiles(asList(explodedPlugin.getMain()));
- def.addFiles(explodedPlugin.getLibs());
- def.addMainClass(info.getKey(), info.getMainClass());
-
- for (String defaultSharedResource : DEFAULT_SHARED_RESOURCES) {
- def.getExportMask().addInclusion(String.format("%s/%s/api/", defaultSharedResource, info.getKey()));
- }
-
- // The plugins that extend other plugins can only add some files to classloader.
- // They can't change metadata like ordering strategy or compatibility mode.
- if (Strings.isNullOrEmpty(info.getBasePlugin())) {
- def.setSelfFirstStrategy(info.isUseChildFirstClassLoader());
- Version minSqVersion = info.getMinimalSqVersion();
- boolean compatibilityMode = minSqVersion != null && minSqVersion.compareToIgnoreQualifier(COMPATIBILITY_MODE_MAX_VERSION) < 0;
- if (compatibilityMode) {
- Loggers.get(getClass()).warn("API compatibility mode is no longer supported. In case of error, plugin {} [{}] should package its dependencies.",
- info.getName(), info.getKey());
- }
- }
- }
- return classloadersByBasePlugin.values();
-
- }
-
- /**
- * Instantiates collection of {@link org.sonar.api.Plugin} according to given metadata and classloaders
- *
- * @return the instances grouped by plugin key
- * @throws IllegalStateException if at least one plugin can't be correctly loaded
- */
- @VisibleForTesting
- Map<String, Plugin> instantiatePluginClasses(Map<PluginClassLoaderDef, ClassLoader> classloaders) {
- // instantiate plugins
- Map<String, Plugin> instancesByPluginKey = new HashMap<>();
- for (Map.Entry<PluginClassLoaderDef, ClassLoader> entry : classloaders.entrySet()) {
- PluginClassLoaderDef def = entry.getKey();
- ClassLoader classLoader = entry.getValue();
-
- // the same classloader can be used by multiple plugins
- for (Map.Entry<String, String> mainClassEntry : def.getMainClassesByPluginKey().entrySet()) {
- String pluginKey = mainClassEntry.getKey();
- String mainClass = mainClassEntry.getValue();
- try {
- instancesByPluginKey.put(pluginKey, (Plugin) classLoader.loadClass(mainClass).newInstance());
- } catch (UnsupportedClassVersionError e) {
- throw new IllegalStateException(String.format("The plugin [%s] does not support Java %s",
- pluginKey, SystemUtils.JAVA_VERSION_TRIMMED), e);
- } catch (Throwable e) {
- throw new IllegalStateException(String.format(
- "Fail to instantiate class [%s] of plugin [%s]", mainClass, pluginKey), e);
- }
- }
- }
- return instancesByPluginKey;
- }
-
- public void unload(Collection<Plugin> plugins) {
- for (Plugin plugin : plugins) {
- ClassLoader classLoader = plugin.getClass().getClassLoader();
- if (classLoader instanceof Closeable && classLoader != classloaderFactory.baseClassLoader()) {
- try {
- ((Closeable) classLoader).close();
- } catch (Exception e) {
- Loggers.get(getClass()).error("Fail to close classloader " + classLoader.toString(), e);
- }
- }
- }
- }
-
- /**
- * Get the root key of a tree of plugins. For example if plugin C depends on B, which depends on A, then
- * B and C must be attached to the classloader of A. The method returns A in the three cases.
- */
- static String basePluginKey(PluginInfo plugin, Map<String, PluginInfo> allPluginsPerKey) {
- String base = plugin.getKey();
- String parentKey = plugin.getBasePlugin();
- while (!Strings.isNullOrEmpty(parentKey)) {
- PluginInfo parentPlugin = allPluginsPerKey.get(parentKey);
- base = parentPlugin.getKey();
- parentKey = parentPlugin.getBasePlugin();
- }
- return base;
- }
- }
|