KickJava   Java API By Example, From Geeks To Geeks.

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


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
8 package com.sun.jmx.trace;
9
10 // java import
11
//
12
import java.util.Properties JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * Sends trace to a pluggable destination.
19  *
20  * @since 1.5
21  * @since.unbundled JMX RI 1.2
22  */

23 public final class Trace implements TraceTags {
24
25     private static TraceDestination out = initDestination();
26     // private static TraceDestination out =
27
// TraceImplementation.newDestination(2);
28

29     // private constructor defined to "hide" the default public constructor
30
private Trace() {
31     
32     }
33
34     // Method used to test if the implementation of TraceDestination which relies
35
// on the java.util.logging API (com.sun.jmx.trace.TraceManager) can be used
36
// to initialize the trace destination. This is the case iff. we are running
37
// J2SE 1.4 or higher. Otherwise, the trace destination is null, and needs to
38
// be later initialized if we want to see traces !
39
//
40
private static TraceDestination initDestination()
41     {
42       // Attempt to locate class java.util.logging.LogManager
43
//
44
try
45       {
46         Class.forName("java.util.logging.LogManager");
47         
48         // Class could be loaded, use a new instance of TraceManager as the trace
49
// destination
50
//
51
return new TraceManager();
52       }
53       catch (ClassNotFoundException JavaDoc e)
54       {
55         // Class could not be loaded, means that we are running J2SE 1.3 or below.
56
//
57
return null;
58       }
59     }
60
61     // --------------
62
// public methods
63
// --------------
64

65     /**
66      * Set the trace destination.
67      **/

68     public static synchronized void setDestination(TraceDestination output) {
69     out = output;
70     }
71
72     /**
73      * Verify whether the specified info level and the info type are
74      * selected by a listener.
75      *
76      * <P>It is strongly recommended to call this method before sending
77      * an information to this Trace class.
78      *
79      * @param level the level of trace information.
80      * @param type the type of the trace information.
81      */

82     public static boolean isSelected(int level, int type) {
83     final TraceDestination output = out();
84     if (output != null) return output.isSelected(level,type);
85     return false;
86     }
87
88
89     /**
90      * Send a new information to this Trace class
91      *
92      * @param level the level of trace information to be sent.
93      * @param type the type of trace information to be sent.
94      * @param className the name of the class from which the trace
95      * information is from.
96      * @param methodName the name of the method from which the trace
97      * information is from.
98      * @param info the trace information to be sent.
99      * @return false if the level and the type are not selected.
100      */

101     public static boolean send(int level,
102                    int type,
103                    String JavaDoc className,
104                    String JavaDoc methodName,
105                    String JavaDoc info) {
106
107     final TraceDestination output = out();
108     if (output == null) return false;
109     if (!(output.isSelected(level, type))) return false;
110     return output.send(level,type,className,methodName,info);
111     }
112
113    /**
114      * Send an exception to this Trace class.
115      *
116      * @param level the level of trace information to be sent.
117      * @param type the type of trace information to be sent.
118      * @param className the name of the class from which the trace
119      * information is from.
120      * @param methodName the name of the method from which the trace
121      * information is from.
122      * @param exception exception sent as the trace information.
123      */

124     public static boolean send(int level,
125                    int type,
126                    String JavaDoc className,
127                    String JavaDoc methodName,
128                    Throwable JavaDoc exception) {
129     final TraceDestination output = out();
130     if (output == null) return false;
131     if (!(output.isSelected(level, type))) return false;
132     return output.send(level,type,className,methodName,exception);
133     }
134
135     /**
136      * Logs a warning message.
137      * This is equivalent to
138      * <code>Logger.getLogger(loggerName).warning(msg);</code>
139      * This method is a hack for backward compatibility with J2SE 1.3.
140      *
141      * @since.unbundled JMX RI 1.2.1
142      **/

143     public static void warning(String JavaDoc loggerName, String JavaDoc msg) {
144     final TraceDestination output = out();
145     if (output instanceof TraceManager)
146         // Log a warning message
147
((TraceManager)output).warning(loggerName,msg);
148     else if (output != null)
149         // Best effort
150
output.send(LEVEL_TRACE,INFO_MISC,null,null,msg);
151     }
152
153     /**
154      * Logs a fine message.
155      * This is equivalent to
156      * <code>Logger.getLogger(loggerName).fine(msg);</code>
157      * This method is a hack for backward compatibility with J2SE 1.3.
158      *
159      * @since.unbundled JMX RI 1.2.1
160      **/

161     public static void fine(String JavaDoc loggerName, String JavaDoc msg) {
162     final TraceDestination output = out();
163     if (output instanceof TraceManager)
164         // Log a fine message
165
((TraceManager)output).fine(loggerName,msg);
166     else if (output != null)
167         // Best effort
168
output.send(LEVEL_TRACE,INFO_MISC,null,null,msg);
169     }
170
171     /**
172      * Return the trace destination.
173      **/

174     private static synchronized TraceDestination out() {
175     return out;
176     }
177
178 }
179
Popular Tags