KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > 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.test.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.net.URLClassLoader JavaDoc;
29 import java.util.Date JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.LinkRef JavaDoc;
33 import javax.naming.NamingEnumeration JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.naming.NameClassPair JavaDoc;
36 import javax.naming.NameParser JavaDoc;
37
38 /** A trivial utility class that is placed into the lib/util.jar directory
39  * of the war archive and used by servlets in the war to test access to the
40  * lib jars.
41  *
42  * @author Scott.Stark@jboss.org
43  * @version $Revision: 58115 $
44  */

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

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