KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > util > ClassLoaderDumper


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.net.URL JavaDoc;
21 import org.apache.geronimo.kernel.config.MultiParentClassLoader;
22
23 /**
24  * Shows the ID and contents of a ClassLoader
25  *
26  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
27  */

28 public class ClassLoaderDumper {
29     public static void dump(Object JavaDoc o) {
30         if(o != null) dump(o.getClass().getClassLoader());
31     }
32     public static void dump(Class JavaDoc cls) {
33         System.out.println("ClassLoader dump for "+cls.getName());
34         dump(cls.getClassLoader());
35     }
36     public static void dump(ClassLoader JavaDoc loader) {
37         dumpIDs("", loader);
38         dumpContents("", loader);
39     }
40     private static void dumpIDs(String JavaDoc prefix, ClassLoader JavaDoc 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 JavaDoc[] 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 JavaDoc prefix, ClassLoader JavaDoc loader) {
55         if(loader == null) return;
56         System.out.println(prefix+"ClassLoader is "+loader);
57
58         if(loader instanceof URLClassLoader JavaDoc) {
59             URLClassLoader JavaDoc url = (URLClassLoader JavaDoc) loader;
60             URL JavaDoc[] entries = url.getURLs();
61             for (int i = 0; i < entries.length; i++) {
62                 URL JavaDoc entry = entries[i];
63                 System.out.println(prefix+" "+entry);
64             }
65         }
66         if(loader instanceof MultiParentClassLoader) {
67             MultiParentClassLoader mp = (MultiParentClassLoader) loader;
68             ClassLoader JavaDoc[] 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 }
77
Popular Tags