Source for javax.management.DefaultLoaderRepository

   1: /* DefaultLoaderRepository.java -- Manages class loaders for the servers.
   2:    Copyright (C) 2007 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.management;
  39: 
  40: import java.util.List;
  41: 
  42: /**
  43:  * Maintains a list of the {@link ClassLoader} instances
  44:  * registered with the management servers, allowing it
  45:  * to be used to load classes.  In early versions of the
  46:  * JMX API, this class represented a shared repository for
  47:  * the classloaders of all management servers.  The management
  48:  * of classloaders is now decentralised and this class is
  49:  * deprecated.  The old behaviour is emulated by consulting
  50:  * the {@link MBeanServer#getClassLoaderRepository()} method
  51:  * of all the servers obtained from
  52:  * {@link MBeanServerFactory#findMBeanServer(String)}.  Use of
  53:  * this class should be avoided in new code.
  54:  *
  55:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  56:  * @since 1.5
  57:  * @deprecated Use {@link MBeanServer#getClassLoaderRepository()}
  58:  *             instead.
  59:  */
  60: @Deprecated public class DefaultLoaderRepository
  61: {
  62: 
  63:   /**
  64:    * Attempts to load the given class using class loaders
  65:    * supplied by the repository of each {@link MBeanServer}.
  66:    * The {@link ClassLoader#loadClass(String)}
  67:    * method of each class loader is called.  If the method
  68:    * returns successfully, then the returned {@link Class} instance
  69:    * is returned.  If a {@link ClassNotFoundException} is thrown,
  70:    * then the next loader is tried.  Any other exception thrown
  71:    * by the method is passed back to the caller.  This method
  72:    * throws a {@link ClassNotFoundException} itself if all the
  73:    * class loaders listed prove fruitless.
  74:    *
  75:    * @param name the name of the class to load.
  76:    * @return the loaded class.
  77:    * @throws ClassNotFoundException if all the class loaders fail
  78:    *                                to load the class.
  79:    */
  80:   // API issue with lack of <?> on Class
  81:   @SuppressWarnings("rawtypes")
  82:   public static Class loadClass(String name)
  83:     throws ClassNotFoundException
  84:   {
  85:     List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
  86:     for (MBeanServer server : servers)
  87:       {
  88:         try
  89:           {
  90:             return server.getClassLoaderRepository().loadClass(name);
  91:           }
  92:         catch (ClassNotFoundException e)
  93:           {
  94:             /* Ignored; try the next server. */
  95:           }
  96:       }
  97:     throw new ClassNotFoundException("The class loaders of all registered " +
  98:                                      "servers failed to load the class, " +
  99:                                      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("rawtypes")
 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: }