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:   public static Class loadClass(String name)
  81:     throws ClassNotFoundException
  82:   {
  83:     List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
  84:     for (MBeanServer server : servers)
  85:       {
  86:     try
  87:       {
  88:         return server.getClassLoaderRepository().loadClass(name);
  89:       }
  90:     catch (ClassNotFoundException e)
  91:       {
  92:         /* Ignored; try the next server. */
  93:       }
  94:       }
  95:     throw new ClassNotFoundException("The class loaders of all registered " +
  96:                      "servers failed to load the class, " +
  97:                      name);
  98:   }
  99: 
 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: }