2014-06-06 08:57:41 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
|
2020-03-03 10:39:07 +01:00
|
|
|
* <p>
|
2014-06-06 08:57:41 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2020-03-03 10:39:07 +01:00
|
|
|
* <p>
|
2014-06-06 08:57:41 +02:00
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer. 2. Redistributions in
|
|
|
|
|
* binary form must reproduce the above copyright notice, this list of
|
|
|
|
|
* conditions and the following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
|
|
|
|
|
* nor the names of its contributors may be used to endorse or promote products
|
|
|
|
|
* derived from this software without specific prior written permission.
|
2020-03-03 10:39:07 +01:00
|
|
|
* <p>
|
2014-06-06 08:57:41 +02:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2020-03-03 10:39:07 +01:00
|
|
|
* <p>
|
2014-06-06 08:57:41 +02:00
|
|
|
* http://bitbucket.org/sdorra/scm-manager
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|