2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2014-06-06 08:57:41 +02:00
|
|
|
*/
|
2020-03-23 15:35:58 +01:00
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
package sonia.scm.plugin;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
2014-06-06 08:57:41 +02:00
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Enumeration;
|
2020-03-03 10:39:07 +01:00
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
|
import java.util.Set;
|
2014-06-06 08:57:41 +02:00
|
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
|
|
|
|
|
2014-06-06 08:57:41 +02:00
|
|
|
/**
|
|
|
|
|
* {@link ClassLoader} which is able to load classes and resources from all
|
|
|
|
|
* plugins.
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
2020-03-03 10:39:07 +01:00
|
|
|
public final class UberClassLoader extends ClassLoader {
|
|
|
|
|
|
|
|
|
|
private final Set<ClassLoader> pluginClassLoaders;
|
|
|
|
|
private final ConcurrentMap<String, WeakReference<Class<?>>> cache = Maps.newConcurrentMap();
|
|
|
|
|
|
|
|
|
|
public UberClassLoader(ClassLoader parent, Iterable<InstalledPlugin> plugins) {
|
|
|
|
|
this(parent, collectClassLoaders(plugins));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Set<ClassLoader> collectClassLoaders(Iterable<InstalledPlugin> plugins) {
|
|
|
|
|
ImmutableSet.Builder<ClassLoader> classLoaders = ImmutableSet.builder();
|
|
|
|
|
plugins.forEach(plugin -> classLoaders.add(plugin.getClassLoader()));
|
|
|
|
|
return classLoaders.build();
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
@VisibleForTesting
|
|
|
|
|
UberClassLoader(ClassLoader parent, Set<ClassLoader> pluginClassLoaders) {
|
|
|
|
|
super(parent);
|
|
|
|
|
this.pluginClassLoaders = pluginClassLoaders;
|
|
|
|
|
}
|
2014-06-06 08:57:41 +02:00
|
|
|
|
|
|
|
|
@Override
|
2020-03-03 10:39:07 +01:00
|
|
|
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
2014-06-06 08:57:41 +02:00
|
|
|
Class<?> clazz = getFromCache(name);
|
|
|
|
|
|
2019-01-07 07:57:58 +01:00
|
|
|
if (clazz == null) {
|
|
|
|
|
clazz = findClassInPlugins(name);
|
|
|
|
|
cache.put(name, new WeakReference<>(clazz));
|
|
|
|
|
}
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2019-01-07 07:57:58 +01:00
|
|
|
return clazz;
|
|
|
|
|
}
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2019-01-07 07:57:58 +01:00
|
|
|
private Class<?> findClassInPlugins(String name) throws ClassNotFoundException {
|
2020-03-03 10:39:07 +01:00
|
|
|
for (ClassLoader pluginClassLoader : pluginClassLoaders) {
|
|
|
|
|
Class<?> clazz = findClass(pluginClassLoader, name);
|
2019-01-07 07:57:58 +01:00
|
|
|
if (clazz != null) {
|
|
|
|
|
return clazz;
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-07 07:57:58 +01:00
|
|
|
throw new ClassNotFoundException("could not find class " + name + " in any of the installed plugins");
|
|
|
|
|
}
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2019-01-07 07:57:58 +01:00
|
|
|
private Class<?> findClass(ClassLoader classLoader, String name) {
|
|
|
|
|
try {
|
|
|
|
|
// load class could be slow, perhaps we should call
|
|
|
|
|
// find class via reflection ???
|
|
|
|
|
return classLoader.loadClass(name);
|
|
|
|
|
} catch (ClassNotFoundException ex) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2020-03-03 10:39:07 +01:00
|
|
|
protected URL findResource(String name) {
|
2014-06-06 08:57:41 +02:00
|
|
|
URL url = null;
|
|
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
for (ClassLoader pluginClassLoader : pluginClassLoaders) {
|
|
|
|
|
url = pluginClassLoader.getResource(name);
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
if (url != null) {
|
2014-06-06 08:57:41 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2020-03-03 10:39:07 +01:00
|
|
|
@SuppressWarnings("squid:S2112")
|
|
|
|
|
protected Enumeration<URL> findResources(String name) throws IOException {
|
|
|
|
|
Set<URL> urls = new LinkedHashSet<>();
|
2014-06-06 08:57:41 +02:00
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
for (ClassLoader pluginClassLoader : pluginClassLoaders) {
|
|
|
|
|
urls.addAll(Collections.list(pluginClassLoader.getResources(name)));
|
2014-06-06 08:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Collections.enumeration(urls);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
private Class<?> getFromCache(String name) {
|
2014-06-06 08:57:41 +02:00
|
|
|
Class<?> clazz = null;
|
|
|
|
|
WeakReference<Class<?>> ref = cache.get(name);
|
|
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
if (ref != null) {
|
2014-06-06 08:57:41 +02:00
|
|
|
clazz = ref.get();
|
|
|
|
|
|
2020-03-03 10:39:07 +01:00
|
|
|
if (clazz == null) {
|
2014-06-06 08:57:41 +02:00
|
|
|
cache.remove(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return clazz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|