KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > reflect > ReflectClassesJava2


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

21
22 package org.apache.derby.impl.services.reflect;
23 import org.apache.derby.iapi.util.ByteArray;
24
25 /**
26     Relfect loader with Privileged block for Java 2 security.
27 */

28
29 public final class ReflectClassesJava2 extends DatabaseClasses
30     implements java.security.PrivilegedAction JavaDoc
31 {
32
33     private java.util.HashMap JavaDoc preCompiled;
34
35     private int action = -1;
36
37     synchronized LoadedGeneratedClass loadGeneratedClassFromData(String JavaDoc fullyQualifiedName, ByteArray classDump) {
38
39         if (classDump == null || classDump.getArray() == null) {
40
41             if (preCompiled == null)
42                 preCompiled = new java.util.HashMap JavaDoc();
43             else
44             {
45                 ReflectGeneratedClass gc = (ReflectGeneratedClass) preCompiled.get(fullyQualifiedName);
46                 if (gc != null)
47                     return gc;
48             }
49
50             // not a generated class, just load the class directly.
51
try {
52                 Class JavaDoc jvmClass = Class.forName(fullyQualifiedName);
53                 ReflectGeneratedClass gc = new ReflectGeneratedClass(this, jvmClass, null);
54                 preCompiled.put(fullyQualifiedName, gc);
55                 return gc;
56             } catch (ClassNotFoundException JavaDoc cnfe) {
57                 throw new NoClassDefFoundError JavaDoc(cnfe.toString());
58             }
59         }
60
61         action = 1;
62         return ((ReflectLoaderJava2) java.security.AccessController.doPrivileged(this)).loadGeneratedClass(fullyQualifiedName, classDump);
63     }
64
65     public final Object JavaDoc run() {
66
67         try {
68             // SECURITY PERMISSION - MP2
69
switch (action) {
70             case 1:
71                 return new ReflectLoaderJava2(getClass().getClassLoader(), this);
72             case 2:
73                 return Thread.currentThread().getContextClassLoader();
74             default:
75                 return null;
76             }
77         } finally {
78             action = -1;
79         }
80         
81     }
82
83     Class JavaDoc loadClassNotInDatabaseJar(String JavaDoc name) throws ClassNotFoundException JavaDoc {
84         
85         Class JavaDoc foundClass = null;
86         
87         // We may have two problems with calling getContextClassLoader()
88
// when trying to find our own classes for aggregates.
89
// 1) If using the URLClassLoader a ClassNotFoundException may be
90
// thrown (Beetle 5002).
91
// 2) If cloudscape is loaded with JNI, getContextClassLoader()
92
// may return null. (Beetle 5171)
93
//
94
// If this happens we need to user the class loader of this object
95
// (the classLoader that loaded Cloudscape).
96
// So we call Class.forName to ensure that we find the class.
97
try {
98             ClassLoader JavaDoc cl;
99             synchronized(this) {
100               action = 2;
101               cl = ((ClassLoader JavaDoc)
102                   java.security.AccessController.doPrivileged(this));
103             }
104             
105             foundClass = (cl != null) ? cl.loadClass(name)
106                       :Class.forName(name);
107         } catch (ClassNotFoundException JavaDoc cnfe) {
108             foundClass = Class.forName(name);
109         }
110         return foundClass;
111     }
112 }
113
Popular Tags