1 57 58 package org.apache.wsif.util.jms; 59 60 import java.net.URL ; 61 import java.util.ArrayList ; 62 import java.util.Arrays ; 63 import java.util.HashMap ; 64 import java.util.Hashtable ; 65 import java.util.Iterator ; 66 67 import javax.jms.JMSException ; 68 import javax.jms.Message ; 69 import javax.jms.Queue ; 70 import javax.jms.TextMessage ; 71 import org.apache.soap.rpc.SOAPContext; 72 import org.apache.soap.transport.TransportMessage; 73 import org.apache.soap.util.IOUtils; 74 import org.apache.soap.util.net.HTTPUtils; 75 import org.apache.wsif.WSIFException; 76 import org.apache.wsif.logging.Trc; 77 78 88 public class JMS2HTTPBridge { 89 private URL httpURL = null; 90 private JMS2HTTPBridgeDestination destination = null; 91 92 private static final ArrayList outGoingHeaders = 93 new ArrayList (Arrays.asList(new String [] 94 { "SOAPAction" })); 95 96 private static final ArrayList interestingProperties = 97 new ArrayList (Arrays.asList(new String [] { "JMSPriority", "JMSDeliveryMode" })); 98 private boolean verbose; 99 100 private WSIFJMSListener list = new WSIFJMSListener() { 101 public void onException(JMSException arg1) { 102 Trc.entry(this, arg1); 103 System.out.println("Caught an exception!"); 104 arg1.printStackTrace(); 105 Trc.exit(); 106 } 107 108 public void onMessage(Message message) { 109 Trc.entry(this, message); 110 receiveMessage(message); 111 Trc.exit(); 112 } 113 }; 114 115 public JMS2HTTPBridge( 116 String initialContextFactory, 117 String jndiUrl, 118 String queueConnectionFactory, 119 String readQueue, 120 String httpUrlString, 121 String startType, 122 boolean verbose) 123 throws Exception { 124 Trc.entry( 125 this, 126 initialContextFactory, 127 jndiUrl, 128 queueConnectionFactory, 129 readQueue, 130 httpUrlString, 131 startType, 132 new Boolean (verbose)); 133 134 System.out.println( 135 "Starting the JMS2HTTPBridge with" 136 + "\n" 137 + "Initial Context Factory = " 138 + initialContextFactory 139 + "\n" 140 + "JNDI URL = " 141 + jndiUrl 142 + "\n" 143 + "Queue Connection Factory = " 144 + queueConnectionFactory 145 + "\n" 146 + "JNDI Read Queue = " 147 + readQueue 148 + "\n" 149 + "HTTP URL = " 150 + httpUrlString 151 + "\n" 152 + "Start Type = " 153 + startType 154 + "\n" 155 + "Verbose = " 156 + verbose); 157 158 destination = 159 new JMS2HTTPBridgeDestination( 160 new WSIFJMSFinderForJndi( 161 null, 162 initialContextFactory, 163 jndiUrl, 164 WSIFJMSFinder.STYLE_QUEUE, 165 queueConnectionFactory, 166 readQueue, 167 null), 168 null, 169 WSIFJMSConstants.WAIT_FOREVER, 170 startType, 171 verbose); 172 173 httpURL = new URL (httpUrlString); 174 this.verbose = verbose; 175 Trc.exit(); 176 } 177 178 public static void main(String [] args) throws Exception { 179 Trc.entry(null, args); 180 181 String usage = 182 "Usage: java " 183 + JMS2HTTPBridge.class.getName() 184 + " [-cold|-warm] " 185 + "-icf <initialContextFactory> " 186 + "-jndi <jndiUrl> " 187 + "-s <sampleName> " 188 + "-qcf <queueConnectionFactory> " 189 + "-q <readQueue> " 190 + "-http <httpUrl>" 191 + "-v"; 192 193 String startType = JMS2HTTPBridgeDestination.WARMSTART; 194 String initialContextFactory = "com.sun.jndi.fscontext.RefFSContextFactory"; 195 String jndiUrl = "file:///JNDI-Directory"; 196 String sampleName = null; 197 String queueConnectionFactory = "WSIFSampleQCF"; 198 String readQueue = null; 199 String httpUrlString = "http://localhost:8080/soap/servlet/rpcrouter"; 200 boolean verbose = false; 201 202 for (int idx = 0; idx < args.length; idx++) { 203 if (!args[idx].startsWith("-")) 204 throw new Exception ("Bad parameter\n" + usage); 205 206 if (args[idx].equals("-cold")) { 207 startType = JMS2HTTPBridgeDestination.COLDSTART; 208 } else if (args[idx].equals("-warm")) { 209 startType = JMS2HTTPBridgeDestination.WARMSTART; 210 } else if (args[idx].equals("-icf")) { 211 idx++; 212 initialContextFactory = args[idx]; 213 } else if (args[idx].equals("-jndi")) { 214 idx++; 215 jndiUrl = args[idx]; 216 } else if (args[idx].equals("-s")) { 217 idx++; 218 sampleName = args[idx]; 219 } else if (args[idx].equals("-qcf")) { 220 idx++; 221 queueConnectionFactory = args[idx]; 222 } else if (args[idx].equals("-q")) { 223 idx++; 224 readQueue = args[idx]; 225 } else if (args[idx].equals("-http")) { 226 idx++; 227 httpUrlString = args[idx]; 228 } else if (args[idx].equals("-v")) { 229 verbose = true; 230 } else 231 throw new Exception ("Bad parameter\n" + usage); 232 } 233 234 if (readQueue == null && sampleName != null) 235 readQueue = "SoapJms" + sampleName + "Queue"; 236 237 if (startType == null 238 || initialContextFactory == null 239 || jndiUrl == null 240 || queueConnectionFactory == null 241 || readQueue == null 242 || httpUrlString == null) 243 throw new Exception ("Missing parameter\n" + usage); 244 245 JMS2HTTPBridge j2h = 246 new JMS2HTTPBridge( 247 initialContextFactory, 248 jndiUrl, 249 queueConnectionFactory, 250 readQueue, 251 httpUrlString, 252 startType, 253 verbose); 254 255 j2h.listen(); 256 Trc.exit(); 257 } 258 259 public void listen() throws WSIFException { 260 Trc.entry(this); 261 destination.listen(list); 262 Trc.exit(); 263 } 264 265 void receiveMessage(Message msg) { 266 Trc.entry(this, msg); 267 String payload = null; 268 269 try { 270 if (verbose) 271 System.out.println("Caught a message!"); 272 273 if (msg instanceof TextMessage ) { 274 String body = ((TextMessage ) msg).getText(); 275 if (body != null) { 276 if (verbose) 277 System.out.println("Message contained '" + body + "'"); 278 279 TransportMessage tmsg = 280 new TransportMessage(body, new SOAPContext(), new Hashtable ()); 281 setOutGoingHeaders(msg, tmsg); 282 tmsg.save(); 283 284 TransportMessage response = HTTPUtils.post(getServiceURL(msg), tmsg, 30000, null, 0); 285 payload = IOUtils.getStringFromReader(response.getEnvelopeReader()); 286 if (verbose) 287 System.out.println("HTTP RESPONSE IS: '" + payload + "'"); 288 } else { 289 System.err.println("error: message contained no body"); 290 payload = "error: message contained no body"; 291 } 292 } else { 293 System.err.println("error: message was not a TextMessage as expected"); 294 System.err.println(msg); 295 payload = "error: message was not a TextMessage as expected"; 296 } 297 298 } catch (Exception e) { 299 e.printStackTrace(); 300 payload = e.toString(); 301 } 302 303 try { 304 WSIFJMSProperties props = new WSIFJMSProperties(WSIFJMSProperties.OUT); 309 props.getPropertiesFromMessage(msg); 310 HashMap kept = new HashMap (); 311 Iterator it = interestingProperties.iterator(); 312 while (true) { 313 try { 314 if (!it.hasNext()) 315 break; 316 String prop = (String ) it.next(); 317 if (props.containsKey(prop)) 318 kept.put(prop, props.get(prop)); 319 } catch (Exception e) { 320 System.err.println("JMS2HTTPBridge properties caught " + e); 321 } 322 } 323 324 destination.setProperties(kept); 325 destination.setReplyToQueue((Queue ) msg.getJMSReplyTo()); 326 destination.send(payload, msg.getJMSMessageID()); 327 } catch (JMSException je) { 328 je.printStackTrace(); 329 } catch (WSIFException we) { 330 we.printStackTrace(); 331 } 332 333 Trc.exit(); 334 return; 335 } 336 337 private void setOutGoingHeaders(Message m, TransportMessage tmsg) { 338 for (Iterator i = outGoingHeaders.iterator(); i.hasNext(); ) { 339 try { 340 String name = (String ) i.next(); 341 String value = m.getStringProperty(name); 342 if (value != null && value.length() > 0 ) { 343 tmsg.setHeader(name, "\""+value+"\""); 344 } 345 } catch (JMSException e) { 346 Trc.ignoredException(e); 347 } 348 } 349 } 350 351 private URL getServiceURL(Message m) { 352 URL serviceURL = null; 353 try { 354 String s = m.getStringProperty( "ServiceURL" ); 355 serviceURL = new URL (s); 356 } catch (Exception e) { 357 Trc.ignoredException(e); 358 serviceURL = httpURL; 359 } 360 return serviceURL; 361 } 362 363 } | Popular Tags |