1 22 package org.jboss.test.web.util; 23 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.util.Date ; 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.LinkRef ; 33 import javax.naming.NamingEnumeration ; 34 import javax.naming.NamingException ; 35 import javax.naming.NameClassPair ; 36 import javax.naming.NameParser ; 37 38 45 public class Util 46 { 47 static org.jboss.logging.Logger log = 48 org.jboss.logging.Logger.getLogger(Util.class); 49 50 public static String getTime() 51 { 52 return new Date ().toString(); 53 } 54 public static URL configureLog4j() 55 { 56 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 57 URL webPropsURL = loader.getResource("weblog4j.properties"); 58 URL web2PropsURL = loader.getResource("web2log4j.properties"); 59 URL propsURL = loader.getResource("log4j.properties"); 60 System.out.println("getResource('weblog4j.properties') via TCL = "+webPropsURL); 61 System.out.println("getResource('web2log4j.properties') via TCL = "+web2PropsURL); 62 System.out.println("getResource('log4j.properties') via TCL = "+propsURL); 63 URL webPropsURL2 = Util.class.getResource("/weblog4j.properties"); 64 URL web2PropsURL2 = Util.class.getResource("/web2log4j.properties"); 65 URL propsURL2 = Util.class.getResource("/log4j.properties"); 66 System.out.println("getResource('/weblog4j.properties') via CL = "+webPropsURL2); 67 System.out.println("getResource('web2log4j.properties') via CL = "+web2PropsURL2); 68 System.out.println("getResource('/log4j.properties') via CL = "+propsURL2); 69 return propsURL; 70 } 71 72 public static void showTree(String indent, Context ctx, PrintWriter out) 73 throws NamingException 74 { 75 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 76 NamingEnumeration enumeration = ctx.list(""); 77 while( enumeration.hasMoreElements() ) 78 { 79 NameClassPair ncp = (NameClassPair )enumeration.next(); 80 String name = ncp.getName(); 81 out.print(indent + " +- " + name); 82 boolean recursive = false; 83 boolean isLinkRef = false; 84 try 85 { 86 Class c = loader.loadClass(ncp.getClassName()); 87 if( Context .class.isAssignableFrom(c) ) 88 recursive = true; 89 if( LinkRef .class.isAssignableFrom(c) ) 90 isLinkRef = true; 91 } 92 catch(ClassNotFoundException cnfe) 93 { 94 } 95 96 if( isLinkRef ) 97 { 98 try 99 { 100 LinkRef link = (LinkRef ) ctx.lookupLink(name); 101 out.print("[link -> "); 102 out.print(link.getLinkName()); 103 out.print(']'); 104 } 105 catch(Throwable e) 106 { 107 log.debug("failed", e); 108 out.print("[invalid]"); 109 } 110 } 111 out.println(); 112 113 if( recursive ) 114 { 115 try 116 { 117 Object value = ctx.lookup(name); 118 if( value instanceof Context ) 119 { 120 Context subctx = (Context ) value; 121 showTree(indent + " | ", subctx, out); 122 } 123 else 124 { 125 out.println(indent + " | NonContext: "+value); 126 } 127 } 128 catch(Throwable t) 129 { 130 out.println("Failed to lookup: "+name+", errmsg="+t.getMessage()); 131 } 132 } 133 134 } 135 } 136 137 public static void dumpClassLoader(ClassLoader cl, PrintWriter out) 138 { 139 int level = 0; 140 while( cl != null ) 141 { 142 String msg = "Servlet ClassLoader["+level+"]: "+cl.getClass().getName()+':'+cl.hashCode(); 143 out.println(msg); 144 URL [] urls = getClassLoaderURLs(cl); 145 msg = " URLs:"; 146 out.println(msg); 147 for(int u = 0; u < urls.length; u ++) 148 { 149 msg = " ["+u+"] = "+urls[u]; 150 out.println(msg); 151 } 152 cl = cl.getParent(); 153 level ++; 154 } 155 } 156 157 public static void dumpENC(PrintWriter out) throws NamingException 158 { 159 InitialContext iniCtx = new InitialContext (); 160 Context enc = (Context ) iniCtx.lookup("java:comp/env"); 161 showTree("", enc, out); 162 } 163 164 public static String displayClassLoaders(ClassLoader cl) throws NamingException 165 { 166 StringWriter sw = new StringWriter (); 167 PrintWriter out = new PrintWriter (sw); 168 dumpClassLoader(cl, out); 169 return sw.toString(); 170 } 171 172 public static String displayENC() throws NamingException 173 { 174 StringWriter sw = new StringWriter (); 175 PrintWriter out = new PrintWriter (sw); 176 dumpENC(out); 177 return sw.toString(); 178 } 179 180 183 private static URL [] getClassLoaderURLs(ClassLoader cl) 184 { 185 URL [] urls = {}; 186 try 187 { 188 Class returnType = urls.getClass(); 189 Class [] parameterTypes = {}; 190 Method getURLs = cl.getClass().getMethod("getURLs", parameterTypes); 191 if( returnType.isAssignableFrom(getURLs.getReturnType()) ) 192 { 193 Object [] args = {}; 194 urls = (URL []) getURLs.invoke(cl, args); 195 } 196 } 197 catch(Exception ignore) 198 { 199 } 200 return urls; 201 } 202 } 203 | Popular Tags |