KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.sun.enterprise.util.diagnostics;
2
3 import java.io.*;
4 import java.util.*;
5 import com.sun.enterprise.util.Assertion;
6 //Bug 4677074 begin
7
import java.util.logging.Logger JavaDoc;
8 import java.util.logging.Level JavaDoc;
9 import com.sun.logging.LogDomains;
10 //Bug 4677074 end
11

12 //import netscape.blizzard.util.*;
13
/*
14  * The contents of this file are subject to the terms
15  * of the Common Development and Distribution License
16  * (the License). You may not use this file except in
17  * compliance with the License.
18  *
19  * You can obtain a copy of the license at
20  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
21  * glassfish/bootstrap/legal/CDDLv1.0.txt.
22  * See the License for the specific language governing
23  * permissions and limitations under the License.
24  *
25  * When distributing Covered Code, include this CDDL
26  * Header Notice in each file and include the License file
27  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
28  * If applicable, add the following below the CDDL Header,
29  * with the fields enclosed by brackets [] replaced by
30  * you own identifying information:
31  * "Portions Copyrighted [year] [name of copyright owner]"
32  *
33  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
34  */

35
36 /**
37  * The class <code>CallerInfo</code> is a simple <I>C-struct</I> type of
38  * data structure that contains a caller's class name, line number and method name.
39  * It is used by Reporter classes in the util package of iAB
40  *
41  * @author Byron Nevins
42  * @version 1.1, 05/31/00
43  * @see netscape.blizzard.util.Reporter
44  * @since iAB 6.0
45  */

46
47 public class CallerInfo
48 {
49 //Bug 4677074 begin
50
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
51 //Bug 4677074 end
52
public CallerInfo() throws CallerInfoException
53     {
54         this(null);
55     }
56
57     /////////////////////////////////////////////////////////////////
58

59     public CallerInfo(Object JavaDoc[] ignoreUsToo) throws CallerInfoException
60     {
61         // STUPID PROGRAMMER TRICKS -- create an Exception and make it print a stack trace to an
62
// output stream that we can convert to a String -- then look for the first line that does *not* have
63
// our classname in it for the needed information...
64
//
65

66         ByteArrayOutputStream baos = new ByteArrayOutputStream();
67         PrintWriter pw = new PrintWriter(baos);
68         Throwable JavaDoc t = new Throwable JavaDoc();
69         String JavaDoc me = getClass().getName() + ".";//NOI18N
70

71         ignoreVec.addElement(me);
72
73         if(ignoreUsToo != null)
74         {
75             for(int i = 0; i < ignoreUsToo.length; i++)
76                 ignoreVec.addElement(ignoreUsToo[i].getClass().getName() + ".");//NOI18N
77
}
78
79         if(globalIgnoreVec.size() > 0)
80         {
81             for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); )
82                 ignoreVec.addElement(e.nextElement().getClass().getName() + ".");//NOI18N
83
}
84         
85         /*
86         System.out.println("**** Ignore List Dump...*****");
87 //Bug 4677074 begin
88          _logger.log(Level.FINE,"**** Ignore List Dump...*****");
89 //Bug 4677074 end
90         for(Enumeration e = ignoreVec.elements(); e.hasMoreElements(); )
91         {
92             System.out.println(e.nextElement().toString());
93 //Bug 4677074 begin
94             _logger.log(Level.FINE,e.nextElement().toString());
95 //Bug 4677074 end
96         }
97         */

98
99         // create the stack trace
100
t.printStackTrace(pw);
101         pw.flush();
102         
103         // parse it...
104
StringTokenizer st = new StringTokenizer(baos.toString(), "\n\r\t");//NOI18N
105

106         if(st.countTokens() < 3)
107         {
108             // minimum:
109
// 1. java.lang.Throwable:
110
// 2. this method
111
// 3. The method from who-knows-where that is calling a Reporter method...
112
throw new CallerInfoException("Expected at least 3 lines from the stack dump -- only saw " + st.countTokens() + " lines");//NOI18N
113
}
114         
115         st.nextToken(); // throw away first line ("java.lang.Throwable")//NOI18N
116

