KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > service > pi > TracingServiceInterceptor


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.tracing.service.pi;
26
27 import org.omg.CORBA.*;
28 import org.omg.IOP.*;
29 import org.omg.IOP.CodecPackage.*;
30 import org.omg.PortableInterceptor.*;
31 import java.util.Hashtable JavaDoc;
32 import org.omg.PortableInterceptor.Current JavaDoc;
33 import org.coach.tracing.api.*;
34 import org.coach.tracing.api.pi.*;
35 import org.coach.tracing.service.*;
36 import org.coach.util.IorPrinter;
37 import org.omg.Dynamic.Parameter JavaDoc;
38 import org.omg.PortableServer.*;
39
40 public class TracingServiceInterceptor extends LocalObject implements ClientRequestInterceptor, ServerRequestInterceptor
41 {
42     private int slotId;
43     private Codec codec;
44     private Current JavaDoc piCurrent;
45     private static boolean ready;
46     private static Sender sender;
47     private static ORB orb;
48     private static Hashtable JavaDoc requestContextTable = new Hashtable JavaDoc();
49
50     private static final int serviceContextId = 100;
51
52     public TracingServiceInterceptor(int slotId)
53     {
54         this.slotId = slotId;
55 //System.err.println("TracingServiceInterceptor slot: " + slotId);
56
}
57
58     void init(Current JavaDoc piCurrent, Codec codec)
59     {
60         this.piCurrent = piCurrent;
61         this.codec = codec;
62     }
63
64     public static void init()
65     {
66         if (sender == null && !Sender.isInitializing())
67         {
68             try
69             {
70                 orb = org.objectweb.openccm.corba.TheORB.getORB();
71                 if (orb != null)
72                 {
73                     sender = Sender.createSender(orb);
74                     ready = true;
75                 }
76             }
77             catch (Throwable JavaDoc e)
78             {
79             }
80         }
81     }
82
83     //
84
// Interceptor operations
85
//
86

87     public String JavaDoc name()
88     {
89         return "TracingServiceInterceptor";
90     }
91
92     public void destroy()
93     {
94     }
95
96     //
97
// ClientRequestInterceptor operations
98
//
99
public void send_request(ClientRequestInfo ri)
100     {
101         // send_request does not execute in the client thread!
102
if (ready && !ri.operation().equals("receiveEvent"))
103         {
104             try
105             {
106                 // Obtain the any containing a ServiceData struct from the PICurrent slot.
107
// This slot was filled by the operations in the local object ServiceImpl.
108
Any any = ri.get_slot(slotId);
109                 if (any != null && any.type().kind().equals(TCKind.tk_string))
110                 {
111                     String JavaDoc key = any.extract_string();
112                     String JavaDoc ior = orb.object_to_string(ri.effective_target());
113                     IorPrinter iorPrinter = new IorPrinter(ior);
114                     String JavaDoc id = iorPrinter.getTypeId();
115                     
116 //System.err.println("TracingServiceInterceptor (" + ri.operation() + ") send_request: " + id);
117
// if (!sender.exclude(id))
118
// {
119
ThreadContext tc = ThreadContext.getThreadContext(key);
120                         tc.setObjectInstanceId("Stub_" + ri.effective_target().hashCode());
121                         tc.setObjectRepositoryId(id);
122                         if (ri.response_expected())
123                         {
124                             Integer JavaDoc rid = new Integer JavaDoc(ri.request_id());
125                             tc.setCurrentOperation(ri.operation());
126                             requestContextTable.put(rid, tc);
127                         }
128                         String JavaDoc name = tc.getThreadId();
129 //System.err.println("stubPreInvoke: " + id + ", " + ri.operation() + ", key: " + key);
130
sender.stubPreInvoke(tc, ri.operation(), null, null, !ri.response_expected());
131     
132                         // insert the updated ServiceData in an any
133
any = orb.create_any();
134                         PropagationContext pc = tc.getPropagationContext();
135                         PropagationContextHelper.insert(any, pc);
136                         // encode the any to a byte array and create a new ServiceContext with it.
137
byte[] serviceContextData = codec.encode(any);
138                         ServiceContext serviceContext = new ServiceContext(serviceContextId, serviceContextData);
139     
140                         // Add the ServiceContext to the outgoing request.
141
ri.add_request_service_context(serviceContext, true);
142                         if (ri.response_expected())
143                         {
144                             tc.pushRequestContext();
145                         }
146 // }
147
}
148             }
149             catch (InvalidSlot e)
150             {
151             }
152             catch (Exception JavaDoc e)
153             {
154                   e.printStackTrace();
155             }
156         }
157     }
158
159     public void send_poll(ClientRequestInfo ri)
160     {
161 // System.err.println("TracingServiceInterceptor (" + ri.operation() + ") send_poll");
162
}
163
164     public void receive_reply(ClientRequestInfo ri)
165     {
166         ThreadContext tc = (ThreadContext)requestContextTable.remove(new Integer JavaDoc(ri.request_id()));
167         if (tc != null)
168         {
169             try
170             {
171                 tc.popRequestContext();
172
173 //System.err.println("stubPostInvoke: " + id + ", " + ri.operation() + ", key: " + name);
174
sender.stubPostInvoke(tc, ri.operation(), null, null, false, false);
175             }
176             catch (Exception JavaDoc e)
177             {
178             }
179         }
180     }
181
182     public void receive_exception(ClientRequestInfo ri)
183     {
184 // System.err.println("TracingServiceInterceptor (" + ri.operation() + ") receive_exception");
185
}
186
187     public void receive_other(ClientRequestInfo ri)
188     {
189 // System.err.println("TracingServiceInterceptor (" + ri.operation() + ") receive_other");
190
}
191
192     //
193
// ServerRequestInterceptor operations
194
//
195
public void receive_request_service_contexts(ServerRequestInfo ri)
196     {
197         init();
198 // System.err.println("TracingServiceInterceptor (" + ri.operation() + ") receive_request_service_contexts");
199

200         if (ready && !ri.operation().equals("receiveEvent"))
201         {
202 // System.err.println("TracingServiceInterceptor (" + ri.operation() + ") receive_request_service_contexts");
203
try
204             {
205                 // Obtain the service context from the request buffer
206
ServiceContext serviceContext = ri.get_request_service_context(serviceContextId);
207                 Any any = codec.decode(serviceContext.context_data);
208                 if (any.type().equal(PropagationContextHelper.type()))
209                 {
210                     // store the any with the ServiceData struct in the PICurrent slot
211
//System.err.println("TracingServiceInterceptor set slot: " + slotId);
212
ri.set_slot(slotId, any);
213                     // After the return of this method, the ORB copies the Request Scope Current to the Thread Scope Current.
214
// The ORB first handles all receive_request_service_contexts methods of all interceptors and then
215
// calls the receive_request methods.
216
}
217             }
218             catch (Exception JavaDoc e)
219             {
220 // e.printStackTrace();
221
}
222         }
223     }
224
225     public void receive_request(ServerRequestInfo ri)
226     {
227 // System.err.println("TraceServiceInterceptor (" + ri.operation() + ") receive_request");
228

229         // This method is called in the same thread as the target operation. See page Chapter 21 21-37 of
230
// the CORBA v2.5 (September 2001) document.
231
// Therefor write access to the Thread Scope Current is possible.
232
if (ready && !ri.operation().equals("receiveEvent"))
233         {
234 // System.err.println("TraceServiceInterceptor (" + ri.operation() + ") receive_request");
235
try
236             {
237                 Any tsc = ri.get_slot(slotId);
238                 PropagationContext pc = null;
239                 if (tsc != null && tsc.type().equal(PropagationContextHelper.type()))
240                 {
241                     // receive_request_service_contexts has copied the ServiceData from the RSC to the TSC
242
// Extract the data and store it in the dataTable using the current thread id as key.
243
// Replace the any in the TSC slot with an any that contains this thread id so that it can
244
// be used as a key for subsequent operations.
245
try
246                     {
247                         pc = PropagationContextHelper.extract(tsc);
248
249                         ThreadContext tc = ThreadContext.getCurrentThreadContext();
250                         String JavaDoc id = ri.target_most_derived_interface();
251                         tc.setCurrentInterfaceId(id);
252                         String JavaDoc key = tc.getThreadId();
253                         // Here the proper component name and type should be set in the ThreadContext.
254
// However, with the PI approach there is no way to get to that information therefore
255
// we use a dummy name.
256
tc.setComponentName("CCM_Component_" + id.hashCode());
257                         tc.setComponentType(id);
258
259                         tc.setObjectInstanceId("Poa_" + id);
260                         tc.setObjectRepositoryId(id);
261                         tc.setPropagationContext(pc);
262
263 //System.err.println("poaPreInvoke: " + id + ", " + ri.operation() + ", key: " + key);
264
sender.poaPreInvoke(tc, ri.operation(), null, null, !ri.response_expected());
265
266                         // Store the request id in the requestIdList so that we can match the reply
267
if (ri.response_expected())
268                         {
269                             Integer JavaDoc rid = new Integer JavaDoc(ri.request_id());
270                             tc.setCurrentOperation(ri.operation());
271                             requestContextTable.put(rid, tc);
272                         }
273
274                         Any any = orb.create_any();
275                         any.insert_string(key);
276                         ri.set_slot(slotId, any);
277                     }
278                     catch (Exception JavaDoc ex)
279                     {
280                         ex.printStackTrace();
281                     }
282                 }
283             }
284             catch (Exception JavaDoc e)
285             {
286                 e.printStackTrace();
287             }
288         }
289     }
290
291     public void send_reply(ServerRequestInfo ri)
292     {
293         // This method is called in the same thread as the target operation. See page Chapter 21 21-37 of
294
// the CORBA v2.5 (September 2001) document.
295
ThreadContext tc = (ThreadContext)requestContextTable.remove(new Integer JavaDoc(ri.request_id()));
296         if (tc != null)
297         {
298             try
299             {
300 //System.err.println("poaPostInvoke: " + id + ", " + ri.operation() + ", key: " + name);
301
sender.poaPostInvoke(tc, ri.operation(), null, null);
302             }
303             catch (Exception JavaDoc e)
304             {
305                 e.printStackTrace();
306             }
307         }
308     }
309
310     public void send_exception(ServerRequestInfo ri)
311     {
312         // not implemented yet
313
// System.err.println("TracingServiceInterceptor (" + ri.operation() + ") send_exception");
314
}
315
316     public void send_other(ServerRequestInfo ri)
317     {
318 // System.err.println("TracingServiceInterceptor (" + ri.operation() + ") send_other");
319
}
320 }
321
Popular Tags