[Openbus-cvs-commits] Openbus/OpenbusJBI/src/java/org/openbus/jbi/loader CompositeClassLoader.java,N
Status: Alpha
Brought to you by:
esancho
|
From: Esteban S. <es...@us...> - 2005-06-24 15:34:04
|
Update of /cvsroot/openbus/Openbus/OpenbusJBI/src/java/org/openbus/jbi/loader In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24925/src/java/org/openbus/jbi/loader Added Files: CompositeClassLoader.java DelegateClassLoader.java Log Message: Adding custom class loaders --- NEW FILE: DelegateClassLoader.java --- /* * Copyright 2001-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.openbus.jbi.loader; import java.net.URL; import java.net.URLClassLoader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * ClassLoader with a configurable delegation model * * @author <a href="mailto:es...@op...">Esteban Sancho </a> */ public class DelegateClassLoader extends URLClassLoader { private static final Log log = LogFactory.getLog(DelegateClassLoader.class); private boolean parentFirst; public DelegateClassLoader(URL[] urls_, boolean parentFirst_) { super(urls_); parentFirst = parentFirst_; } public DelegateClassLoader(URL[] urls_, ClassLoader parent_, boolean parentFirst_) { super(urls_, parent_); parentFirst = parentFirst_; } public synchronized Class loadClass(String name_, boolean resolve_) throws ClassNotFoundException { if (parentFirst || isSystemClass(name_)) { // default Java 2 implementation if (log.isDebugEnabled()) { log.debug("Delegating resolution of class " + name_ + " to the parent first"); } return super.loadClass(name_, resolve_); } else { // our custom implementation for resolving self first if (log.isDebugEnabled()) { log.debug("Trying to resolve class " + name_ + " here first"); } return selfFirstLoadClass(name_, resolve_); } } protected boolean isSystemClass(String name_) { boolean isSystem = (name_.startsWith("java.")) || (name_.startsWith("javax.")); if (log.isDebugEnabled()) { log.debug("Class " + name_ + (isSystem ? " is " : " is not ") + "a system class"); } return isSystem; } protected Class selfFirstLoadClass(String name_, boolean resolve_) throws ClassNotFoundException { // First check if the class is already loaded Class clazz = findLoadedClass(name_); if (clazz == null) { try { // first look here clazz = findClass(name_); if (log.isDebugEnabled()) { log.debug("Class " + name_ + " resolved here"); } } catch (ClassNotFoundException e) { // If still not found, then delegate to the parent if (getParent() != null) { clazz = getParent().loadClass(name_); if (log.isDebugEnabled()) { log.debug("Class " + name_ + " resolved in parent"); } // returning in order to don't resolve it again return clazz; } } } if (resolve_) { resolveClass(clazz); } return clazz; } } --- NEW FILE: CompositeClassLoader.java --- /* * Copyright 2001-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.openbus.jbi.loader; import java.security.SecureClassLoader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * * @author <a href="mailto:es...@op...">Esteban Sancho </a> */ public class CompositeClassLoader extends SecureClassLoader { private static final Log log = LogFactory .getLog(CompositeClassLoader.class); private Collection classLoaders; public CompositeClassLoader(ClassLoader parent_) { super(parent_); classLoaders = new ArrayList(); } public CompositeClassLoader(ClassLoader parent_, DelegateClassLoader[] classLoaders_) { super(parent_); classLoaders = Arrays.asList(classLoaders_); } public void addClassLoader(DelegateClassLoader loader_) { if (log.isDebugEnabled()) { log.debug("Adding class loader " + loader_); } classLoaders.add(loader_); } public void removeClassLoader(DelegateClassLoader loader_) { boolean removed = classLoaders.remove(loader_); if (log.isDebugEnabled()) { log.debug("Removing class loader " + loader_ + " - Found: " + removed); } } public Class loadClass(String name_) throws ClassNotFoundException { return loadClass(name_, true); } protected synchronized Class loadClass(String name_, boolean resolve_) throws ClassNotFoundException { // look into the cache Class clazz = findLoadedClass(name_); if (clazz != null) { if (log.isDebugEnabled()) { log.debug("Class " + name_ + " was already loaded"); } return clazz; } // look into the backend class-loaders for (Iterator i = classLoaders.iterator(); i.hasNext();) { DelegateClassLoader loader = (DelegateClassLoader) i.next(); try { clazz = loader.loadClass(name_, resolve_); if (log.isDebugEnabled()) { log.debug("Class " + name_ + " found in class loader: " + loader); } return clazz; } catch (ClassNotFoundException e) { if (log.isTraceEnabled()) { log.trace("Class " + name_ + " not found in class loader: " + loader); } } } if (log.isDebugEnabled()) { log.debug("Class not found: " + name_); } // @todo i18n throw new ClassNotFoundException("Class not found: " + name_); } } |