001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.runtime.tomcat.dev;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.net.URL;
024import java.net.URLClassLoader;
025import java.util.Enumeration;
026
027/**
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public class LocalURLClassLoader extends URLClassLoader implements LocalClassLoader {
031
032    public LocalURLClassLoader(ClassLoader parent) {
033        super(new URL[0], parent);
034    }
035
036    public LocalURLClassLoader(URL[] urls, ClassLoader parent) {
037        super(urls == null ? new URL[0] : urls, parent);
038    }
039
040    @Override
041    public void addURL(URL url) {
042        super.addURL(url);
043    }
044
045    @Override
046    public Class<?> loadLocalClass(String name, boolean resolve) throws ClassNotFoundException {
047        // do not look into parent
048        synchronized (getParent()) {
049            Class<?> clazz = findLoadedClass(name);
050
051            if (clazz == null) {
052                clazz = findClass(name);
053            }
054
055            if (resolve) {
056                resolveClass(clazz);
057            }
058
059            return clazz;
060        }
061    }
062
063    @Override
064    public URL getLocalResource(String name) {
065        return findResource(name);
066    }
067
068    @Override
069    public Enumeration<URL> getLocalResources(String name) throws IOException {
070        return findResources(name);
071    }
072
073    @Override
074    public InputStream getLocalResourceAsStream(String name) throws IOException {
075        URL location = getLocalResource(name);
076        if (location == null) {
077            return null;
078        }
079        return location.openStream();
080    }
081
082}