View Javadoc

1   /*
2    * Copyright (c) 2004 Peter Antman, Mogul  <peter.antman@mogul.com>
3    *
4    * $Id: ClassLoaderUtil.java,v 1.1.1.1 2004/05/19 12:07:30 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.utils.lang;
21  import java.net.URL;
22  import java.net.URLClassLoader;
23  /***
24   * Commpon functions needed when working whith classloader.
25   * 
26   * @todo move in methods from other classes
27   * @author <a href="mailto:pra@mogul.com">Peter Antman</a>
28   * @version $Revision: 1.1.1.1 $
29   */
30  
31  public class ClassLoaderUtil {
32     //--- Some handy debug methods ----
33     public static void printLoader(Object o ){
34        System.out.println(o + " loaded by " + o.getClass().getClassLoader() + " in context classloader " + Thread.currentThread().getContextClassLoader());
35     }
36  
37     public static String getURLs(URL[] urls) {
38        if ( urls == null) {
39           return "{}";
40        } // end of if ()
41        StringBuffer b = new StringBuffer("{");
42        for ( int i = 0;i<urls.length;i++) {
43           b.append( urls[i] ).append(":");
44        } // end of for ()
45        b.append("}");
46        return b.toString();
47     }
48     
49     public static void printClassLoaderTree(ClassLoader l) {
50        ClassLoader p = l.getParent();
51        if ( p!= null) {
52           printClassLoaderTree(p);
53        } // end of if ()
54        String u = "";
55        if ( l instanceof URLClassLoader) {
56           u = getURLs(  ((URLClassLoader)l).getURLs());
57        } // end of if ()
58        
59        System.out.println("\t|\n"+l+" " +u);
60     } 
61     public static void printClassLoaderTree(Object o) {
62        System.out.println("\nClass loader tree");
63        printClassLoaderTree( o.getClass().getClassLoader());
64        System.out.println("\nContext loader tree");
65        ClassLoader l = Thread.currentThread().getContextClassLoader();
66        printClassLoaderTree(l );
67     }
68    
69  }// ClassLoaderUtil