1. /*
  2. * @(#)Trace.java 1.19 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.jmx.trace;
  8. // java import
  9. //
  10. import java.util.Properties;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.io.IOException;
  14. /**
  15. * Sends trace to a pluggable destination.
  16. *
  17. * @since 1.5
  18. * @since.unbundled JMX RI 1.2
  19. */
  20. public final class Trace implements TraceTags {
  21. private static TraceDestination out = initDestination();
  22. // private static TraceDestination out =
  23. // TraceImplementation.newDestination(2);
  24. // private constructor defined to "hide" the default public constructor
  25. private Trace() {
  26. }
  27. // Method used to test if the implementation of TraceDestination which relies
  28. // on the java.util.logging API (com.sun.jmx.trace.TraceManager) can be used
  29. // to initialize the trace destination. This is the case iff. we are running
  30. // J2SE 1.4 or higher. Otherwise, the trace destination is null, and needs to
  31. // be later initialized if we want to see traces !
  32. //
  33. private static TraceDestination initDestination()
  34. {
  35. // Attempt to locate class java.util.logging.LogManager
  36. //
  37. try
  38. {
  39. Class.forName("java.util.logging.LogManager");
  40. // Class could be loaded, use a new instance of TraceManager as the trace
  41. // destination
  42. //
  43. return new TraceManager();
  44. }
  45. catch (ClassNotFoundException e)
  46. {
  47. // Class could not be loaded, means that we are running J2SE 1.3 or below.
  48. //
  49. return null;
  50. }
  51. }
  52. // --------------
  53. // public methods
  54. // --------------
  55. /**
  56. * Set the trace destination.
  57. **/
  58. public static synchronized void setDestination(TraceDestination output) {
  59. out = output;
  60. }
  61. /**
  62. * Verify whether the specified info level and the info type are
  63. * selected by a listener.
  64. *
  65. * <P>It is strongly recommended to call this method before sending
  66. * an information to this Trace class.
  67. *
  68. * @param level the level of trace information.
  69. * @param type the type of the trace information.
  70. */
  71. public static boolean isSelected(int level, int type) {
  72. final TraceDestination output = out();
  73. if (output != null) return output.isSelected(level,type);
  74. return false;
  75. }
  76. /**
  77. * Send a new information to this Trace class
  78. *
  79. * @param level the level of trace information to be sent.
  80. * @param type the type of trace information to be sent.
  81. * @param className the name of the class from which the trace
  82. * information is from.
  83. * @param methodName the name of the method from which the trace
  84. * information is from.
  85. * @param info the trace information to be sent.
  86. * @return false if the level and the type are not selected.
  87. */
  88. public static boolean send(int level,
  89. int type,
  90. String className,
  91. String methodName,
  92. String info) {
  93. final TraceDestination output = out();
  94. if (output == null) return false;
  95. if (!(output.isSelected(level, type))) return false;
  96. return output.send(level,type,className,methodName,info);
  97. }
  98. /**
  99. * Send an exception to this Trace class.
  100. *
  101. * @param level the level of trace information to be sent.
  102. * @param type the type of trace information to be sent.
  103. * @param className the name of the class from which the trace
  104. * information is from.
  105. * @param methodName the name of the method from which the trace
  106. * information is from.
  107. * @param exception exception sent as the trace information.
  108. */
  109. public static boolean send(int level,
  110. int type,
  111. String className,
  112. String methodName,
  113. Throwable exception) {
  114. final TraceDestination output = out();
  115. if (output == null) return false;
  116. if (!(output.isSelected(level, type))) return false;
  117. return output.send(level,type,className,methodName,exception);
  118. }
  119. /**
  120. * Logs a warning message.
  121. * This is equivalent to
  122. * <code>Logger.getLogger(loggerName).warning(msg);</code>
  123. * This method is a hack for backward compatibility with J2SE 1.3.
  124. *
  125. * @since.unbundled JMX RI 1.2.1
  126. **/
  127. public static void warning(String loggerName, String msg) {
  128. final TraceDestination output = out();
  129. if (output instanceof TraceManager)
  130. // Log a warning message
  131. ((TraceManager)output).warning(loggerName,msg);
  132. else if (output != null)
  133. // Best effort
  134. output.send(LEVEL_TRACE,INFO_MISC,null,null,msg);
  135. }
  136. /**
  137. * Logs a fine message.
  138. * This is equivalent to
  139. * <code>Logger.getLogger(loggerName).fine(msg);</code>
  140. * This method is a hack for backward compatibility with J2SE 1.3.
  141. *
  142. * @since.unbundled JMX RI 1.2.1
  143. **/
  144. public static void fine(String loggerName, String msg) {
  145. final TraceDestination output = out();
  146. if (output instanceof TraceManager)
  147. // Log a fine message
  148. ((TraceManager)output).fine(loggerName,msg);
  149. else if (output != null)
  150. // Best effort
  151. output.send(LEVEL_TRACE,INFO_MISC,null,null,msg);
  152. }
  153. /**
  154. * Return the trace destination.
  155. **/
  156. private static synchronized TraceDestination out() {
  157. return out;
  158. }
  159. }