001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: Debug.java,v 1.3 2006/09/29 12:32:08 drmlipp Exp $
021: *
022: * $Log: Debug.java,v $
023: * Revision 1.3 2006/09/29 12:32:08 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.2 2005/09/10 21:44:18 mlipp
027: * Fixed JDK 5.0 warnings.
028: *
029: * Revision 1.1.1.3 2003/12/19 13:01:36 drmlipp
030: * Updated to 1.1rc1
031: *
032: * Revision 1.8 2003/11/21 14:47:32 lipp
033: * Fixed debugging output.
034: *
035: * Revision 1.7 2003/08/25 12:33:50 lipp
036: * Made compilation independant of JBoss.
037: *
038: * Revision 1.6 2003/06/27 08:51:47 lipp
039: * Fixed copyright/license information.
040: *
041: * Revision 1.5 2003/03/31 16:50:27 huaiyang
042: * Logging using common-logging.
043: *
044: * Revision 1.4 2002/11/28 22:49:51 lipp
045: * Added possibility to output to stderr.
046: *
047: * Revision 1.3 2002/09/08 18:37:00 lipp
048: * More info in class loader output.
049: *
050: * Revision 1.2 2002/07/24 05:48:16 huaiyang
051: * javadocs added.
052: *
053: * Revision 1.1 2002/06/24 19:58:17 lipp
054: * Helper class.
055: *
056: */
057: package de.danet.an.util;
058:
059: import java.lang.reflect.Method;
060: import java.net.URL;
061: import java.net.URLClassLoader;
062:
063: import org.apache.commons.logging.Log;
064:
065: /**
066: * This class provides some utility methods for debugging.
067: *
068: * @author <a href="mailto:mnl@mnl.de">Michael N. Lipp</a>
069: * @version $Revision: 1.3 $
070: */
071:
072: public class Debug {
073:
074: /**
075: * Dumps the class loader used from booting to current.
076: * @param logger the logger (<code>org.apache.commons.logging.Log</code>).
077: * @param cl current used class loader.
078: */
079: public static void dumpClassLoaders(Log logger, ClassLoader cl) {
080: log(logger, "Class loaders (from boot to current):");
081: dumpLoader(logger, cl);
082: }
083:
084: private static void dumpLoader(Log logger, ClassLoader cl) {
085: if (cl.getParent() != null) {
086: dumpLoader(logger, cl.getParent());
087: }
088: log(logger, " " + cl.toString());
089: if (cl instanceof URLClassLoader) {
090: URL[] urls = ((URLClassLoader) cl).getURLs();
091: for (int i = 0; i < urls.length; i++) {
092: log(logger, " " + urls[i].toString());
093: }
094: }
095: try {
096: Class ucf = Thread.currentThread().getContextClassLoader()
097: .loadClass(
098: "org.jboss.mx.loading.UnifiedClassLoader");
099: if (ucf.isAssignableFrom(cl.getClass())) {
100: log(logger, " -- URLs from repository -- ");
101: Method m = ucf.getMethod("getAllURLs", null);
102: URL[] urls = (URL[]) m.invoke(cl, (Class[]) null);
103: for (int i = 0; i < urls.length; i++) {
104: log(logger, " " + urls[i].toString());
105: }
106: }
107: } catch (Exception e) {
108: // If not used in jboss environment, nothing missed.
109: }
110: }
111:
112: /**
113: * Writes a log message either to the logger or to standard error.
114: * @param logger the logger (<code>org.apache.commons.logging.Log</code>).
115: * @param msg the message.
116: */
117: private static void log(Log logger, String msg) {
118: if (logger != null) {
119: logger.debug(msg);
120: } else {
121: System.err.println(msg);
122: }
123: }
124: }
|