All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.it.commons.lang.CompositeClassLoader Maven / Gradle / Ivy

The newest version!
package com.sap.it.commons.lang;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class CompositeClassLoader extends ClassLoader {
    private volatile List classLoaders = Collections. emptyList();

    public CompositeClassLoader(Collection classloaders) {
        setClassLoaders(classloaders);
    }

    public synchronized List setClassLoaders(Collection classloaders) {
        List old = this.classLoaders;
        this.classLoaders = classloaders != null ? new ArrayList(classloaders) : Collections. emptyList();
        return Collections.unmodifiableList(old);
    }

    public List getClassLoaders() {
        return Collections.unmodifiableList(classLoaders);
    }

    @Override
    public Class loadClass(String name) throws ClassNotFoundException {
        for (ClassLoader cl : classLoaders) {
            try {
                return cl.loadClass(name);
            } catch (ClassNotFoundException e) {
                continue;
            }
        }
        throw new ClassNotFoundException(name);
    }

    //
    // Resources
    // 

    @Override
    public URL getResource(String name) {
        for (ClassLoader cl : classLoaders) {
            URL url = cl.getResource(name);
            if (url != null) {
                return url;
            }
        }
        return null;
    }

    @Override
    public InputStream getResourceAsStream(String name) {
        for (ClassLoader cl : classLoaders) {
            InputStream is = cl.getResourceAsStream(name);
            if (is != null) {
                return is;
            }
        }
        return null;
    }

    @Override
    public Enumeration getResources(String name) throws IOException {
        final List urls = new ArrayList();
        for (ClassLoader cl : classLoaders) {
            Enumeration resources = cl.getResources(name);
            if (resources != null) {
                while (resources.hasMoreElements()) {
                    URL url = resources.nextElement();
                    if (!urls.contains(url)) {
                        urls.add(url);
                    }
                }
            }
        }
        return LangTools.asEnumeration(urls.iterator());
    }

    //
    // Assertion status
    //
    @Override
    public synchronized void clearAssertionStatus() {
        for (ClassLoader cl : classLoaders) {
            cl.clearAssertionStatus();
        }
    }

    @Override
    public synchronized void setClassAssertionStatus(String className, boolean enabled) {
        for (ClassLoader cl : classLoaders) {
            cl.setClassAssertionStatus(className, enabled);
        }
    }

    @Override
    public synchronized void setDefaultAssertionStatus(boolean enabled) {
        for (ClassLoader cl : classLoaders) {
            cl.setDefaultAssertionStatus(enabled);
        }
    }

    @Override
    public synchronized void setPackageAssertionStatus(String packageName, boolean enabled) {
        for (ClassLoader cl : classLoaders) {
            cl.setPackageAssertionStatus(packageName, enabled);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy