1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.coach.tracing.service; 26 27 import org.omg.CORBA.Any ; 28 import java.util.*; 29 import org.coach.tracing.api.*; 30 31 35 public class ThreadContext 36 { 37 private static int frameCounter; 38 private String trailLabel = ""; 39 private String threadId = ""; 40 private String trailId = ""; 41 private String senderThreadId = ""; 42 private Stack requestStack; 43 private RequestContext requestContext; 44 private static int eventCounter = -1; private static String vmId; 46 private static HashMap contextMap = new HashMap(); 47 private static Hashtable frameRequestContext = new Hashtable(); 48 49 static 50 { 51 try 52 { 53 java.io.File tmp = java.io.File.createTempFile("coach", ""); 55 tmp.deleteOnExit(); 56 vmId = java.net.InetAddress.getLocalHost().getHostName().replace('.', '_') + "_" + tmp.getName().substring(5); 57 } 58 catch (Exception e) 59 { 60 e.printStackTrace(); 61 } 62 } 63 64 68 private ThreadContext() 69 { 70 threadId = vmId + "_" + Thread.currentThread().getName(); 71 trailId = threadId; requestStack = new Stack(); 74 requestContext = new RequestContext(); 75 } 76 77 80 public static synchronized ThreadContext getCurrentThreadContext() 81 { 82 ThreadContext tc = null; 83 if (Thread.currentThread().getName().startsWith("AWT")) 84 { 85 java.awt.Frame f = getCurrentFrame(); 88 if (f != null) 89 { 90 tc = (ThreadContext)(contextMap.get(f)); 91 } 92 } 93 if (tc == null) 94 { 95 tc = (ThreadContext)(contextMap.get(vmId + "_" + Thread.currentThread().getName())); 96 } 97 if (tc == null) 98 { 99 tc = new ThreadContext(); 100 contextMap.put(tc.getThreadId(), tc); 101 } 102 return tc; 103 } 104 105 108 public static synchronized ThreadContext getThreadContext(String id) 109 { 110 ThreadContext tc = null; 111 if (id.indexOf("AWT-EventQueue") >= 0) 112 { 113 java.awt.Frame f = getCurrentFrame(); 114 if (f != null) 115 { 116 tc = (ThreadContext)(contextMap.get(f)); 117 } 119 } 120 if (tc == null) 121 { 122 tc = (ThreadContext)(contextMap.get(id)); 123 } 124 if (tc == null) 125 { 126 tc = new ThreadContext(); 127 contextMap.put(tc.getThreadId(), tc); 128 } 129 return tc; 130 } 131 132 private void setThreadId(String threadId) 133 { 134 this.threadId = threadId; 135 } 136 137 142 public String getTrailLabel() 143 { 144 return trailLabel; 145 } 146 147 152 public void setTrailLabel(String label) 153 { 154 trailLabel = label; 155 } 156 157 162 public String getCurrentOperation() 163 { 164 return requestContext.getCurrentOperation(); 165 } 166 167 172 public void setCurrentOperation(String opr) 173 { 174 requestContext.setCurrentOperation(opr); 175 } 176 177 182 public String getMessageId() 183 { 184 return requestContext.getMessageId(); 185 } 186 187 192 public void setMessageId(String id) 193 { 194 requestContext.setMessageId(id); 195 } 196 197 202 public String getCurrentInterfaceId() 203 { 204 return requestContext.getCurrentInterfaceId(); 205 } 206 207 212 public void setCurrentInterfaceId(String id) 213 { 214 requestContext.setCurrentInterfaceId(id); 215 } 216 217 222 public String getThreadId() 223 { 224 return threadId; 225 } 226 227 232 public String getTrailId() 233 { 234 return trailId; 235 } 236 237 242 public void setTrailId(String id) 243 { 244 trailId = id; 245 } 246 247 252 public static synchronized int getEventCounter() 253 { 254 return eventCounter; 255 } 256 257 262 public static synchronized int incrementEventCounter() 263 { 264 return ++eventCounter; 265 } 266 267 272 public String getVmId() 273 { 274 return vmId; 275 } 276 277 282 public String getComponentName() 283 { 284 return requestContext.getComponentName(); 285 } 286 287 292 public void setComponentName(String name) 293 { 294 requestContext.setComponentName(name); 295 } 296 297 302 public String getComponentType() 303 { 304 return requestContext.getComponentType(); 305 } 306 307 312 public void setComponentType(String type) 313 { 314 requestContext.setComponentType(type); 315 } 316 317 322 public static String getContainerName() 323 { 324 return RequestContext.getContainerName(); 325 } 326 327 332 public static void setContainerName(String name) 333 { 334 RequestContext.setContainerName(name); 335 } 336 337 342 public static String getContainerType() 343 { 344 return RequestContext.getContainerType(); 345 } 346 347 352 public static void setContainerType(String type) 353 { 354 RequestContext.setContainerType(type); 355 } 356 357 362 public String getObjectInstanceId() 363 { 364 return requestContext.getObjectInstanceId(); 365 } 366 367 372 public void setObjectInstanceId(String id) 373 { 374 requestContext.setObjectInstanceId(id); 375 } 376 377 382 public String getObjectRepositoryId() 383 { 384 return requestContext.getObjectRepositoryId(); 385 } 386 387 392 public void setObjectRepositoryId(String id) 393 { 394 requestContext.setObjectRepositoryId(id); 395 } 396 397 402 public void pushRequestContext() 403 { 404 requestStack.push(requestContext); 405 requestContext = (RequestContext)requestContext.clone(); 407 } 408 409 412 public void popRequestContext() 413 { 414 try 415 { 416 requestContext = (RequestContext)requestStack.pop(); 417 } 418 catch (Exception e) 419 { 420 } 421 } 422 423 public void print() 424 { 425 System.err.println("*** ThreadContext ***"); 426 System.err.println("trail id: " + getTrailId()); 427 System.err.println("message id: " + getMessageId()); 428 System.err.println("thread id: " + getThreadId()); 429 System.err.println("process id: " + getVmId()); 430 System.err.println("container name: " + getContainerName()); 431 System.err.println("container type: " + getContainerType()); 432 System.err.println("component name: " + getComponentName()); 433 System.err.println("component type: " + getComponentType()); 434 System.err.println("object instance id: " + getObjectInstanceId()); 435 System.err.println("object repository id: " + getObjectRepositoryId()); 436 System.err.println("operation: " + getCurrentOperation()); 437 System.err.println("interface id: " + getCurrentInterfaceId()); 438 System.err.println("event counter: " + getEventCounter()); 439 System.err.println("request stack size: " + requestStack.size()); 440 } 441 442 private RequestContext getRequestContext() 443 { 444 return requestContext; 445 } 446 447 private void setRequestContext(RequestContext ctx) 448 { 449 requestContext = (RequestContext)ctx.clone(); 450 } 451 452 459 public static void setFrameThreadContext(java.awt.Frame frame) 460 { 461 ThreadContext ftc = (ThreadContext)(contextMap.get(frame)); 462 if (ftc == null) 463 { 464 ftc = new ThreadContext(); 466 ftc.setThreadId(ftc.getThreadId() + "_GuiFrame_" + frameCounter++); 467 ftc.setTrailId(ftc.getThreadId()); 469 } 470 471 if (Thread.currentThread().getName().startsWith("AWT")) 472 { 473 java.awt.Frame parent = getCurrentFrame(); 476 if (parent != null) 477 { 478 ThreadContext parentFrameContext = (ThreadContext)(contextMap.get(parent)); 479 ftc.setRequestContext(parentFrameContext.getRequestContext()); 480 } 481 } 482 else 483 { 484 ftc.setRequestContext(getCurrentThreadContext().getRequestContext()); 486 } 487 contextMap.put(frame, ftc); 488 490 } 491 492 496 private static java.awt.Frame getCurrentFrame() 497 { 498 java.awt.AWTEvent awtEvent = java.awt.EventQueue.getCurrentEvent(); 499 if (awtEvent != null) 500 { 501 Object evtSrc = awtEvent.getSource(); 502 while (evtSrc instanceof java.awt.Component ) 503 { 504 if (evtSrc instanceof java.awt.Frame ) 505 { 506 return (java.awt.Frame )evtSrc; 508 } 509 evtSrc = ((java.awt.Component )evtSrc).getParent(); 510 } 511 } 512 return null; 513 } 514 515 519 public synchronized PropagationContext getPropagationContext() 520 { 521 PropagationContext propagationContext = new PropagationContext(); 522 propagationContext.trail_label = getTrailLabel(); 523 propagationContext.trail_id = getTrailId(); 524 propagationContext.message_id = getMessageId(); 525 return propagationContext; 526 } 527 528 531 public synchronized void setPropagationContext(PropagationContext propagationContext) 532 { 533 setTrailLabel(propagationContext.trail_label); 534 setTrailId(propagationContext.trail_id); 535 setMessageId(propagationContext.message_id); 536 } 537 538 543 public synchronized void clearContext() 544 { 545 if (requestStack.size() == 0) 546 { 547 setTrailLabel(""); 548 setTrailId(""); 549 setMessageId(""); 550 requestContext.clear(); 551 requestStack.clear(); 552 } 553 } 554 555 556 561 static class RequestContext extends InheritableThreadLocal implements Cloneable  562 { 563 private static String node_name = ""; 564 private static String cnt_name = ""; 565 private static String cnt_type = ""; 566 private ComponentId componentId; 568 private String object_instance_id; 569 private String object_repository_id; 570 private String message_id = ""; 571 private String currentInterfaceId = ""; 572 private String currentOperation = ""; 573 574 public RequestContext() 575 { 576 componentId = (ComponentId)get(); 578 if (componentId == null) 579 { 580 componentId = new ComponentId("", ""); 582 set(componentId); 583 } 584 } 585 586 public RequestContext(String name, String type) 587 { 588 componentId = new ComponentId(name, type); 589 set(componentId); 592 } 593 594 public void setComponentName(String name) 595 { 596 componentId.name = name; 597 } 598 599 public void setComponentType(String type) 600 { 601 componentId.type = type; 602 } 603 604 public String getCurrentInterfaceId() 605 { 606 return currentInterfaceId; 607 } 608 609 public void setCurrentInterfaceId(String id) 610 { 611 currentInterfaceId = id; 612 } 613 614 public String getCurrentOperation() 615 { 616 return currentOperation; 617 } 618 619 public void setCurrentOperation(String opr) 620 { 621 currentOperation = opr; 622 } 623 624 public String getComponentName() 625 { 626 return componentId.name; 627 } 628 629 public String getComponentType() 630 { 631 return componentId.type; 632 } 633 634 public static String getContainerName() 635 { 636 return cnt_name; 637 } 638 639 public static String getContainerType() 640 { 641 return cnt_type; 642 } 643 644 public void setObjectInstanceId(String instance_id) 645 { 646 object_instance_id = instance_id; 647 } 648 649 public void setObjectRepositoryId(String repository_id) 650 { 651 object_repository_id = repository_id; 652 } 653 654 public String getObjectInstanceId() 655 { 656 return object_instance_id; 657 } 658 659 public String getObjectRepositoryId() 660 { 661 return object_repository_id; 662 } 663 664 public static void setContainerName(String name) 665 { 666 cnt_name = name; 667 } 668 669 public static void setContainerType(String type) 670 { 671 cnt_type = type; 672 } 673 674 public void setMessageId(String id) 675 { 676 message_id = id; 677 } 678 679 public String getMessageId() 680 { 681 return message_id; 682 } 683 684 public void clear() 685 { 686 componentId.name = ""; 687 componentId.type = ""; 688 object_instance_id = ""; 689 object_repository_id = ""; 690 message_id = ""; 691 } 692 693 public Object clone() 694 { 695 RequestContext r = new RequestContext(getComponentName(), getComponentType()); 696 r.object_instance_id = object_instance_id; 697 r.object_repository_id = object_repository_id; 698 r.currentInterfaceId = currentInterfaceId; 699 r.message_id = message_id; 700 return r; 701 } 702 703 708 protected Object childValue(Object parentValue) 709 { 710 return ((ComponentId)parentValue).clone(); 711 } 712 713 class ComponentId implements Cloneable { 714 public String name; 715 public String type; 716 717 public ComponentId(String name, String type) 718 { 719 this.name = name; 720 this.type = type; 721 } 722 723 public Object clone() 724 { 725 return new ComponentId(name, type); 726 } 727 } 728 } 729 }
| Popular Tags
|