117         while(st.hasMoreTokens())
118         {
119             String JavaDoc s = st.nextToken();
120
121             if(!ignoreVec(s))
122             {
123                 parseCallerInfo(s);
124                 return;
125             }
126         }
127         
128         throw new CallerInfoException("Couldn't find a caller method");//NOI18N
129
}
130
131     /////////////////////////////////////////////////////////////////
132

133     public static void addToGlobalIgnore(Object JavaDoc o)
134     {
135         // check for dupes...
136
for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); )
137             if(o == e.nextElement())
138                 return;
139
140         globalIgnoreVec.addElement(o);
141     }
142
143     /////////////////////////////////////////////////////////////////
144

145     void parseCallerInfo(String JavaDoc stackDumpLine) throws CallerInfoException
146     {
147         /* format of line:
148         // "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java:26)"
149         // "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java:)"
150         // "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java)"
151         // "at netscape.blizzard.util.ReporterTester.goo(Unknown Source)"*/

152  
153         if(!stackDumpLine.startsWith("at "))//NOI18N
154
throw new CallerInfoException(badFormat + " -- no \"at \" at start of line (" + stackDumpLine +")");//NOI18N
155

156         stackDumpLine = stackDumpLine.substring(3);
157         String JavaDoc classInfo = parseAndRemoveLineInfo(stackDumpLine);
158         parseClassInfo(classInfo);
159     }
160
161     /////////////////////////////////////////////////////////////////
162

163     public String JavaDoc getClassName()
164     {
165         Assertion.check(className);
166         return className;
167     }
168
169
170     /////////////////////////////////////////////////////////////////
171

172     public String JavaDoc getFileName()
173     {
174         Assertion.check(fileName != null);
175         return fileName;
176     }
177
178     /////////////////////////////////////////////////////////////////
179

180     public String JavaDoc getMethodName()
181     {
182         Assertion.check(methodName);
183         return methodName;
184     }
185
186     /////////////////////////////////////////////////////////////////
187

188     public int getLineNumber()
189     {
190         return lineNumber;
191     }
192
193     /////////////////////////////////////////////////////////////////
194

195     public String JavaDoc toString()
196     {
197         Assertion.check(className);
198         Assertion.check(methodName);
199         Assertion.check(fileName != null);
200
201         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
202         sb.append(className);
203         sb.append(".");//NOI18N
204
sb.append(methodName);
205
206         if(fileName.length() > 0)
207             sb.append("(" + fileName + ":" + lineNumber + ")");//NOI18N
208
else
209             sb.append("(Unknown Source");//NOI18N
210

211         return sb.toString();
212     }
213
214     /////////////////////////////////////////////////////////////////
215

216     public String JavaDoc toStringDebug()
217     {
218         Assertion.check(className);
219         Assertion.check(methodName);
220         Assertion.check(fileName != null);
221
222         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(getClass().getName());
223         sb.append(" dump:");//NOI18N
224
sb.append("\nClass Name: ");//NOI18N
225
sb.append(className);
226         sb.append("\nMethod Name: ");//NOI18N
227
sb.append(methodName);
228         sb.append("\nFile Name: ");//NOI18N
229

230         if(fileName.length() > 0)
231         {
232             sb.append(fileName);
233             sb.append("\nLine Number: ");//NOI18N
234
sb.append(lineNumber);
235         }
236         else
237             sb.append("unknown");//NOI18N
238

239         return sb.toString();
240     }
241
242     ///////////////////////////////////////////////////////////////////////////////////////////////////////
243
/////////////////////////// ////////////////////////////////////////
244
/////////////////////////// PRIVATE STUFF (and main) ////////////////////////////////////////
245
/////////////////////////// ////////////////////////////////////////
246
///////////////////////////////////////////////////////////////////////////////////////////////////////
247

