001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.kernel.util;
018    
019    import java.net.URLClassLoader;
020    import java.net.URL;
021    import org.apache.geronimo.kernel.config.MultiParentClassLoader;
022    
023    /**
024     * Shows the ID and contents of a ClassLoader
025     *
026     * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
027     */
028    public class ClassLoaderDumper {
029        public static void dump(Object o) {
030            if(o != null) dump(o.getClass().getClassLoader());
031        }
032        public static void dump(Class cls) {
033            System.out.println("ClassLoader dump for "+cls.getName());
034            dump(cls.getClassLoader());
035        }
036        public static void dump(ClassLoader loader) {
037            dumpIDs("", loader);
038            dumpContents("", loader);
039        }
040        private static void dumpIDs(String prefix, ClassLoader loader) {
041            if(loader == null) return;
042            System.out.println(prefix+"ClassLoader is "+loader);
043    
044            if(loader instanceof MultiParentClassLoader) {
045                MultiParentClassLoader mp = (MultiParentClassLoader) loader;
046                ClassLoader[] parents = mp.getParents();
047                for (int i = 0; i < parents.length; i++) {
048                    dumpIDs(prefix+"  ", parents[i]);
049                }
050            } else {
051                dumpIDs(prefix+"  ", loader.getParent());
052            }
053        }
054        private static void dumpContents(String prefix, ClassLoader loader) {
055            if(loader == null) return;
056            System.out.println(prefix+"ClassLoader is "+loader);
057    
058            if(loader instanceof URLClassLoader) {
059                URLClassLoader url = (URLClassLoader) loader;
060                URL[] entries = url.getURLs();
061                for (int i = 0; i < entries.length; i++) {
062                    URL entry = entries[i];
063                    System.out.println(prefix+"  "+entry);
064                }
065            }
066            if(loader instanceof MultiParentClassLoader) {
067                MultiParentClassLoader mp = (MultiParentClassLoader) loader;
068                ClassLoader[] parents = mp.getParents();
069                for (int i = 0; i < parents.length; i++) {
070                    dumpContents(prefix+"    ", parents[i]);
071                }
072            } else {
073                dumpContents(prefix+"    ", loader.getParent());
074            }
075        }
076    }