KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > trace > TraceManager


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
9 import java.io.IOException JavaDoc;
10
11 import java.util.logging.Level JavaDoc;
12 import java.util.logging.Logger JavaDoc;
13
14 /**
15  * Provides an implementation of the {@link com.sun.jmx.trace.TraceDestination}
16  * interface which uses the J2SE logging API.
17  * <br><br>
18  * Note that this implementation can be used only with J2SE version 1.4 or
19  * higher.
20  * <br><br>
21  * All {@link Logger}s are contained in the <code>javax.management</code> namespace, which corresponds
22  * to the name of the root package hosting all public JMX interfaces. For each log type
23  * defined in {@link TraceTags}, we use a dedicated {@link Logger} with the same name
24  * under <code>javax.management</code>.
25  * <br><br>
26  * The table below shows the list of {@link Logger} objects used in this implementation and
27  * their corresponding category of activity.
28  * <br><br>
29  * <table cols="3" width="100%" border="1" align="center">
30  * <tr>
31  * <th>Logger Name</th>
32  * <th>JMX log type</th>
33  * <th>Information logged</th>
34  * </tr>
35  * <tr>
36  * <td><code>javax.management.mbeanserver</code></td>
37  * <td>{@link TraceTags#INFO_MBEANSERVER}</td>
38  * <td>Information about the MBean Server</td>
39  * </tr>
40  * <tr>
41  * <td><code>com.sun.jmx.snmp.daemon</code></td>
42  * <td>{@link TraceTags#INFO_ADAPTOR_SNMP}</td>
43  * <td>Information about the SNMP Adaptor</td>
44  * </tr>
45  * <tr>
46  * <td><code>com.sun.jmx.snmp</code></td>
47  * <td>{@link TraceTags#INFO_SNMP}</td>
48  * <td>Information about SNMP</td>
49  * </tr>
50  * <tr>
51  * <td><code>javax.management.mlet</code></td>
52  * <td>{@link TraceTags#INFO_MLET}</td>
53  * <td>Information from an MLet service</td>
54  * </tr>
55  * <tr>
56  * <td><code>javax.management.monitor</code></td>
57  * <td>{@link TraceTags#INFO_MONITOR}</td>
58  * <td>Information from a monitor</td>
59  * </tr>
60  * <tr>
61  * <td><code>javax.management.timer</code></td>
62  * <td>{@link TraceTags#INFO_TIMER}</td>
63  * <td>Information from a timer</td>
64  * </tr>
65  * <tr>
66  * <td><code>javax.management.notification</code></td>
67  * <td>{@link TraceTags#INFO_NOTIFICATION}</td>
68  * <td>Information from the notification mechanism</td>
69  * </tr>
70  * <tr>
71  * <td><code>javax.management.relation</code></td>
72  * <td>{@link TraceTags#INFO_RELATION}</td>
73  * <td>Information from the Relation Service</td>
74  * </tr>
75  * <tr>
76  * <td><code>javax.management.modelmbean</code></td>
77  * <td>{@link TraceTags#INFO_MODELMBEAN}</td>
78  * <td>Information from the Model MBean components</td>
79  * </tr>
80  * <tr>
81  * <td><code>javax.management.misc</code></td>
82  * <td>{@link TraceTags#INFO_MISC}</td>
83  * <td>Information sent from any other class</td>
84  * </tr>
85  * </table>
86  * <br><br>
87  * The mapping for the JMX log levels is the following:
88  * <br><br>
89  * <table cols="2" width="50%" border="1" align="center">
90  * <tr>
91  * <th>JMX log level</th>
92  * <th>J2SE logging API log level</th>
93  * </tr>
94  * <tr>
95  * <td><center>{@link TraceTags#LEVEL_DEBUG}</center></td>
96  * <td><center>{@link Level#FINEST}</center></td>
97  * </tr>
98  * <tr>
99  * <td><center>{@link TraceTags#LEVEL_TRACE}</center></td>
100  * <td><center>{@link Level#FINER}</center></td>
101  * </tr>
102  * <tr>
103  * <td><center>{@link TraceTags#LEVEL_ERROR}</center></td>
104  * <td><center>{@link Level#SEVERE}</center></td>
105  * </tr>
106  * </table>
107  *
108  * @since 1.5
109  * @since.unbundled JMX RI 1.2
110  */

