1 57 58 package org.apache.wsif.util.jms; 59 60 import inout.wsiftypes.InoutImpl; 61 import inout.wsiftypes.Mutablestring; 62 import java.io.BufferedReader ; 63 import java.io.FileReader ; 64 import java.io.IOException ; 65 import java.io.Serializable ; 66 import java.util.Date ; 67 import java.util.Enumeration ; 68 import java.util.HashMap ; 69 70 import javax.jms.JMSException ; 71 import javax.jms.Message ; 72 import javax.jms.ObjectMessage ; 73 import javax.jms.Queue ; 74 import javax.jms.QueueReceiver ; 75 import javax.jms.TextMessage ; 76 77 import org.apache.wsif.WSIFConstants; 78 import org.apache.wsif.WSIFException; 79 import org.apache.wsif.logging.Trc; 80 import stockquote.wsiftypes.StockQuote; 81 import util.TestUtilities; 82 83 import addressbook.wsiftypes.Address; 84 import addressbook.wsiftypes.AddressBook; 85 86 89 public class NativeJMSRequestListener extends JMS2HTTPBridgeDestination { 90 int counter = 0; 91 static final String startType = JMS2HTTPBridgeDestination.COLDSTART; 92 static final boolean VERBOSE = TestUtilities.isJmsVerbose(); 93 94 private Thread listenerThread; 95 96 static AddressBook ab = new AddressBook(); 97 static StockQuote sq = new StockQuote(); 98 static InoutImpl inout = new InoutImpl(); 99 100 static final Object LOCK = new Lock(); 101 static class Lock {} 102 103 private WSIFJMSListener list = new WSIFJMSListener() { 104 public void onException(JMSException arg1) { 105 Trc.entry(this, arg1); 106 arg1.printStackTrace(); 107 Trc.exit(); 108 } 109 110 public void onMessage(Message message) { 111 try { 112 Trc.entry(this, message); 113 synchronized ( getLock() ) { 114 processResponse(message); 115 } 116 Trc.exit(); 117 } catch (Exception ex) { 118 ex.printStackTrace(); 119 } 120 } 121 }; 122 123 124 public NativeJMSRequestListener(String msgQ) throws WSIFException { 125 super( 126 new WSIFJMSFinderForJndi( 127 null, 128 TestUtilities.getWsifProperty( 129 "wsif.jms2httpbridge.initialcontextfactory"), 130 TestUtilities.getWsifProperty( 131 "wsif.jms2httpbridge.jndiproviderurl"), 132 WSIFJMSFinder.STYLE_QUEUE, 133 TestUtilities.getWsifProperty( 134 "wsif.jms2httpbridge.jndiconnectionfactoryname"), 135 msgQ, 136 null), 137 null, 138 WSIFJMSConstants.WAIT_FOREVER, 139 startType, 140 VERBOSE); 141 142 listenerThread = new Thread () { 143 public void run() { 144 try { 145 listen( list ); 146 } catch (WSIFException ex) { 147 ex.printStackTrace(); 148 } 149 } 150 }; 151 listenerThread.start(); 152 } 153 159 public void listen(WSIFJMSListener listener, Queue queue) 160 throws WSIFException { 161 Trc.entry(this, listener, queue); 162 areWeClosed(); 163 164 try { 165 QueueReceiver qr = session.createReceiver(queue); 166 qr.setMessageListener(listener); 167 connection.setExceptionListener(listener); 168 169 connection.start(); 170 171 for (int i = 1; !Thread.interrupted(); i++) { 172 Thread.yield(); 173 Thread.sleep(5000); 174 if (VERBOSE) System.out.println("JMSAsyncListener waiting... " + i); 175 } 176 } catch (JMSException je) { 177 je.printStackTrace(); 178 throw new WSIFException( je.getMessage() ); 179 } catch (InterruptedException ignored) { 180 if (VERBOSE) System.out.println("JMSAsyncListener Exitting"); 181 } 182 Trc.exit(); 183 } 184 185 public void stop() { 186 listenerThread.interrupt(); 187 } 188 189 private void processResponse(Message msg) { 190 if (VERBOSE) System.out.println( "NativeJMSRequestListener got msg:" + msg ); 191 try { 192 String operationName = null; 193 String input = null; 194 String output = null; 195 String fake = null; 196 try { 197 operationName = 198 msg.getStringProperty( 199 WSIFConstants.JMS_PROP_OPERATION_NAME); 200 input = 201 msg.getStringProperty(WSIFConstants.JMS_PROP_INPUT_NAME); 202 output = 203 msg.getStringProperty(WSIFConstants.JMS_PROP_OUTPUT_NAME); 204 fake = msg.getStringProperty("WSIF_FAKE"); 205 } catch (javax.jms.JMSException e) { 206 } 207 208 Object reply = null; 209 Object dummyReply = "input only, so no reply"; 210 if ( fake != null ) { 211 sendReply( msg, doFakeOp( fake ) ); 212 } else if ( "getQuote".equals( operationName ) ) { 213 reply = new Float ( sqGetQuote( (ObjectMessage )msg ) ); 214 sendReply( msg, reply ); 215 } else if ( "AddEntryFirstAndLastNamesRequest".equals( input ) ) { 216 abAddEntryFL( (ObjectMessage ) msg ); 217 sendReply( msg, dummyReply ); } else if ( "addEntry".equals( operationName ) ) { 219 abAddEntry( (ObjectMessage ) msg ); 220 sendReply( msg, dummyReply ); } else if ( "addEntryWholeName".equals( operationName ) ) { 222 abAddEntry( (ObjectMessage ) msg ); 223 sendReply( msg, dummyReply ); } else if ( "addEntryUserProp".equals( operationName ) ) { 225 abAddEntry( (ObjectMessage ) msg ); 226 sendReply( msg, dummyReply ); } else if ( "addEntryJmsProp".equals( operationName ) ) { 228 abAddEntry( (ObjectMessage ) msg ); 229 sendReply( msg, dummyReply ); } else if ( "addEntryFirstAndLastNames".equals( operationName ) ) { 231 abAddEntryFL( (ObjectMessage ) msg ); 232 sendReply( msg, dummyReply ); } else if ( "GetAddressFromNameMSRequest".equals( input ) ) { 234 reply = inoutGetAddressFromName( (ObjectMessage ) msg ); 235 sendReply( msg, reply ); 236 } else if ( "getAddressFromName".equals( operationName ) ) { 237 reply = abGetAddressFromName( (ObjectMessage ) msg ); 238 sendReply( msg, reply ); 239 } else if ( "getAddressFromName".equals( operationName ) ) { 240 reply = abGetAddressFromName( (ObjectMessage ) msg ); 241 sendReply( msg, reply ); 242 } else if ( "getAddressFromNameMS".equals( operationName ) ) { 243 reply = inoutGetAddressFromName( (ObjectMessage ) msg ); 244 sendReply( msg, reply ); 245 } else if ( "addNumbers".equals( operationName ) ) { 246 reply = inoutAddNumbers( (ObjectMessage ) msg ); 247 sendReply( msg, reply ); 248 } else if ( "getDate".equals( operationName ) ) { 249 reply = inoutGetDate( (ObjectMessage ) msg ); 250 sendReply( msg, reply ); 251 } else if ( "whoamiString".equals( operationName ) ) { 252 reply = inoutWhoami( (ObjectMessage ) msg ); 253 sendReply( msg, reply ); 254 } else if ( "whoamiFloat".equals( operationName ) ) { 255 reply = inoutWhoami( (ObjectMessage ) msg ); 256 sendReply( msg, reply ); 257 } else if ( "whoamiInt".equals( operationName ) ) { 258 reply = inoutWhoami( (ObjectMessage ) msg ); 259 sendReply( msg, reply ); 260 } else if ( "whoamiAddress".equals( operationName ) ) { 261 reply = inoutWhoami( (ObjectMessage ) msg ); 262 sendReply( msg, reply ); 263 } else if ( "inoutArgs".equals( operationName ) ) { 264 reply = inoutArgs( (ObjectMessage ) msg ); 265 sendReply( msg, reply ); 266 } else if ( "whoami".equals( operationName ) ) { 267 reply = inoutWhoami( (ObjectMessage ) msg ); 268 sendReply( msg, reply ); 269 } else if (operationName.startsWith("throw")) { 270 throwFault(operationName, (ObjectMessage ) msg); 271 } else { 272 System.err.println("unknown operation: " + operationName); 273 } 274 } catch (Exception ex) { 275 ex.printStackTrace(); 276 } 277 } 278 279 private void abAddEntry(ObjectMessage msg) throws JMSException { 280 HashMap hm = (HashMap ) msg.getObject(); 281 String name = (String )hm.get( "name" ); 282 Address addr = (Address)hm.get( "address" ); 283 ab.addEntry( name, addr ); 284 inout.addEntry( name, addr ); 285 } 286 287 private void abAddEntryFL(ObjectMessage msg) throws JMSException { 288 HashMap hm = (HashMap ) msg.getObject(); 289 String first = (String )hm.get( "firstName" ); 290 String last = (String )hm.get( "lastName" ); 291 Address addr = (Address)hm.get( "address" ); 292 ab.addEntry( first, last, addr ); 293 inout.addEntry( first, last, addr ); 294 } 295 296 private Address abGetAddressFromName(ObjectMessage msg) throws JMSException { 297 String name = (String )msg.getObject(); 298 return ab.getAddressFromName( name ); 299 } 300 301 private float sqGetQuote(ObjectMessage msg) throws Exception { 302 String name = (String ) msg.getObject(); 303 return sq.getQuote( name ); 304 } 305 306 private Address inoutGetAddressFromName(ObjectMessage msg) throws JMSException { 307 Mutablestring name = (Mutablestring)msg.getObject(); 308 return inout.getAddressFromName( name ); 309 } 310 311 private Date inoutGetDate(ObjectMessage msg) throws JMSException { 312 return inout.getDate(); 313 } 314 315 private String inoutWhoami(ObjectMessage msg) throws JMSException { 316 Object o = null; 317 o = msg.getObject(); 318 if ( o instanceof String ) { 319 return inout.whoami( (String ) o ); 320 } 321 o = msg.getObject(); 322 if ( o instanceof Address ) { 323 return inout.whoami( (Address) o ); 324 } 325 o = msg.getObject(); 326 if ( o instanceof Integer ) { 327 return inout.whoami( ((Integer )o).intValue() ); 328 } 329 o = msg.getObject(); 330 if ( o instanceof Float ) { 331 return inout.whoami( ((Float )o).floatValue() ); 332 } 333 return "errror - unknown obj"; 334 } 335 336 private Integer inoutAddNumbers(ObjectMessage msg) throws JMSException { 337 int[] nums = (int[])msg.getObject(); 338 return new Integer ( inout.addNumbers( nums ) ); 339 } 340 341 private HashMap inoutArgs(ObjectMessage msg) throws JMSException { 342 HashMap hm = (HashMap ) msg.getObject(); 343 Mutablestring ms1 = (Mutablestring)hm.get( "ms1" ); 344 Mutablestring ms2 = (Mutablestring)hm.get( "ms2" ); 345 HashMap hmr = new HashMap (); 347 hmr.put( "ms2", ms2 ); 349 return hmr; 350 } 351 352 private void throwFault(String operationName, ObjectMessage msg) 353 throws Exception { 354 355 Queue replyTo = (Queue ) (msg.getJMSReplyTo()); 356 if (replyTo == null) 357 return; 358 359 ObjectMessage faultMsg = (ObjectMessage ) session.createObjectMessage(); 360 int choice = ((Integer ) msg.getObject()).intValue(); 361 System.out.println("NativeJMSRequestListener throwSimple choice="+choice); 362 switch (choice) { 363 case 0 : 364 break; 365 case 1 : 366 faultMsg.setStringProperty("faultIndicator", "simple"); 367 faultMsg.setObject((Serializable ) "A Simple Fault"); 368 break; 369 case 2 : 370 case 3 : 371 faultMsg.setStringProperty( 372 "faultIndicator", 373 choice == 2 ? "ints" : "twoints"); 374 HashMap hm = new HashMap (); 375 hm.put("faultInt1", new Integer (1)); 376 hm.put("faultInt2", new Integer (2)); 377 hm.put("faultInt3", new Integer (3)); 378 faultMsg.setObject((Serializable ) hm); 379 break; 380 case 4 : 381 faultMsg.setIntProperty("faultIndicator", 43); 382 faultMsg.setObject((Serializable ) "A Simple Fault"); 383 break; 384 case 5 : 385 faultMsg.setStringProperty("faultIndicator", "not a fault"); 386 faultMsg.setObject((Serializable ) "Not a Fault"); 387 break; 388 case 6 : 389 faultMsg.setByteProperty("faultIndicator", (byte)-1); 390 faultMsg.setObject((Serializable ) "A Fault Indicator"); 391 break; 392 case 7 : 393 faultMsg.setByteProperty("faultIndicator", (byte)-2); 394 faultMsg.setObject((Serializable ) "Another Property Fault"); 395 faultMsg.setStringProperty("anotherProperty", "Another JMS Property"); 396 break; 397 case 8 : 398 faultMsg.setByteProperty("faultIndicator", (byte)-3); 399 break; 400 case 9 : 401 faultMsg.setByteProperty("faultIndicator", (byte)-4); 402 faultMsg.setStringProperty("anotherProperty", "Another JMS Property"); 403 break; 404 case 10 : 405 faultMsg.setByteProperty("faultIndicator", (byte)-5); 406 break; 407 default : 408 throw new RuntimeException ("throwSimple: Bad choice"); 409 } 410 411 setReplyToQueue(replyTo); 412 String o = msg.getJMSMessageID(); 413 try { 414 send(faultMsg, o, false); 415 } catch (Exception ex) { 416 ex.printStackTrace(); 417 } 418 } 419 420 private String doFakeOp(String fake) { 421 fake = fake.substring( 1 ); fake = fake.substring( 0, fake.length() - 1 ); return getFakeReply( fake ); 424 } 425 426 private String getFakeReply(String name) { 427 String s; 428 StringBuffer sb = new StringBuffer (); 429 try { 430 BufferedReader in = new BufferedReader ( 431 new FileReader ( name ) ); 432 while (( s = in.readLine() ) != null) { 433 sb.append( s ); 434 } 435 in.close(); 436 } catch (IOException ex) { 437 ex.printStackTrace(); 438 } 439 return sb.toString(); 440 } 441 442 private void sendReply(Message msg, Object response) throws Exception { 443 Queue replyTo = (Queue ) (msg.getJMSReplyTo()); 444 if (replyTo == null) 445 return; 446 447 Message replyMsg = session.createObjectMessage(); 448 ((ObjectMessage ) replyMsg).setObject((Serializable ) response); 449 450 setEchoProperties( msg, replyMsg ); 451 452 setReplyToQueue(replyTo); 453 String o = msg.getJMSMessageID(); 454 try { 455 send(replyMsg, o, false); 456 } catch (Exception ex) { 457 ex.printStackTrace(); 458 } 459 } 460 461 private void sendReply(Message msg, String response) throws Exception { 462 Queue replyTo = (Queue ) (msg.getJMSReplyTo()); 463 if (replyTo == null) 464 return; 465 466 Message replyMsg = session.createTextMessage(); 467 ((TextMessage ) replyMsg).setText( response ); 468 469 setEchoProperties( msg, replyMsg ); 470 471 setReplyToQueue(replyTo); 472 String o = msg.getJMSMessageID(); 473 try { 474 send(replyMsg, o, false); 475 } catch (Exception ex) { 476 ex.printStackTrace(); 477 } 478 } 479 480 private void setEchoProperties(Message inMsg, Message outMsg) { 481 try { 482 for (Enumeration en = inMsg.getPropertyNames(); en.hasMoreElements(); ) { 483 String propName = (String )en.nextElement(); 484 if ( propName.startsWith( "Echo" ) ) { 485 String key = propName; 486 Object propValue = inMsg.getObjectProperty( propName ); 487 outMsg.setObjectProperty( key, propValue ); 488 } 489 } 490 } catch (JMSException ex) { 491 ex.printStackTrace(); 492 } 493 } 494 495 500 public final Object getLock() { 501 return LOCK; 502 } 503 504 } | Popular Tags |