blob: 0d514dd780a80e9b12fa91e0579a218127b8514d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.management.loading;
27
28import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
29import java.util.Iterator;
30import java.util.List;
31import java.util.logging.Level;
32import javax.management.MBeanServer;
33import javax.management.MBeanServerFactory;
34
35/**
36 * <p>Keeps the list of Class Loaders registered in the MBean Server.
37 * It provides the necessary methods to load classes using the registered
38 * Class Loaders.</p>
39 *
40 * <p>This deprecated class is maintained for compatibility. In
41 * previous versions of JMX, there was one
42 * <code>DefaultLoaderRepository</code> shared by all MBean servers.
43 * As of JMX 1.2, that functionality is approximated by using {@link
44 * MBeanServerFactory#findMBeanServer} to find all known MBean
45 * servers, and consulting the {@link ClassLoaderRepository} of each
46 * one. It is strongly recommended that code referencing
47 * <code>DefaultLoaderRepository</code> be rewritten.</p>
48 *
49 * @deprecated Use
50 * {@link javax.management.MBeanServer#getClassLoaderRepository()}}
51 * instead.
52 *
53 * @since 1.5
54 */
55@Deprecated
56public class DefaultLoaderRepository {
57
58 /**
59 * Go through the list of class loaders and try to load the requested
60 * class.
61 * The method will stop as soon as the class is found. If the class
62 * is not found the method will throw a <CODE>ClassNotFoundException</CODE>
63 * exception.
64 *
65 * @param className The name of the class to be loaded.
66 *
67 * @return the loaded class.
68 *
69 * @exception ClassNotFoundException The specified class could not be
70 * found.
71 */
72 public static Class loadClass(String className)
73 throws ClassNotFoundException {
74 MBEANSERVER_LOGGER.logp(Level.FINEST,
75 DefaultLoaderRepository.class.getName(),
76 "loadClass", className);
77 return load(null, className);
78 }
79
80 /**
81 * Go through the list of class loaders but exclude the given
82 * class loader, then try to load
83 * the requested class.
84 * The method will stop as soon as the class is found. If the class
85 * is not found the method will throw a <CODE>ClassNotFoundException</CODE>
86 * exception.
87 *
88 * @param className The name of the class to be loaded.
89 * @param loader The class loader to be excluded.
90 *
91 * @return the loaded class.
92 *
93 * @exception ClassNotFoundException The specified class could not be
94 * found.
95 */
96 public static Class loadClassWithout(ClassLoader loader,
97 String className)
98 throws ClassNotFoundException {
99 MBEANSERVER_LOGGER.logp(Level.FINEST,
100 DefaultLoaderRepository.class.getName(),
101 "loadClassWithout", className);
102 return load(loader, className);
103 }
104
105 private static Class load(ClassLoader without, String className)
106 throws ClassNotFoundException {
107 final List mbsList = MBeanServerFactory.findMBeanServer(null);
108
109 for (Iterator it = mbsList.iterator(); it.hasNext(); ) {
110 MBeanServer mbs = (MBeanServer) it.next();
111 ClassLoaderRepository clr = mbs.getClassLoaderRepository();
112 try {
113 return clr.loadClassWithout(without, className);
114 } catch (ClassNotFoundException e) {
115 // OK : Try with next one...
116 }
117 }
118 throw new ClassNotFoundException(className);
119 }
120
121 }