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.ejb3.test.dd.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.util.Date;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.LinkRef;
032: import javax.naming.NamingEnumeration;
033: import javax.naming.NamingException;
034: import javax.naming.NameClassPair;
035:
036: /** A trivial utility class that is placed into the lib/util.jar directory
037: * of the war archive and used by servlets in the war to test access to the
038: * lib jars.
039: *
040: * @author Scott.Stark@jboss.org
041: * @version $Revision: 60233 $
042: */
043: public class Util {
044: static org.apache.log4j.Logger log = org.apache.log4j.Logger
045: .getLogger(Util.class);
046:
047: public static String getTime() {
048: return new Date().toString();
049: }
050:
051: public static URL configureLog4j() {
052: ClassLoader loader = Thread.currentThread()
053: .getContextClassLoader();
054: URL webPropsURL = loader.getResource("weblog4j.properties");
055: URL web2PropsURL = loader.getResource("web2log4j.properties");
056: URL propsURL = loader.getResource("log4j.properties");
057: System.out
058: .println("getResource('weblog4j.properties') via TCL = "
059: + webPropsURL);
060: System.out
061: .println("getResource('web2log4j.properties') via TCL = "
062: + web2PropsURL);
063: System.out.println("getResource('log4j.properties') via TCL = "
064: + propsURL);
065: URL webPropsURL2 = Util.class
066: .getResource("/weblog4j.properties");
067: URL web2PropsURL2 = Util.class
068: .getResource("/web2log4j.properties");
069: URL propsURL2 = Util.class.getResource("/log4j.properties");
070: System.out
071: .println("getResource('/weblog4j.properties') via CL = "
072: + webPropsURL2);
073: System.out
074: .println("getResource('web2log4j.properties') via CL = "
075: + web2PropsURL2);
076: System.out.println("getResource('/log4j.properties') via CL = "
077: + propsURL2);
078: return propsURL;
079: }
080:
081: public static void showTree(String indent, Context ctx,
082: PrintWriter out) throws NamingException {
083: ClassLoader loader = Thread.currentThread()
084: .getContextClassLoader();
085: NamingEnumeration enumeration = ctx.list("");
086: while (enumeration.hasMoreElements()) {
087: NameClassPair ncp = (NameClassPair) enumeration.next();
088: String name = ncp.getName();
089: out.print(indent + " +- " + name);
090: boolean recursive = false;
091: boolean isLinkRef = false;
092: try {
093: Class c = loader.loadClass(ncp.getClassName());
094: if (Context.class.isAssignableFrom(c))
095: recursive = true;
096: if (LinkRef.class.isAssignableFrom(c))
097: isLinkRef = true;
098: } catch (ClassNotFoundException cnfe) {
099: }
100:
101: if (isLinkRef) {
102: try {
103: LinkRef link = (LinkRef) ctx.lookupLink(name);
104: out.print("[link -> ");
105: out.print(link.getLinkName());
106: out.print(']');
107: } catch (Throwable e) {
108: log.debug("failed", e);
109: out.print("[invalid]");
110: }
111: }
112: out.println();
113:
114: if (recursive) {
115: try {
116: Object value = ctx.lookup(name);
117: if (value instanceof Context) {
118: Context subctx = (Context) value;
119: showTree(indent + " | ", subctx, out);
120: } else {
121: out.println(indent + " | NonContext: "
122: + value);
123: }
124: } catch (Throwable t) {
125: out.println("Failed to lookup: " + name
126: + ", errmsg=" + t.getMessage());
127: }
128: }
129:
130: }
131: }
132:
133: public static void dumpClassLoader(ClassLoader cl, PrintWriter out) {
134: int level = 0;
135: while (cl != null) {
136: String msg = "Servlet ClassLoader[" + level + "]: "
137: + cl.getClass().getName() + ':' + cl.hashCode();
138: out.println(msg);
139: URL[] urls = getClassLoaderURLs(cl);
140: msg = " URLs:";
141: out.println(msg);
142: for (int u = 0; u < urls.length; u++) {
143: msg = " [" + u + "] = " + urls[u];
144: out.println(msg);
145: }
146: cl = cl.getParent();
147: level++;
148: }
149: }
150:
151: public static void dumpENC(PrintWriter out) throws NamingException {
152: InitialContext iniCtx = new InitialContext();
153: Context enc = (Context) iniCtx.lookup("java:comp/env");
154: showTree("", enc, out);
155: }
156:
157: public static String displayClassLoaders(ClassLoader cl)
158: throws NamingException {
159: StringWriter sw = new StringWriter();
160: PrintWriter out = new PrintWriter(sw);
161: dumpClassLoader(cl, out);
162: return sw.toString();
163: }
164:
165: public static String displayENC() throws NamingException {
166: StringWriter sw = new StringWriter();
167: PrintWriter out = new PrintWriter(sw);
168: dumpENC(out);
169: return sw.toString();
170: }
171:
172: /** Use reflection to access a URL[] getURLs method so that non-URLClassLoader
173: *class loaders that support this method can provide info.
174: */
175: private static URL[] getClassLoaderURLs(ClassLoader cl) {
176: URL[] urls = {};
177: try {
178: Class returnType = urls.getClass();
179: Class[] parameterTypes = {};
180: Method getURLs = cl.getClass().getMethod("getURLs",
181: parameterTypes);
182: if (returnType.isAssignableFrom(getURLs.getReturnType())) {
183: Object[] args = {};
184: urls = (URL[]) getURLs.invoke(cl, args);
185: }
186: } catch (Exception ignore) {
187: }
188: return urls;
189: }
190: }
|