111 public class TraceManager implements TraceDestination {
112
113   /**
114    * Returns the {@link Level} corresponding to the integer value passed as
115    * argument. This value is assumed to be part of the log levels defined by
116    * class {@link TraceTags}.
117    *
118    * @return
119    * <ul>
120    * <li>{@link Level#FINEST} if value is {@link TraceTags#LEVEL_DEBUG}</li>
121    * <li>{@link Level#FINER} if value is {@link TraceTags#LEVEL_TRACE}</li>
122    * <li>{@link Level#SEVERE} if value is {@link TraceTags#LEVEL_ERROR}</li>
123    * <li><code>null</code> otherwise</li>
124    * </ul>
125    */

126   private static Level JavaDoc getLevel(int level)
127   {
128     switch(level)
129     {
130     case TraceTags.LEVEL_DEBUG:
131       return Level.FINEST;
132     case TraceTags.LEVEL_TRACE:
133       return Level.FINER;
134     case TraceTags.LEVEL_ERROR:
135       return Level.SEVERE;
136     default:
137       return null;
138     }
139   }
140
141   /**
142    * Returns the {@link Logger} corresponding to the integer value passed as
143    * argument. This value is assumed to be part of the log types defined by
144    * class {@link TraceTags}.
145    *
146    * @return
147    * <ul>
148    * <li><code>Logger.getLogger(<b>"javax.management.mbeanserver"</b>)</code>if value is {@link TraceTags#INFO_MBEANSERVER}</li>
149    * <li><code>Logger.getLogger(<b>"javax.management.mlet"</b>)</code>if value is {@link TraceTags#INFO_MLET}</li>
150    * <li><code>Logger.getLogger(<b>"javax.management.monitor"</b>)</code>if value is {@link TraceTags#INFO_MONITOR}</li>
151    * <li><code>Logger.getLogger(<b>"javax.management.timer"</b>)</code>if value is {@link TraceTags#INFO_TIMER}</li>
152    * <li><code>Logger.getLogger(<b>"javax.management.misc"</b>)</code>if value is {@link TraceTags#INFO_MISC}</li>
153    * <li><code>Logger.getLogger(<b>"javax.management.notification"</b>)</code>if value is {@link TraceTags#INFO_NOTIFICATION}</li>
154    * <li><code>Logger.getLogger(<b>"javax.management.relation"</b>)</code>if value is {@link TraceTags#INFO_RELATION}</li>
155    * <li><code>Logger.getLogger(<b>"javax.management.modelmbean"</b>)</code>if value is {@link TraceTags#INFO_MODELMBEAN}</li>
156    * <li><code>Logger.getLogger(<b>"com.sun.jmx.snmp.daemon"</b>)</code>if value is {@link TraceTags#INFO_ADAPTOR_SNMP}</li>
157    * <li><code>Logger.getLogger(<b>"com.sun.jmx.snmp"</b>)</code>if value is {@link TraceTags#INFO_SNMP}</li>
158    * <li><code>null</code> otherwise</li>
159    * </ul>
160    */

161   private static Logger JavaDoc getLogger(int type)
162   {
163     switch(type)
164     {
165     case TraceTags.INFO_MBEANSERVER:
166       return Logger.getLogger("javax.management.mbeanserver");
167     case TraceTags.INFO_ADAPTOR_SNMP:
168       return Logger.getLogger("com.sun.jmx.snmp.daemon");
169     case TraceTags.INFO_SNMP:
170       return Logger.getLogger("com.sun.jmx.snmp");
171     case TraceTags.INFO_MLET:
172       return Logger.getLogger("javax.management.mlet");
173     case TraceTags.INFO_MONITOR:
174       return Logger.getLogger("javax.management.monitor");
175     case TraceTags.INFO_TIMER:
176       return Logger.getLogger("javax.management.timer");
177     case TraceTags.INFO_MISC:
178       return Logger.getLogger("javax.management.misc");
179     case TraceTags.INFO_NOTIFICATION:
180       return Logger.getLogger("javax.management.notification");
181     case TraceTags.INFO_RELATION:
182       return Logger.getLogger("javax.management.relation");
183     case TraceTags.INFO_MODELMBEAN:
184       return Logger.getLogger("javax.management.modelmbean");
185     default:
186       return null;
187     }
188   }
189
190   /**
191    * @see TraceDestination#isSelected
192    */

193   public boolean isSelected(int level, int type)
194   {
195     Logger JavaDoc logger;
196     Level JavaDoc lvl;
197     if (((logger = getLogger(type)) != null) &&
198         ((lvl = getLevel(level)) != null))
199     {
200       return logger.isLoggable(lvl);
201     }
202     return false;
203   }
204
205   /**
206    * Note that the provided log type and log level have to be part of the
207    * enumerated values defined in class {@link TraceTags}. Otherwise, nothing is
208    * logged.
209    * @see TraceDestination#send(int, int, String, String, String)
210    */

211   public boolean send(int level,
212                       int type,
213                       String JavaDoc className,
214                       String JavaDoc methodName,
215                       String JavaDoc info)
216   {
217     if (isSelected(level, type))
218     {
219       getLogger(type).logp(getLevel(level), className, methodName, info);
220       return true;
221     }
222     return false;
223   }
224   
225   /**
226    * Note that the provided log type and log level have to be part of the
227    * enumerated values defined in class {@link TraceTags}. Otherwise, nothing is
228    * logged.
229    * @see TraceDestination#send(int, int, String, String, Throwable)
230    */

231   public boolean send(int level,
232                       int type,
233                       String JavaDoc className,
234                       String JavaDoc methodName,
235                       Throwable JavaDoc exception)
236   {
237       if (isSelected(level, type)) {
238       getLogger(type).log(getLevel(level),
239           className + ": Exception occured in " + methodName ,
240           exception);
241       return true;
242   }
243       return false;
244   }
245
246   /**
247    * Not implemented, as the control over log levels and output handler is
248    * deferred to the J2SE logging API.
249    * @see TraceDestination#reset
250    **/

251   public void reset() throws IOException JavaDoc
252   {
253     
254   }
255
256     /**
257      * Logs a warning message.
258      * This is equivalent to
259      * <code>Logger.getLogger(loggerName).warning(msg);</code>
260      *
261      * @since.unbundled JMX RI 1.2.1
262      **/

263     void warning(String JavaDoc loggerName, String JavaDoc msg) {
264     Logger.getLogger(loggerName).warning(msg);
265     }
266     
267     /**
268      * Logs a fine message.
269      * This is equivalent to
270      * <code>Logger.getLogger(loggerName).fine(msg);</code>
271      *
272      * @since.unbundled JMX RI 1.2.1
273      **/

274     void fine(String JavaDoc loggerName, String JavaDoc msg) {
275     Logger.getLogger(loggerName).fine(msg);
276     }
277
278 }
279  
280
Popular Tags