KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > loading > DefaultLoaderRepository


1 /*
2  * @(#)DefaultLoaderRepository.java 1.18 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management.loading;
9
10
11 // Java import
12

13 import java.security.AccessController JavaDoc;
14 import java.security.Permission JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 // JMX import
19
import javax.management.MBeanServer JavaDoc;
20 import javax.management.MBeanServerFactory JavaDoc;
21 import javax.management.loading.ClassLoaderRepository JavaDoc;
22
23 import com.sun.jmx.trace.Trace;
24
25 /**
26  * <p>Keeps the list of Class Loaders registered in the MBean Server.
27  * It provides the necessary methods to load classes using the registered
28  * Class Loaders.</p>
29  *
30  * <p>This deprecated class is maintained for compatibility. In
31  * previous versions of JMX, there was one
32  * <code>DefaultLoaderRepository</code> shared by all MBean servers.
33  * As of JMX 1.2, that functionality is approximated by using {@link
34  * MBeanServerFactory#findMBeanServer} to find all known MBean
35  * servers, and consulting the {@link ClassLoaderRepository} of each
36  * one. It is strongly recommended that code referencing
37  * <code>DefaultLoaderRepository</code> be rewritten.</p>
38  *
39  * @deprecated Use
40  * {@link javax.management.MBeanServer#getClassLoaderRepository()}}
41  * instead.
42  *
43  * @since 1.5
44  */

45 @Deprecated JavaDoc
46 public class DefaultLoaderRepository {
47
48     private static final String JavaDoc dbgTag = "DefaultLoaderRepository";
49
50     /**
51      * Go through the list of class loaders and try to load the requested
52      * class.
53      * The method will stop as soon as the class is found. If the class
54      * is not found the method will throw a <CODE>ClassNotFoundException</CODE>
55      * exception.
56      *
57      * @param className The name of the class to be loaded.
58      *
59      * @return the loaded class.
60      *
61      * @exception ClassNotFoundException The specified class could not be
62      * found.
63      */

64     public static Class JavaDoc loadClass(String JavaDoc className)
65     throws ClassNotFoundException JavaDoc {
66     debug("loadClass",className);
67     return load(null, className);
68     }
69
70     /**
71      * Go through the list of class loaders but exclude the given
72      * class loader, then try to load
73      * the requested class.
74      * The method will stop as soon as the class is found. If the class
75      * is not found the method will throw a <CODE>ClassNotFoundException</CODE>
76      * exception.
77      *
78      * @param className The name of the class to be loaded.
79      * @param loader The class loader to be excluded.
80      *
81      * @return the loaded class.
82      *
83      * @exception ClassNotFoundException The specified class could not be
84      * found.
85      */

86     public static Class JavaDoc loadClassWithout(ClassLoader JavaDoc loader,
87                      String JavaDoc className)
88     throws ClassNotFoundException JavaDoc {
89     debug("loadClassWithout",className);
90     return load(loader, className);
91     }
92
93     private static Class JavaDoc load(ClassLoader JavaDoc without, String JavaDoc className)
94         throws ClassNotFoundException JavaDoc {
95     final List JavaDoc mbsList = MBeanServerFactory.findMBeanServer(null);
96
97     for (Iterator JavaDoc it = mbsList.iterator(); it.hasNext(); ) {
98         MBeanServer JavaDoc mbs = (MBeanServer JavaDoc) it.next();
99         ClassLoaderRepository JavaDoc clr = mbs.getClassLoaderRepository();
100         try {
101         return clr.loadClassWithout(without, className);
102         } catch (ClassNotFoundException JavaDoc e) {
103         // OK : Try with next one...
104
}
105     }
106     throw new ClassNotFoundException JavaDoc(className);
107     }
108     
109     // TRACES & DEBUG
110
//---------------
111

112     private static boolean isTraceOn() {
113         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
114     }
115
116     private static void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
117         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
118     }
119
120     private static void trace(String JavaDoc func, String JavaDoc info) {
121         trace(dbgTag, func, info);
122     }
123
124     private static boolean isDebugOn() {
125         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
126     }
127
128     private static void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
129         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
130     }
131
132     private static void debug(String JavaDoc func, String JavaDoc info) {
133         debug(dbgTag, func, info);
134     }
135  
136  }
137
Popular Tags