1. /*
  2. * @(#)DefaultLoaderRepository.java 1.18 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management.loading;
  8. // Java import
  9. import java.security.AccessController;
  10. import java.security.Permission;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. // JMX import
  14. import javax.management.MBeanServer;
  15. import javax.management.MBeanServerFactory;
  16. import javax.management.loading.ClassLoaderRepository;
  17. import com.sun.jmx.trace.Trace;
  18. /**
  19. * <p>Keeps the list of Class Loaders registered in the MBean Server.
  20. * It provides the necessary methods to load classes using the registered
  21. * Class Loaders.</p>
  22. *
  23. * <p>This deprecated class is maintained for compatibility. In
  24. * previous versions of JMX, there was one
  25. * <code>DefaultLoaderRepository</code> shared by all MBean servers.
  26. * As of JMX 1.2, that functionality is approximated by using {@link
  27. * MBeanServerFactory#findMBeanServer} to find all known MBean
  28. * servers, and consulting the {@link ClassLoaderRepository} of each
  29. * one. It is strongly recommended that code referencing
  30. * <code>DefaultLoaderRepository</code> be rewritten.</p>
  31. *
  32. * @deprecated Use
  33. * {@link javax.management.MBeanServer#getClassLoaderRepository()}}
  34. * instead.
  35. *
  36. * @since 1.5
  37. */
  38. @Deprecated
  39. public class DefaultLoaderRepository {
  40. private static final String dbgTag = "DefaultLoaderRepository";
  41. /**
  42. * Go through the list of class loaders and try to load the requested
  43. * class.
  44. * The method will stop as soon as the class is found. If the class
  45. * is not found the method will throw a <CODE>ClassNotFoundException</CODE>
  46. * exception.
  47. *
  48. * @param className The name of the class to be loaded.
  49. *
  50. * @return the loaded class.
  51. *
  52. * @exception ClassNotFoundException The specified class could not be
  53. * found.
  54. */
  55. public static Class loadClass(String className)
  56. throws ClassNotFoundException {
  57. debug("loadClass",className);
  58. return load(null, className);
  59. }
  60. /**
  61. * Go through the list of class loaders but exclude the given
  62. * class loader, then try to load
  63. * the requested class.
  64. * The method will stop as soon as the class is found. If the class
  65. * is not found the method will throw a <CODE>ClassNotFoundException</CODE>
  66. * exception.
  67. *
  68. * @param className The name of the class to be loaded.
  69. * @param loader The class loader to be excluded.
  70. *
  71. * @return the loaded class.
  72. *
  73. * @exception ClassNotFoundException The specified class could not be
  74. * found.
  75. */
  76. public static Class loadClassWithout(ClassLoader loader,
  77. String className)
  78. throws ClassNotFoundException {
  79. debug("loadClassWithout",className);
  80. return load(loader, className);
  81. }
  82. private static Class load(ClassLoader without, String className)
  83. throws ClassNotFoundException {
  84. final List mbsList = MBeanServerFactory.findMBeanServer(null);
  85. for (Iterator it = mbsList.iterator(); it.hasNext(); ) {
  86. MBeanServer mbs = (MBeanServer) it.next();
  87. ClassLoaderRepository clr = mbs.getClassLoaderRepository();
  88. try {
  89. return clr.loadClassWithout(without, className);
  90. } catch (ClassNotFoundException e) {
  91. // OK : Try with next one...
  92. }
  93. }
  94. throw new ClassNotFoundException(className);
  95. }
  96. // TRACES & DEBUG
  97. //---------------
  98. private static boolean isTraceOn() {
  99. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
  100. }
  101. private static void trace(String clz, String func, String info) {
  102. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
  103. }
  104. private static void trace(String func, String info) {
  105. trace(dbgTag, func, info);
  106. }
  107. private static boolean isDebugOn() {
  108. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
  109. }
  110. private static void debug(String clz, String func, String info) {
  111. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
  112. }
  113. private static void debug(String func, String info) {
  114. debug(dbgTag, func, info);
  115. }
  116. }