001package org.nuxeo.runtime.tomcat.dev;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.net.URL;
006import java.net.URLClassLoader;
007import java.util.Enumeration;
008
009/*
010 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
011 *
012 * All rights reserved. This program and the accompanying materials
013 * are made available under the terms of the GNU Lesser General Public License
014 * (LGPL) version 2.1 which accompanies this distribution, and is available at
015 * http://www.gnu.org/licenses/lgpl.html
016 *
017 * This library is distributed in the hope that it will be useful,
018 * but WITHOUT ANY WARRANTY; without even the implied warranty of
019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020 * Lesser General Public License for more details.
021 *
022 * Contributors:
023 *     bstefanescu
024 */
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class LocalURLClassLoader extends URLClassLoader implements LocalClassLoader {
030
031    public LocalURLClassLoader(ClassLoader parent) {
032        super(new URL[0], parent);
033    }
034
035    public LocalURLClassLoader(URL[] urls, ClassLoader parent) {
036        super(urls == null ? new URL[0] : urls, parent);
037    }
038
039    @Override
040    public void addURL(URL url) {
041        super.addURL(url);
042    }
043
044    @Override
045    public Class<?> loadLocalClass(String name, boolean resolve) throws ClassNotFoundException {
046        // do not look into parent
047        synchronized (getParent()) {
048            Class<?> clazz = findLoadedClass(name);
049
050            if (clazz == null) {
051                clazz = findClass(name);
052            }
053
054            if (resolve) {
055                resolveClass(clazz);
056            }
057
058            return clazz;
059        }
060    }
061
062    @Override
063    public URL getLocalResource(String name) {
064        return findResource(name);
065    }
066
067    @Override
068    public Enumeration<URL> getLocalResources(String name) throws IOException {
069        return findResources(name);
070    }
071
072    @Override
073    public InputStream getLocalResourceAsStream(String name) throws IOException {
074        URL location = getLocalResource(name);
075        if (location == null) {
076            return null;
077        }
078        return location.openStream();
079    }
080
081}