1 16 17 package org.apache.axis.transport.jms; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Message; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.handlers.BasicHandler; 23 import org.apache.axis.attachments.Attachments; 24 25 import javax.jms.Destination ; 26 import java.io.ByteArrayOutputStream ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 37 public class JMSSender extends BasicHandler 38 { 39 public JMSSender() 40 { 41 } 42 43 52 public void invoke(MessageContext msgContext) throws AxisFault 53 { 54 JMSConnector connector = null; 55 try 56 { 57 Object destination = msgContext.getProperty(JMSConstants.DESTINATION); 58 if(destination == null) 59 throw new AxisFault("noDestination"); 60 61 connector = (JMSConnector)msgContext.getProperty(JMSConstants.CONNECTOR); 62 63 JMSEndpoint endpoint = null; 64 if(destination instanceof String ) 65 endpoint = connector.createEndpoint((String )destination); 66 else 67 endpoint = connector.createEndpoint((Destination )destination); 68 69 ByteArrayOutputStream out = new ByteArrayOutputStream (); 70 msgContext.getRequestMessage().writeTo(out); 71 72 HashMap props = createSendProperties(msgContext); 73 74 String ret = null; 77 Message message = msgContext.getRequestMessage(); 78 Attachments mAttachments = message.getAttachmentsImpl(); 79 if (mAttachments != null && 0 != mAttachments.getAttachmentCount()) 80 { 81 String contentType = mAttachments.getContentType(); 82 if(contentType != null && !contentType.trim().equals("")) 83 { 84 props.put("contentType", contentType); 85 } 86 } 87 88 boolean waitForResponse = true; 89 if(msgContext.containsProperty(JMSConstants.WAIT_FOR_RESPONSE)) 90 waitForResponse = 91 ((Boolean )msgContext.getProperty( 92 JMSConstants.WAIT_FOR_RESPONSE)).booleanValue(); 93 if(waitForResponse) 94 { 95 long timeout = (long) msgContext.getTimeout(); 96 byte[] response = endpoint.call(out.toByteArray(), timeout, props); 97 Message msg = new Message(response); 98 msgContext.setResponseMessage(msg); 99 } 100 else 101 { 102 endpoint.send(out.toByteArray(), props); 103 } 104 } 105 catch(Exception e) 106 { 107 throw new AxisFault("failedSend", e); 108 } 109 finally 110 { 111 if (connector != null) 112 JMSConnectorManager.getInstance().release(connector); 113 } 114 } 115 116 private HashMap createSendProperties(MessageContext context) 117 { 118 HashMap props = createApplicationProperties(context); 123 124 if(context.containsProperty(JMSConstants.PRIORITY)) 125 props.put(JMSConstants.PRIORITY, 126 context.getProperty(JMSConstants.PRIORITY)); 127 if(context.containsProperty(JMSConstants.DELIVERY_MODE)) 128 props.put(JMSConstants.DELIVERY_MODE, 129 context.getProperty(JMSConstants.DELIVERY_MODE)); 130 if(context.containsProperty(JMSConstants.TIME_TO_LIVE)) 131 props.put(JMSConstants.TIME_TO_LIVE, 132 context.getProperty(JMSConstants.TIME_TO_LIVE)); 133 if(context.containsProperty(JMSConstants.JMS_CORRELATION_ID)) 134 props.put(JMSConstants.JMS_CORRELATION_ID, 135 context.getProperty(JMSConstants.JMS_CORRELATION_ID)); 136 return props; 137 } 138 139 142 protected HashMap createApplicationProperties(MessageContext context) { 143 HashMap props = null; 144 if (context.containsProperty( 145 JMSConstants.JMS_APPLICATION_MSG_PROPS)) { 146 props = new HashMap (); 147 props.putAll((Map )context.getProperty( 148 JMSConstants.JMS_APPLICATION_MSG_PROPS)); 149 } 150 return props; 151 } 152 } | Popular Tags |