KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > dd > web > util > Util


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.ejb3.test.dd.web.util;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Date JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.LinkRef JavaDoc;
32 import javax.naming.NamingEnumeration JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.NameClassPair JavaDoc;
35
36 /** A trivial utility class that is placed into the lib/util.jar directory
37  * of the war archive and used by servlets in the war to test access to the
38  * lib jars.
39  *
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 58110 $
42  */

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 JavaDoc getTime()
49    {
50       return new Date JavaDoc().toString();
51    }
52    public static URL JavaDoc configureLog4j()
53    {
54       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
55       URL JavaDoc webPropsURL = loader.getResource("weblog4j.properties");
56       URL JavaDoc web2PropsURL = loader.getResource("web2log4j.properties");
57       URL JavaDoc 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 JavaDoc webPropsURL2 = Util.class.getResource("/weblog4j.properties");
62       URL JavaDoc web2PropsURL2 = Util.class.getResource("/web2log4j.properties");
63       URL JavaDoc 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 JavaDoc indent, Context JavaDoc ctx, PrintWriter JavaDoc out)
71       throws NamingException JavaDoc
72    {
73       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
74       NamingEnumeration JavaDoc enumeration = ctx.list("");
75       while( enumeration.hasMoreElements() )
76       {
77          NameClassPair JavaDoc ncp = (NameClassPair JavaDoc)enumeration.next();
78          String JavaDoc name = ncp.getName();
79          out.print(indent + " +- " + name);
80          boolean recursive = false;
81          boolean isLinkRef = false;
82          try
83          {
84             Class JavaDoc c = loader.loadClass(ncp.getClassName());
85             if( Context JavaDoc.class.isAssignableFrom(c) )
86                recursive = true;
87             if( LinkRef JavaDoc.class.isAssignableFrom(c) )
88                isLinkRef = true;
89          }
90          catch(ClassNotFoundException JavaDoc cnfe)
91          {
92          }
93          
94          if( isLinkRef )
95          {
96             try
97             {
98                LinkRef JavaDoc link = (LinkRef JavaDoc) ctx.lookupLink(name);
99                out.print("[link -> ");
100                out.print(link.getLinkName());
101                out.print(']');
102             }
103             catch(Throwable JavaDoc 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 JavaDoc value = ctx.lookup(name);
116                if( value instanceof Context JavaDoc )
117                {
118                   Context JavaDoc subctx = (Context JavaDoc) value;
119                   showTree(indent + " | ", subctx, out);
120                }
121                else
122                {
123                   out.println(indent + " | NonContext: "+value);
124                }
125             }
126             catch(Throwable JavaDoc t)
127             {
128                out.println("Failed to lookup: "+name+", errmsg="+t.getMessage());
129             }
130          }
131          
132       }
133    }
134    
135    public static void dumpClassLoader(ClassLoader JavaDoc cl, PrintWriter JavaDoc out)
136    {
137       int level = 0;
138       while( cl != null )
139       {
140          String JavaDoc msg = "Servlet ClassLoader["+level+"]: "+cl.getClass().getName()+':'+cl.hashCode();
141          out.println(msg);
142          URL JavaDoc[] 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 JavaDoc out) throws NamingException JavaDoc
156    {
157       InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
158       Context JavaDoc enc = (Context JavaDoc) iniCtx.lookup("java:comp/env");
159       showTree("", enc, out);
160    }
161
162    public static String JavaDoc displayClassLoaders(ClassLoader JavaDoc cl) throws NamingException JavaDoc
163    {
164       StringWriter JavaDoc sw = new StringWriter JavaDoc();
165       PrintWriter JavaDoc out = new PrintWriter JavaDoc(sw);
166       dumpClassLoader(cl, out);
167       return sw.toString();
168    }
169    
170    public static String JavaDoc displayENC() throws NamingException JavaDoc
171    {
172       StringWriter JavaDoc sw = new StringWriter JavaDoc();
173       PrintWriter JavaDoc out = new PrintWriter JavaDoc(sw);
174       dumpENC(out);
175       return sw.toString();
176    }
177
178    /** Use reflection to access a URL[] getURLs method so that non-URLClassLoader
179     *class loaders that support this method can provide info.
180     */

181    private static URL JavaDoc[] getClassLoaderURLs(ClassLoader JavaDoc cl)
182    {
183       URL JavaDoc[] urls = {};
184       try
185       {
186          Class JavaDoc returnType = urls.getClass();
187          Class JavaDoc[] parameterTypes = {};
188          Method JavaDoc getURLs = cl.getClass().getMethod("getURLs", parameterTypes);
189          if( returnType.isAssignableFrom(getURLs.getReturnType()) )
190          {
191             Object JavaDoc[] args = {};
192             urls = (URL JavaDoc[]) getURLs.invoke(cl, args);
193          }
194       }
195       catch(Exception JavaDoc ignore)
196       {
197       }
198       return urls;
199    }
200 }
201
Popular Tags