001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.xbean.classloader;
018
019import java.net.URL;
020import java.net.URLClassLoader;
021import java.net.URLStreamHandlerFactory;
022import java.util.Arrays;
023
024
025/**
026 * The NamedClassLoader is a simple extension to URLClassLoader that adds a name and a destroy method that cleans up
027 * the commons logging and JavaVM caches of the classloader.
028 *
029 * @author Dain Sundstrom
030 * @version $Id: NamedClassLoader.java 517223 2007-03-12 14:02:22Z gnodet $
031 * @since 2.0
032 */
033public class NamedClassLoader extends URLClassLoader implements DestroyableClassLoader {
034    private final String name;
035    private volatile boolean destroyed = false;
036
037    /**
038     * Creates a named class loader with no parents.
039     * @param name the name of this class loader
040     * @param urls the urls from which this class loader will classes and resources
041     */
042    public NamedClassLoader(String name, URL[] urls) {
043        super(urls);
044        this.name = name;
045    }
046
047    /**
048     * Creates a named class loader as a child of the specified parent.
049     * @param name the name of this class loader
050     * @param urls the urls from which this class loader will classes and resources
051     * @param parent the parent of this class loader
052     */
053    public NamedClassLoader(String name, URL[] urls, ClassLoader parent) {
054        super(urls, parent);
055        this.name = name;
056    }
057
058    /**
059     * Creates a named class loader as a child of the specified parent and using the specified URLStreamHandlerFactory
060     * for accessing the urls..
061     * @param name the name of this class loader
062     * @param urls the urls from which this class loader will classes and resources
063     * @param parent the parent of this class loader
064     * @param factory the URLStreamHandlerFactory used to access the urls
065     */
066    public NamedClassLoader(String name, URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
067        super(urls, parent, factory);
068        this.name = name;
069    }
070
071    /**
072     * Check if this classloader has been destroyed 
073     * @return
074     */
075    public boolean isDestroyed() {
076        return destroyed;
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    public void destroy() {
083        synchronized(this) {
084            if (destroyed) return;
085            destroyed = true;
086        }
087        ClassLoaderUtil.destroy(this);
088    }
089
090    /**
091     * Gets the name of this class loader.
092     * @return the name of this class loader
093     */
094    public String getName() {
095        return name;
096    }
097
098    /**
099     * {@inheritDoc}
100     */
101    public String toString() {
102        return "[" + getClass().getName() + ":" +
103                " name=" + getName() +
104                " urls=" + Arrays.asList(getURLs()) +
105                "]";
106    }
107}