001: /*
002:
003: Derby - Class org.apache.derby.impl.services.reflect.ReflectClassesJava2
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.reflect;
023:
024: import org.apache.derby.iapi.util.ByteArray;
025:
026: /**
027: Relfect loader with Privileged block for Java 2 security.
028: */
029:
030: public final class ReflectClassesJava2 extends DatabaseClasses
031: implements java.security.PrivilegedAction {
032:
033: private java.util.HashMap preCompiled;
034:
035: private int action = -1;
036:
037: synchronized LoadedGeneratedClass loadGeneratedClassFromData(
038: String fullyQualifiedName, ByteArray classDump) {
039:
040: if (classDump == null || classDump.getArray() == null) {
041:
042: if (preCompiled == null)
043: preCompiled = new java.util.HashMap();
044: else {
045: ReflectGeneratedClass gc = (ReflectGeneratedClass) preCompiled
046: .get(fullyQualifiedName);
047: if (gc != null)
048: return gc;
049: }
050:
051: // not a generated class, just load the class directly.
052: try {
053: Class jvmClass = Class.forName(fullyQualifiedName);
054: ReflectGeneratedClass gc = new ReflectGeneratedClass(
055: this , jvmClass, null);
056: preCompiled.put(fullyQualifiedName, gc);
057: return gc;
058: } catch (ClassNotFoundException cnfe) {
059: throw new NoClassDefFoundError(cnfe.toString());
060: }
061: }
062:
063: action = 1;
064: return ((ReflectLoaderJava2) java.security.AccessController
065: .doPrivileged(this )).loadGeneratedClass(
066: fullyQualifiedName, classDump);
067: }
068:
069: public final Object run() {
070:
071: try {
072: // SECURITY PERMISSION - MP2
073: switch (action) {
074: case 1:
075: return new ReflectLoaderJava2(getClass()
076: .getClassLoader(), this );
077: case 2:
078: return Thread.currentThread().getContextClassLoader();
079: default:
080: return null;
081: }
082: } finally {
083: action = -1;
084: }
085:
086: }
087:
088: Class loadClassNotInDatabaseJar(String name)
089: throws ClassNotFoundException {
090:
091: Class foundClass = null;
092:
093: // We may have two problems with calling getContextClassLoader()
094: // when trying to find our own classes for aggregates.
095: // 1) If using the URLClassLoader a ClassNotFoundException may be
096: // thrown (Beetle 5002).
097: // 2) If cloudscape is loaded with JNI, getContextClassLoader()
098: // may return null. (Beetle 5171)
099: //
100: // If this happens we need to user the class loader of this object
101: // (the classLoader that loaded Cloudscape).
102: // So we call Class.forName to ensure that we find the class.
103: try {
104: ClassLoader cl;
105: synchronized (this ) {
106: action = 2;
107: cl = ((ClassLoader) java.security.AccessController
108: .doPrivileged(this ));
109: }
110:
111: foundClass = (cl != null) ? cl.loadClass(name) : Class
112: .forName(name);
113: } catch (ClassNotFoundException cnfe) {
114: foundClass = Class.forName(name);
115: }
116: return foundClass;
117: }
118: }
|