KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > memory > SelectedClassnameClassLoader


1 package org.jboss.cache.pojo.memory;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.util.Map JavaDoc;
11
12 /**
13  * A ClassLoader that loads classes whose classname begins with one of a
14  * given set of strings, without attempting first to delegate to its
15  * parent loader.
16  * <p/>
17  * This class is intended to allow emulation of 2 different types of common J2EE
18  * classloading situations.
19  * <ul>
20  * <li>Servlet-style child-first classloading, where this class is the
21  * child loader.</li>
22  * <li>Parent-first classloading where the parent does not have access to
23  * certain classes</li>
24  * </ul>
25  * </p>
26  * <p/>
27  * This class can also be configured to raise a ClassNotFoundException if
28  * asked to load certain classes, thus allowing classes on the classpath
29  * to be hidden from a test environment.
30  * </p>
31  *
32  * @author Brian Stansberry
33  */

34 public class SelectedClassnameClassLoader extends ClassLoader JavaDoc
35 {
36
37    private String JavaDoc[] includedClasses = null;
38    private String JavaDoc[] excludedClasses = null;
39    private String JavaDoc[] notFoundClasses = null;
40    private Log log = LogFactory.getLog(SelectedClassnameClassLoader.class);
41
42    private Map JavaDoc classes = new java.util.HashMap JavaDoc();
43
44    /**
45     * Creates a new classloader that loads the given classes.
46     *
47     * @param includedClasses array of class or package names that should be
48     * directly loaded by this loader. Classes
49     * whose name starts with any of the strings
50     * in this array will be loaded by this class,
51     * unless their name appears in
52     * <code>excludedClasses</code>.
53     * Can be <code>null</code>
54     * @param excludedClasses array of class or package names that should NOT
55     * be directly loaded by this loader. Loading of
56     * classes whose name starts with any of the
57     * strings in this array will be delegated to
58     * <code>parent</code>, even if the classes
59     * package or classname appears in
60     * <code>includedClasses</code>. Typically this
61     * parameter is used to exclude loading one or
62     * more classes in a package whose other classes
63     * are loaded by this object.
64     * @param parent ClassLoader to which loading of classes should
65     * be delegated if necessary
66     */

67    public SelectedClassnameClassLoader(String JavaDoc[] includedClasses,
68                                        String JavaDoc[] excludedClasses,
69                                        ClassLoader JavaDoc parent)
70    {
71       super(parent);
72       this.includedClasses = includedClasses;
73       this.excludedClasses = excludedClasses;
74    }
75
76    /**
77     * Creates a new classloader that loads the given classes.
78     *
79     * @param includedClasses array of class or package names that should be
80     * directly loaded by this loader. Classes
81     * whose name starts with any of the strings
82     * in this array will be loaded by this class,
83     * unless their name appears in
84     * <code>excludedClasses</code>.
85     * Can be <code>null</code>
86     * @param excludedClasses array of class or package names that should NOT
87     * be directly loaded by this loader. Loading of
88     * classes whose name starts with any of the
89     * strings in this array will be delegated to
90     * <code>parent</code>, even if the classes
91     * package or classname appears in
92     * <code>includedClasses</code>. Typically this
93     * parameter is used to exclude loading one or
94     * more classes in a package whose other classes
95     * are loaded by this object.
96     * @param notFoundClasses array of class or package names for which this
97     * should raise a ClassNotFoundException
98     * @param parent ClassLoader to which loading of classes should
99     * be delegated if necessary
100     */

101    public SelectedClassnameClassLoader(String JavaDoc[] includedClasses,
102                                        String JavaDoc[] excludedClasses,
103                                        String JavaDoc[] notFoundClasses,
104                                        ClassLoader JavaDoc parent)
105    {
106       super(parent);
107       this.includedClasses = includedClasses;
108       this.excludedClasses = excludedClasses;
109       this.notFoundClasses = notFoundClasses;
110    }
111
112    protected synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
113            throws ClassNotFoundException JavaDoc
114    {
115       log.info("In SelectedClassnameClassLoader.loadClass(" + name + "," + resolve + ")");
116       if (isIncluded(name) && (isExcluded(name) == false))
117       {
118          Class JavaDoc c = findClass(name);
119
120          if (resolve)
121          {
122             resolveClass(c);
123          }
124          return c;
125       } else
126       {
127          return super.loadClass(name, resolve);
128       }
129    }
130
131    protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc
132    {
133
134       log.info("In SelectedClassnameClassLoader.findClass()");
135       Class JavaDoc result = (Class JavaDoc) classes.get(name);
136       if (result != null)
137       {
138          return result;
139       }
140
141       if (isIncluded(name) && (isExcluded(name) == false))
142       {
143          try
144          {
145             InputStream JavaDoc is = getResourceAsStream(name.replace('.', '/').concat(".class"));
146             byte[] bytes = new byte[1024];
147             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(1024);
148             int read;
149             while ((read = is.read(bytes)) > -1)
150             {
151                baos.write(bytes, 0, read);
152             }
153             bytes = baos.toByteArray();
154             result = this.defineClass(name, bytes, 0, bytes.length);
155          } catch (FileNotFoundException JavaDoc e)
156          {
157             throw new ClassNotFoundException JavaDoc("cannot find " + name, e);
158          } catch (IOException JavaDoc e)
159          {
160             throw new ClassNotFoundException JavaDoc("cannot read " + name, e);
161          }
162       } else if (isNotFound(name))
163       {
164          throw new ClassNotFoundException JavaDoc(name + " is discarded");
165       } else
166       {
167          result = super.findClass(name);
168       }
169
170       classes.put(name, result);
171
172       return result;
173    }
174
175    private boolean isIncluded(String JavaDoc className)
176    {
177
178       if (includedClasses != null)
179       {
180          for (int i = 0; i < includedClasses.length; i++)
181          {
182             if (className.startsWith(includedClasses[i]))
183             {
184                return true;
185             }
186          }
187       }
188
189       return false;
190    }
191
192    private boolean isExcluded(String JavaDoc className)
193    {
194
195       if (excludedClasses != null)
196       {
197          for (int i = 0; i < excludedClasses.length; i++)
198          {
199             if (className.startsWith(excludedClasses[i]))
200             {
201                return true;
202             }
203          }
204       }
205
206       return false;
207    }
208
209    private boolean isNotFound(String JavaDoc className)
210    {
211
212       if (notFoundClasses != null)
213       {
214          for (int i = 0; i < notFoundClasses.length; i++)
215          {
216             if (className.startsWith(notFoundClasses[i]))
217             {
218                return true;
219             }
220          }
221       }
222
223       return false;
224    }
225 }
226
Popular Tags