123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * 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.
- */
-
- package com.dmdirc.plugins;
-
- import com.dmdirc.util.resourcemanager.ResourceManager;
-
- import java.io.IOException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Enumeration;
- import java.util.List;
-
- public class PluginClassLoader extends ClassLoader {
-
- /** The plugin Info object for the plugin we are loading. */
- private final PluginInfo pluginInfo;
- /** Global Class Loader */
- private final GlobalClassLoader globalLoader;
- /** The parent class loaders. */
- private final PluginClassLoader[] parents;
-
- /**
- * Create a new PluginClassLoader.
- *
- * @param info PluginInfo this classloader will be for
- * @param globalLoader Global class loader to use where needed.
- * @param parents Parent ClassLoaders
- */
- public PluginClassLoader(final PluginInfo info, final GlobalClassLoader globalLoader,
- final PluginClassLoader... parents) {
- this.pluginInfo = info;
- this.parents = parents;
- this.globalLoader = globalLoader;
- }
-
- /**
- * Get a PluginClassLoader that is a subclassloader of this one.
- *
- * @param info PluginInfo the new classloader will be for
- *
- * @return A classloader configured with this one as its parent
- */
- public PluginClassLoader getSubClassLoader(final PluginInfo info) {
- return new PluginClassLoader(info, globalLoader, this);
- }
-
- /**
- * Load the plugin with the given className.
- *
- * @param name Class Name of plugin
- *
- * @return plugin class
- *
- * @throws ClassNotFoundException if the class to be loaded could not be found.
- */
- @Override
- public Class<?> loadClass(final String name) throws ClassNotFoundException {
- return loadClass(name, true);
- }
-
- /**
- * Have we already loaded the given class name?
- *
- * @param name Name to check.
- * @param checkGlobal Should we check if the GCL has loaded it as well?
- *
- * @return True if the specified class is loaded, false otherwise
- */
- public boolean isClassLoaded(final String name, final boolean checkGlobal) {
- return findLoadedClass(name) != null || (checkGlobal
- && globalLoader.isClassLoaded(name));
- }
-
- /**
- * Load the plugin with the given className.
- *
- * @param name Class Name of plugin
- * @param askGlobal Ask the global class loaded for this class if we can't find it?
- *
- * @return plugin class
- *
- * @throws ClassNotFoundException if the class to be loaded could not be found.
- */
- @Override
- public Class<?> loadClass(final String name, final boolean askGlobal) throws
- ClassNotFoundException {
- Class<?> loadedClass = null;
- for (PluginClassLoader parent : parents) {
- try {
- loadedClass = parent.loadClass(name, false);
- if (loadedClass != null) {
- return loadedClass;
- }
- } catch (ClassNotFoundException cnfe) {
- // Parent doesn't have the class, carry on trying...
- }
- }
-
- ResourceManager res;
- try {
- res = pluginInfo.getResourceManager();
- } catch (IOException ioe) {
- throw new ClassNotFoundException("Error with resourcemanager", ioe);
- }
-
- final String fileName = name.replace('.', '/') + ".class";
- try {
- if (pluginInfo.isPersistent(name) || !res.resourceExists(fileName)) {
- if (!pluginInfo.isPersistent(name) && askGlobal) {
- return globalLoader.loadClass(name);
- } else {
- // Try to load class from previous load.
- try {
- if (askGlobal) {
- return globalLoader.loadClass(name, pluginInfo);
- }
- } catch (ClassNotFoundException e) {
- /* Class doesn't exist, we load it ourself below */
- }
- }
- }
- } catch (NoClassDefFoundError e) {
- throw new ClassNotFoundException("Error loading '" + name + "' (wanted by "
- + pluginInfo.getMetaData().getName() + ") -> " + e.getMessage(), e);
- }
-
- // Don't duplicate a class
- if (isClassLoaded(name, false)) {
- return findLoadedClass(name);
- }
-
- // We are meant to be loading this one!
- byte[] data;
- if (res.resourceExists(fileName)) {
- data = res.getResourceBytes(fileName);
- } else {
- throw new ClassNotFoundException("Resource '" + name + "' (wanted by " + pluginInfo.
- getMetaData().getName() + ") does not exist.");
- }
-
- try {
- if (pluginInfo.isPersistent(name)) {
- globalLoader.defineClass(name, data);
- } else {
- loadedClass = defineClass(name, data, 0, data.length);
- }
- } catch (LinkageError e) {
- throw new ClassNotFoundException(e.getMessage(), e);
- }
-
- if (loadedClass == null) {
- throw new ClassNotFoundException("Could not load " + name);
- } else {
- resolveClass(loadedClass);
- }
-
- return loadedClass;
- }
-
- /**
- * {@inheritDoc}
- *
- * @since 0.6.3
- */
- @Override
- protected URL findResource(final String name) {
- try {
- final ResourceManager res = pluginInfo.getResourceManager();
- final URL url = res.getResourceURL(name);
-
- if (url != null) {
- return url;
- }
- } catch (IOException ioe) {
- // Do nothing, fall through
- }
-
- // Try the parents
- for (PluginClassLoader parent : parents) {
- final URL url = parent.findResource(name);
-
- if (url != null) {
- return url;
- }
- }
-
- return super.findResource(name);
- }
-
- /**
- * {@inheritDoc}
- *
- * @since 0.6.3
- */
- @Override
- protected Enumeration<URL> findResources(final String name)
- throws IOException {
- final URL resource = findResource(name);
- final List<URL> resources = new ArrayList<>();
-
- if (resource != null) {
- resources.add(resource);
- }
-
- final Enumeration<URL> urls = super.findResources(name);
-
- while (urls.hasMoreElements()) {
- resources.add(urls.nextElement());
- }
-
- return Collections.enumeration(resources);
- }
-
- }
|