1 23 24 package com.sun.enterprise.tools.common.util.diagnostics; 25 26 import java.io.*; 27 import java.util.*; 28 import com.sun.enterprise.tools.common.util.Assertion; 29 import com.sun.enterprise.tools.common.util.ObjectAnalyzer; 30 33 41 42 public class ReporterImpl implements IReporterEnum 43 { 44 45 public ReporterImpl() 46 { 47 ctor(null, -1); 48 } 49 50 52 ReporterImpl(int theSeverityLevel) 53 { 54 ctor(null, theSeverityLevel); 55 } 56 57 59 public ReporterImpl(String sid) 60 { 61 ctor(sid, -1); 62 } 63 64 66 ReporterImpl(String sid, int theSeverityLevel) 67 { 68 ctor(sid, theSeverityLevel); 69 } 70 71 77 public void setSeverityLevel(int level) 78 { 79 debug("setSeverityLevel(" + level + ")"); 81 if(level < 0) 82 level = 0; 83 84 severityLevel = level; 85 } 86 87 89 public void setSeverityLevel(String level) 90 { 91 debug("setSeverityLevel(" + level + ")"); 93 severityLevel = calcSeverityLevel(level); 94 } 95 96 98 public int getSeverityLevel() 99 { 100 return severityLevel; 101 } 102 103 105 public void setName(String theName) 106 { 107 Assertion.check(theName); 108 name = theName; 109 } 110 111 113 public String getName() 114 { 115 return name; 116 } 117 118 124 public void verbose(Object o) 125 { 126 if(checkSeverity(VERBOSE)) 127 pr(VERBOSE, o); 128 } 129 130 132 public void info(Object o) 133 { 134 if(checkSeverity(INFO)) 135 pr(INFO, o); 136 } 137 138 140 public void warn(Object o) 141 { 142 if(checkSeverity(WARN)) 144 pr(WARNING, o); 145 } 146 147 149 public void warning(Object o) 150 { 151 if(checkSeverity(WARN)) 152 pr(WARNING, o); 153 } 154 155 157 public void error(Object o) 158 { 159 if(checkSeverity(ERROR)) 160 pr(ERROR, o); 161 } 162 163 165 public void critical(Object o) 166 { 167 if(checkSeverity(CRIT)) 168 pr(CRITICAL, o); 169 } 170 172 public void crit(Object o) 173 { 174 if(checkSeverity(CRIT)) 175 pr(CRITICAL, o); 176 } 177 178 180 public void dump(Object o, String s) 181 { 182 if(checkSeverity(DUMP) && o != null) 183 { 184 if(s == null) 185 s = ""; 187 String s2 = s + "\n********** Object Dump Start ***********\n" + ObjectAnalyzer.toStringWithSuper(o) + "\n********** Object Dump End ***********"; pr(OBJECT_DUMP, s2); 190 } 191 } 192 193 195 public void dump(String s) 196 { 197 if(checkSeverity(DUMP)) 198 { 199 pr(OBJECT_DUMP, s); 200 } 201 } 202 203 204 206 public void dump(Object o) 207 { 208 dump(o, null); 209 } 210 211 217 220 public void assertIt(String s) 221 { 222 if(checkSeverity(ASSERT)) 223 { 224 try 225 { 226 Assertion.check(s); 227 } 228 catch(Assertion.Failure f) 229 { 230 pr(ASSERT, new StackTrace().toString() + f); 231 throw f; 232 } 233 } 234 } 235 236 238 public void assertIt(String checkme, String s) 239 { 240 if(checkSeverity(ASSERT)) 241 { 242 try 243 { 244 Assertion.check(checkme, s); 245 } 246 catch(Assertion.Failure f) 247 { 248 pr(ASSERT, new StackTrace().toString() + f); 249 throw f; 250 } 251 } 252 } 253 254 256 public void assertIt(boolean b) 257 { 258 if(checkSeverity(ASSERT)) 259 { 260 try 261 { 262 Assertion.check(b); 263 } 264 catch(Assertion.Failure f) 265 { 266 pr(ASSERT, new StackTrace().toString() + f); 267 throw f; 268 } 269 } 270 } 271 272 274 public void assertIt(boolean b, String s) 275 { 276 if(checkSeverity(ASSERT)) 277 { 278 try 279 { 280 Assertion.check(b, s); 281 } 282 catch(Assertion.Failure f) 283 { 284 pr(ASSERT, new StackTrace().toString() + f); 285 throw f; 286 } 287 } 288 } 289 290 292 public void assertIt(Object o) 293 { 294 if(checkSeverity(ASSERT)) 295 { 296 try 297 { 298 Assertion.check(o); 299 } 300 catch(Assertion.Failure f) 301 { 302 pr(ASSERT, new StackTrace().toString() + f); 303 throw f; 304 } 305 } 306 } 307 308 310 public void assertIt(Object o, String s) 311 { 312 if(checkSeverity(ASSERT)) 313 { 314 try 315 { 316 Assertion.check(o, s); 317 } 318 catch(Assertion.Failure f) 319 { 320 pr(ASSERT, new StackTrace().toString() + f); 321 throw f; 322 } 323 } 324 } 325 326 328 public void assertIt(double z) 329 { 330 if(checkSeverity(ASSERT)) 331 { 332 try 333 { 334 Assertion.check(z); 335 } 336 catch(Assertion.Failure f) 337 { 338 pr(ASSERT, new StackTrace().toString() + f); 339 throw f; 340 } 341 } 342 } 343 344 346 public void assertIt(double z, String s) 347 { 348 if(checkSeverity(ASSERT)) 349 { 350 try 351 { 352 Assertion.check(z, s); 353 } 354 catch(Assertion.Failure f) 355 { 356 pr(ASSERT, new StackTrace().toString() + f); 357 throw f; 358 } 359 } 360 } 361 362 364 public void assertIt(long l) 365 { 366 if(checkSeverity(ASSERT)) 367 { 368 try 369 { 370 Assertion.check(l); 371 } 372 catch(Assertion.Failure f) 373 { 374 pr(ASSERT, new StackTrace().toString() + f); 375 throw f; 376 } 377 } 378 } 379 380 382 public void assertIt(long l, String s) 383 { 384 if(checkSeverity(ASSERT)) 385 { 386 try 387 { 388 Assertion.check(l, s); 389 } 390 catch(Assertion.Failure f) 391 { 392 pr(ASSERT, new StackTrace().toString() + f); 393 throw f; 394 } 395 } 396 } 397 398 402 public ReporterWriter setWriter(ReporterWriter lwriter) { 403 ReporterWriter retVal = null; 404 if (null != lwriter) { 405 retVal = this.writer; 406 this.writer = lwriter; 407 } 408 return retVal; 409 } 410 411 417 private void ctor(String theName, int theSeverityLevel) 418 { 419 if(theName != null && theName.length() > 0) 420 setName(theName); 421 422 if(theSeverityLevel >= 0) 423 setSeverityLevel(theSeverityLevel); 424 else 425 { 426 String sl = System.getProperty("ForteReporterDebugLevel"); if(sl != null && sl.length() > 0) 428 { 429 int sli = Integer.parseInt(sl); 430 setSeverityLevel(sli); 431 } 432 } 433 434 className = getClass().getName(); 435 writer = new ReporterWriter(getName()); 436 437 debug("Ctor called"); debug("ReporterImpl Severity Level: " + getSeverityLevel()); } 440 441 443 private boolean checkSeverity(int severity) 444 { 445 if(severity >= severityLevel) 446 return true; 447 448 return false; 449 } 450 451 453 private CallerInfo getCallerInfo() 454 { 455 try 456 { 457 return new CallerInfo(new Object [] { this }); 458 } 459 catch(CallerInfoException e) 460 { 461 debug(e.toString()); 462 return null; 463 } 464 } 465 466 468 private void pr(int severity, Object o) 469 { 470 try 471 { 472 CallerInfo ci = getCallerInfo(); 473 474 String s; 475 476 if(o == null) 477 { 478 s = "null Object argument"; } 480 else 481 { 482 s = o.toString(); 483 484 if(s == null) s = "null toString result from Object"; } 487 488 489 if(ci != null) 490 s = ci.toString() + ": " + s; 492 writer.println(severity, s); 493 } 494 catch(Throwable e) 495 { 496 System.out.println("Got exception in ReporterImpl.pr(): " + e); } 498 } 499 500 502 private String getClassName() 503 { 504 Assertion.check(className); 505 return className; 506 } 507 508 510 private int calcSeverityLevel(String original) 511 { 512 if(original == null || original.length() <= 0) 513 return DISABLED; 514 515 String s = original.toUpperCase(); 516 517 519 try 520 { 521 int ret = Integer.parseInt(s); 522 523 if(ret < 0) 524 ret = 0; 525 526 return ret; 527 } 528 catch(NumberFormatException e) 529 { 530 } 531 532 534 if(s.equals("ALL") || s.equals("NOISY") || s.equals("EVERYTHING") || s.equals("ON") || s.equals("MONDO") || s.equals("YES") || s.equals("TRUE") || s.equals("DUMP") || s.startsWith("MAX")) return 0; 537 if(s.startsWith("NO") || s.equals("OFF") || s.equals("FALSE") || s.equals("QUIET") || s.startsWith("MIN")) return DISABLED; 539 540 char first = s.charAt(0); 545 for(int i = 0; i < severityNames.length; i++) 546 { 547 if(severityNames[i].toUpperCase().charAt(0) == first) 548 return i; 549 } 550 551 debug("Unknown value for commandline argument \"-DIABDebug=" + original + "\""); return DISABLED; 554 } 555 556 private void debug(String s) 557 { 558 if(doDebug) 559 System.out.println("ReporterImpl Report --> " + s); } 561 562 564 private int severityLevel = DISABLED; 565 private String name = "Main"; private ReporterWriter writer; 567 private String className; 568 private static final boolean doDebug = false; 569 } 570 571 | Popular Tags |