001 /* DefaultLoaderRepository.java -- Manages class loaders for the servers.
002 Copyright (C) 2007 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package javax.management;
039
040 import java.util.List;
041
042 /**
043 * Maintains a list of the {@link ClassLoader} instances
044 * registered with the management servers, allowing it
045 * to be used to load classes. In early versions of the
046 * JMX API, this class represented a shared repository for
047 * the classloaders of all management servers. The management
048 * of classloaders is now decentralised and this class is
049 * deprecated. The old behaviour is emulated by consulting
050 * the {@link MBeanServer#getClassLoaderRepository()} method
051 * of all the servers obtained from
052 * {@link MBeanServerFactory#findMBeanServer(String)}. Use of
053 * this class should be avoided in new code.
054 *
055 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
056 * @since 1.5
057 * @deprecated Use {@link MBeanServer#getClassLoaderRepository()}
058 * instead.
059 */
060 @Deprecated public class DefaultLoaderRepository
061 {
062
063 /**
064 * Attempts to load the given class using class loaders
065 * supplied by the repository of each {@link MBeanServer}.
066 * The {@link ClassLoader#loadClass(String)}
067 * method of each class loader is called. If the method
068 * returns successfully, then the returned {@link Class} instance
069 * is returned. If a {@link ClassNotFoundException} is thrown,
070 * then the next loader is tried. Any other exception thrown
071 * by the method is passed back to the caller. This method
072 * throws a {@link ClassNotFoundException} itself if all the
073 * class loaders listed prove fruitless.
074 *
075 * @param name the name of the class to load.
076 * @return the loaded class.
077 * @throws ClassNotFoundException if all the class loaders fail
078 * to load the class.
079 */
080 // API issue with lack of <?> on Class
081 @SuppressWarnings("unchecked")
082 public static Class loadClass(String name)
083 throws ClassNotFoundException
084 {
085 List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
086 for (MBeanServer server : servers)
087 {
088 try
089 {
090 return server.getClassLoaderRepository().loadClass(name);
091 }
092 catch (ClassNotFoundException e)
093 {
094 /* Ignored; try the next server. */
095 }
096 }
097 throw new ClassNotFoundException("The class loaders of all registered " +
098 "servers failed to load the class, " +
099 name);
100 }
101
102 /**
103 * <p>
104 * Attempts to load the given class using class loaders
105 * supplied by the repository of each {@link MBeanServer}.
106 * The {@link ClassLoader#loadClass(String)}
107 * method of each class loader is called. If the method
108 * returns successfully, then the returned {@link Class} instance
109 * is returned. If a {@link ClassNotFoundException} is thrown,
110 * then the next loader is tried. Any other exception thrown
111 * by the method is passed back to the caller. This method
112 * throws a {@link ClassNotFoundException} itself if all the
113 * class loaders listed prove fruitless.
114 * </p>
115 * <p>
116 * Note that this method may deadlock if called simultaneously
117 * by two class loaders in the list.
118 * {@link loadClassBefore(ClassLoader, String)} should be used
119 * in preference to this method to avoid this.
120 * </p>
121 *
122 * @param exclude the class loader to exclude, or <code>null</code>
123 * to obtain the same behaviour as {@link #loadClass(String)}.
124 * @param name the name of the class to load.
125 * @return the loaded class.
126 * @throws ClassNotFoundException if all the class loaders fail
127 * to load the class.
128 */
129 // API issue with lack of <?> on Class
130 @SuppressWarnings("unchecked")
131 public static Class loadClassWithout(ClassLoader exclude, String name)
132 throws ClassNotFoundException
133 {
134 List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
135 for (MBeanServer server : servers)
136 {
137 try
138 {
139 return server.getClassLoaderRepository().loadClassWithout(exclude,
140 name);
141 }
142 catch (ClassNotFoundException e)
143 {
144 /* Ignored; try the next server. */
145 }
146 }
147 throw new ClassNotFoundException("The class loaders of all registered " +
148 "servers failed to load the class, " +
149 name);
150 }
151
152 }