1 10 11 package org.mule.providers.soap.glue; 12 13 import electric.glue.context.ProxyContext; 14 import electric.glue.context.ThreadContext; 15 import electric.proxy.IProxy; 16 import electric.registry.Registry; 17 import org.mule.config.MuleProperties; 18 import org.mule.impl.MuleMessage; 19 import org.mule.providers.AbstractMessageDispatcher; 20 import org.mule.umo.UMOEvent; 21 import org.mule.umo.UMOException; 22 import org.mule.umo.UMOMessage; 23 import org.mule.umo.endpoint.MalformedEndpointException; 24 import org.mule.umo.endpoint.UMOImmutableEndpoint; 25 import org.mule.umo.provider.DispatchException; 26 import org.mule.umo.provider.ReceiveException; 27 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 35 36 public class GlueMessageDispatcher extends AbstractMessageDispatcher 37 { 38 protected IProxy proxy = null; 39 40 public GlueMessageDispatcher(UMOImmutableEndpoint endpoint) 41 { 42 super(endpoint); 43 } 44 45 protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception 46 { 47 if (proxy == null) 48 { 49 String bindAddress = endpoint.getEndpointURI().getAddress(); 50 String method = (String )endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY); 51 if (bindAddress.indexOf(".wsdl") == -1 && method != null) 52 { 53 bindAddress = bindAddress.replaceAll("/" + method, ".wsdl/" + method); 54 } 55 int i = bindAddress.indexOf("?"); 56 if (i > -1) 57 { 58 bindAddress = bindAddress.substring(0, i); 59 } 60 61 if (endpoint.getEndpointURI().getUsername() != null) 63 { 64 ProxyContext context = new ProxyContext(); 65 context.setAuthUser(endpoint.getEndpointURI().getUsername()); 66 context.setAuthPassword(new String (endpoint.getEndpointURI().getPassword())); 67 proxy = Registry.bind(bindAddress, context); 68 } 69 else 70 { 71 proxy = Registry.bind(bindAddress); 72 } 73 } 74 } 75 76 protected void doDisconnect() throws Exception 77 { 78 proxy = null; 79 } 80 81 protected void doDispatch(UMOEvent event) throws Exception 82 { 83 doSend(event); 84 } 85 86 protected UMOMessage doSend(UMOEvent event) throws Exception 87 { 88 89 String method = event.getMessage().getStringProperty(MuleProperties.MULE_METHOD_PROPERTY, null); 90 if (method == null) 91 { 92 method = (String )event.getEndpoint().getProperty(MuleProperties.MULE_METHOD_PROPERTY); 93 } 94 setContext(event); 95 96 Object payload = event.getTransformedMessage(); 97 Object [] args; 98 if (payload instanceof Object []) 99 { 100 args = (Object [])payload; 101 } 102 else 103 { 104 args = new Object []{payload}; 105 } 106 if (event.getMessage().getReplyTo() != null) 107 { 108 ThreadContext.setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, event.getMessage().getReplyTo()); 109 } 110 if (event.getMessage().getCorrelationId() != null) 111 { 112 ThreadContext.setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, event.getMessage() 113 .getCorrelationId()); 114 } 115 try 116 { 117 Object result = proxy.invoke(method, args); 118 if (result == null) 119 { 120 return null; 121 } 122 else 123 { 124 return new MuleMessage(result); 125 } 126 } 127 catch (Throwable t) 128 { 129 throw new DispatchException(event.getMessage(), event.getEndpoint(), t); 130 } 131 } 132 133 145 protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception 146 { 147 Map props = new HashMap (); 148 props.putAll(endpoint.getProperties()); 149 String method = (String )props.remove(MuleProperties.MULE_METHOD_PROPERTY); 150 try 151 { 152 Object result = proxy.invoke(method, props.values().toArray()); 153 return new MuleMessage(result); 154 } 155 catch (Throwable t) 156 { 157 throw new ReceiveException(endpoint, timeout, t); 158 } 159 } 160 161 public Object getDelegateSession() throws UMOException 162 { 163 return null; 164 } 165 166 protected void doDispose() 167 { 168 } 170 171 protected String getMethod(String endpoint) throws MalformedEndpointException 172 { 173 int i = endpoint.lastIndexOf("/"); 174 String method = endpoint.substring(i + 1); 175 if (method.indexOf(".wsdl") != -1) 176 { 177 throw new MalformedEndpointException( 178 "Soap url must contain method to invoke as a param [method=X] or as the last path element"); 179 } 180 else 181 { 182 return method; 183 } 186 } 187 188 protected void setContext(UMOEvent event) 189 { 190 Object replyTo = event.getMessage().getReplyTo(); 191 if (replyTo != null) 192 { 193 ThreadContext.setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, replyTo); 194 } 195 196 String correlationId = event.getMessage().getCorrelationId(); 197 if (replyTo != null) 198 { 199 ThreadContext.setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, correlationId); 200 } 201 202 int value = event.getMessage().getCorrelationSequence(); 203 if (value > 0) 204 { 205 ThreadContext.setProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, 206 String.valueOf(value)); 207 } 208 209 value = event.getMessage().getCorrelationGroupSize(); 210 if (value > 0) 211 { 212 ThreadContext.setProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, 213 String.valueOf(value)); 214 } 215 } 216 } 217 | Popular Tags |