View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.kernel.util;
18  
19  import java.net.URLClassLoader;
20  import java.net.URL;
21  import org.apache.geronimo.kernel.config.MultiParentClassLoader;
22  
23  /**
24   * Shows the ID and contents of a ClassLoader
25   *
26   * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
27   */
28  public class ClassLoaderDumper {
29      public static void dump(Object o) {
30          if(o != null) dump(o.getClass().getClassLoader());
31      }
32      public static void dump(Class cls) {
33          System.out.println("ClassLoader dump for "+cls.getName());
34          dump(cls.getClassLoader());
35      }
36      public static void dump(ClassLoader loader) {
37          dumpIDs("", loader);
38          dumpContents("", loader);
39      }
40      private static void dumpIDs(String prefix, ClassLoader loader) {
41          if(loader == null) return;
42          System.out.println(prefix+"ClassLoader is "+loader);
43  
44          if(loader instanceof MultiParentClassLoader) {
45              MultiParentClassLoader mp = (MultiParentClassLoader) loader;
46              ClassLoader[] parents = mp.getParents();
47              for (int i = 0; i < parents.length; i++) {
48                  dumpIDs(prefix+"  ", parents[i]);
49              }
50          } else {
51              dumpIDs(prefix+"  ", loader.getParent());
52          }
53      }
54      private static void dumpContents(String prefix, ClassLoader loader) {
55          if(loader == null) return;
56          System.out.println(prefix+"ClassLoader is "+loader);
57  
58          if(loader instanceof URLClassLoader) {
59              URLClassLoader url = (URLClassLoader) loader;
60              URL[] entries = url.getURLs();
61              for (int i = 0; i < entries.length; i++) {
62                  URL entry = entries[i];
63                  System.out.println(prefix+"  "+entry);
64              }
65          }
66          if(loader instanceof MultiParentClassLoader) {
67              MultiParentClassLoader mp = (MultiParentClassLoader) loader;
68              ClassLoader[] parents = mp.getParents();
69              for (int i = 0; i < parents.length; i++) {
70                  dumpContents(prefix+"    ", parents[i]);
71              }
72          } else {
73              dumpContents(prefix+"    ", loader.getParent());
74          }
75      }
76  }