248     private String JavaDoc parseAndRemoveLineInfo(String JavaDoc s) throws CallerInfoException
249     {
250         // lineInfo --> debug build: "(ReporterTester.java:26)"
251
// release build: "(Unknown Source)"
252
// I think this is a possibility: (foo.java) or (foo.java:) -- but can't reproduce...
253

254         fileName = "";//NOI18N
255
lineNumber = -1;
256         
257         int left = s.indexOf('(');
258         int right = s.indexOf(')');
259         
260         if(left < 0 || right < 0 || right <= left)
261             throw new CallerInfoException(badFormat + " -- no parenthesis in line:" + s);//NOI18N
262

263         String JavaDoc lineInfo = s.substring(left + 1, right);
264         s = s.substring(0, left);
265
266         if(lineInfo.length() <= 0)
267             return s; //()
268

269         if(lineInfo.equals("Unknown Source"))//NOI18N
270
return s;
271
272         int colon = lineInfo.indexOf(':');
273
274         if(colon < 0)
275         {
276             // (foo.java)
277
fileName = lineInfo;
278             return s;
279         }
280         if(colon == lineInfo.length() - 1)
281         {
282             // (foo.java:)
283
fileName = lineInfo.substring(0, colon);
284             return s;
285         }
286         // (foo.java:125)
287

288         fileName = lineInfo.substring(0, colon);
289         
290         try
291         {
292             lineNumber = Integer.parseInt(lineInfo.substring(colon + 1));
293         }
294         catch(NumberFormatException JavaDoc e)
295         {
296             // it's already set to -1
297
}
298         
299         return s;
300     }
301
302     /////////////////////////////////////////////////////////////////
303

304     private void parseClassInfo(String JavaDoc s) throws CallerInfoException
305     {
306         // format: netscape.blizzard.foo.goo.aMethod
307

308         if(s.indexOf('.') < 0)
309             throw new CallerInfoException(badFormat + " -- no \".\" in the fully-qualified method name");//NOI18N
310

311         if(s.indexOf('.') == 0)
312             throw new CallerInfoException(badFormat + " fully-qualified method name starts with a dot");//NOI18N
313

314         int index = s.lastIndexOf('.');
315
316         className = s.substring(0, index);
317         methodName = s.substring(index + 1);
318     }
319
320     /////////////////////////////////////////////////////////////////
321

322     private boolean ignoreVec(String JavaDoc s)
323     {
324         Assertion.check(ignoreVec);
325         final int size = ignoreVec.size();
326         Assertion.check(size > 0);
327
328         for(int i = 0; i < size; i++)
329             if(s.indexOf((String JavaDoc)(ignoreVec.elementAt(i))) >= 0)
330                 return true;
331
332         return false;
333     }
334
335     /////////////////////////////////////////////////////////////////
336

337 /*
338  public static void main(String[] args)
339     {
340         new CallerInfoTester();
341     }
342 */

343     /////////////////////////////////////////////////////////////////
344

345     private String JavaDoc className;
346     private String JavaDoc fileName;
347     private String JavaDoc methodName;
348     private int lineNumber;
349     private final String JavaDoc badFormat = "Bad Format in stack dump line";//NOI18N
350
private Vector ignoreVec = new Vector();
351     private static Vector globalIgnoreVec = new Vector();
352 }
353
354 /*
355 class CallerInfoTester
356 {
357     CallerInfoTester()
358     {
359         try
360         {
361             CallerInfo ci = new CallerInfo(null);
362             System.out.println("CallerInfoTester here!!!");
363             System.out.println(ci.toString());
364 //Bug 4677074 begin
365             _logger.log(Level.FINE,"CallerInfoTester here!!!");
366             _logger.log(Level.FINE,ci.toString());
367 //Bug 4677074 end
368         }
369         catch(CallerInfoException e)
370         {
371             System.out.println(e.toString());
372 //Bug 4677074 begin
373             _logger.log(Level.WARNING,"iplanet_util.callerinfo_exception",e);
374 //Bug 4677074 end
375 //Bug 4677074 end
376         }
377     }
378 }
379 */

380
Popular Tags