1. /*
  2. * @(#)TraceManager.java 1.16 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. import java.io.IOException;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. /**
  12. * Provides an implementation of the {@link com.sun.jmx.trace.TraceDestination}
  13. * interface which uses the J2SE logging API.
  14. * <br><br>
  15. * Note that this implementation can be used only with J2SE version 1.4 or
  16. * higher.
  17. * <br><br>
  18. * All {@link Logger}s are contained in the <code>javax.management</code> namespace, which corresponds
  19. * to the name of the root package hosting all public JMX interfaces. For each log type
  20. * defined in {@link TraceTags}, we use a dedicated {@link Logger} with the same name
  21. * under <code>javax.management</code>.
  22. * <br><br>
  23. * The table below shows the list of {@link Logger} objects used in this implementation and
  24. * their corresponding category of activity.
  25. * <br><br>
  26. * <table cols="3" width="100%" border="1" align="center">
  27. * <tr>
  28. * <th>Logger Name</th>
  29. * <th>JMX log type</th>
  30. * <th>Information logged</th>
  31. * </tr>
  32. * <tr>
  33. * <td><code>javax.management.mbeanserver</code></td>
  34. * <td>{@link TraceTags#INFO_MBEANSERVER}</td>
  35. * <td>Information about the MBean Server</td>
  36. * </tr>
  37. * <tr>
  38. * <td><code>com.sun.jmx.snmp.daemon</code></td>
  39. * <td>{@link TraceTags#INFO_ADAPTOR_SNMP}</td>
  40. * <td>Information about the SNMP Adaptor</td>
  41. * </tr>
  42. * <tr>
  43. * <td><code>com.sun.jmx.snmp</code></td>
  44. * <td>{@link TraceTags#INFO_SNMP}</td>
  45. * <td>Information about SNMP</td>
  46. * </tr>
  47. * <tr>
  48. * <td><code>javax.management.mlet</code></td>
  49. * <td>{@link TraceTags#INFO_MLET}</td>
  50. * <td>Information from an MLet service</td>
  51. * </tr>
  52. * <tr>
  53. * <td><code>javax.management.monitor</code></td>
  54. * <td>{@link TraceTags#INFO_MONITOR}</td>
  55. * <td>Information from a monitor</td>
  56. * </tr>
  57. * <tr>
  58. * <td><code>javax.management.timer</code></td>
  59. * <td>{@link TraceTags#INFO_TIMER}</td>
  60. * <td>Information from a timer</td>
  61. * </tr>
  62. * <tr>
  63. * <td><code>javax.management.notification</code></td>
  64. * <td>{@link TraceTags#INFO_NOTIFICATION}</td>
  65. * <td>Information from the notification mechanism</td>
  66. * </tr>
  67. * <tr>
  68. * <td><code>javax.management.relation</code></td>
  69. * <td>{@link TraceTags#INFO_RELATION}</td>
  70. * <td>Information from the Relation Service</td>
  71. * </tr>
  72. * <tr>
  73. * <td><code>javax.management.modelmbean</code></td>
  74. * <td>{@link TraceTags#INFO_MODELMBEAN}</td>
  75. * <td>Information from the Model MBean components</td>
  76. * </tr>
  77. * <tr>
  78. * <td><code>javax.management.misc</code></td>
  79. * <td>{@link TraceTags#INFO_MISC}</td>
  80. * <td>Information sent from any other class</td>
  81. * </tr>
  82. * </table>
  83. * <br><br>
  84. * The mapping for the JMX log levels is the following:
  85. * <br><br>
  86. * <table cols="2" width="50%" border="1" align="center">
  87. * <tr>
  88. * <th>JMX log level</th>
  89. * <th>J2SE logging API log level</th>
  90. * </tr>
  91. * <tr>
  92. * <td><center>{@link TraceTags#LEVEL_DEBUG}</center></td>
  93. * <td><center>{@link Level#FINEST}</center></td>
  94. * </tr>
  95. * <tr>
  96. * <td><center>{@link TraceTags#LEVEL_TRACE}</center></td>
  97. * <td><center>{@link Level#FINER}</center></td>
  98. * </tr>
  99. * <tr>
  100. * <td><center>{@link TraceTags#LEVEL_ERROR}</center></td>
  101. * <td><center>{@link Level#SEVERE}</center></td>
  102. * </tr>
  103. * </table>
  104. *
  105. * @since 1.5
  106. * @since.unbundled JMX RI 1.2
  107. */
  108. public class TraceManager implements TraceDestination {
  109. /**
  110. * Returns the {@link Level} corresponding to the integer value passed as
  111. * argument. This value is assumed to be part of the log levels defined by
  112. * class {@link TraceTags}.
  113. *
  114. * @return
  115. * <ul>
  116. * <li>{@link Level#FINEST} if value is {@link TraceTags#LEVEL_DEBUG}</li>
  117. * <li>{@link Level#FINER} if value is {@link TraceTags#LEVEL_TRACE}</li>
  118. * <li>{@link Level#SEVERE} if value is {@link TraceTags#LEVEL_ERROR}</li>
  119. * <li><code>null</code> otherwise</li>
  120. * </ul>
  121. */
  122. private static Level getLevel(int level)
  123. {
  124. switch(level)
  125. {
  126. case TraceTags.LEVEL_DEBUG:
  127. return Level.FINEST;
  128. case TraceTags.LEVEL_TRACE:
  129. return Level.FINER;
  130. case TraceTags.LEVEL_ERROR:
  131. return Level.SEVERE;
  132. default:
  133. return null;
  134. }
  135. }
  136. /**
  137. * Returns the {@link Logger} corresponding to the integer value passed as
  138. * argument. This value is assumed to be part of the log types defined by
  139. * class {@link TraceTags}.
  140. *
  141. * @return
  142. * <ul>
  143. * <li><code>Logger.getLogger(<b>"javax.management.mbeanserver"</b>)</code>if value is {@link TraceTags#INFO_MBEANSERVER}</li>
  144. * <li><code>Logger.getLogger(<b>"javax.management.mlet"</b>)</code>if value is {@link TraceTags#INFO_MLET}</li>
  145. * <li><code>Logger.getLogger(<b>"javax.management.monitor"</b>)</code>if value is {@link TraceTags#INFO_MONITOR}</li>
  146. * <li><code>Logger.getLogger(<b>"javax.management.timer"</b>)</code>if value is {@link TraceTags#INFO_TIMER}</li>
  147. * <li><code>Logger.getLogger(<b>"javax.management.misc"</b>)</code>if value is {@link TraceTags#INFO_MISC}</li>
  148. * <li><code>Logger.getLogger(<b>"javax.management.notification"</b>)</code>if value is {@link TraceTags#INFO_NOTIFICATION}</li>
  149. * <li><code>Logger.getLogger(<b>"javax.management.relation"</b>)</code>if value is {@link TraceTags#INFO_RELATION}</li>
  150. * <li><code>Logger.getLogger(<b>"javax.management.modelmbean"</b>)</code>if value is {@link TraceTags#INFO_MODELMBEAN}</li>
  151. * <li><code>Logger.getLogger(<b>"com.sun.jmx.snmp.daemon"</b>)</code>if value is {@link TraceTags#INFO_ADAPTOR_SNMP}</li>
  152. * <li><code>Logger.getLogger(<b>"com.sun.jmx.snmp"</b>)</code>if value is {@link TraceTags#INFO_SNMP}</li>
  153. * <li><code>null</code> otherwise</li>
  154. * </ul>
  155. */
  156. private static Logger getLogger(int type)
  157. {
  158. switch(type)
  159. {
  160. case TraceTags.INFO_MBEANSERVER:
  161. return Logger.getLogger("javax.management.mbeanserver");
  162. case TraceTags.INFO_ADAPTOR_SNMP:
  163. return Logger.getLogger("com.sun.jmx.snmp.daemon");
  164. case TraceTags.INFO_SNMP:
  165. return Logger.getLogger("com.sun.jmx.snmp");
  166. case TraceTags.INFO_MLET:
  167. return Logger.getLogger("javax.management.mlet");
  168. case TraceTags.INFO_MONITOR:
  169. return Logger.getLogger("javax.management.monitor");
  170. case TraceTags.INFO_TIMER:
  171. return Logger.getLogger("javax.management.timer");
  172. case TraceTags.INFO_MISC:
  173. return Logger.getLogger("javax.management.misc");
  174. case TraceTags.INFO_NOTIFICATION:
  175. return Logger.getLogger("javax.management.notification");
  176. case TraceTags.INFO_RELATION:
  177. return Logger.getLogger("javax.management.relation");
  178. case TraceTags.INFO_MODELMBEAN:
  179. return Logger.getLogger("javax.management.modelmbean");
  180. default:
  181. return null;
  182. }
  183. }
  184. /**
  185. * @see TraceDestination#isSelected
  186. */
  187. public boolean isSelected(int level, int type)
  188. {
  189. Logger logger;
  190. Level lvl;
  191. if (((logger = getLogger(type)) != null) &&
  192. ((lvl = getLevel(level)) != null))
  193. {
  194. return logger.isLoggable(lvl);
  195. }
  196. return false;
  197. }
  198. /**
  199. * Note that the provided log type and log level have to be part of the
  200. * enumerated values defined in class {@link TraceTags}. Otherwise, nothing is
  201. * logged.
  202. * @see TraceDestination#send(int, int, String, String, String)
  203. */
  204. public boolean send(int level,
  205. int type,
  206. String className,
  207. String methodName,
  208. String info)
  209. {
  210. if (isSelected(level, type))
  211. {
  212. getLogger(type).logp(getLevel(level), className, methodName, info);
  213. return true;
  214. }
  215. return false;
  216. }
  217. /**
  218. * Note that the provided log type and log level have to be part of the
  219. * enumerated values defined in class {@link TraceTags}. Otherwise, nothing is
  220. * logged.
  221. * @see TraceDestination#send(int, int, String, String, Throwable)
  222. */
  223. public boolean send(int level,
  224. int type,
  225. String className,
  226. String methodName,
  227. Throwable exception)
  228. {
  229. if (isSelected(level, type)) {
  230. getLogger(type).log(getLevel(level),
  231. className + ": Exception occured in " + methodName ,
  232. exception);
  233. return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * Not implemented, as the control over log levels and output handler is
  239. * deferred to the J2SE logging API.
  240. * @see TraceDestination#reset
  241. **/
  242. public void reset() throws IOException
  243. {
  244. }
  245. /**
  246. * Logs a warning message.
  247. * This is equivalent to
  248. * <code>Logger.getLogger(loggerName).warning(msg);</code>
  249. *
  250. * @since.unbundled JMX RI 1.2.1
  251. **/
  252. void warning(String loggerName, String msg) {
  253. Logger.getLogger(loggerName).warning(msg);
  254. }
  255. /**
  256. * Logs a fine message.
  257. * This is equivalent to
  258. * <code>Logger.getLogger(loggerName).fine(msg);</code>
  259. *
  260. * @since.unbundled JMX RI 1.2.1
  261. **/
  262. void fine(String loggerName, String msg) {
  263. Logger.getLogger(loggerName).fine(msg);
  264. }
  265. }