Debian Codegraph @

ssl-proxies/src/main/java/org/globus/util/ClassLoaderUtils.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.util;
16
17 import java.io.InputStream;
18
19 /**
20 * Various classloader utils. Extends the standard ways of loading classes or
21 * resources with a fallback mechanism to the thread context classloader.
22 */
23 public class ClassLoaderUtils {
24
25 private static DummySecurityManager MANAGER = new DummySecurityManager();
26
27 private static class DummySecurityManager extends SecurityManager {
28 public Class[] getClassContext() {
29 return super.getClassContext();
30 }
31 }
32
33 /**
34 * Returns the current execution stack as an array of classes.
35 * <p>
36 * The length of the array is the number of methods on the execution stack.
37 * The element at index <code>0</code> is the class of the currently
38 * executing method, the element at index <code>1</code> is the class of
39 * that method's caller, and so on.
40 *
41 * @return the execution stack.
42 */
43 public static Class[] getClassContext() {
44 return MANAGER.getClassContext();
45 }
46
47 /**
48 * Returns a class at specified depth of the current execution stack.
49 *
50 * @return the class at the specified depth of the current execution stack.
51 * Migth return null if depth is out of range.
52 */
53 public static Class getClassContextAt(int i) {
54 Class[] classes = MANAGER.getClassContext();
55 if (classes != null && classes.length > i) {
56 return classes[i];
57 }
58 return null;
59 }
60
61 /**
62 * Returns a classloader at specified depth of the current execution stack.
63 *
64 * @return the classloader at the specified depth of the current execution
65 * stack. Migth return null if depth is out of range.
66 */
67 public static ClassLoader getClassLoaderContextAt(int i) {
68 Class[] classes = MANAGER.getClassContext();
69 if (classes != null && classes.length > i) {
70 return classes[i].getClassLoader();
71 }
72 return null;
73 }
74
75 /**
76 * Gets an InputStream to a resource of a specified name. First, the
77 * caller's classloader is used to load the resource and if it fails the
78 * thread's context classloader is used to load the resource.
79 */
80 public static InputStream getResourceAsStream(String name) {
81 // try with caller classloader
82 ClassLoader loader = getClassLoaderContextAt(3);
83 InputStream in = (loader == null) ? null : loader.getResourceAsStream(name);
84 if (in == null) {
85 // try with context classloader if set & different
86 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
87 if (contextLoader != null && contextLoader != loader) {
88 in = contextLoader.getResourceAsStream(name);
89 }
90 }
91 return in;
92 }
93
94 /**
95 * Loads a specified class. First, the caller's classloader is used to load
96 * the class and if it fails the thread's context classloader is used to
97 * load the specified class.
98 */
99 public static Class forName(String name) throws ClassNotFoundException {
100 // try with caller classloader
101 ClassLoader loader = getClassLoaderContextAt(3);
102 try {
103 return Class.forName(name, true, loader);
104 } catch (ClassNotFoundException e) {
105 // try with context classloader if set & different
106 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
107 if (contextLoader == null || contextLoader == loader) {
108 throw e;
109 } else {
110 return Class.forName(name, true, contextLoader);
111 }
112 }
113 }
114
115 public static Class<?> loadClass(String className, Class<?> callingClass) throws ClassNotFoundException {
116 try {
117 ClassLoader cl = Thread.currentThread().getContextClassLoader();
118 if (cl != null) {
119 return cl.loadClass(className);
120 }
121 } catch (ClassNotFoundException e) {
122 // ignore
123 }
124 return loadClass2(className, callingClass);
125 }
126
127 private static Class<?> loadClass2(String className, Class<?> callingClass) throws ClassNotFoundException {
128 try {
129 return Class.forName(className);
130 } catch (ClassNotFoundException ex) {
131 try {
132 if (ClassLoaderUtils.class.getClassLoader() != null) {
133 return ClassLoaderUtils.class.getClassLoader().loadClass(className);
134 }
135 } catch (ClassNotFoundException exc) {
136 if (callingClass != null && callingClass.getClassLoader() != null) {
137 return callingClass.getClassLoader().loadClass(className);
138 }
139 }
140 throw ex;
141 }
142 }
143
144 }