KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

41
42 public class CallerInfo
43 {
44     public CallerInfo() throws CallerInfoException
45     {
46         this(null);
47     }
48
49     /////////////////////////////////////////////////////////////////
50

51     public CallerInfo(Object JavaDoc[] ignoreUsToo) throws CallerInfoException
52     {
53         // STUPID PROGRAMMER TRICKS -- create an Exception and make it print a stack trace to an
54
// output stream that we can convert to a String -- then look for the first line that does *not* have
55
// our classname in it for the needed information...
56
//
57

58         ByteArrayOutputStream baos = new ByteArrayOutputStream();
59         PrintWriter pw = new PrintWriter(baos);
60         Throwable JavaDoc t = new Throwable JavaDoc();
61         String JavaDoc me = getClass().getName() + ".";//NOI18N
62

63         ignoreVec.addElement(me);
64
65         if(ignoreUsToo != null)
66         {
67             for(int i = 0; i < ignoreUsToo.length; i++)
68                 ignoreVec.addElement(ignoreUsToo[i].getClass().getName() + ".");//NOI18N
69
}
70
71         if(globalIgnoreVec.size() > 0)
72         {
73             for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); )
74                 ignoreVec.addElement(e.nextElement().getClass().getName() + ".");//NOI18N
75
}
76         
77         /*
78         System.out.println("**** Ignore List Dump...*****");
79         for(Enumeration e = ignoreVec.elements(); e.hasMoreElements(); )
80         {
81             System.out.println(e.nextElement().toString());
82         }
83         */

84
85         // create the stack trace
86
t.printStackTrace(pw);
87         pw.flush();
88         
89         // parse it...
90
StringTokenizer st = new StringTokenizer(baos.toString(), "\n\r\t");//NOI18N
91

92         if(st.countTokens() < 3)
93         {
94             // minimum:
95
// 1. java.lang.Throwable:
96
// 2. this method
97
// 3. The method from who-knows-where that is calling a Reporter method...
98
throw new CallerInfoException("Expected at least 3 lines from the stack dump -- only saw " + st.countTokens() + " lines");//NOI18N
99
}
100         
101         st.nextToken(); // throw away first line ("java.lang.Throwable")//NOI18N
102

103         while(st.hasMoreTokens())
104         {
105             String JavaDoc s = st.nextToken();
106
107             if(!ignoreVec(s))
108             {
109                 parseCallerInfo(s);
110                 return;
111             }
112         }
113         
114         throw new CallerInfoException("Couldn't find a caller method");//NOI18N
115
}
116
117     /////////////////////////////////////////////////////////////////
118

119     public static void addToGlobalIgnore(Object JavaDoc o)
120     {
121         // check for dupes...
122
for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); )
123             if(o == e.nextElement())
124                 return;
125
126         globalIgnoreVec.addElement(o);
127     }
128
129     /////////////////////////////////////////////////////////////////
130

131     void parseCallerInfo(String JavaDoc stackDumpLine) throws CallerInfoException
132     {
133         /* format of line:
134         // "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java:26)"
135         // "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java:)"
136         // "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java)"
137         // "at netscape.blizzard.util.ReporterTester.goo(Unknown Source)"*/

138  
139         if(!stackDumpLine.startsWith("at "))//NOI18N
140
throw new CallerInfoException(badFormat + " -- no \"at \" at start of line (" + stackDumpLine +")");//NOI18N
141

142         stackDumpLine = stackDumpLine.substring(3);
143         String JavaDoc classInfo = parseAndRemoveLineInfo(stackDumpLine);
144         parseClassInfo(classInfo);
145     }
146
147     /////////////////////////////////////////////////////////////////
148

149     public String JavaDoc getClassName()
150     {
151         Assertion.check(className);
152         return className;
153     }
154
155
156     /////////////////////////////////////////////////////////////////
157

158     public String JavaDoc getFileName()
159     {
160         Assertion.check(fileName != null);
161         return fileName;
162     }
163
164     /////////////////////////////////////////////////////////////////
165

166     public String JavaDoc getMethodName()
167     {
168         Assertion.check(methodName);
169         return methodName;
170     }
171
172     /////////////////////////////////////////////////////////////////
173

174     public int getLineNumber()
175     {
176         return lineNumber;
177     }
178
179     /////////////////////////////////////////////////////////////////
180

181     public String JavaDoc toString()
182     {
183         Assertion.check(className);
184         Assertion.check(methodName);
185         Assertion.check(fileName != null);
186
187         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
188         sb.append(className);
189         sb.append(".");//NOI18N
190
sb.append(methodName);
191
192         if(fileName.length() > 0)
193             sb.append("(" + fileName + ":" + lineNumber + ")");//NOI18N
194
else
195             sb.append("(Unknown Source");//NOI18N
196

197         return sb.toString();
198     }
199
200     /////////////////////////////////////////////////////////////////
201

