01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.test.util;
23:
24: import java.lang.reflect.Method;
25: import java.net.URL;
26: import java.security.CodeSource;
27: import java.security.ProtectionDomain;
28:
29: /** Various debugging utility methods available for use in unit tests
30: * @author Scott.Stark@jboss.org
31: */
32: public class Debug {
33: /** Format a string buffer containing the Class, Interfaces, CodeSource,
34: and ClassLoader information for the given object clazz.
35:
36: @param clazz the Class
37: @param results, the buffer to write the info to
38: */
39: public static void displayClassInfo(Class clazz,
40: StringBuffer results) {
41: // Print out some codebase info for the ProbeHome
42: ClassLoader cl = clazz.getClassLoader();
43: results.append("\n" + clazz.getName() + ".ClassLoader=" + cl);
44: ClassLoader parent = cl;
45: while (parent != null) {
46: results.append("\n.." + parent);
47: URL[] urls = getClassLoaderURLs(parent);
48: int length = urls != null ? urls.length : 0;
49: for (int u = 0; u < length; u++) {
50: results.append("\n...." + urls[u]);
51: }
52: if (parent != null)
53: parent = parent.getParent();
54: }
55: CodeSource clazzCS = clazz.getProtectionDomain()
56: .getCodeSource();
57: if (clazzCS != null)
58: results.append("\n++++CodeSource: " + clazzCS);
59: else
60: results.append("\n++++Null CodeSource");
61:
62: results.append("\nImplemented Interfaces:");
63: Class[] ifaces = clazz.getInterfaces();
64: for (int i = 0; i < ifaces.length; i++) {
65: results.append("\n++" + ifaces[i]);
66: ClassLoader loader = ifaces[i].getClassLoader();
67: results.append("\n++++ClassLoader: " + loader);
68: ProtectionDomain pd = ifaces[i].getProtectionDomain();
69: CodeSource cs = pd.getCodeSource();
70: if (cs != null)
71: results.append("\n++++CodeSource: " + cs);
72: else
73: results.append("\n++++Null CodeSource");
74: }
75: }
76:
77: /** Use reflection to access a URL[] getURLs or ULR[] getAllURLs method so
78: that non-URLClassLoader class loaders, or class loaders that override
79: getURLs to return null or empty, can provide the true classpath info.
80: */
81: public static URL[] getClassLoaderURLs(ClassLoader cl) {
82: URL[] urls = {};
83: try {
84: Class returnType = urls.getClass();
85: Class[] parameterTypes = {};
86: Method getURLs = cl.getClass().getMethod("getURLs",
87: parameterTypes);
88: if (returnType.isAssignableFrom(getURLs.getReturnType())) {
89: Object[] args = {};
90: urls = (URL[]) getURLs.invoke(cl, args);
91: }
92: } catch (Exception ignore) {
93: }
94: return urls;
95: }
96:
97: }
|