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