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      public static Class loadClass(String name)
081        throws ClassNotFoundException
082      {
083        List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
084        for (MBeanServer server : servers)
085          {
086            try
087              {
088                return server.getClassLoaderRepository().loadClass(name);
089              }
090            catch (ClassNotFoundException e)
091              {
092                /* Ignored; try the next server. */
093              }
094          }
095        throw new ClassNotFoundException("The class loaders of all registered " +
096                                         "servers failed to load the class, " +
097                                         name);
098      }
099    
100      /**
101       * <p>
102       * Attempts to load the given class using class loaders
103       * supplied by the repository of each {@link MBeanServer}.
104       * The {@link ClassLoader#loadClass(String)}
105       * method of each class loader is called.  If the method
106       * returns successfully, then the returned {@link Class} instance
107       * is returned.  If a {@link ClassNotFoundException} is thrown,
108       * then the next loader is tried.  Any other exception thrown
109       * by the method is passed back to the caller.  This method
110       * throws a {@link ClassNotFoundException} itself if all the
111       * class loaders listed prove fruitless.
112       * </p>
113       * <p>
114       * Note that this method may deadlock if called simultaneously
115       * by two class loaders in the list.
116       * {@link loadClassBefore(ClassLoader, String)} should be used
117       * in preference to this method to avoid this.
118       * </p>
119       *
120       * @param exclude the class loader to exclude, or <code>null</code>
121       *             to obtain the same behaviour as {@link #loadClass(String)}.
122       * @param name the name of the class to load.
123       * @return the loaded class.
124       * @throws ClassNotFoundException if all the class loaders fail
125       *                                to load the class.
126       */
127      public static Class loadClassWithout(ClassLoader exclude, String name)
128        throws ClassNotFoundException
129      {
130        List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
131        for (MBeanServer server : servers)
132          {
133            try
134              {
135                return server.getClassLoaderRepository().loadClassWithout(exclude,
136                                                                          name);
137              }
138            catch (ClassNotFoundException e)
139              {
140                /* Ignored; try the next server. */
141              }
142          }
143        throw new ClassNotFoundException("The class loaders of all registered " +
144                                         "servers failed to load the class, " +
145                                         name);
146      }
147    
148    }