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 java.net.*; 28 import java.io.*; 29 import java.util.*; 30 import org.coach.tracing.service.ntp.*; 31 import org.coach.tracing.api.*; 32 import org.omg.CORBA.*; 33 import org.omg.CosNaming.*; 34 import org.omg.CosNaming.NamingContextPackage.*; 35 36 public class Sender 37 { 38 private static String [] excludeTable; 39 private i_Trace traceServer = null; 40 public String name = ""; 41 private ORB orb = null; 42 private boolean initialized = false; 43 private static boolean initializing = false; 44 private static String hostName; 45 private static String hostAddress; 46 private Queue sendQueue = new Queue(); 47 private NamingContextExt root_nc = null; 48 private NameComponent[] traceServerPath; 49 50 private Parameter[] exceptionParameter = new Parameter[] {new Parameter("exception", "exception", "exception")}; 51 52 private static Sender sender; 54 public static Sender createSender(ORB orb) 55 { 56 if (sender == null) 57 { 58 initializing = true; 59 System.err.println("Create COACH Tracing Sender."); 60 sender = new Sender(orb); 61 } 62 return sender; 63 } 64 65 protected Sender(ORB orb) 66 { 67 try 68 { 69 String traceServerName = "COACH_TracingServer"; 70 traceServerPath = new NameComponent[] {new NameComponent(traceServerName, "")}; 71 hostName = InetAddress.getLocalHost().getHostName(); 72 hostAddress = InetAddress.getLocalHost().getHostAddress(); 73 root_nc = null; 74 try 75 { 76 root_nc = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService")); 78 } 79 catch (Exception e1) 80 { 81 System.err.println("No nameserver found"); 82 } 83 84 try 85 { 86 org.omg.CORBA.Object obj = root_nc.resolve(traceServerPath); 87 traceServer = i_TraceHelper.narrow(obj); 88 } 89 catch (Exception e) 90 { 91 System.err.println("No tracing server found: " + traceServerName + " error: " + e.toString()); 92 } 93 this.orb = orb; 94 if (traceServer != null) 95 { 96 SendThread st = new SendThread(); 97 st.setPriority(Thread.currentThread().getPriority() - 1); 98 st.start(); 99 } 100 } 101 catch (Exception e) 102 { 103 e.printStackTrace(); 104 } 105 } 106 107 public static boolean isInitializing() 108 { 109 return initializing && sender == null; 110 } 111 112 public static boolean exclude(String id) 113 { 114 if (excludeTable == null) 115 { 116 Vector v = new Vector(); 118 String excludes = System.getProperty("coach.tracing.exclude"); 119 if (excludes != null && !excludes.equals("")) 120 { 121 StringTokenizer st = new StringTokenizer(excludes, ", "); 122 while (st.hasMoreTokens()) 123 { 124 String t = st.nextToken(); 125 if (t.endsWith("*")) 126 { 127 t = t.substring(0, t.lastIndexOf("*")); 128 } 129 v.add(t); 130 } 131 } 132 excludeTable = new String [v.size()]; 133 v.toArray(excludeTable); 134 } 135 136 for (int i = 0; i < excludeTable.length; i++) 137 { 138 if (id.startsWith(excludeTable[i])) 139 { 140 return true; 141 } 142 } 143 return false; 144 } 145 146 protected TraceEvent createEvent(ThreadContext tc) 147 { 148 TraceEvent traceEvent = new TraceEvent(); 149 try 150 { 151 traceEvent.identity = new IdentityDescriptor(); 152 traceEvent.identity.object_instance_id = tc.getObjectInstanceId(); 153 traceEvent.identity.object_repository_id = tc.getObjectRepositoryId(); 154 traceEvent.identity.process_id = tc.getVmId(); 155 traceEvent.identity.node_name = hostName; 156 traceEvent.identity.node_ip = hostAddress; 157 traceEvent.identity.cmp_name = tc.getComponentName(); 158 traceEvent.identity.cmp_type = tc.getComponentType(); 159 traceEvent.identity.cnt_name = tc.getContainerName(); 160 traceEvent.identity.cnt_type = tc.getContainerType(); 161 traceEvent.trail_label = tc.getTrailLabel(); 162 traceEvent.trail_id = tc.getTrailId(); 163 traceEvent.event_counter = tc.incrementEventCounter(); 164 traceEvent.time_stamp = PhysicalTime.getInstance().getTime(); 165 traceEvent.thread_id = tc.getThreadId(); 166 traceEvent.op_name = ""; 167 traceEvent.parameters = new Parameter[0]; 168 traceEvent.parameter_values = new Any[0]; 169 } 170 catch (Exception e) 171 { 172 e.printStackTrace(); 173 } 174 return traceEvent; 175 } 176 177 public synchronized void stubPreInvoke(ThreadContext tc, String op_name, Parameter[] parameters, Any[] parameter_values, boolean isOneway) 178 { 179 try 180 { 181 TraceEvent event = createEvent(tc); 183 tc.setMessageId(tc.getVmId() + "_" + tc.getEventCounter()); 185 event.message_id = tc.getMessageId() + "F"; 186 187 if (isOneway) 189 { 190 event.interaction_point = InteractionPoint.ONEWAY_STUB_OUT; 191 } 192 else 193 { 194 event.interaction_point = InteractionPoint.STUB_OUT; 195 } 196 event.op_name = op_name; 197 if (parameter_values != null) 198 { 199 event.parameters = parameters; 200 event.parameter_values = parameter_values; 201 } 202 send(event); 203 } 204 catch (Exception e) 205 { 206 e.printStackTrace(); 207 } 208 } 209 210 public synchronized void poaPreInvoke(ThreadContext tc, String op_name, Parameter[] parameters, Any[] parameter_values, boolean isOneway) 211 { 212 try 213 { 214 TraceEvent event = createEvent(tc); 216 event.message_id = tc.getMessageId() + "F"; 217 if (isOneway) 219 { 220 event.interaction_point = InteractionPoint.ONEWAY_POA_IN; 221 } 222 else 223 { 224 event.interaction_point = InteractionPoint.POA_IN; 225 } 226 event.op_name = op_name; 227 if (parameter_values != null) 228 { 229 event.parameters = parameters; 230 event.parameter_values = parameter_values; 231 } 232 send(event); 233 } 234 catch (Exception e) 235 { 236 e.printStackTrace(); 238 } 239 } 240 241 public synchronized void poaPostInvoke(ThreadContext tc, String op_name, Parameter[] parameters, Any[] parameter_values) 242 { 243 try 244 { 245 TraceEvent event = createEvent(tc); 246 event.op_name = op_name; 247 event.message_id = tc.getMessageId() + "B"; 248 if (parameter_values != null) 249 { 250 event.parameters = parameters; 251 event.parameter_values = parameter_values; 252 } 253 254 event.interaction_point = InteractionPoint.POA_OUT; 256 257 send(event); 258 } 259 catch (Exception e) 260 { 261 e.printStackTrace(); 263 } 264 } 265 266 public synchronized void poaPostException(ThreadContext tc, String op_name, Any exception_value) 267 { 268 try 269 { 270 TraceEvent event = createEvent(tc); 272 event.op_name = op_name; 273 event.parameters = exceptionParameter; 274 event.parameter_values = new Any[] {exception_value}; 275 event.message_id = tc.getMessageId() + "B"; 276 277 event.interaction_point = InteractionPoint.POA_OUT_EXCEPTION; 279 280 send(event); 281 } 282 catch (Exception e) 283 { 284 e.printStackTrace(); 286 } 287 } 288 289 public synchronized void stubPostInvoke(ThreadContext tc, String op_name, Parameter[] parameters, Any[] parameter_values, boolean isExceptional, boolean isOneway) 290 { 291 if (!isOneway) 292 { 293 try 295 { 296 TraceEvent event = createEvent(tc); 298 event.message_id = tc.getMessageId() + "B"; 299 event.op_name = op_name; 300 if (parameter_values != null) 301 { 302 event.parameters = parameters; 303 event.parameter_values = parameter_values; 304 } 305 306 event.interaction_point = InteractionPoint.STUB_IN; 308 309 send(event); 310 } 311 catch (Exception e) 312 { 313 e.printStackTrace(); 315 } 316 } 317 } 318 319 public synchronized void stubPostException(ThreadContext tc, String op_name, Any exception_value) 320 { 321 try 323 { 324 TraceEvent event = createEvent(tc); 326 event.message_id = tc.getMessageId() + "B"; 327 event.op_name = op_name; 328 event.parameters = exceptionParameter; 329 event.parameter_values = new Any[] {exception_value}; 330 331 event.interaction_point = InteractionPoint.STUB_IN_EXCEPTION; 333 334 send(event); 335 } 336 catch (Exception e) 337 { 338 e.printStackTrace(); 340 } 341 } 342 343 private void send(TraceEvent event) 344 { 345 if (traceServer == null) 346 { 347 try 349 { 350 traceServer = i_TraceHelper.narrow(root_nc.resolve(traceServerPath)); 351 } 352 catch (Exception e) 353 { 354 } 355 } 356 if (traceServer != null) 357 { 358 sendQueue.add(event); 359 } 364 } 365 366 private void printEvent(TraceEvent event) 367 { 368 System.err.println("*******************"); 369 System.err.println("object_instance_id: " + event.identity.object_instance_id); 370 System.err.println("object_repository_id: " + event.identity.object_repository_id); 371 System.err.println("interaction_point: " + getInteractionPoint(event.interaction_point.value())); 372 System.err.println("process_id: " + event.identity.process_id); 373 System.err.println("node_name: " + event.identity.node_name); 374 System.err.println("node_ip: " + event.identity.node_ip); 375 System.err.println("trail_id: " + event.trail_id); 376 System.err.println("event_counter: " + event.event_counter); 377 System.err.println("op_name: " + event.op_name); 378 System.err.println("cmp_name: " + event.identity.cmp_name); 379 System.err.println("cmp_type: " + event.identity.cmp_type); 380 System.err.println("cnt_name: " + event.identity.cnt_name); 381 System.err.println("cnt_type: " + event.identity.cnt_type); 382 System.err.println("time_stamp: " + event.time_stamp); 383 System.err.println("thread_id: " + event.thread_id); 384 System.err.println("message_id: " + event.message_id); 385 System.err.println("trail_label: " + event.trail_label); 386 System.err.println("*******************"); 387 } 388 389 private String getInteractionPoint(int v) 390 { 391 switch(v) 392 { 393 case 0: return "STUB_OUT"; 394 case 1: return "POA_IN"; 395 case 2: return "POA_OUT"; 396 case 3: return "POA_OUT_EXCEPTION"; 397 case 4: return "STUB_IN"; 398 case 5: return "STUB_IN_EXCEPTION"; 399 case 6: return "ONEWAY_STUB_OUT"; 400 case 7: return "ONEWAY_POA_IN"; 401 default: return "unknown"; 402 } 403 } 404 405 private String getNamingIor(String namingserver) 406 { 407 String ior = null; 408 try 409 { 410 java.net.URL u = null; 411 try 412 { 413 u = new java.net.URL (namingserver); 415 } 416 catch (java.net.MalformedURLException e1) 417 { 418 419 try 420 { 421 u = new java.net.URL ("http://" + namingserver + "/etc/naming.ior"); 423 } 424 catch (java.net.MalformedURLException e2) 425 { 426 } 427 } 428 BufferedReader in = null; 429 try 430 { 431 in = new BufferedReader(new InputStreamReader(u.openStream())); 432 ior = in.readLine(); 433 } 434 catch (Exception e1) 435 { 436 System.out.println("Failed to find name server!"); 437 return null; 438 } 439 440 while (ior != null && !ior.startsWith("IOR:")) 441 { 442 ior = in.readLine(); 443 } 444 in.close(); 445 } 446 catch (Exception e) 447 { 448 e.printStackTrace(); 449 } 450 return ior; 451 } 452 453 public class Queue 454 { 455 private LinkedList list = new LinkedList(); 456 457 public synchronized void add(TraceEvent event) 458 { 459 list.add(event); 461 notifyAll(); 462 } 463 464 public synchronized TraceEvent[] take() 465 { 466 if (list.size() == 0) 467 { 468 try 469 { 470 wait(); 471 } 472 catch (Exception e) 473 { 474 } 475 } 476 TraceEvent[] all = new TraceEvent[list.size()]; 477 list.toArray(all); 478 list.clear(); 479 return all; 480 } 481 } 482 483 public class SendThread extends Thread 484 { 485 Random random = new Random(); 486 487 public void run() 488 { 489 while(true) 490 { 491 try 492 { 493 TraceEvent[] events = sendQueue.take(); 494 traceServer.receiveEvent(events); 495 } 496 catch (Exception e) 497 { 498 } 499 } 500 } 501 } 502 } | Popular Tags |