KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > util > tracing > ClientTraceInterceptor


1 package org.jacorb.util.tracing;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.Calendar JavaDoc;
25 import org.jacorb.orb.portableInterceptor.ClientRequestInfoImpl;
26 import org.jacorb.orb.portableInterceptor.RecursionAwareCI;
27 import org.omg.CORBA.Any JavaDoc;
28 import org.omg.CORBA.TCKind JavaDoc;
29 import org.omg.IOP.Codec JavaDoc;
30 import org.omg.IOP.ServiceContext JavaDoc;
31 import org.omg.PortableInterceptor.ClientRequestInfo JavaDoc;
32 import org.omg.PortableInterceptor.ForwardRequest JavaDoc;
33
34 public class ClientTraceInterceptor
35     extends RecursionAwareCI
36 {
37     private static final int TRACE_POLICY_TYPE = 303;
38     private static final String JavaDoc TRACE = "trace";
39     private static final String JavaDoc OFF = "off";
40
41     private Calendar JavaDoc date;
42     private TracingService tracer;
43     private int myTraceId;
44     private Codec JavaDoc codec;
45
46     private Request JavaDoc current_request = null;
47
48     private int slot_id;
49     private Timer timer;
50
51     public ClientTraceInterceptor(Codec JavaDoc 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     // implementation InterceptorOperations interface
71
public String JavaDoc name()
72     {
73         return "ClientTraceInterceptor";
74     }
75
76     public void destroy()
77     {
78     }
79
80     /**
81      * Add the propagation context to the outgoing message
82      */

83
84     public void do_send_request( ClientRequestInfo JavaDoc ri )
85         throws ForwardRequest JavaDoc
86     {
87         try
88         {
89             // only for requests which return
90
if( ri.response_expected() )
91             {
92                 System.out.println("request: call to op " + ri.operation());
93
94
95                 current_request = new Request JavaDoc( myTraceId,
96                                                ri.effective_target().
97                                                hashCode() << 32 |
98                                                ((myTraceId & 0xffff) << 16) |
99                                                (ri.request_id() & 0xffff));
100
101                 Any JavaDoc any = ri.get_slot(slot_id);
102                 if (any.type().kind().value() != TCKind._tk_null)
103                 {
104                     /*
105                        we are not the initiatiator of the call:
106                        extract the existing request
107                     */

108
109                     Request JavaDoc origin = RequestHelper.extract( any );
110
111                     tracer.registerSubTrace( origin,
112                                              current_request );
113                 }
114
115                 /* insert the context data into an any and then encode
116                    it into the context */

117                 Any JavaDoc ctx_any = ((ClientRequestInfoImpl) ri).orb.create_any();
118                 RequestHelper.insert( ctx_any, current_request );
119                 ServiceContext JavaDoc context =
120                     new ServiceContext JavaDoc ( 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 JavaDoc e)
129     {
130             e.printStackTrace();
131         }
132     }
133
134     public void do_send_poll(ClientRequestInfo JavaDoc ri)
135     {
136     }
137
138     public void do_receive_reply(ClientRequestInfo JavaDoc 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 JavaDoc e)
162     {
163             e.printStackTrace();
164         }
165     }
166
167     public void do_receive_exception(ClientRequestInfo JavaDoc ri)
168         throws ForwardRequest JavaDoc
169     {
170     }
171
172     public void do_receive_other(ClientRequestInfo JavaDoc ri)
173         throws ForwardRequest JavaDoc
174     {
175     }
176
177     private void printTrace( TraceData trace ,
178                              String JavaDoc 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