KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > util > Debug


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * 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 JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.security.CodeSource JavaDoc;
27 import java.security.ProtectionDomain JavaDoc;
28
29 /** Various debugging utility methods available for use in unit tests
30  * @author Scott.Stark@jboss.org
31  */

32 public class Debug
33 {
34    /** Format a string buffer containing the Class, Interfaces, CodeSource,
35     and ClassLoader information for the given object clazz.
36
37     @param clazz the Class
38     @param results, the buffer to write the info to
39     */

40    public static void displayClassInfo(Class JavaDoc clazz, StringBuffer JavaDoc results)
41    {
42       // Print out some codebase info for the ProbeHome
43
ClassLoader JavaDoc cl = clazz.getClassLoader();
44       results.append("\n"+clazz.getName()+".ClassLoader="+cl);
45       ClassLoader JavaDoc parent = cl;
46       while( parent != null )
47       {
48          results.append("\n.."+parent);
49          URL JavaDoc[] urls = getClassLoaderURLs(parent);
50          int length = urls != null ? urls.length : 0;
51          for(int u = 0; u < length; u ++)
52          {
53             results.append("\n...."+urls[u]);
54          }
55          if( parent != null )
56             parent = parent.getParent();
57       }
58       CodeSource JavaDoc clazzCS = clazz.getProtectionDomain().getCodeSource();
59       if( clazzCS != null )
60          results.append("\n++++CodeSource: "+clazzCS);
61       else
62          results.append("\n++++Null CodeSource");
63
64       results.append("\nImplemented Interfaces:");
65       Class JavaDoc[] ifaces = clazz.getInterfaces();
66       for(int i = 0; i < ifaces.length; i ++)
67       {
68          results.append("\n++"+ifaces[i]);
69          ClassLoader JavaDoc loader = ifaces[i].getClassLoader();
70          results.append("\n++++ClassLoader: "+loader);
71          ProtectionDomain JavaDoc pd = ifaces[i].getProtectionDomain();
72          CodeSource JavaDoc cs = pd.getCodeSource();
73          if( cs != null )
74             results.append("\n++++CodeSource: "+cs);
75          else
76             results.append("\n++++Null CodeSource");
77       }
78    }
79
80    /** Use reflection to access a URL[] getURLs or ULR[] getAllURLs method so
81     that non-URLClassLoader class loaders, or class loaders that override
82     getURLs to return null or empty, can provide the true classpath info.
83     */

84    public static URL JavaDoc[] getClassLoaderURLs(ClassLoader JavaDoc cl)
85    {
86       URL JavaDoc[] urls = {};
87       try
88       {
89          Class JavaDoc returnType = urls.getClass();
90          Class JavaDoc[] parameterTypes = {};
91          Method JavaDoc getURLs = cl.getClass().getMethod("getURLs", parameterTypes);
92          if( returnType.isAssignableFrom(getURLs.getReturnType()) )
93          {
94             Object JavaDoc[] args = {};
95             urls = (URL JavaDoc[]) getURLs.invoke(cl, args);
96          }
97       }
98       catch(Exception JavaDoc ignore)
99       {
100       }
101       return urls;
102    }
103    
104 }
105
Popular Tags