1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.services.reflect.ReflectClassesJava2 |
4 | |
5 | Copyright 2000, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.services.reflect; |
22 | import org.apache.derby.iapi.util.ByteArray; |
23 | |
24 | /** |
25 | Relfect loader with Privileged block for Java 2 security. |
26 | */ |
27 | |
28 | public final class ReflectClassesJava2 extends DatabaseClasses |
29 | implements java.security.PrivilegedAction |
30 | { |
31 | |
32 | private java.util.HashMap preCompiled; |
33 | |
34 | private int action = -1; |
35 | |
36 | synchronized LoadedGeneratedClass loadGeneratedClassFromData(String fullyQualifiedName, ByteArray classDump) { |
37 | |
38 | if (classDump == null || classDump.getArray() == null) { |
39 | |
40 | if (preCompiled == null) |
41 | preCompiled = new java.util.HashMap(); |
42 | else |
43 | { |
44 | ReflectGeneratedClass gc = (ReflectGeneratedClass) preCompiled.get(fullyQualifiedName); |
45 | if (gc != null) |
46 | return gc; |
47 | } |
48 | |
49 | // not a generated class, just load the class directly. |
50 | try { |
51 | Class jvmClass = Class.forName(fullyQualifiedName); |
52 | ReflectGeneratedClass gc = new ReflectGeneratedClass(this, jvmClass, null); |
53 | preCompiled.put(fullyQualifiedName, gc); |
54 | return gc; |
55 | } catch (ClassNotFoundException cnfe) { |
56 | throw new NoClassDefFoundError(cnfe.toString()); |
57 | } |
58 | } |
59 | |
60 | action = 1; |
61 | return ((ReflectLoaderJava2) java.security.AccessController.doPrivileged(this)).loadGeneratedClass(fullyQualifiedName, classDump); |
62 | } |
63 | |
64 | public final Object run() { |
65 | |
66 | try { |
67 | // SECURITY PERMISSION - MP2 |
68 | switch (action) { |
69 | case 1: |
70 | return new ReflectLoaderJava2(getClass().getClassLoader(), this); |
71 | case 2: |
72 | return Thread.currentThread().getContextClassLoader(); |
73 | default: |
74 | return null; |
75 | } |
76 | } finally { |
77 | action = -1; |
78 | } |
79 | |
80 | } |
81 | |
82 | Class loadClassNotInDatabaseJar(String name) throws ClassNotFoundException { |
83 | |
84 | Class foundClass = null; |
85 | |
86 | // We may have two problems with calling getContextClassLoader() |
87 | // when trying to find our own classes for aggregates. |
88 | // 1) If using the URLClassLoader a ClassNotFoundException may be |
89 | // thrown (Beetle 5002). |
90 | // 2) If cloudscape is loaded with JNI, getContextClassLoader() |
91 | // may return null. (Beetle 5171) |
92 | // |
93 | // If this happens we need to user the class loader of this object |
94 | // (the classLoader that loaded Cloudscape). |
95 | // So we call Class.forName to ensure that we find the class. |
96 | try { |
97 | ClassLoader cl; |
98 | synchronized(this) { |
99 | action = 2; |
100 | cl = ((ClassLoader) |
101 | java.security.AccessController.doPrivileged(this)); |
102 | } |
103 | |
104 | foundClass = (cl != null) ? cl.loadClass(name) |
105 | :Class.forName(name); |
106 | } catch (ClassNotFoundException cnfe) { |
107 | foundClass = Class.forName(name); |
108 | } |
109 | return foundClass; |
110 | } |
111 | } |