KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > service > Sender


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;
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 JavaDoc[] excludeTable;
39     private i_Trace traceServer = null;
40     public String JavaDoc name = "";
41     private ORB orb = null;
42     private boolean initialized = false;
43     private static boolean initializing = false;
44     private static String JavaDoc hostName;
45     private static String JavaDoc 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; // singleton
53

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 JavaDoc 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                 // first try to resolve the naming context using resolve_initial_references
77
root_nc = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
78             }
79             catch (Exception JavaDoc e1)
80             {
81                 System.err.println("No nameserver found");
82             }
83
84             try
85             {
86                 org.omg.CORBA.Object JavaDoc obj = root_nc.resolve(traceServerPath);
87                 traceServer = i_TraceHelper.narrow(obj);
88             }
89             catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc id)
113     {
114         if (excludeTable == null)
115         {
116             // initialize the exclude table
117
Vector v = new Vector();
118             String JavaDoc 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 JavaDoc 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 JavaDoc[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 JavaDoc e)
171         {
172             e.printStackTrace();
173         }
174         return traceEvent;
175     }
176
177     public synchronized void stubPreInvoke(ThreadContext tc, String JavaDoc op_name, Parameter[] parameters, Any[] parameter_values, boolean isOneway)
178     {
179         try
180         {
181             //generate the event generic part
182
TraceEvent event = createEvent(tc);
183             // Message Id is set only on stub pre invoke.
184
tc.setMessageId(tc.getVmId() + "_" + tc.getEventCounter());
185             event.message_id = tc.getMessageId() + "F";
186
187             //Add the interaction specific fields
188
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 JavaDoc e)
205         {
206             e.printStackTrace();
207         }
208     }
209        
210     public synchronized void poaPreInvoke(ThreadContext tc, String JavaDoc op_name, Parameter[] parameters, Any[] parameter_values, boolean isOneway)
211     {
212         try
213         {
214             //generate the event generic part
215
TraceEvent event = createEvent(tc);
216             event.message_id = tc.getMessageId() + "F";
217             //Add the interaction specific fields
218
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 JavaDoc e)
235         {
236             //problems...
237
e.printStackTrace();
238         }
239     }
240
241     public synchronized void poaPostInvoke(ThreadContext tc, String JavaDoc 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             //Add the interaction specific fields
255
event.interaction_point = InteractionPoint.POA_OUT;
256
257             send(event);
258         }
259         catch (Exception JavaDoc e)
260         {
261             //problems...
262
e.printStackTrace();
263         }
264     }
265
266     public synchronized void poaPostException(ThreadContext tc, String JavaDoc op_name, Any exception_value)
267     {
268         try
269         {
270             //generate the event generic part
271
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             //Add the interaction specific fields
278
event.interaction_point = InteractionPoint.POA_OUT_EXCEPTION;
279
280             send(event);
281         }
282         catch (Exception JavaDoc e)
283         {
284             //problems...
285
e.printStackTrace();
286         }
287     }
288
289     public synchronized void stubPostInvoke(ThreadContext tc, String JavaDoc op_name, Parameter[] parameters, Any[] parameter_values, boolean isExceptional, boolean isOneway)
290     {
291         if (!isOneway)
292         {
293             //the previous call to the notification handler in the stub has done the filtering, if any
294
try
295             {
296                 //generate the event generic part
297
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                 //Add the interaction specific fields
307
event.interaction_point = InteractionPoint.STUB_IN;
308             
309                 send(event);
310             }
311             catch (Exception JavaDoc e)
312             {
313                 //problems...
314
e.printStackTrace();
315             }
316         }
317     }
318     
319     public synchronized void stubPostException(ThreadContext tc, String JavaDoc op_name, Any exception_value)
320     {
321         //the previous call to the notification handler in the stub has done the filtering, if any
322
try
323         {
324             //generate the event generic part
325
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             //Add the interaction specific fields
332
event.interaction_point = InteractionPoint.STUB_IN_EXCEPTION;
333
334             send(event);
335         }
336         catch (Exception JavaDoc e)
337         {
338             //problems...
339
e.printStackTrace();
340         }
341     }
342
343     private void send(TraceEvent event)
344     {
345         if (traceServer == null)
346         {
347             // Check for the tracing server, it may have been started after this container.
348
try
349             {
350                 traceServer = i_TraceHelper.narrow(root_nc.resolve(traceServerPath));
351             }
352             catch (Exception JavaDoc e)
353             {
354             }
355         }
356         if (traceServer != null)
357         {
358             sendQueue.add(event);
359 // TraceEvent[] events = new TraceEvent[1];
360
// events[0] = event;
361
//
362
// traceServer.receiveEvent(events);
363
}
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 JavaDoc 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 JavaDoc getNamingIor(String JavaDoc namingserver)
406     {
407         String JavaDoc ior = null;
408         try
409         {
410             java.net.URL JavaDoc u = null;
411             try
412             {
413                 // check for a normal URL
414
u = new java.net.URL JavaDoc(namingserver);
415             }
416             catch (java.net.MalformedURLException JavaDoc e1)
417             {
418     
419                 try
420                 {
421                     // our last chance is that it's a plain old style hostname
422
u = new java.net.URL JavaDoc("http://" + namingserver + "/etc/naming.ior");
423                 }
424                 catch (java.net.MalformedURLException JavaDoc 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 JavaDoc 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 JavaDoc 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 //printEvent(event);
460
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 JavaDoc 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 JavaDoc
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 JavaDoc e)
497                 {
498                 }
499             }
500         }
501     }
502 }
Popular Tags