User: starksm
Date: 02/02/18 11:25:29
Modified: src/main/org/jboss/test/web/util Util.java
Log:
Use reflection to obtain the classloader URL[] list rather than relying
solely on the URLClassLoader type.
Revision Changes Path
1.6 +153 -132 jbosstest/src/main/org/jboss/test/web/util/Util.java
Index: Util.java
===================================================================
RCS file: /cvsroot/jboss/jbosstest/src/main/org/jboss/test/web/util/Util.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Util.java 16 Feb 2002 11:27:04 -0000 1.5
+++ Util.java 18 Feb 2002 19:25:29 -0000 1.6
@@ -2,6 +2,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Date;
@@ -14,148 +15,168 @@
import javax.naming.NameParser;
/** A trivial utility class that is placed into the lib/util.jar directory
-of the war archive and used by servlets in the war to test access to the
-lib jars.
-
-@author [EMAIL PROTECTED]
-@version $Revision: 1.5 $
-*/
+ * of the war archive and used by servlets in the war to test access to the
+ * lib jars.
+ *
+ * @author [EMAIL PROTECTED]
+ * @version $Revision: 1.6 $
+ */
public class Util
{
- static org.apache.log4j.Category log =
- org.apache.log4j.Category.getInstance(Util.class);
+ static org.apache.log4j.Category log =
+ org.apache.log4j.Category.getInstance(Util.class);
- public static String getTime()
- {
- return new Date().toString();
- }
- public static URL configureLog4j()
- {
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- URL webPropsURL = loader.getResource("weblog4j.properties");
- URL web2PropsURL = loader.getResource("web2log4j.properties");
- URL propsURL = loader.getResource("log4j.properties");
- System.out.println("getResource('weblog4j.properties') via TCL =
"+webPropsURL);
- System.out.println("getResource('web2log4j.properties') via TCL =
"+web2PropsURL);
- System.out.println("getResource('log4j.properties') via TCL = "+propsURL);
- URL webPropsURL2 = Util.class.getResource("/weblog4j.properties");
- URL web2PropsURL2 = Util.class.getResource("/web2log4j.properties");
- URL propsURL2 = Util.class.getResource("/log4j.properties");
- System.out.println("getResource('/weblog4j.properties') via CL =
"+webPropsURL2);
- System.out.println("getResource('web2log4j.properties') via CL =
"+web2PropsURL2);
- System.out.println("getResource('/log4j.properties') via CL = "+propsURL2);
- return propsURL;
- }
-
- public static void showTree(String indent, Context ctx, PrintWriter out)
- throws NamingException
- {
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- NamingEnumeration enum = ctx.list("");
- while( enum.hasMoreElements() )
- {
- NameClassPair ncp = (NameClassPair)enum.next();
- String name = ncp.getName();
- out.print(indent + " +- " + name);
- boolean recursive = false;
- boolean isLinkRef = false;
+ public static String getTime()
+ {
+ return new Date().toString();
+ }
+ public static URL configureLog4j()
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ URL webPropsURL = loader.getResource("weblog4j.properties");
+ URL web2PropsURL = loader.getResource("web2log4j.properties");
+ URL propsURL = loader.getResource("log4j.properties");
+ System.out.println("getResource('weblog4j.properties') via TCL =
"+webPropsURL);
+ System.out.println("getResource('web2log4j.properties') via TCL =
"+web2PropsURL);
+ System.out.println("getResource('log4j.properties') via TCL = "+propsURL);
+ URL webPropsURL2 = Util.class.getResource("/weblog4j.properties");
+ URL web2PropsURL2 = Util.class.getResource("/web2log4j.properties");
+ URL propsURL2 = Util.class.getResource("/log4j.properties");
+ System.out.println("getResource('/weblog4j.properties') via CL =
"+webPropsURL2);
+ System.out.println("getResource('web2log4j.properties') via CL =
"+web2PropsURL2);
+ System.out.println("getResource('/log4j.properties') via CL = "+propsURL2);
+ return propsURL;
+ }
+
+ public static void showTree(String indent, Context ctx, PrintWriter out)
+ throws NamingException
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ NamingEnumeration enum = ctx.list("");
+ while( enum.hasMoreElements() )
+ {
+ NameClassPair ncp = (NameClassPair)enum.next();
+ String name = ncp.getName();
+ out.print(indent + " +- " + name);
+ boolean recursive = false;
+ boolean isLinkRef = false;
+ try
+ {
+ Class c = loader.loadClass(ncp.getClassName());
+ if( Context.class.isAssignableFrom(c) )
+ recursive = true;
+ if( LinkRef.class.isAssignableFrom(c) )
+ isLinkRef = true;
+ }
+ catch(ClassNotFoundException cnfe)
+ {
+ }
+
+ if( isLinkRef )
+ {
try
{
- Class c = loader.loadClass(ncp.getClassName());
- if( Context.class.isAssignableFrom(c) )
- recursive = true;
- if( LinkRef.class.isAssignableFrom(c) )
- isLinkRef = true;
+ LinkRef link = (LinkRef) ctx.lookupLink(name);
+ out.print("[link -> ");
+ out.print(link.getLinkName());
+ out.print(']');
}
- catch(ClassNotFoundException cnfe)
+ catch(Throwable e)
{
+ log.debug("failed", e);
+ out.print("[invalid]");
}
-
- if( isLinkRef )
+ }
+ out.println();
+
+ if( recursive )
+ {
+ try
{
- try
- {
- LinkRef link = (LinkRef) ctx.lookupLink(name);
- out.print("[link -> ");
- out.print(link.getLinkName());
- out.print(']');
- }
- catch(Throwable e)
- {
- log.debug("failed", e);
- out.print("[invalid]");
- }
+ Object value = ctx.lookup(name);
+ if( value instanceof Context )
+ {
+ Context subctx = (Context) value;
+ showTree(indent + " | ", subctx, out);
+ }
+ else
+ {
+ out.println(indent + " | NonContext: "+value);
+ }
}
- out.println();
-
- if( recursive )
- {
- try
- {
- Object value = ctx.lookup(name);
- if( value instanceof Context )
- {
- Context subctx = (Context) value;
- showTree(indent + " | ", subctx, out);
- }
- else
- {
- out.println(indent + " | NonContext: "+value);
- }
- }
- catch(Throwable t)
- {
- out.println("Failed to lookup: "+name+",
errmsg="+t.getMessage());
- }
- }
-
- }
- }
-
- public static void dumpClassLoader(ClassLoader cl, PrintWriter out)
- {
- int level = 0;
- while( cl != null )
- {
- String msg = "Servlet ClassLoader["+level+"]:
"+cl.getClass().getName()+':'+cl.hashCode();
- out.println(msg);
- if( cl instanceof URLClassLoader )
+ catch(Throwable t)
{
- URLClassLoader ucl = (URLClassLoader) cl;
- URL[] urls = ucl.getURLs();
- msg = " URLs:";
- out.println(msg);
- for(int u = 0; u < urls.length; u ++)
- {
- msg = " ["+u+"] = "+urls[u];
- out.println(msg);
- }
+ out.println("Failed to lookup: "+name+", errmsg="+t.getMessage());
}
- cl = cl.getParent();
- level ++;
- }
- }
-
- public static void dumpENC(PrintWriter out) throws NamingException
- {
- InitialContext iniCtx = new InitialContext();
- Context enc = (Context) iniCtx.lookup("java:comp/env");
- showTree("", enc, out);
- }
-
- public static String displayClassLoaders(ClassLoader cl) throws NamingException
- {
- StringWriter sw = new StringWriter();
- PrintWriter out = new PrintWriter(sw);
- dumpClassLoader(cl, out);
- return sw.toString();
- }
-
- public static String displayENC() throws NamingException
- {
- StringWriter sw = new StringWriter();
- PrintWriter out = new PrintWriter(sw);
- dumpENC(out);
- return sw.toString();
- }
+ }
+
+ }
+ }
+
+ public static void dumpClassLoader(ClassLoader cl, PrintWriter out)
+ {
+ int level = 0;
+ while( cl != null )
+ {
+ String msg = "Servlet ClassLoader["+level+"]:
"+cl.getClass().getName()+':'+cl.hashCode();
+ out.println(msg);
+ URL[] urls = getClassLoaderURLs(cl);
+ msg = " URLs:";
+ out.println(msg);
+ for(int u = 0; u < urls.length; u ++)
+ {
+ msg = " ["+u+"] = "+urls[u];
+ out.println(msg);
+ }
+ cl = cl.getParent();
+ level ++;
+ }
+ }
+
+ public static void dumpENC(PrintWriter out) throws NamingException
+ {
+ InitialContext iniCtx = new InitialContext();
+ Context enc = (Context) iniCtx.lookup("java:comp/env");
+ showTree("", enc, out);
+ }
+
+ public static String displayClassLoaders(ClassLoader cl) throws NamingException
+ {
+ StringWriter sw = new StringWriter();
+ PrintWriter out = new PrintWriter(sw);
+ dumpClassLoader(cl, out);
+ return sw.toString();
+ }
+
+ public static String displayENC() throws NamingException
+ {
+ StringWriter sw = new StringWriter();
+ PrintWriter out = new PrintWriter(sw);
+ dumpENC(out);
+ return sw.toString();
+ }
+
+ /** Use reflection to access a URL[] getURLs method so that non-URLClassLoader
+ *class loaders that support this method can provide info.
+ */
+ private 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;
+ }
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development