001package runwar.util; 002/* 003 * Copyright 2002-2005 the original author or authors. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 019/** 020 * Utility class for diagnostic purposes, to analyze the 021 * ClassLoader hierarchy for any given object or class loader. 022 * 023 * @author Rod Johnson 024 * @author Juergen Hoeller 025 * @since 02 April 2001 026 * @see java.lang.ClassLoader 027 */ 028public abstract class ClassLoaderUtils { 029 030 /** 031 * Show the class loader hierarchy for this class. 032 * Uses default line break and tab text characters. 033 * @param obj object to analyze loader hierarchy for 034 * @param role a description of the role of this class in the application 035 * (e.g., "servlet" or "EJB reference") 036 * @return a String showing the class loader hierarchy for this class 037 */ 038 public static String showClassLoaderHierarchy(Object obj, String role) { 039 return showClassLoaderHierarchy(obj, role, "\n", "\t"); 040 } 041 042 /** 043 * Show the class loader hierarchy for this class. 044 * @param obj object to analyze loader hierarchy for 045 * @param role a description of the role of this class in the application 046 * (e.g., "servlet" or "EJB reference") 047 * @param lineBreak line break 048 * @param tabText text to use to set tabs 049 * @return a String showing the class loader hierarchy for this class 050 */ 051 public static String showClassLoaderHierarchy(Object obj, String role, String lineBreak, String tabText) { 052 String s = "object of " + obj.getClass() + ": role is " + role + lineBreak; 053 return s + showClassLoaderHierarchy(obj.getClass().getClassLoader(), lineBreak, tabText, 0); 054 } 055 056 /** 057 * Show the class loader hierarchy for the given class loader. 058 * Uses default line break and tab text characters. 059 * @param cl class loader to analyze hierarchy for 060 * @return a String showing the class loader hierarchy for this class 061 */ 062 public static String showClassLoaderHierarchy(ClassLoader cl) { 063 return showClassLoaderHierarchy(cl, "\n", "\t"); 064 } 065 066 /** 067 * Show the class loader hierarchy for the given class loader. 068 * @param cl class loader to analyze hierarchy for 069 * @param lineBreak line break 070 * @param tabText text to use to set tabs 071 * @return a String showing the class loader hierarchy for this class 072 */ 073 public static String showClassLoaderHierarchy(ClassLoader cl, String lineBreak, String tabText) { 074 return showClassLoaderHierarchy(cl, lineBreak, tabText, 0); 075 } 076 077 /** 078 * Show the class loader hierarchy for the given class loader. 079 * @param cl class loader to analyze hierarchy for 080 * @param lineBreak line break 081 * @param tabText text to use to set tabs 082 * @param indent nesting level (from 0) of this loader; used in pretty printing 083 * @return a String showing the class loader hierarchy for this class 084 */ 085 private static String showClassLoaderHierarchy(ClassLoader cl, String lineBreak, String tabText, int indent) { 086 if (cl == null) { 087 ClassLoader ccl = Thread.currentThread().getContextClassLoader(); 088 return "context class loader=[" + ccl + "] hashCode=" + ccl.hashCode(); 089 } 090 StringBuffer buf = new StringBuffer(); 091 for (int i = 0; i < indent; i++) { 092 buf.append(tabText); 093 } 094 buf.append("[").append(cl).append("] hashCode=").append(cl.hashCode()).append(lineBreak); 095 ClassLoader parent = cl.getParent(); 096 return buf.toString() + showClassLoaderHierarchy(parent, lineBreak, tabText, indent + 1); 097 } 098 099}