001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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    }