001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.web.util;
023:
024: import java.io.PrintWriter;
025: import java.io.StringWriter;
026: import java.lang.reflect.Method;
027: import java.net.URL;
028: import java.net.URLClassLoader;
029: import java.util.Date;
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.naming.LinkRef;
033: import javax.naming.NamingEnumeration;
034: import javax.naming.NamingException;
035: import javax.naming.NameClassPair;
036: import javax.naming.NameParser;
037:
038: /** A trivial utility class that is placed into the lib/util.jar directory
039: * of the war archive and used by servlets in the war to test access to the
040: * lib jars.
041: *
042: * @author Scott.Stark@jboss.org
043: * @version $Revision: 57211 $
044: */
045: public class Util {
046: static org.apache.log4j.Category log = org.apache.log4j.Category
047: .getInstance(Util.class);
048:
049: public static String getTime() {
050: return new Date().toString();
051: }
052:
053: public static URL configureLog4j() {
054: ClassLoader loader = Thread.currentThread()
055: .getContextClassLoader();
056: URL webPropsURL = loader.getResource("weblog4j.properties");
057: URL web2PropsURL = loader.getResource("web2log4j.properties");
058: URL propsURL = loader.getResource("log4j.properties");
059: System.out
060: .println("getResource('weblog4j.properties') via TCL = "
061: + webPropsURL);
062: System.out
063: .println("getResource('web2log4j.properties') via TCL = "
064: + web2PropsURL);
065: System.out.println("getResource('log4j.properties') via TCL = "
066: + propsURL);
067: URL webPropsURL2 = Util.class
068: .getResource("/weblog4j.properties");
069: URL web2PropsURL2 = Util.class
070: .getResource("/web2log4j.properties");
071: URL propsURL2 = Util.class.getResource("/log4j.properties");
072: System.out
073: .println("getResource('/weblog4j.properties') via CL = "
074: + webPropsURL2);
075: System.out
076: .println("getResource('web2log4j.properties') via CL = "
077: + web2PropsURL2);
078: System.out.println("getResource('/log4j.properties') via CL = "
079: + propsURL2);
080: return propsURL;
081: }
082:
083: public static void showTree(String indent, Context ctx,
084: PrintWriter out) throws NamingException {
085: ClassLoader loader = Thread.currentThread()
086: .getContextClassLoader();
087: NamingEnumeration i = ctx.list("");
088: while (i.hasMoreElements()) {
089: NameClassPair ncp = (NameClassPair) i.next();
090: String name = ncp.getName();
091: out.print(indent + " +- " + name);
092: boolean recursive = false;
093: boolean isLinkRef = false;
094: try {
095: Class c = loader.loadClass(ncp.getClassName());
096: if (Context.class.isAssignableFrom(c))
097: recursive = true;
098: if (LinkRef.class.isAssignableFrom(c))
099: isLinkRef = true;
100: } catch (ClassNotFoundException cnfe) {
101: }
102:
103: if (isLinkRef) {
104: try {
105: LinkRef link = (LinkRef) ctx.lookupLink(name);
106: out.print("[link -> ");
107: out.print(link.getLinkName());
108: out.print(']');
109: } catch (Throwable e) {
110: log.debug("failed", e);
111: out.print("[invalid]");
112: }
113: }
114: out.println();
115:
116: if (recursive) {
117: try {
118: Object value = ctx.lookup(name);
119: if (value instanceof Context) {
120: Context subctx = (Context) value;
121: showTree(indent + " | ", subctx, out);
122: } else {
123: out.println(indent + " | NonContext: "
124: + value);
125: }
126: } catch (Throwable t) {
127: out.println("Failed to lookup: " + name
128: + ", errmsg=" + t.getMessage());
129: }
130: }
131:
132: }
133: }
134:
135: public static void dumpClassLoader(ClassLoader cl, PrintWriter out) {
136: int level = 0;
137: while (cl != null) {
138: String msg = "Servlet ClassLoader[" + level + "]: "
139: + cl.getClass().getName() + ':' + cl.hashCode();
140: out.println(msg);
141: URL[] urls = getClassLoaderURLs(cl);
142: msg = " URLs:";
143: out.println(msg);
144: for (int u = 0; u < urls.length; u++) {
145: msg = " [" + u + "] = " + urls[u];
146: out.println(msg);
147: }
148: cl = cl.getParent();
149: level++;
150: }
151: }
152:
153: public static void dumpENC(PrintWriter out) throws NamingException {
154: InitialContext iniCtx = new InitialContext();
155: Context enc = (Context) iniCtx.lookup("java:comp/env");
156: showTree("", enc, out);
157: }
158:
159: public static String displayClassLoaders(ClassLoader cl)
160: throws NamingException {
161: StringWriter sw = new StringWriter();
162: PrintWriter out = new PrintWriter(sw);
163: dumpClassLoader(cl, out);
164: return sw.toString();
165: }
166:
167: public static String displayENC() throws NamingException {
168: StringWriter sw = new StringWriter();
169: PrintWriter out = new PrintWriter(sw);
170: dumpENC(out);
171: return sw.toString();
172: }
173:
174: /** Use reflection to access a URL[] getURLs method so that non-URLClassLoader
175: *class loaders that support this method can provide info.
176: */
177: private static URL[] getClassLoaderURLs(ClassLoader cl) {
178: URL[] urls = {};
179: try {
180: Class returnType = urls.getClass();
181: Class[] parameterTypes = {};
182: Method getURLs = cl.getClass().getMethod("getURLs",
183: parameterTypes);
184: if (returnType.isAssignableFrom(getURLs.getReturnType())) {
185: Object[] args = {};
186: urls = (URL[]) getURLs.invoke(cl, args);
187: }
188: } catch (Exception ignore) {
189: }
190: return urls;
191: }
192: }
|