1 package com.sun.enterprise.util.diagnostics; 2 3 import java.io.*; 4 import java.util.*; 5 import com.sun.enterprise.util.Assertion; 6 import java.util.logging.Logger ; 8 import java.util.logging.Level ; 9 import com.sun.logging.LogDomains; 10 12 35 36 46 47 public class CallerInfo 48 { 49 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 51 public CallerInfo() throws CallerInfoException 53 { 54 this(null); 55 } 56 57 59 public CallerInfo(Object [] ignoreUsToo) throws CallerInfoException 60 { 61 66 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 67 PrintWriter pw = new PrintWriter(baos); 68 Throwable t = new Throwable (); 69 String me = getClass().getName() + "."; 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() + "."); } 78 79 if(globalIgnoreVec.size() > 0) 80 { 81 for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); ) 82 ignoreVec.addElement(e.nextElement().getClass().getName() + "."); } 84 85 98 99 t.printStackTrace(pw); 101 pw.flush(); 102 103 StringTokenizer st = new StringTokenizer(baos.toString(), "\n\r\t"); 106 if(st.countTokens() < 3) 107 { 108 throw new CallerInfoException("Expected at least 3 lines from the stack dump -- only saw " + st.countTokens() + " lines"); } 114 115 st.nextToken(); 117 while(st.hasMoreTokens()) 118 { 119 String 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"); } 130 131 133 public static void addToGlobalIgnore(Object o) 134 { 135 for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); ) 137 if(o == e.nextElement()) 138 return; 139 140 globalIgnoreVec.addElement(o); 141 } 142 143 145 void parseCallerInfo(String stackDumpLine) throws CallerInfoException 146 { 147 152 153 if(!stackDumpLine.startsWith("at ")) throw new CallerInfoException(badFormat + " -- no \"at \" at start of line (" + stackDumpLine +")"); 156 stackDumpLine = stackDumpLine.substring(3); 157 String classInfo = parseAndRemoveLineInfo(stackDumpLine); 158 parseClassInfo(classInfo); 159 } 160 161 163 public String getClassName() 164 { 165 Assertion.check(className); 166 return className; 167 } 168 169 170 172 public String getFileName() 173 { 174 Assertion.check(fileName != null); 175 return fileName; 176 } 177 178 180 public String getMethodName() 181 { 182 Assertion.check(methodName); 183 return methodName; 184 } 185 186 188 public int getLineNumber() 189 { 190 return lineNumber; 191 } 192 193 195 public String toString() 196 { 197 Assertion.check(className); 198 Assertion.check(methodName); 199 Assertion.check(fileName != null); 200 201 StringBuffer sb = new StringBuffer (); 202 sb.append(className); 203 sb.append("."); sb.append(methodName); 205 206 if(fileName.length() > 0) 207 sb.append("(" + fileName + ":" + lineNumber + ")"); else 209 sb.append("(Unknown Source"); 211 return sb.toString(); 212 } 213 214 216 public String toStringDebug() 217 { 218 Assertion.check(className); 219 Assertion.check(methodName); 220 Assertion.check(fileName != null); 221 222 StringBuffer sb = new StringBuffer (getClass().getName()); 223 sb.append(" dump:"); sb.append("\nClass Name: "); sb.append(className); 226 sb.append("\nMethod Name: "); sb.append(methodName); 228 sb.append("\nFile Name: "); 230 if(fileName.length() > 0) 231 { 232 sb.append(fileName); 233 sb.append("\nLine Number: "); sb.append(lineNumber); 235 } 236 else 237 sb.append("unknown"); 239 return sb.toString(); 240 } 241 242 248 private String parseAndRemoveLineInfo(String s) throws CallerInfoException 249 { 250 254 fileName = ""; 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); 263 String lineInfo = s.substring(left + 1, right); 264 s = s.substring(0, left); 265 266 if(lineInfo.length() <= 0) 267 return s; 269 if(lineInfo.equals("Unknown Source")) return s; 271 272 int colon = lineInfo.indexOf(':'); 273 274 if(colon < 0) 275 { 276 fileName = lineInfo; 278 return s; 279 } 280 if(colon == lineInfo.length() - 1) 281 { 282 fileName = lineInfo.substring(0, colon); 284 return s; 285 } 286 288 fileName = lineInfo.substring(0, colon); 289 290 try 291 { 292 lineNumber = Integer.parseInt(lineInfo.substring(colon + 1)); 293 } 294 catch(NumberFormatException e) 295 { 296 } 298 299 return s; 300 } 301 302 304 private void parseClassInfo(String s) throws CallerInfoException 305 { 306 308 if(s.indexOf('.') < 0) 309 throw new CallerInfoException(badFormat + " -- no \".\" in the fully-qualified method name"); 311 if(s.indexOf('.') == 0) 312 throw new CallerInfoException(badFormat + " fully-qualified method name starts with a dot"); 314 int index = s.lastIndexOf('.'); 315 316 className = s.substring(0, index); 317 methodName = s.substring(index + 1); 318 } 319 320 322 private boolean ignoreVec(String 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 )(ignoreVec.elementAt(i))) >= 0) 330 return true; 331 332 return false; 333 } 334 335 337 343 345 private String className; 346 private String fileName; 347 private String methodName; 348 private int lineNumber; 349 private final String badFormat = "Bad Format in stack dump line"; private Vector ignoreVec = new Vector(); 351 private static Vector globalIgnoreVec = new Vector(); 352 } 353 354 380 | Popular Tags |