KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > FilteringClassLoader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.classloader;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.jboss.util.collection.Iterators;
31
32 /**
33  * FilteringClassLoader.
34  *
35  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
36  * @version $Revision: 1.1 $
37  */

38 public class FilteringClassLoader extends ClassLoader JavaDoc
39 {
40    /** The packages to filter out, e.g. org.jboss.test.something */
41    private String JavaDoc[] packages;
42    
43    /** The paths to filter out, e.g. org/jboss/test/something */
44    private String JavaDoc[] paths;
45    
46    /** The urls to filter out, e.g. file:/home/whatever/project/output/classes/org/jboss/test/something */
47    private String JavaDoc[] urls;
48    
49    /**
50     * Create a new FilteringClassLoader.
51     *
52     * @param parent the parent classloader
53     * @param packages the packages to filter out
54     */

55    public FilteringClassLoader(ClassLoader JavaDoc parent, String JavaDoc[] packages)
56    {
57       super(parent);
58       if (packages == null)
59          throw new IllegalArgumentException JavaDoc("Null packages");
60       this.packages = packages;
61       
62       // Determine the filtered paths
63
paths = new String JavaDoc[packages.length];
64       for (int i = 0; i < packages.length; ++i)
65          paths[i] = packages[i].replace('.', '/');
66       
67       // Determine the filtered roots
68
try
69       {
70          Enumeration JavaDoc<URL JavaDoc> enumeration = super.getResources("");
71          List JavaDoc<URL JavaDoc> rootURLs = new ArrayList JavaDoc<URL JavaDoc>();
72          while (enumeration.hasMoreElements())
73             rootURLs.add(enumeration.nextElement());
74          urls = new String JavaDoc[paths.length * rootURLs.size()];
75          int i = 0;
76          for (String JavaDoc path : paths)
77          {
78             for (URL JavaDoc rootURL : rootURLs)
79                urls[i++] = new URL JavaDoc(rootURL, path).toString();
80          }
81       }
82       catch (Exception JavaDoc e)
83       {
84          throw new RuntimeException JavaDoc("Error determining classloader urls", e);
85       }
86    }
87
88    @Override JavaDoc
89    protected synchronized Class JavaDoc<?> loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc
90    {
91       for (String JavaDoc pkg : packages)
92       {
93          if (name.startsWith(pkg))
94             throw new ClassNotFoundException JavaDoc("Class not found (filtered): " + name);
95       }
96       return super.loadClass(name, resolve);
97    }
98
99    @Override JavaDoc
100    public URL JavaDoc getResource(String JavaDoc name)
101    {
102       for (String JavaDoc path : paths)
103       {
104          if (name.startsWith(path))
105             return null;
106       }
107       return super.getResource(name);
108    }
109
110    @SuppressWarnings JavaDoc("unchecked")
111    @Override JavaDoc
112    public Enumeration JavaDoc<URL JavaDoc> getResources(String JavaDoc name) throws IOException JavaDoc
113    {
114       Enumeration JavaDoc<URL JavaDoc> unfiltered = super.getResources(name);
115       List JavaDoc<URL JavaDoc> filtered = new ArrayList JavaDoc<URL JavaDoc>();
116       while (unfiltered.hasMoreElements())
117       {
118          URL JavaDoc next = unfiltered.nextElement();
119          boolean ignore =false;
120          for (String JavaDoc url : urls)
121          {
122             if (next.toString().startsWith(url))
123                ignore = true;
124          }
125          if (ignore == false)
126             filtered.add(next);
127       }
128       return Iterators.toEnumeration(filtered.iterator());
129    }
130 }
131
Popular Tags