KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > JpaUnitClassLoader


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.jpa;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.security.SecureClassLoader JavaDoc;
26
27 /**
28  * A special class loader that uses parent loader to get resoirces, but does not load any
29  * classes though the parent to avoid parent pollution with un-enhanced classes.
30  *
31  * @author Andrus Adamchik
32  */

33 class JpaUnitClassLoader extends SecureClassLoader JavaDoc {
34
35     JpaUnitClassLoader(ClassLoader JavaDoc parent) {
36         super(parent);
37     }
38
39     @Override JavaDoc
40     protected synchronized Class JavaDoc<?> loadClass(String JavaDoc name, boolean resolve)
41             throws ClassNotFoundException JavaDoc {
42
43         if (name.startsWith("java.") || name.startsWith("javax.")) {
44             return super.loadClass(name, resolve);
45         }
46
47         Class JavaDoc c = findLoadedClass(name);
48
49         if (c == null) {
50             c = findClass(name);
51         }
52
53         if (resolve) {
54             resolveClass(c);
55         }
56
57         return c;
58     }
59
60     /**
61      * Loads a class bypassing parent class loader.
62      */

63     @Override JavaDoc
64     protected Class JavaDoc<?> findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
65         String JavaDoc path = name.replace('.', '/') + ".class";
66
67         InputStream JavaDoc in = getResourceAsStream(path);
68         if (in == null) {
69             throw new ClassNotFoundException JavaDoc(name);
70         }
71
72         try {
73             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(1024);
74             byte[] buffer = new byte[1024];
75             int read;
76
77             while ((read = in.read(buffer, 0, 1024)) > 0) {
78                 out.write(buffer, 0, read);
79             }
80
81             out.close();
82             byte[] classBytes = out.toByteArray();
83
84             return defineClass(name, classBytes, 0, classBytes.length);
85         }
86         catch (SecurityException JavaDoc e) {
87             // JVM doesn't allow us to define class here... Is there a way to detect this
88
// upfront?
89
return super.findClass(name);
90         }
91         catch (IOException JavaDoc e) {
92             throw new ClassNotFoundException JavaDoc(name, e);
93         }
94         finally {
95             try {
96                 in.close();
97             }
98             catch (IOException JavaDoc e) {
99                 // ignore close exceptions...
100
}
101         }
102     }
103 }
104
Popular Tags