KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)TraceImplementation.java 1.17 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
8 package com.sun.jmx.trace;
9
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13
14 /**
15  * Example implementation of the {@link TraceDestination} interface.
16  * <br>
17  * This implementation sends log records to a file (specified by the value of the
18  * <code>com.sun.jmx.trace.file</code> system property) or to the system
19  * console if no file is specified.
20  * <br>
21  * The log level is specified by the value of system property
22  * <code>com.sun.jmx.trace.level</code>, which can be : <code>DEBUG</code>,
23  * <code>TRACE</code>, <code>ERROR</code>. If no trace level is specified, the
24  * default is <code>ERROR</code>.
25  * <br>
26  * Note that this implementation does not provide any filtering based on the log
27  * types. More precisely, the implementation of method {@link #isSelected} only
28  * checks the provided log level.
29  *
30  * @since 1.5
31  */

32 public class TraceImplementation implements TraceDestination
33 {
34   // output stream
35
//
36
private PrintWriter JavaDoc out;
37   
38   // log level
39
//
40
private int level;
41
42   static TraceImplementation newDestination(int level)
43   {
44       try {
45       final TraceImplementation impl = new TraceImplementation();
46       impl.level = level;
47       return impl;
48       } catch (IOException JavaDoc x) {
49       return null;
50       }
51   }
52
53   /**
54    * Registers a new instance of class {@link TraceImplementation} as the
55    * trace destination in class {@link Trace}.
56    *
57    * @exception IOException
58    * may be thrown when creating the new log file based on the
59    * value of system property <code>com.sun.jmx.trace.file</code>
60    * @see Trace#setDestination
61    */

62   public static void init() throws IOException JavaDoc
63   {
64     Trace.setDestination(new TraceImplementation());
65   }
66
67   /**
68    * Registers a new instance of class {@link TraceImplementation} as the
69    * trace destination in class {@link Trace}.
70    *
71    * @param level Initial trace level (see {@link TraceTags})
72    * @exception IOException
73    * may be thrown when creating the new log file based on the
74    * value of system property <code>com.sun.jmx.trace.file</code>
75    * @see Trace#setDestination
76    */

77   public static void init(int level) throws IOException JavaDoc
78   {
79       final TraceImplementation impl = new TraceImplementation();
80       impl.level = level;
81       Trace.setDestination(impl);
82   }
83
84   /**
85    * Constructor.
86    * @exception IOException
87    * may be thrown when creating the new log file based on the
88    * value of system property <code>com.sun.jmx.trace.file</code>
89    */

90   public TraceImplementation() throws IOException JavaDoc
91   {
92     String JavaDoc filename;
93     if ((filename = System.getProperty("com.sun.jmx.trace.file")) != null)
94     {
95       // Output sent to the specified log file
96
//
97
this.out = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(filename), true);
98     }
99     else
100     {
101       // Output sent to the system console
102
//
103
this.out = new PrintWriter JavaDoc(System.err, true);
104     }
105
106     String JavaDoc level;
107     if ((level = System.getProperty("com.sun.jmx.trace.level")) != null)
108     {
109       // Read log level from value of system property
110
//
111
if (level.equals("DEBUG"))
112       {
113         this.level = TraceTags.LEVEL_DEBUG;
114       }
115       else if (level.equals("TRACE"))
116       {
117         this.level = TraceTags.LEVEL_TRACE;
118       }
119       else
120       {
121         this.level = TraceTags.LEVEL_ERROR;
122       }
123     }
124     else
125     {
126       // Log level defaults to ERROR
127
//
128
this.level = TraceTags.LEVEL_ERROR;
129     }
130   }
131
132   /**
133    * Only tests whether the provided log level will generate some output if
134    * used as an argument to {@link #send(int, int, String, String, Throwable)}
135    * or {@link #send(int, int, String, String, String)}
136    *
137    * @see TraceDestination#isSelected
138    */

139   public boolean isSelected(int level, int type)
140   {
141     return (level <= this.level);
142   }
143
144   /**
145    * @see TraceDestination#send(int, int, String, String, String)
146    */

147   public boolean send(int level,
148                       int type,
149                       String JavaDoc className,
150                       String JavaDoc methodName,
151                       String JavaDoc info)
152   {
153     if (isSelected(level, type))
154     {
155       out.println(((className!=null)?"Class: " + className:"")+
156                   ((methodName!=null)?"\nMethod: " + methodName:"") +
157                   "\n\tlevel: " + getLevel(level) +
158                   "\n\ttype: " + getType(type) +
159                   "\n\tmessage: " + info);
160       //out.flush();
161
return true;
162     }
163     return false;
164   }
165   
166   /**
167    * @see TraceDestination#send(int, int, String, String, Throwable)
168    */

169   public boolean send(int level,
170                       int type,
171                       String JavaDoc className,
172                       String JavaDoc methodName,
173                       Throwable JavaDoc exception)
174   {
175       final boolean result = send(level, type, className, methodName,
176                   exception.toString());
177       if (result)
178       exception.printStackTrace(out);
179       
180       return result;
181   }
182
183   /**
184    * Not implemented.
185    *
186    * @see TraceDestination#reset
187    **/

188   public void reset() throws IOException JavaDoc
189   {
190
191   }
192
193   /**
194    * Return the string representation of a trace type, as defined in
195    * {@link TraceTags}
196    */

197   private static String JavaDoc getType(int type) {
198        
199     switch (type) {
200     
201     case TraceTags.INFO_MBEANSERVER:
202       return "INFO_MBEANSERVER";
203
204     case TraceTags.INFO_ADAPTOR_SNMP:
205       return "INFO_ADAPTOR_SNMP";
206
207     case TraceTags.INFO_SNMP:
208       return "INFO_SNMP";
209
210     case TraceTags.INFO_MLET:
211       return "INFO_MLET";
212
213     case TraceTags.INFO_MONITOR:
214       return "INFO_MONITOR";
215
216     case TraceTags.INFO_TIMER:
217       return "INFO_TIMER";
218
219     case TraceTags.INFO_MISC:
220       return "INFO_MISC";
221
222     case TraceTags.INFO_NOTIFICATION:
223       return "INFO_NOTIFICATION";
224
225     case TraceTags.INFO_RELATION:
226       return "INFO_RELATION";
227    
228     case TraceTags.INFO_MODELMBEAN:
229       return "INFO_MODELMBEAN";
230
231     default:
232       return "UNKNOWN_TRACE_TYPE";
233     }
234   }
235
236   /**
237    * Return the string representation of a trace level, as defined in
238    * {@link TraceTags}
239    */

240   private static String JavaDoc getLevel(int level) {
241     
242     switch (level) {
243     
244     case TraceTags.LEVEL_ERROR:
245       return "LEVEL_ERROR";
246
247     case TraceTags.LEVEL_TRACE:
248       return "LEVEL_TRACE";
249
250     case TraceTags.LEVEL_DEBUG:
251       return "LEVEL_DEBUG";
252
253     default :
254       return "UNKNOWN_TRACE_LEVEL";
255     }
256   }
257 }
258
Popular Tags