[prev in list] [next in list] [prev in thread] [next in thread]
List: jboss-cvs-commits
Subject: [jboss-cvs] jmx/src/main/org/jboss/mx/loading ClassLoaderUtils.java
From: Scott M Stark <starksm () users ! sourceforge ! net>
Date: 2002-10-31 7:49:44
[Download RAW message or body]
User: starksm
Date: 02/10/30 23:49:44
Modified: src/main/org/jboss/mx/loading Tag: Branch_3_0
ClassLoaderUtils.java
Log:
- Add a displayClassInfo utility method that prints a complete description
of a class, its interfaces, code source and class loader heirarchy.
- Log any package for which more than one class loader exists as this
may indicate a problem with duplicate classes
Revision Changes Path
No revision
No revision
1.1.2.5 +81 -2 jmx/src/main/org/jboss/mx/loading/ClassLoaderUtils.java
Index: ClassLoaderUtils.java
===================================================================
RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/loading/ClassLoaderUtils.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- ClassLoaderUtils.java 20 Oct 2002 08:49:27 -0000 1.1.2.4
+++ ClassLoaderUtils.java 31 Oct 2002 07:49:44 -0000 1.1.2.5
@@ -13,7 +13,10 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
+import java.lang.reflect.Method;
import java.net.URL;
+import java.security.CodeSource;
+import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
@@ -25,13 +28,84 @@
/** Utility methods for class loader to package names, etc.
*
* @author Scott.Stark@jboss.org
- * @version $Revision: 1.1.2.4 $
+ * @version $Revision: 1.1.2.5 $
*/
public class ClassLoaderUtils
{
private static Logger log = Logger.getLogger(ClassLoaderUtils.class);
+ /** Format a string buffer containing the Class, Interfaces, CodeSource,
+ and ClassLoader information for the given object clazz.
+
+ @param clazz the Class
+ @param results, the buffer to write the info to
+ */
+ public static void displayClassInfo(Class clazz, StringBuffer results)
+ {
+ // Print out some codebase info for the ProbeHome
+ ClassLoader cl = clazz.getClassLoader();
+ results.append("\n"+clazz.getName()+"("+Integer.toHexString(clazz.hashCode())+").ClassLoader="+cl);
+ ClassLoader parent = cl;
+ while( parent != null )
+ {
+ results.append("\n.."+parent);
+ URL[] urls = getClassLoaderURLs(parent);
+ int length = urls != null ? urls.length : 0;
+ for(int u = 0; u < length; u ++)
+ {
+ results.append("\n...."+urls[u]);
+ }
+ if( parent != null )
+ parent = parent.getParent();
+ }
+ CodeSource clazzCS = clazz.getProtectionDomain().getCodeSource();
+ if( clazzCS != null )
+ results.append("\n++++CodeSource: "+clazzCS);
+ else
+ results.append("\n++++Null CodeSource");
+
+ results.append("\nImplemented Interfaces:");
+ Class[] ifaces = clazz.getInterfaces();
+ for(int i = 0; i < ifaces.length; i ++)
+ {
+ Class iface = ifaces[i];
+ results.append("\n++"+iface+"("+Integer.toHexString(iface.hashCode())+")");
+ ClassLoader loader = ifaces[i].getClassLoader();
+ results.append("\n++++ClassLoader: "+loader);
+ ProtectionDomain pd = ifaces[i].getProtectionDomain();
+ CodeSource cs = pd.getCodeSource();
+ if( cs != null )
+ results.append("\n++++CodeSource: "+cs);
+ else
+ results.append("\n++++Null CodeSource");
+ }
+ }
+
+ /** Use reflection to access a URL[] getURLs or ULR[] getAllURLs method so
+ that non-URLClassLoader class loaders, or class loaders that override
+ getURLs to return null or empty, can provide the true classpath info.
+ */
+ public static URL[] getClassLoaderURLs(ClassLoader cl)
+ {
+ URL[] urls = {};
+ try
+ {
+ Class returnType = urls.getClass();
+ Class[] parameterTypes = {};
+ Method getURLs = cl.getClass().getMethod("getURLs", parameterTypes);
+ if( returnType.isAssignableFrom(getURLs.getReturnType()) )
+ {
+ Object[] args = {};
+ urls = (URL[]) getURLs.invoke(cl, args);
+ }
+ }
+ catch(Exception ignore)
+ {
+ }
+ return urls;
+ }
+
/** Parse a class name into its package prefix. This has to handle
array classes whose name is prefixed with [L.
*/
@@ -145,8 +219,13 @@
{
pkgSet.add(cl);
pkgNameSet.add(pkgName);
+ // Anytime more than one class loader exists this may indicate a problem
+ if( pkgSet.size() > 1 )
+ {
+ log.debug("Multiple class loaders found for pkg: "+pkgName);
+ }
if( trace )
- log.trace(" Indexed pkg: "+pkgName);
+ log.trace("Indexed pkg: "+pkgName+", UCL: "+cl);
}
}
-------------------------------------------------------
This sf.net email is sponsored by: Influence the future
of Java(TM) technology. Join the Java Community
Process(SM) (JCP(SM)) program now.
http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0004en
_______________________________________________
jboss-cvs-commits mailing list
jboss-cvs-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-cvs-commits
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic