    001package com.hammurapi.common;
    002
    003import java.io.IOException;
    004import java.net.URL;
    005import java.util.Enumeration;
    006import java.util.Vector;
    007
    008/**
    009 * Class loader which filters classes/resources from another classloader. This classloader can be
    010 * used for fine-grained loading of classes from a jar file which contains several logical groups of classes (e.g. packages)
    011 * which have to be loaded individually, e.g. they have different dependencies.
    012 * @author Pavel
    013 *
    014 */
    015public abstract class FilterClassLoader extends ClassLoader {
    016
    017        private ClassLoader filtered;
    018
    019        protected FilterClassLoader(ClassLoader filtered) {
    020                super();
    021                this.filtered = filtered;
    022        }
    023
    024        protected FilterClassLoader(ClassLoader filtered, ClassLoader parent) {
    025                super(parent);
    026                this.filtered = filtered;
    027        }
    028
    029        protected abstract boolean accept(String name);
    030
    031        @Override
    032        protected URL findResource(String name) {
    033                return accept(name) ? filtered.getResource(name) : null;
    034        }
    035
    036        @Override
    037        protected Enumeration<URL> findResources(String name) throws IOException {
    038                return accept(name) ? filtered.getResources(name) : new Vector<URL>().elements();
    039        }
    040
    041        @Override
    042        protected Class<?> findClass(String name) throws ClassNotFoundException {
    043                if (accept(name.replace('.', '/')+".class")) {
    044                        return filtered.loadClass(name);
    045                }
    046                return super.findClass(name);
    047        }
    048
    049}