KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > diagnostics > ReporterImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.util.diagnostics;
25
26 import java.io.*;
27 import java.util.*;
28 import com.sun.enterprise.util.Assertion;
29 //Bug 4677074 begin
30
import java.util.logging.Logger JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import com.sun.logging.LogDomains;
33 //Bug 4677074 end
34

35 /** General Purpose Debugging Output
36  ** -- create a ReporterImpl object and then write with
37  ** pr()
38  ** If you construct with an Object -- that Object's class name will automatically be prepended to each message
39  ** If you use pr(String metName, String mesg) -- the metName will be added to the ObjectName
40  ** The output of ReporterImpl is controlled by an environmental variable
41  ** if you call it with java -DaibDebug=true -- it gets turned on...
42  **/

43
44 public class ReporterImpl implements IReporterEnum
45 {
46
47 //Bug 4677074 begin
48
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
49 //Bug 4677074 end
50
public ReporterImpl()
51     {
52         ctor(null, -1);
53     }
54     
55     ///////////////////////////////////////////////////////////////////////////////////////////////////////
56

57     ReporterImpl(int theSeverityLevel)
58     {
59         ctor(null, theSeverityLevel);
60     }
61     
62     ///////////////////////////////////////////////////////////////////////////////////////////////////////
63

64     public ReporterImpl(String JavaDoc sid)
65     {
66         ctor(sid, -1);
67     }
68     
69     ///////////////////////////////////////////////////////////////////////////////////////////////////////
70

71     ReporterImpl(String JavaDoc sid, int theSeverityLevel)
72     {
73         ctor(sid, theSeverityLevel);
74     }
75
76     ///////////////////////////////////////////////////////////////////////////////////////////////////////
77
/////////////////////////// //////////////////////////////////////////////////
78
/////////////////////////// Configurating Stuff //////////////////////////////////////////////////
79
/////////////////////////// //////////////////////////////////////////////////
80
///////////////////////////////////////////////////////////////////////////////////////////////////////
81

82     public void setSeverityLevel(int level)
83     {
84         debug("setSeverityLevel(" + level + ")");//NOI18N
85

86         if(level < 0)
87             level = 0;
88
89         severityLevel = level;
90     }
91
92     ///////////////////////////////////////////////////////////////////////////////////////////////////////
93

94     public void setSeverityLevel(String JavaDoc level)
95     {
96         debug("setSeverityLevel(" + level + ")");//NOI18N
97

98         severityLevel = calcSeverityLevel(level);
99     }
100
101     ///////////////////////////////////////////////////////////////////////////////////////////////////////
102

103     public int getSeverityLevel()
104     {
105         return severityLevel;
106     }
107
108     ///////////////////////////////////////////////////////////////////////////////////////////////////////
109

110     public String JavaDoc getSeverityLevelString()
111     {
112         if(severityLevel <= OFF)
113             return severityNames[severityLevel];
114         
115         return severityNames[OFF];
116     }
117
118     ///////////////////////////////////////////////////////////////////////////////////////////////////////
119

120     public void setName(String JavaDoc theName)
121     {
122         Assertion.check(theName);
123         name = theName;
124     }
125
126     ///////////////////////////////////////////////////////////////////////////////////////////////////////
127

128     public String JavaDoc getName()
129     {
130         return name;
131     }
132
133     ///////////////////////////////////////////////////////////////////////////////////////////////////////
134
/////////////////////////// //////////////////////////////////////////////////
135
/////////////////////////// Message Writing //////////////////////////////////////////////////
136
/////////////////////////// //////////////////////////////////////////////////
137
///////////////////////////////////////////////////////////////////////////////////////////////////////
138

139     public void verbose(Object JavaDoc o)
140     {
141         if(checkSeverity(VERBOSE))
142             pr(VERBOSE, o);
143     }
144
145     ///////////////////////////////////////////////////////////////////////////////////////////////////////
146

147     public void info(Object JavaDoc o)
148     {
149         if(checkSeverity(INFO))
150             pr(INFO, o);
151     }
152
153     ///////////////////////////////////////////////////////////////////////////////////////////////////////
154

155     public void warn(Object JavaDoc o)
156     {
157         // convenience method
158
if(checkSeverity(WARN))
159             pr(WARNING, o);
160     }
161
162     ///////////////////////////////////////////////////////////////////////////////////////////////////////
163

164     public void warning(Object JavaDoc o)
165     {
166         if(checkSeverity(WARN))
167             pr(WARNING, o);
168     }
169
170     ///////////////////////////////////////////////////////////////////////////////////////////////////////
171

172     public void error(Object JavaDoc o)
173     {
174         if(checkSeverity(ERROR))
175             pr(ERROR, o);
176     }
177     
178     ///////////////////////////////////////////////////////////////////////////////////////////////////////
179

180     public void critical(Object JavaDoc o)
181     {
182         if(checkSeverity(CRIT))
183             pr(CRITICAL, o);
184     }
185     ///////////////////////////////////////////////////////////////////////////////////////////////////////
186

187     public void crit(Object JavaDoc o)
188     {
189         if(checkSeverity(CRIT))
190             pr(CRITICAL, o);
191     }
192
193     ///////////////////////////////////////////////////////////////////////////////////////////////////////
194

195     public void dump(Object JavaDoc o, String JavaDoc s)
196     {
197         if(checkSeverity(DUMP) && o != null)
198         {
199             if(s == null)
200                 s = "";
201             
202             String JavaDoc s2 = s + "\n********** Object Dump Start ***********\n" +//NOI18N
203
ObjectAnalyzer.toStringWithSuper(o) + "\n********** Object Dump End ***********";//NOI18N
204
pr(OBJECT_DUMP, s2);
205         }
206     }
207
208     ///////////////////////////////////////////////////////////////////////////////////////////////////////
209

210     public void dump(String JavaDoc s)
211     {
212         if(checkSeverity(DUMP))
213         {
214             pr(OBJECT_DUMP, s);
215         }
216     }
217
218
219     ///////////////////////////////////////////////////////////////////////////////////////////////////////
220

221     public void dump(Object JavaDoc o)
222     {
223         dump(o, null);
224     }
225
226     ///////////////////////////////////////////////////////////////////////////////////////////////////////
227
/////////////////////////// //////////////////////////////////////////////////
228
/////////////////////////// ASSERTION STUFF //////////////////////////////////////////////////
229
/////////////////////////// //////////////////////////////////////////////////
230
///////////////////////////////////////////////////////////////////////////////////////////////////////
231

232     // TBD: Nice to have the assert's print the CallerInfo stuff.
233
// lots-o-typing needed!!
234

235     public void insist(String JavaDoc s)
236     {
237         if(checkSeverity(ASSERT))
238         {
239             try
240             {
241                 Assertion.check(s);
242             }
243             catch(Assertion.Failure f)
244             {
245                 pr(ASSERT, new StackTrace().toString() + f);
246                 throw f;
247             }
248         }
249     }
250
251     ///////////////////////////////////////////////////////////////////////////////////////////////////////
252

253     public void insist(String JavaDoc checkme, String JavaDoc s)
254     {
255         if(checkSeverity(ASSERT))
256         {
257             try
258             {
259                 Assertion.check(checkme, s);
260             }
261             catch(Assertion.Failure f)
262             {
263                 pr(ASSERT, new StackTrace().toString() + f);
264                 throw f;
265             }
266         }
267     }
268
269     ///////////////////////////////////////////////////////////////////////////////////////////////////////
270

271     public void insist(boolean b)
272     {
273         if(checkSeverity(ASSERT))
274         {
275             try
276             {
277                 Assertion.check(b);
278             }
279             catch(Assertion.Failure f)
280             {
281                 pr(ASSERT, new StackTrace().toString() + f);
282                 throw f;
283             }
284         }
285     }
286
287     ///////////////////////////////////////////////////////////////////////////////////////////////////////
288

289     public void insist(boolean b, String JavaDoc s)
290     {
291         if(checkSeverity(ASSERT))
292         {
293             try
294             {
295                 Assertion.check(b, s);
296             }
297             catch(Assertion.Failure f)
298             {
299                 pr(ASSERT, new StackTrace().toString() + f);
300                 throw f;
301             }
302         }
303     }
304
305     ///////////////////////////////////////////////////////////////////////////////////////////////////////
306

307     public void insist(Object JavaDoc o)
308     {
309         if(checkSeverity(ASSERT))
310         {
311             try
312             {
313                 Assertion.check(o);
314             }
315             catch(Assertion.Failure f)
316             {
317                 pr(ASSERT, new StackTrace().toString() + f);
318                 throw f;
319             }
320         }
321     }
322
323     ///////////////////////////////////////////////////////////////////////////////////////////////////////
324

325     public void insist(Object JavaDoc o, String JavaDoc s)
326     {
327         if(checkSeverity(ASSERT))
328         {
329             try
330             {
331                 Assertion.check(o, s);
332             }
333             catch(Assertion.Failure f)
334             {
335                 pr(ASSERT, new StackTrace().toString() + f);
336                 throw f;
337             }
338         }
339     }
340
341     ///////////////////////////////////////////////////////////////////////////////////////////////////////
342

343     public void insist(double z)
344     {
345         if(checkSeverity(ASSERT))
346         {
347             try
348             {
349                 Assertion.check(z);
350             }
351             catch(Assertion.Failure f)
352             {
353                 pr(ASSERT, new StackTrace().toString() + f);
354                 throw f;
355             }
356         }
357     }
358
359     ///////////////////////////////////////////////////////////////////////////////////////////////////////
360

361     public void insist(double z, String JavaDoc s)
362     {
363         if(checkSeverity(ASSERT))
364         {
365             try
366             {
367                 Assertion.check(z, s);
368             }
369             catch(Assertion.Failure f)
370             {
371                 pr(ASSERT, new StackTrace().toString() + f);
372                 throw f;
373             }
374         }
375     }
376
377     ///////////////////////////////////////////////////////////////////////////////////////////////////////
378

379     public void insist(long l)
380     {
381         if(checkSeverity(ASSERT))
382         {
383             try
384             {
385                 Assertion.check(l);
386             }
387             catch(Assertion.Failure f)
388             {
389                 pr(ASSERT, new StackTrace().toString() + f);
390                 throw f;
391             }
392         }
393     }
394
395     ///////////////////////////////////////////////////////////////////////////////////////////////////////
396

397     public void insist(long l, String JavaDoc s)
398     {
399         if(checkSeverity(ASSERT))
400         {
401             try
402             {
403                 Assertion.check(l, s);
404             }
405             catch(Assertion.Failure f)
406             {
407                 pr(ASSERT, new StackTrace().toString() + f);
408                 throw f;
409             }
410         }
411     }
412         
413         /** Change the mechanism this object uses to deliver output to the user
414          * @param lwriter A new output mechanism
415          * @return The previous output mechanism.
416          */

417         public ReporterWriter setWriter(ReporterWriter lwriter) {
418             ReporterWriter retVal = null;
419             if (null != lwriter) {
420                 retVal = this.writer;
421                 this.writer = lwriter;
422             }
423             return retVal;
424         }
425
426     ///////////////////////////////////////////////////////////////////////////////////////////////////////
427
/////////////////////////// //////////////////////////////////////////////////
428
/////////////////////////// PRIVATE STUFF //////////////////////////////////////////////////
429
/////////////////////////// //////////////////////////////////////////////////
430
///////////////////////////////////////////////////////////////////////////////////////////////////////
431

432     private void ctor(String JavaDoc theName, int theSeverityLevel)
433     {
434         if(theName != null && theName.length() > 0)
435             setName(theName);
436
437         if(theSeverityLevel >= 0)
438             setSeverityLevel(theSeverityLevel);
439         else
440         {
441             String JavaDoc sl = System.getProperty("ForteReporterDebugLevel");//NOI18N
442
if(sl != null && sl.length() > 0)
443             {
444                 //int sli = Integer.parseInt(sl);
445
//setSeverityLevel(sli);
446
setSeverityLevel(sl);
447             }
448
449             sl = System.getProperty("com.iplanet.util.diagnostic.Reporter.severityLevel");//NOI18N
450
if(sl != null && sl.length() > 0)
451             {
452                 //int sli = Integer.parseInt(sl);
453
//setSeverityLevel(sli);
454
setSeverityLevel(sl);
455             }
456
457             sl = System.getProperty("com.sun.enterprise.util.diagnostic.Reporter.severityLevel");//NOI18N
458
if(sl != null && sl.length() > 0)
459             {
460                 //int sli = Integer.parseInt(sl);
461
//setSeverityLevel(sli);
462
setSeverityLevel(sl);
463             }
464
465             sl = System.getProperty("Reporter");//NOI18N
466
if(sl != null && sl.length() > 0)
467             {
468                 //int sli = Integer.parseInt(sl);
469
//setSeverityLevel(sli);
470
setSeverityLevel(sl);
471             }
472         }
473
474         className = getClass().getName();
475         writer = new ReporterWriter(getName());
476
477         debug("Ctor called");//NOI18N
478
debug("ReporterImpl Severity Level: " + getSeverityLevel());//NOI18N
479
}
480     
481     ///////////////////////////////////////////////////////////////////////////////////////////////////////
482

483     private boolean checkSeverity(int severity)
484     {
485         if(severity >= severityLevel)
486             return true;
487
488         return false;
489     }
490     
491     ///////////////////////////////////////////////////////////////////////////////////////////////////////
492

493     private CallerInfo getCallerInfo()
494     {
495         try
496         {
497             return new CallerInfo(new Object JavaDoc[] { this });
498         }
499         catch(CallerInfoException e)
500         {
501             debug(e.toString());
502             return null;
503         }
504     }
505     
506     ///////////////////////////////////////////////////////////////////////////////////////////////////////
507

508     private void pr(int severity, Object JavaDoc o)
509     {
510         try
511         {
512             CallerInfo ci = getCallerInfo();
513             
514             String JavaDoc s;
515
516             if(o == null)
517             {
518                 s = "null Object argument";//NOI18N
519
}
520             else
521             {
522                 s = o.toString();
523
524                 if(s == null) // || s.length() <= 0)
525
s = "null toString result from Object";//NOI18N
526
}
527
528
529             if(ci != null)
530                 s = ci.toString() + ": " + s;//NOI18N
531

532             writer.println(severity, s);
533         }
534         catch(Throwable JavaDoc e)
535         {
536
537 //Bug 4677074 System.out.println("Got exception in ReporterImpl.pr(): " + e);//NOI18N
538
//Bug 4677074 begin
539
_logger.log(Level.WARNING,"iplanet_util.pr_exception",e);
540 //Bug 4677074 end
541
}
542     }
543     
544     ///////////////////////////////////////////////////////////////////////////////////////////////////////
545

546     private String JavaDoc getClassName()
547     {
548         Assertion.check(className);
549         return className;
550     }
551     
552     ///////////////////////////////////////////////////////////////////////////////////////////////////////
553

554     private int calcSeverityLevel(String JavaDoc original)
555     {
556         if(original == null || original.length() <= 0)
557             return DISABLED;
558
559         String JavaDoc s = original.toUpperCase();
560
561         // first let's see if it is an integer...
562

563         try
564         {
565             int ret = Integer.parseInt(s);
566
567             if(ret < 0)
568                 ret = 0;
569
570             return ret;
571         }
572         catch(NumberFormatException JavaDoc e)
573         {
574         }
575
576         // not a number -- let's check a few more possibilities...
577

578         if(s.equals("ALL") || s.equals("NOISY") || s.equals("EVERYTHING") || s.equals("ON") //NOI18N
579
|| s.equals("MONDO") || s.equals("YES") || s.equals("TRUE") || s.equals("DUMP") || s.startsWith("MAX"))//NOI18N
580
return 0;
581         if(s.startsWith("NO") || s.equals("OFF") || s.equals("FALSE") || s.equals("QUIET") || s.startsWith("MIN"))//NOI18N
582
return DISABLED;
583
584         // it should be "WARN", "CRITICAL", etc.
585
// since all of the values start with a different character,
586
// just check the first character...
587
char first = s.charAt(0); // uppercase!!
588

589         for(int i = 0; i < severityNames.length; i++)
590         {
591             if(severityNames[i].toUpperCase().charAt(0) == first)
592                 return i;
593         }
594
595         // I give up!
596
debug("Unknown value for commandline argument \"-DIABDebug=" + original + "\"");//NOI18N
597
return DISABLED;
598     }
599
600     private void debug(String JavaDoc s)
601     {
602         if(doDebug)
603 //Bug 4677074 System.out.println("ReporterImpl Report --> " + s);//NOI18N
604
//Bug 4677074 begin
605
_logger.log(Level.FINE,"ReporterImpl Report --> " +s);
606 //Bug 4677074 end
607
}
608     
609     ///////////////////////////////////////////////////////////////////////////////////////////////////////
610

611     private int severityLevel = OFF;
612     private String JavaDoc name = "Main";//NOI18N
613
private ReporterWriter writer;
614     private String JavaDoc className;
615     private static final boolean doDebug = false;
616 }
617
618
Popular Tags