View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.util;
20  
21  import java.net.URL;
22  
23  /**
24   * Class containing debugging utility methods
25   * 
26   * @author gavinc
27   */
28  public class Debug
29  {
30     /**
31      * Returns the location of the file that will be loaded for the given class name 
32      * 
33      * @param className The class to load
34      * @return The location of the file that will be loaded
35      * @throws ClassNotFoundException
36      */
37     public static String whichClass(String className) throws ClassNotFoundException
38     {
39        String path = className;
40        
41        // prepare the resource path
42        if (path.startsWith("/") == false)
43        {
44           path = "/" + path;
45        }
46        path = path.replace('.', '/');
47        path = path + ".class";
48        
49        // get the location
50        URL url = Debug.class.getResource(path);
51        if (url == null)
52        {
53           throw new ClassNotFoundException(className);
54        }
55        
56        // format the result
57        String location = url.toExternalForm();
58        if (location.startsWith("jar"))
59        {
60           location = location.substring(10, location.lastIndexOf("!"));
61        }
62        else if (location.startsWith("file:"))
63        {
64           location = location.substring(6);
65        }
66        
67        return location;
68     }
69     
70     /**
71      * Returns the class loader that will load the given class name
72      * 
73      * @param className The class to load
74      * @return The class loader the class will be loaded in
75      * @throws ClassNotFoundException
76      */
77     public static String whichClassLoader(String className) throws ClassNotFoundException
78     {
79        String result = "Could not determine class loader for " + className;
80        
81        Class clazz = Class.forName(className);
82        ClassLoader loader = clazz.getClassLoader();
83        
84        if (loader != null)
85        {
86           result = clazz.getClassLoader().toString();
87        }
88        
89        return result;
90     }
91     
92     /**
93      * Returns the class loader hierarchy that will load the given class name
94      * 
95      * @param className The class to load
96      * @return The hierarchy of class loaders used to load the class
97      * @throws ClassNotFoundException
98      */
99     public static String whichClassLoaderHierarchy(String className) throws ClassNotFoundException
100    {
101       StringBuffer buffer = new StringBuffer();
102       Class clazz = Class.forName(className);
103       ClassLoader loader = clazz.getClassLoader();
104       if (loader != null)
105       {
106          buffer.append(loader.toString());
107          
108          ClassLoader parent = loader.getParent();
109          while (parent != null)
110          {
111             buffer.append("\n-> ").append(parent.toString());
112             parent = parent.getParent();
113          }
114       }
115       else
116       {
117          buffer.append("Could not determine class loader for " + className);
118       }
119       
120       return buffer.toString();
121    }
122 }