001: package org.jacorb.util.tracing;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1999-2004 Gerald Brose
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: import org.jacorb.orb.portableInterceptor.ClientRequestInfoImpl;
025: import org.jacorb.orb.portableInterceptor.RecursionAwareCI;
026: import org.omg.CORBA.Any;
027: import org.omg.CORBA.TCKind;
028: import org.omg.IOP.Codec;
029: import org.omg.IOP.ServiceContext;
030: import org.omg.PortableInterceptor.ClientRequestInfo;
031: import org.omg.PortableInterceptor.ForwardRequest;
032:
033: public class ClientTraceInterceptor extends RecursionAwareCI {
034: private TracingService tracer;
035: private int myTraceId;
036: private Codec codec;
037:
038: private Request current_request = null;
039:
040: private int slot_id;
041: private Timer timer;
042:
043: public ClientTraceInterceptor(Codec codec, int slot_id,
044: TracingService tracer) {
045: super (true);
046:
047: this .tracer = tracer;
048: this .codec = codec;
049: this .slot_id = slot_id;
050:
051: myTraceId = tracer.get_id();
052: timer = new Timer();
053:
054: System.out.println(" ********************************** ");
055: System.out.println(" My id: " + myTraceId);
056: System.out.println(" ********************************** ");
057: }
058:
059: // implementation InterceptorOperations interface
060: public String name() {
061: return "ClientTraceInterceptor";
062: }
063:
064: public void destroy() {
065: }
066:
067: /**
068: * Add the propagation context to the outgoing message
069: */
070:
071: public void do_send_request(ClientRequestInfo ri)
072: throws ForwardRequest {
073: try {
074: // only for requests which return
075: if (ri.response_expected()) {
076: System.out.println("request: call to op "
077: + ri.operation());
078:
079: current_request = new Request(myTraceId, ri
080: .effective_target().hashCode() << 32
081: | ((myTraceId & 0xffff) << 16)
082: | (ri.request_id() & 0xffff));
083:
084: Any any = ri.get_slot(slot_id);
085: if (any.type().kind().value() != TCKind._tk_null) {
086: /*
087: we are not the initiatiator of the call:
088: extract the existing request
089: */
090:
091: Request origin = RequestHelper.extract(any);
092:
093: tracer.registerSubTrace(origin, current_request);
094: }
095:
096: /* insert the context data into an any and then encode
097: it into the context */
098: Any ctx_any = ((ClientRequestInfoImpl) ri).orb
099: .create_any();
100: RequestHelper.insert(ctx_any, current_request);
101: ServiceContext context = new ServiceContext(
102: TracingContextID.value, codec.encode(ctx_any));
103:
104: timer.start(ri.request_id(), ri.target());
105:
106: ri.add_request_service_context(context, true);
107: }
108: } catch (Exception e) {
109: e.printStackTrace();
110: }
111: }
112:
113: public void do_send_poll(ClientRequestInfo ri) {
114: }
115:
116: public void do_receive_reply(ClientRequestInfo ri) {
117: try {
118:
119: System.out.println("reply: return from op "
120: + ri.operation());
121:
122: long t = timer.stop(ri.request_id(), ri.target());
123:
124: tracer.logTraceAtPoint(current_request, ri.operation(), t,
125: 0);
126:
127: TraceData trace = tracer.getTrace(current_request);
128:
129: System.out.println("-- Trace for request " + ri.operation()
130: + " (rid:" + ri.request_id() + ") -- ");
131:
132: printTrace(trace, "");
133: } catch (Exception e) {
134: e.printStackTrace();
135: }
136: }
137:
138: public void do_receive_exception(ClientRequestInfo ri)
139: throws ForwardRequest {
140: }
141:
142: public void do_receive_other(ClientRequestInfo ri)
143: throws ForwardRequest {
144: }
145:
146: private void printTrace(TraceData trace, String prefix) {
147: System.out.println(prefix + " Request originator: "
148: + trace.tracer_id);
149:
150: System.out.println(prefix + " Operation: " + trace.operation);
151:
152: System.out.println(prefix + " Time: " + trace.client_time
153: + " msecs");
154:
155: for (int i = 0; i < trace.subtrace.length; i++) {
156: System.out.println(prefix + "\tsubtrace " + i + " >>>");
157:
158: printTrace(trace.subtrace[i], prefix + '\t');
159:
160: System.out.println(prefix + "\t<<< subtrace " + i);
161: }
162: }
163: }
|