Skip to content

Commit

Permalink
Make it possible for plugins to request a shared class loader
Browse files Browse the repository at this point in the history
In #2280 we changed the plugin loader so that all plugins share one
class loader and they can see each other. (needed for plugin
inder-dependencies)

This is problematic when different plugins have conflicting
dependencies. (see #2436)

With this change, every plugin gets its own class loader by default so
we can avoid dependency clashes. Plugins which depend on other plugins
can request a shared class loader via a graylog-plugin.properties file.

Fixes #2436
  • Loading branch information
bernd committed Jul 20, 2016
1 parent 9802a85 commit 63fbb50
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -99,9 +100,24 @@ private Iterable<Plugin> loadJarPlugins() {
.filter(Objects::nonNull)
.collect(Collectors.toList());

// Create one URL class loader for all plugins so they can see each other.
// This makes plugin inter-dependencies work.
classLoader.addClassLoader(URLClassLoader.newInstance(urls.toArray(new URL[urls.size()])));
final List<URL> sharedClassLoaderUrls = new ArrayList<>();
urls.forEach(url -> {
final PluginProperties properties = PluginProperties.fromJarFile(url.getFile());

// Decide whether to create a separate, isolated class loader for the plugin. When the plugin is isolated
// (the default), it gets its own class loader and cannot see other plugins. Plugins which are not
// isolated share one class loader so they can see each other. (makes plugin inter-dependencies work)
if (properties.isIsolated()) {
LOG.debug("Create isolated class loader for {}", url);
classLoader.addClassLoader(URLClassLoader.newInstance(new URL[]{url}));
} else {
LOG.debug("Use shared class loader for {}", url);
sharedClassLoaderUrls.add(url);
}
});

LOG.debug("Create shared class loader for {} plugins: {}", sharedClassLoaderUrls.size(), sharedClassLoaderUrls);
classLoader.addClassLoader(URLClassLoader.newInstance(sharedClassLoaderUrls.toArray(new URL[sharedClassLoaderUrls.size()])));

final ServiceLoader<Plugin> pluginServiceLoader = ServiceLoader.load(Plugin.class, classLoader);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.shared.plugins;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Properties;
import java.util.jar.JarFile;

import static java.util.Objects.requireNonNull;

public class PluginProperties {
private static final Logger LOG = LoggerFactory.getLogger(PluginProperties.class);

private static final String GRAYLOG_PLUGIN_PROPERTIES = "graylog-plugin.properties";

private static final String PROPERTY_ISOLATED = "isolated";
private static final String PROPERTY_ISOLATED_DEFAULT = "true";

private final boolean isolated;

public PluginProperties(Properties properties) {
this.isolated = Boolean.parseBoolean((String) properties.getOrDefault(PROPERTY_ISOLATED, PROPERTY_ISOLATED_DEFAULT));
}

public static PluginProperties fromJarFile(final String filename) {
// Try to load the plugin properties from the JAR file. This fails for plugins which do not have the
// properties file.
final Properties properties = new Properties();
try {
LOG.debug("Loading {} from {}", GRAYLOG_PLUGIN_PROPERTIES, filename);
final JarFile jarFile = new JarFile(requireNonNull(filename));
final InputStream inputStream = jarFile.getInputStream(jarFile.getEntry(GRAYLOG_PLUGIN_PROPERTIES));
properties.load(inputStream);
} catch (Exception e) {
LOG.debug("Unable to load {} from plugin {}", GRAYLOG_PLUGIN_PROPERTIES, filename, e);
}

return new PluginProperties(properties);
}

public boolean isIsolated() {
return this.isolated;
}
}

0 comments on commit 63fbb50

Please sign in to comment.