DefaultLoaderRepository.java 6.1 KB

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