1 package org.jacorb.util.tracing; 2 3 23 24 import java.util.Calendar ; 25 import org.jacorb.orb.portableInterceptor.ClientRequestInfoImpl; 26 import org.jacorb.orb.portableInterceptor.RecursionAwareCI; 27 import org.omg.CORBA.Any ; 28 import org.omg.CORBA.TCKind ; 29 import org.omg.IOP.Codec ; 30 import org.omg.IOP.ServiceContext ; 31 import org.omg.PortableInterceptor.ClientRequestInfo ; 32 import org.omg.PortableInterceptor.ForwardRequest ; 33 34 public class ClientTraceInterceptor 35 extends RecursionAwareCI 36 { 37 private static final int TRACE_POLICY_TYPE = 303; 38 private static final String TRACE = "trace"; 39 private static final String OFF = "off"; 40 41 private Calendar date; 42 private TracingService tracer; 43 private int myTraceId; 44 private Codec codec; 45 46 private Request current_request = null; 47 48 private int slot_id; 49 private Timer timer; 50 51 public ClientTraceInterceptor(Codec codec, int slot_id, 52 TracingService tracer) 53 { 54 super(true); 55 56 date = Calendar.getInstance(); 57 58 this.tracer = tracer; 59 this.codec = codec; 60 this.slot_id = slot_id; 61 62 myTraceId = tracer.get_id(); 63 timer = new Timer(); 64 65 System.out.println(" ********************************** "); 66 System.out.println(" My id: " + myTraceId); 67 System.out.println(" ********************************** "); 68 } 69 70 public String name() 72 { 73 return "ClientTraceInterceptor"; 74 } 75 76 public void destroy() 77 { 78 } 79 80 83 84 public void do_send_request( ClientRequestInfo ri ) 85 throws ForwardRequest 86 { 87 try 88 { 89 if( ri.response_expected() ) 91 { 92 System.out.println("request: call to op " + ri.operation()); 93 94 95 current_request = new Request ( myTraceId, 96 ri.effective_target(). 97 hashCode() << 32 | 98 ((myTraceId & 0xffff) << 16) | 99 (ri.request_id() & 0xffff)); 100 101 Any any = ri.get_slot(slot_id); 102 if (any.type().kind().value() != TCKind._tk_null) 103 { 104 108 109 Request origin = RequestHelper.extract( any ); 110 111 tracer.registerSubTrace( origin, 112 current_request ); 113 } 114 115 117 Any ctx_any = ((ClientRequestInfoImpl) ri).orb.create_any(); 118 RequestHelper.insert( ctx_any, current_request ); 119 ServiceContext context = 120 new ServiceContext ( TracingContextID.value, 121 codec.encode( ctx_any ) ); 122 123 timer.start( ri.request_id(), ri.target() ); 124 125 ri.add_request_service_context( context, true ); 126 } 127 } 128 catch( Exception e) 129 { 130 e.printStackTrace(); 131 } 132 } 133 134 public void do_send_poll(ClientRequestInfo ri) 135 { 136 } 137 138 public void do_receive_reply(ClientRequestInfo ri) 139 { 140 try 141 { 142 143 System.out.println("reply: return from op " + ri.operation()); 144 145 long t = timer.stop( ri.request_id(), ri.target()); 146 147 tracer.logTraceAtPoint( current_request, 148 ri.operation(), 149 t, 150 (long) 0); 151 152 TraceData trace = tracer.getTrace( current_request ); 153 154 System.out.println("-- Trace for request " + 155 ri.operation() + 156 " (rid:" + ri.request_id() + 157 ") -- "); 158 159 printTrace( trace, "" ); 160 } 161 catch( Exception e) 162 { 163 e.printStackTrace(); 164 } 165 } 166 167 public void do_receive_exception(ClientRequestInfo ri) 168 throws ForwardRequest 169 { 170 } 171 172 public void do_receive_other(ClientRequestInfo ri) 173 throws ForwardRequest 174 { 175 } 176 177 private void printTrace( TraceData trace , 178 String prefix ) 179 { 180 System.out.println(prefix + " Request originator: " + 181 trace.tracer_id); 182 183 System.out.println(prefix + " Operation: " + 184 trace.operation); 185 186 System.out.println(prefix + " Time: " 187 + trace.client_time + " msecs" ); 188 189 for( int i = 0; i < trace.subtrace.length; i++ ) 190 { 191 System.out.println(prefix + "\tsubtrace " + i + " >>>"); 192 193 printTrace( trace.subtrace[i], 194 prefix + '\t' ); 195 196 System.out.println(prefix + "\t<<< subtrace " + i); 197 } 198 } 199 } 200 | Popular Tags |