KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > Debug


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import java.net.URL JavaDoc;
20
21 /**
22  * Class containing debugging utility methods
23  *
24  * @author gavinc
25  */

26 public class Debug
27 {
28    /**
29     * Returns the location of the file that will be loaded for the given class name
30     *
31     * @param className The class to load
32     * @return The location of the file that will be loaded
33     * @throws ClassNotFoundException
34     */

35    public static String JavaDoc whichClass(String JavaDoc className) throws ClassNotFoundException JavaDoc
36    {
37       String JavaDoc path = className;
38       
39       // prepare the resource path
40
if (path.startsWith("/") == false)
41       {
42          path = "/" + path;
43       }
44       path = path.replace('.', '/');
45       path = path + ".class";
46       
47       // get the location
48
URL JavaDoc url = Debug.class.getResource(path);
49       if (url == null)
50       {
51          throw new ClassNotFoundException JavaDoc(className);
52       }
53       
54       // format the result
55
String JavaDoc location = url.toExternalForm();
56       if (location.startsWith("jar"))
57       {
58          location = location.substring(10, location.lastIndexOf("!"));
59       }
60       else if (location.startsWith("file:"))
61       {
62          location = location.substring(6);
63       }
64       
65       return location;
66    }
67    
68    /**
69     * Returns the class loader that will load the given class name
70     *
71     * @param className The class to load
72     * @return The class loader the class will be loaded in
73     * @throws ClassNotFoundException
74     */

75    public static String JavaDoc whichClassLoader(String JavaDoc className) throws ClassNotFoundException JavaDoc
76    {
77       String JavaDoc result = "Could not determine class loader for " + className;
78       
79       Class JavaDoc clazz = Class.forName(className);
80       ClassLoader JavaDoc loader = clazz.getClassLoader();
81       
82       if (loader != null)
83       {
84          result = clazz.getClassLoader().toString();
85       }
86       
87       return result;
88    }
89    
90    /**
91     * Returns the class loader hierarchy that will load the given class name
92     *
93     * @param className The class to load
94     * @return The hierarchy of class loaders used to load the class
95     * @throws ClassNotFoundException
96     */

97    public static String JavaDoc whichClassLoaderHierarchy(String JavaDoc className) throws ClassNotFoundException JavaDoc
98    {
99       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
100       Class JavaDoc clazz = Class.forName(className);
101       ClassLoader JavaDoc loader = clazz.getClassLoader();
102       if (loader != null)
103       {
104          buffer.append(loader.toString());
105          
106          ClassLoader JavaDoc parent = loader.getParent();
107          while (parent != null)
108          {
109             buffer.append("\n-> ").append(parent.toString());
110             parent = parent.getParent();
111          }
112       }
113       else
114       {
115          buffer.append("Could not determine class loader for " + className);
116       }
117       
118       return buffer.toString();
119    }
120 }
121
Popular Tags