KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > server > ServerMain


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): Wim Hellenthal */
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
26 package org.coach.tracing.server;
27
28 import com.fastcgi.FCGIInterface;
29 import org.coach.tracing.api.TraceEvent;
30 import java.io.*;
31 import java.util.*;
32
33 public class ServerMain
34 {
35     private Queue queue = new Queue();
36     private EventDataBase db;
37
38     public ServerMain ()
39     {
40         db = EventDataBase.getEventDB();
41
42         if (System.getProperty("coach.tracing.viewer", "true").equals("true"))
43         {
44             // Show the internal tracing viewer
45
org.coach.tracing.server.viewer.TraceViewerFrame viewer = new org.coach.tracing.server.viewer.TraceViewerFrame();
46             viewer.setEventDB(db);
47             viewer.show();
48         }
49     }
50
51     public class Queue
52     {
53         private LinkedList list = new LinkedList();
54
55         public synchronized void add(TraceEvent[] events)
56         {
57             list.add(events);
58
59             notifyAll();
60         }
61
62         public synchronized TraceEvent[] take()
63         {
64             if (list.size() == 0)
65             {
66                 try
67                 {
68                     wait();
69                 }
70                 catch (Exception JavaDoc e)
71                 {
72                 }
73             }
74             return (TraceEvent[])list.removeFirst();
75         }
76     }
77
78     public class EventProcessor extends Thread JavaDoc
79     {
80         public void run()
81         {
82             while(true)
83             {
84                 db.addEvents(queue.take());
85             }
86         }
87     }
88
89     public class OrbMain extends Thread JavaDoc
90     {
91         private org.omg.CORBA.ORB JavaDoc orb;
92
93         public OrbMain (org.omg.CORBA.ORB JavaDoc orb)
94         {
95             this.orb = orb;
96         }
97
98         public void run()
99         {
100             orb.run();
101         }
102     }
103
104     /**
105      * This routine initializes the ORB, registers the i_Trace interface at the Name Service, and starts
106      * event processing. It also prints information to the standard output stream This is needed because the FCGI
107      * module will reassigns the standard input, output and error stream. If there are no print statements,
108      * the launch script (at least for the Windows env.) will keep on testing for a valid TracingServer.output.tmp
109      * file.
110      *
111      * @param args The command line arguments.
112      */

113     void init (String JavaDoc args[])
114     {
115         try
116         {
117             String JavaDoc ns_name = parseArguments (args);
118
119             args = org.objectweb.openccm.corba.TheORB.initialize(args);
120
121             org.omg.CORBA.ORB JavaDoc orb = org.objectweb.openccm.corba.TheORB.getORB();
122
123             org.omg.CORBA.Object JavaDoc obj = orb.resolve_initial_references( "RootPOA" );
124
125             org.omg.PortableServer.POA JavaDoc rootPOA = org.omg.PortableServer.POAHelper.narrow(obj);
126
127             org.omg.CORBA.Policy JavaDoc [] policies = new org.omg.CORBA.Policy JavaDoc[ 3 ];
128
129             policies[0] = rootPOA.create_id_uniqueness_policy(org.omg.PortableServer.IdUniquenessPolicyValue.MULTIPLE_ID );
130             policies[1] = rootPOA.create_request_processing_policy(org.omg.PortableServer.RequestProcessingPolicyValue.USE_DEFAULT_SERVANT);
131             policies[2] = rootPOA.create_id_assignment_policy(org.omg.PortableServer.IdAssignmentPolicyValue.USER_ID );
132
133             org.omg.PortableServer.POA JavaDoc childPOA = rootPOA.create_POA("_POA", rootPOA.the_POAManager(), policies );
134
135             i_TraceImpl impl = new i_TraceImpl(queue);
136
137             byte[] servantId = (new String JavaDoc("i_Trace")).getBytes();
138
139             childPOA.activate_object_with_id (servantId, impl);
140
141             obj = orb.resolve_initial_references("NameService");
142
143             org.omg.CosNaming.NamingContext JavaDoc nc = org.omg.CosNaming.NamingContextHelper.narrow(obj);
144
145             obj = childPOA.id_to_reference (servantId);
146
147             org.omg.CosNaming.NameComponent JavaDoc[] ncomp = new org.omg.CosNaming.NameComponent JavaDoc[1];
148
149             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc(ns_name, "");
150
151             nc.rebind(ncomp, obj);
152
153             rootPOA.the_POAManager().activate();
154
155             new EventProcessor().start();
156             new OrbMain(orb).start();
157         }
158         catch (Exception JavaDoc _ex)
159         {
160             _ex.printStackTrace();
161
162             System.exit(-1);
163         }
164     }
165
166     /**
167      * The main method. This method initializes the server and handles the web server FCGI requests.
168      *
169      * @param args The command line arguments.
170      */

171     public static void main(String JavaDoc[] args)
172     {
173         ServerMain application = new ServerMain();
174
175         application.init(args);
176
177         while(new FCGIInterface().FCGIaccept()>= 0)
178         {
179             application.handleFCGIRequest (System.out, System.getProperty ("QUERY_STRING"));
180         }
181     }
182
183     void handleFCGIRequest (java.io.OutputStream JavaDoc os, String JavaDoc query)
184     {
185         // create output print stream
186

187         PrintStream out = new PrintStream (os, true);
188
189         // set content type
190

191         out.println ("Content-type: text/plain\r\n");
192
193         try
194         {
195             // parse query string
196

197             Hashtable pairs = parseQueryString (query);
198
199             // the event_count request is the only request not returning XML
200

201             if ( !pairs.containsKey("event_count") )
202                 System.out.println ("<?xml version='1.0' encoding='UTF-8'?>");
203
204             // handle requests
205

206             DbToXml dbc = new DbToXml (db);
207
208             if (pairs.containsKey("entities"))
209             {
210                 StringTokenizer tokenizer = new StringTokenizer((String JavaDoc)pairs.get("keys"));
211
212                 long keys[] = new long[tokenizer.countTokens()];
213
214                 int index = 0;
215
216                 while (tokenizer.hasMoreTokens())
217                 {
218                     keys[index] = (new Long JavaDoc(tokenizer.nextToken())).longValue();
219
220                     index++;
221                 }
222
223                 String JavaDoc result = dbc.getXmlIdentities(keys);
224
225                 System.out.println (result);
226             }
227             else if (pairs.containsKey("event_count"))
228             {
229                 String JavaDoc result = new Long JavaDoc (db.getMessageCount()*2).toString();
230
231                 System.out.println (result);
232             }
233             else if (pairs.containsKey("events") )
234             {
235                 long start = Long.parseLong((String JavaDoc)pairs.get ("start"));
236                 int len = Integer.parseInt((String JavaDoc)pairs.get ("length"));
237
238                 StringTokenizer tokenizer = new StringTokenizer((String JavaDoc)pairs.get("operation_filter"));
239                 Hashtable operation_filter = new Hashtable();
240
241                 while (tokenizer.hasMoreTokens())
242                     operation_filter.put (tokenizer.nextToken(),"");
243
244                 tokenizer = new StringTokenizer((String JavaDoc)pairs.get("entity_filter"));
245                 Hashtable entity_filter = new Hashtable();
246
247                 while (tokenizer.hasMoreTokens())
248                     entity_filter.put(tokenizer.nextToken(),"");
249
250                 tokenizer = new StringTokenizer((String JavaDoc)pairs.get("toself_filter"));
251                 Hashtable toself_filter = new Hashtable();
252
253                 while (tokenizer.hasMoreTokens())
254                 {
255                     StringTokenizer collapsed_node = new StringTokenizer(tokenizer.nextToken(), ":");
256
257                     String JavaDoc key = new String JavaDoc();
258                     String JavaDoc value = new String JavaDoc();
259
260                     if (collapsed_node.hasMoreTokens())
261                         key = collapsed_node.nextToken();
262                     else;
263
264                     if (collapsed_node.hasMoreTokens())
265                         value = collapsed_node.nextToken();
266                     else;
267
268                     toself_filter.put(key,value);
269                 }
270
271                 String JavaDoc filter_unmatched = (String JavaDoc)pairs.get ("unmatched_filter");
272                 long hide_to = Long.parseLong((String JavaDoc)pairs.get ("hide_to"));
273
274                 String JavaDoc result = dbc.getXmlEvents(start,len, operation_filter, entity_filter, toself_filter, filter_unmatched, hide_to);
275
276                 System.out.println (result);
277             }
278             else if (pairs.containsKey("parameters") )
279             {
280                 String JavaDoc result = dbc.getXmlParameterValues((new Long JavaDoc((String JavaDoc)pairs.get("key"))).longValue());
281
282                 System.out.println (result);
283             }
284
285             out.close();
286         }
287         catch (Exception JavaDoc _ex)
288         {
289             // print exception message to web server log file
290

291             System.err.println (_ex.getMessage());
292         }
293     }
294
295     /**
296      * Parses two program options in random order. -ns_name <name> is the name used to register at the name service,
297      * -fcgi_port <number> is the port the server will listen for web server FCGI request.
298      *
299      * @param args The command line arguments.
300      */

301     String JavaDoc parseArguments (String JavaDoc args[])
302     {
303         String JavaDoc name = "COACH_TracingServer";
304         int index;
305
306         // if there are no arguments we need to print at least one line.
307

308         System.out.println (".................................");
309
310         for (index = 0; index < args.length; index++)
311         {
312             if (args[index].charAt(0) == '-')
313             {
314                 if ( args[index].equals ("-ns_name") )
315                 {
316                     if ( (++index == args.length) || (args[index].charAt(0) == '-') )
317                         System.out.println ("-ns_name option missing argument, using default name '" + name + "'");
318                     else
319                     {
320                         System.out.println ("Registering server using name: " + args[index]);
321
322                         name = new String JavaDoc(args[index]);
323                     }
324                 }
325                 else if ( args[index].equals ("-fcgi_port") )
326                 {
327                     if ( (++index == args.length) || (args[index].charAt(0) == '-') )
328                         System.out.println ("-fcgi_port option missing argument, using default port " + System.getProperty ("FCGI_PORT"));
329                     else
330                     {
331                         System.out.println ("Using FCGI port " + args[index]);
332
333                         System.setProperty ("FCGI_PORT", args[index]);
334                     }
335                 }
336                 else
337                     System.out.println ("Illegal option " + args[index]);
338             }
339             else
340                 break;
341         }
342
343         return name;
344     }
345
346     String JavaDoc urlDecode(String JavaDoc in)
347     {
348         StringBuffer JavaDoc out = new StringBuffer JavaDoc(in.length());
349         int i = 0;
350         int j = 0;
351
352         while (i < in.length())
353         {
354             char ch = in.charAt(i);
355             i++;
356
357             if (ch == '+')
358                 ch = ' ';
359             else if (ch == '%')
360             {
361                 ch = (char)Integer.parseInt(in.substring(i,i+2), 16);
362                 i+=2;
363             }
364             out.append(ch);
365             j++;
366         }
367
368         return new String JavaDoc(out);
369     }
370
371     Hashtable parseQueryString(String JavaDoc s)
372     {
373         Hashtable pairs = new Hashtable();
374
375         StringTokenizer pair_tokenizer = new StringTokenizer(s, "&");
376
377         while (pair_tokenizer.hasMoreTokens())
378         {
379             String JavaDoc pair = urlDecode(pair_tokenizer.nextToken());
380
381             StringTokenizer keyval_tokenizer = new StringTokenizer(pair, "=");
382
383             String JavaDoc key = new String JavaDoc();
384             String JavaDoc value = new String JavaDoc();
385
386             if (keyval_tokenizer.hasMoreTokens())
387                 key = keyval_tokenizer.nextToken();
388             else;
389
390             if (keyval_tokenizer.hasMoreTokens())
391                 value = keyval_tokenizer.nextToken();
392             else;
393
394             pairs.put(key,value);
395         }
396
397         return pairs;
398     }
399 }
400
401
Popular Tags