Java tutorial
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: IBM Corporation - initial API and implementation ******************************************************************************/ package org.cg.dao.webcontainer.tomcat; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.util.Enumeration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * A class loader that combines a plugin class loader with the tomcat class * * loader */ public class WebAppClassLoader extends URLClassLoader { private final static Log logger = LogFactory.getLog(WebAppClassLoader.class); private ClassLoader myPluginLoader; private DaoBundleURLClassLoader tomcatPluginLoader; public WebAppClassLoader(ClassLoader pluginLoader) { super(new URL[0]); this.myPluginLoader = pluginLoader; this.tomcatPluginLoader = new DaoBundleURLClassLoader(Activator.PLUGIN_ID); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { return super.findClass(name); } @Override public URL findResource(String resource) { logger.debug("finding resource " + resource); logger.debug(myPluginLoader.toString() + " is finding resource " + resource); URL url = myPluginLoader.getResource(resource); if (url != null) return url; logger.debug("tomcat classloader finding resource " + resource); url = tomcatPluginLoader.getResource(resource); if (url != null) return url; logger.debug("defer to super classloader to find" + resource); return super.findResource(resource); } @Override public Enumeration<URL> findResources(String resources) throws IOException { logger.debug("finding resources " + resources); logger.debug(myPluginLoader.toString() + " is finding resourced" + resources); Enumeration<URL> urls = myPluginLoader.getResources(resources); if (urls != null && urls.hasMoreElements()) return urls; logger.debug("tomcat classloader finding resources " + resources); urls = tomcatPluginLoader.getResources(resources); if (urls != null && urls.hasMoreElements()) return urls; logger.debug("defer to super classloader to find resources " + resources); return super.findResources(resources); } @Override public URL getResource(String resName) { logger.debug("getting resource: " + resName); // 1. check the plugin loader, 2. current loader logger.debug(myPluginLoader.toString() + " is loading resource " + resName); URL url = myPluginLoader.getResource(resName); if (url == null) { logger.debug("tomcat classloader is loading resource " + resName); return tomcatPluginLoader.getResource(resName); } return url; } @Override public Enumeration<URL> getResources(String name) throws IOException { logger.debug("getting resources " + name); logger.debug(myPluginLoader.toString() + " is loading, resource is " + name); Enumeration<URL> urls = myPluginLoader.getResources(name); if (urls == null) { logger.debug("tomcat classloader is loading resource " + name); return tomcatPluginLoader.getResources(name); } return urls; } public URL[] getURLs() { URL[] pluginLoaderURLs; if (myPluginLoader instanceof URLClassLoader) pluginLoaderURLs = ((URLClassLoader) myPluginLoader).getURLs(); else pluginLoaderURLs = new URL[0]; URL[] tomcatPluginLoaderURLs = tomcatPluginLoader.getURLs(); URL[] urls = new URL[pluginLoaderURLs.length + tomcatPluginLoaderURLs.length]; System.arraycopy(pluginLoaderURLs, 0, urls, 0, pluginLoaderURLs.length); System.arraycopy(tomcatPluginLoaderURLs, 0, urls, pluginLoaderURLs.length, tomcatPluginLoaderURLs.length); return urls; } @SuppressWarnings({ "rawtypes", "unchecked" }) public Class loadClass(String className) throws ClassNotFoundException { logger.debug("loading class : " + className); // 1. check tomcat plugin loader, 2. the webapp plugin loader Class myClass = null; try { logger.debug("tomcat classloader is loading class " + className); myClass = tomcatPluginLoader.loadClass(className); } catch (ClassNotFoundException e) { myClass = myPluginLoader.loadClass(className); logger.debug("Fail to load class " + className, e); } finally { } return myClass; } }