KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > ClassLoaderUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 /**
20  * Utility class for diagnostic purposes, to analyze the
21  * ClassLoader hierarchy for any given object or class loader.
22  *
23  * @author Rod Johnson
24  * @author Juergen Hoeller
25  * @since 02 April 2001
26  * @see java.lang.ClassLoader
27  */

28 public abstract class ClassLoaderUtils {
29
30     /**
31      * Show the class loader hierarchy for this class.
32      * Uses default line break and tab text characters.
33      * @param obj object to analyze loader hierarchy for
34      * @param role a description of the role of this class in the application
35      * (e.g., "servlet" or "EJB reference")
36      * @return a String showing the class loader hierarchy for this class
37      */

38     public static String JavaDoc showClassLoaderHierarchy(Object JavaDoc obj, String JavaDoc role) {
39         return showClassLoaderHierarchy(obj, role, "\n", "\t");
40     }
41
42     /**
43      * Show the class loader hierarchy for this class.
44      * @param obj object to analyze loader hierarchy for
45      * @param role a description of the role of this class in the application
46      * (e.g., "servlet" or "EJB reference")
47      * @param lineBreak line break
48      * @param tabText text to use to set tabs
49      * @return a String showing the class loader hierarchy for this class
50      */

51     public static String JavaDoc showClassLoaderHierarchy(Object JavaDoc obj, String JavaDoc role, String JavaDoc lineBreak, String JavaDoc tabText) {
52         String JavaDoc s = "object of " + obj.getClass() + ": role is " + role + lineBreak;
53         return s + showClassLoaderHierarchy(obj.getClass().getClassLoader(), lineBreak, tabText, 0);
54     }
55
56     /**
57      * Show the class loader hierarchy for the given class loader.
58      * Uses default line break and tab text characters.
59      * @param cl class loader to analyze hierarchy for
60      * @return a String showing the class loader hierarchy for this class
61      */

62     public static String JavaDoc showClassLoaderHierarchy(ClassLoader JavaDoc cl) {
63         return showClassLoaderHierarchy(cl, "\n", "\t");
64     }
65
66     /**
67      * Show the class loader hierarchy for the given class loader.
68      * @param cl class loader to analyze hierarchy for
69      * @param lineBreak line break
70      * @param tabText text to use to set tabs
71      * @return a String showing the class loader hierarchy for this class
72      */

73     public static String JavaDoc showClassLoaderHierarchy(ClassLoader JavaDoc cl, String JavaDoc lineBreak, String JavaDoc tabText) {
74         return showClassLoaderHierarchy(cl, lineBreak, tabText, 0);
75     }
76
77     /**
78      * Show the class loader hierarchy for the given class loader.
79      * @param cl class loader to analyze hierarchy for
80      * @param lineBreak line break
81      * @param tabText text to use to set tabs
82      * @param indent nesting level (from 0) of this loader; used in pretty printing
83      * @return a String showing the class loader hierarchy for this class
84      */

85     private static String JavaDoc showClassLoaderHierarchy(ClassLoader JavaDoc cl, String JavaDoc lineBreak, String JavaDoc tabText, int indent) {
86         if (cl == null) {
87             ClassLoader JavaDoc ccl = Thread.currentThread().getContextClassLoader();
88             return "context class loader=[" + ccl + "] hashCode=" + ccl.hashCode();
89         }
90         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
91         for (int i = 0; i < indent; i++) {
92             buf.append(tabText);
93         }
94         buf.append("[").append(cl).append("] hashCode=").append(cl.hashCode()).append(lineBreak);
95         ClassLoader JavaDoc parent = cl.getParent();
96         return buf.toString() + showClassLoaderHierarchy(parent, lineBreak, tabText, indent + 1);
97     }
98
99 }
100
Popular Tags