202     public String JavaDoc toStringDebug()
203     {
204         Assertion.check(className);
205         Assertion.check(methodName);
206         Assertion.check(fileName != null);
207
208         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(getClass().getName());
209         sb.append(" dump:");//NOI18N
210
sb.append("\nClass Name: ");//NOI18N
211
sb.append(className);
212         sb.append("\nMethod Name: ");//NOI18N
213
sb.append(methodName);
214         sb.append("\nFile Name: ");//NOI18N
215

216         if(fileName.length() > 0)
217         {
218             sb.append(fileName);
219             sb.append("\nLine Number: ");//NOI18N
220
sb.append(lineNumber);
221         }
222         else
223             sb.append("unknown");//NOI18N
224

225         return sb.toString();
226     }
227
228     ///////////////////////////////////////////////////////////////////////////////////////////////////////
229
/////////////////////////// ////////////////////////////////////////
230
/////////////////////////// PRIVATE STUFF (and main) ////////////////////////////////////////
231
/////////////////////////// ////////////////////////////////////////
232
///////////////////////////////////////////////////////////////////////////////////////////////////////
233

234     private String JavaDoc parseAndRemoveLineInfo(String JavaDoc s) throws CallerInfoException
235     {
236         // lineInfo --> debug build: "(ReporterTester.java:26)"
237
// release build: "(Unknown Source)"
238
// I think this is a possibility: (foo.java) or (foo.java:) -- but can't reproduce...
239

240         fileName = "";//NOI18N
241
lineNumber = -1;
242         
243         int left = s.indexOf('(');
244         int right = s.indexOf(')');
245         
246         if(left < 0 || right < 0 || right <= left)
247             throw new CallerInfoException(badFormat + " -- no parenthesis in line:" + s);//NOI18N
248

249         String JavaDoc lineInfo = s.substring(left + 1, right);
250         s = s.substring(0, left);
251
252         if(lineInfo.length() <= 0)
253             return s; //()
254

255         if(lineInfo.equals("Unknown Source"))//NOI18N
256
return s;
257
258         int colon = lineInfo.indexOf(':');
259
260         if(colon < 0)
261         {
262             // (foo.java)
263
fileName = lineInfo;
264             return s;
265         }
266         if(colon == lineInfo.length() - 1)
267         {
268             // (foo.java:)
269
fileName = lineInfo.substring(0, colon);
270             return s;
271         }
272         // (foo.java:125)
273

274         fileName = lineInfo.substring(0, colon);
275         
276         try
277         {
278             lineNumber = Integer.parseInt(lineInfo.substring(colon + 1));
279         }
280         catch(NumberFormatException JavaDoc e)
281         {
282             // it's already set to -1
283
}
284         
285         return s;
286     }
287
288     /////////////////////////////////////////////////////////////////
289

290     private void parseClassInfo(String JavaDoc s) throws CallerInfoException
291     {
292         // format: netscape.blizzard.foo.goo.aMethod
293

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

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

300         int index = s.lastIndexOf('.');
301
302         className = s.substring(0, index);
303         methodName = s.substring(index + 1);
304     }
305
306     /////////////////////////////////////////////////////////////////
307

308     private boolean ignoreVec(String JavaDoc s)
309     {
310         Assertion.check(ignoreVec);
311         final int size = ignoreVec.size();
312         Assertion.check(size > 0);
313
314         for(int i = 0; i < size; i++)
315             if(s.indexOf((String JavaDoc)(ignoreVec.elementAt(i))) >= 0)
316                 return true;
317
318         return false;
319     }
320
321     /////////////////////////////////////////////////////////////////
322

323 /*
324  public static void main(String[] args)
325     {
326         new CallerInfoTester();
327     }
328 */

329     /////////////////////////////////////////////////////////////////
330

331     private String JavaDoc className;
332     private String JavaDoc fileName;
333     private String JavaDoc methodName;
334     private int lineNumber;
335     private final String JavaDoc badFormat = "Bad Format in stack dump line";//NOI18N
336
private Vector ignoreVec = new Vector();
337     private static Vector globalIgnoreVec = new Vector();
338 }
339
340 /*
341 class CallerInfoTester
342 {
343     CallerInfoTester()
344     {
345         try
346         {
347             CallerInfo ci = new CallerInfo(null);
348             System.out.println("CallerInfoTester here!!!");
349             System.out.println(ci.toString());
350         }
351         catch(CallerInfoException e)
352         {
353             System.out.println(e.toString());
354         }
355     }
356 }
357 */

358
Popular Tags