pluginclassloader.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 137 行

JAVA
137
字号
/*
 * $Id: PluginClassLoader.java,v 1.2 2004/01/03 17:47:39 epr Exp $
 */
package org.jnode.plugin.model;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.jnode.util.FileUtils;

/**
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class PluginClassLoader extends ClassLoader {

	/** The plugin jar file */
	private final PluginJar jar;
	private final PluginClassLoader[] prerequisiteLoaders;

	/**
	 * Initialize this instance.
	 * 
	 * @param jar
	 */
	public PluginClassLoader(PluginJar jar, PluginClassLoader[] prerequisiteLoaders) {
		this.jar = jar;
		this.prerequisiteLoaders = prerequisiteLoaders;
	}

	/**
	 * Finds the specified class. This method should be overridden by class loader implementations that follow the new delegation model for loading classes, and will be called by the loadClass method
	 * after checking the parent class loader for the requested class. The default implementation throws ClassNotFoundException.
	 * 
	 * @param name
	 * @return Class
	 * @throws ClassNotFoundException
	 * @see java.lang.ClassLoader#findClass(java.lang.String)
	 */
	protected Class findClass(String name) throws ClassNotFoundException {
		final Class cls = findPluginClass(name);
		if (cls != null) {
			return cls;
		} else {
			// Not found
			throw new ClassNotFoundException(name);
		}
	}

	/**
	 * Finds the specified class. This method should be overridden by class loader implementations that follow the new delegation model for loading classes, and will be called by the loadClass method
	 * after checking the parent class loader for the requested class. The default implementation throws ClassNotFoundException.
	 * 
	 * @param name
	 * @return Class The class, or null if not found.
	 * @see java.lang.ClassLoader#findClass(java.lang.String)
	 */
	protected final Class findPluginClass(String name) {
		// Try the prerequisite loaders first
		final int max = prerequisiteLoaders.length;
		for (int i = 0; i < max; i++) {
			final PluginClassLoader cl = prerequisiteLoaders[i];
			if (cl != null) {
				final Class cls = cl.findPluginClass(name);
				if (cls != null) {
					return cls;
				}
			}
		}
		// Try the loaded classes first
		final Class loadedCls = findLoadedClass(name);
		if (loadedCls != null) {
			return loadedCls;
		}
		// Look for it in our own jar
		final byte[] b = loadClassData(name);
		if (b != null) {
			final Class cls = defineClass(name, b, 0, b.length);
			resolveClass(cls);
			return cls;
		} else {
			return null;
		}
	}

	/**
	 * Does this classloader contain the specified class.
	 * 
	 * @return boolean
	 */
	protected final boolean containsClass(String name) {
		return jar.containsResource(name.replace('.', '/') + ".class");
	}

	/**
	 * Finds the resource with the given name. Class loader implementations should override this method to specify where to find resources.
	 * 
	 * @param name
	 * @return URL
	 * @see java.lang.ClassLoader#findResource(java.lang.String)
	 */
	protected URL findResource(String name) {
		// Try the prerequisite loaders first
		final int max = prerequisiteLoaders.length;
		for (int i = 0; i < max; i++) {
			final PluginClassLoader cl = prerequisiteLoaders[i];
			if (cl != null) {
				final URL url = cl.findResource(name);
				if (url != null) {
					return url;
				}
			}
		}
		// Not found, try my own plugin
		//System.out.println("Try resource " + name + " on " + jar.getDescriptor().getId());
		return jar.getResource(name);
	}

	/**
	 * Try to load the data of a class with a given name.
	 * 
	 * @param name
	 * @return The loaded class data or null if not found.
	 */
	protected byte[] loadClassData(String name) {
		final InputStream is = jar.getResourceAsStream(name.replace('.', '/') + ".class");
		if (is == null) {
			return null;
		}
		try {
			return FileUtils.load(is, true);
		} catch (IOException ex) {
			return null;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?