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 30 31 41 42 public class CallerInfo 43 { 44 public CallerInfo() throws CallerInfoException 45 { 46 this(null); 47 } 48 49 51 public CallerInfo(Object [] ignoreUsToo) throws CallerInfoException 52 { 53 58 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 59 PrintWriter pw = new PrintWriter(baos); 60 Throwable t = new Throwable (); 61 String me = getClass().getName() + "."; 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() + "."); } 70 71 if(globalIgnoreVec.size() > 0) 72 { 73 for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); ) 74 ignoreVec.addElement(e.nextElement().getClass().getName() + "."); } 76 77 84 85 t.printStackTrace(pw); 87 pw.flush(); 88 89 StringTokenizer st = new StringTokenizer(baos.toString(), "\n\r\t"); 92 if(st.countTokens() < 3) 93 { 94 throw new CallerInfoException("Expected at least 3 lines from the stack dump -- only saw " + st.countTokens() + " lines"); } 100 101 st.nextToken(); 103 while(st.hasMoreTokens()) 104 { 105 String 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"); } 116 117 119 public static void addToGlobalIgnore(Object o) 120 { 121 for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); ) 123 if(o == e.nextElement()) 124 return; 125 126 globalIgnoreVec.addElement(o); 127 } 128 129 131 void parseCallerInfo(String stackDumpLine) throws CallerInfoException 132 { 133 138 139 if(!stackDumpLine.startsWith("at ")) throw new CallerInfoException(badFormat + " -- no \"at \" at start of line (" + stackDumpLine +")"); 142 stackDumpLine = stackDumpLine.substring(3); 143 String classInfo = parseAndRemoveLineInfo(stackDumpLine); 144 parseClassInfo(classInfo); 145 } 146 147 149 public String getClassName() 150 { 151 Assertion.check(className); 152 return className; 153 } 154 155 156 158 public String getFileName() 159 { 160 Assertion.check(fileName != null); 161 return fileName; 162 } 163 164 166 public String getMethodName() 167 { 168 Assertion.check(methodName); 169 return methodName; 170 } 171 172 174 public int getLineNumber() 175 { 176 return lineNumber; 177 } 178 179 181 public String toString() 182 { 183 Assertion.check(className); 184 Assertion.check(methodName); 185 Assertion.check(fileName != null); 186 187 StringBuffer sb = new StringBuffer (); 188 sb.append(className); 189 sb.append("."); sb.append(methodName); 191 192 if(fileName.length() > 0) 193 sb.append("(" + fileName + ":" + lineNumber + ")"); else 195 sb.append("(Unknown Source"); 197 return sb.toString(); 198 } 199 200 202 public String toStringDebug() 203 { 204 Assertion.check(className); 205 Assertion.check(methodName); 206 Assertion.check(fileName != null); 207 208 StringBuffer sb = new StringBuffer (getClass().getName()); 209 sb.append(" dump:"); sb.append("\nClass Name: "); sb.append(className); 212 sb.append("\nMethod Name: "); sb.append(methodName); 214 sb.append("\nFile Name: "); 216 if(fileName.length() > 0) 217 { 218 sb.append(fileName); 219 sb.append("\nLine Number: "); sb.append(lineNumber); 221 } 222 else 223 sb.append("unknown"); 225 return sb.toString(); 226 } 227 228 234 private String parseAndRemoveLineInfo(String s) throws CallerInfoException 235 { 236 240 fileName = ""; 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); 249 String lineInfo = s.substring(left + 1, right); 250 s = s.substring(0, left); 251 252 if(lineInfo.length() <= 0) 253 return s; 255 if(lineInfo.equals("Unknown Source")) return s; 257 258 int colon = lineInfo.indexOf(':'); 259 260 if(colon < 0) 261 { 262 fileName = lineInfo; 264 return s; 265 } 266 if(colon == lineInfo.length() - 1) 267 { 268 fileName = lineInfo.substring(0, colon); 270 return s; 271 } 272 274 fileName = lineInfo.substring(0, colon); 275 276 try 277 { 278 lineNumber = Integer.parseInt(lineInfo.substring(colon + 1)); 279 } 280 catch(NumberFormatException e) 281 { 282 } 284 285 return s; 286 } 287 288 290 private void parseClassInfo(String s) throws CallerInfoException 291 { 292 294 if(s.indexOf('.') < 0) 295 throw new CallerInfoException(badFormat + " -- no \".\" in the fully-qualified method name"); 297 if(s.indexOf('.') == 0) 298 throw new CallerInfoException(badFormat + " fully-qualified method name starts with a dot"); 300 int index = s.lastIndexOf('.'); 301 302 className = s.substring(0, index); 303 methodName = s.substring(index + 1); 304 } 305 306 308 private boolean ignoreVec(String 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 )(ignoreVec.elementAt(i))) >= 0) 316 return true; 317 318 return false; 319 } 320 321 323 329 331 private String className; 332 private String fileName; 333 private String methodName; 334 private int lineNumber; 335 private final String badFormat = "Bad Format in stack dump line"; private Vector ignoreVec = new Vector(); 337 private static Vector globalIgnoreVec = new Vector(); 338 } 339 340 358 | Popular Tags |