1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 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 e) 71 { 72 } 73 } 74 return (TraceEvent[])list.removeFirst(); 75 } 76 } 77 78 public class EventProcessor extends Thread 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 90 { 91 private org.omg.CORBA.ORB orb; 92 93 public OrbMain (org.omg.CORBA.ORB orb) 94 { 95 this.orb = orb; 96 } 97 98 public void run() 99 { 100 orb.run(); 101 } 102 } 103 104 113 void init (String args[]) 114 { 115 try 116 { 117 String ns_name = parseArguments (args); 118 119 args = org.objectweb.openccm.corba.TheORB.initialize(args); 120 121 org.omg.CORBA.ORB orb = org.objectweb.openccm.corba.TheORB.getORB(); 122 123 org.omg.CORBA.Object obj = orb.resolve_initial_references( "RootPOA" ); 124 125 org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(obj); 126 127 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [ 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 childPOA = rootPOA.create_POA("_POA", rootPOA.the_POAManager(), policies ); 134 135 i_TraceImpl impl = new i_TraceImpl(queue); 136 137 byte[] servantId = (new String ("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 nc = org.omg.CosNaming.NamingContextHelper.narrow(obj); 144 145 obj = childPOA.id_to_reference (servantId); 146 147 org.omg.CosNaming.NameComponent [] ncomp = new org.omg.CosNaming.NameComponent [1]; 148 149 ncomp[0] = new org.omg.CosNaming.NameComponent (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 _ex) 159 { 160 _ex.printStackTrace(); 161 162 System.exit(-1); 163 } 164 } 165 166 171 public static void main(String [] 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 os, String query) 184 { 185 187 PrintStream out = new PrintStream (os, true); 188 189 191 out.println ("Content-type: text/plain\r\n"); 192 193 try 194 { 195 197 Hashtable pairs = parseQueryString (query); 198 199 201 if ( !pairs.containsKey("event_count") ) 202 System.out.println ("<?xml version='1.0' encoding='UTF-8'?>"); 203 204 206 DbToXml dbc = new DbToXml (db); 207 208 if (pairs.containsKey("entities")) 209 { 210 StringTokenizer tokenizer = new StringTokenizer((String )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 (tokenizer.nextToken())).longValue(); 219 220 index++; 221 } 222 223 String result = dbc.getXmlIdentities(keys); 224 225 System.out.println (result); 226 } 227 else if (pairs.containsKey("event_count")) 228 { 229 String result = new Long (db.getMessageCount()*2).toString(); 230 231 System.out.println (result); 232 } 233 else if (pairs.containsKey("events") ) 234 { 235 long start = Long.parseLong((String )pairs.get ("start")); 236 int len = Integer.parseInt((String )pairs.get ("length")); 237 238 StringTokenizer tokenizer = new StringTokenizer((String )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 )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 )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 key = new String (); 258 String value = new String (); 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 filter_unmatched = (String )pairs.get ("unmatched_filter"); 272 long hide_to = Long.parseLong((String )pairs.get ("hide_to")); 273 274 String 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 result = dbc.getXmlParameterValues((new Long ((String )pairs.get("key"))).longValue()); 281 282 System.out.println (result); 283 } 284 285 out.close(); 286 } 287 catch (Exception _ex) 288 { 289 291 System.err.println (_ex.getMessage()); 292 } 293 } 294 295 301 String parseArguments (String args[]) 302 { 303 String name = "COACH_TracingServer"; 304 int index; 305 306 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 (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 urlDecode(String in) 347 { 348 StringBuffer out = new StringBuffer (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 (out); 369 } 370 371 Hashtable parseQueryString(String s) 372 { 373 Hashtable pairs = new Hashtable(); 374 375 StringTokenizer pair_tokenizer = new StringTokenizer(s, "&"); 376 377 while (pair_tokenizer.hasMoreTokens()) 378 { 379 String pair = urlDecode(pair_tokenizer.nextToken()); 380 381 StringTokenizer keyval_tokenizer = new StringTokenizer(pair, "="); 382 383 String key = new String (); 384 String value = new String (); 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 |