1 10 11 package org.mule.ra; 12 13 import java.util.Map ; 14 15 import javax.resource.ResourceException ; 16 17 import org.mule.config.MuleProperties; 18 import org.mule.config.i18n.Message; 19 import org.mule.config.i18n.Messages; 20 import org.mule.impl.MuleEvent; 21 import org.mule.impl.MuleMessage; 22 import org.mule.impl.MuleSession; 23 import org.mule.impl.endpoint.MuleEndpoint; 24 import org.mule.impl.endpoint.MuleEndpointURI; 25 import org.mule.impl.security.MuleCredentials; 26 import org.mule.providers.AbstractConnector; 27 import org.mule.umo.UMOEvent; 28 import org.mule.umo.UMOException; 29 import org.mule.umo.UMOMessage; 30 import org.mule.umo.UMOSession; 31 import org.mule.umo.endpoint.UMOEndpoint; 32 import org.mule.umo.endpoint.UMOEndpointURI; 33 import org.mule.umo.manager.UMOManager; 34 import org.mule.umo.provider.DispatchException; 35 import org.mule.umo.provider.ReceiveException; 36 import org.mule.umo.provider.UMOConnector; 37 38 41 public class DefaultMuleConnection implements MuleConnection 42 { 43 private final MuleCredentials credentials; 44 private final UMOManager manager; 45 private MuleManagedConnection managedConnection; 46 47 public DefaultMuleConnection(MuleManagedConnection managedConnection, 48 UMOManager manager, 49 MuleCredentials credentials) 50 { 51 this.manager = manager; 52 this.credentials = credentials; 53 this.managedConnection = managedConnection; 54 } 55 56 68 public void dispatch(String url, Object payload, Map messageProperties) throws UMOException 69 { 70 UMOEndpointURI muleEndpoint = new MuleEndpointURI(url); 71 UMOMessage message = new MuleMessage(payload, messageProperties); 72 UMOEvent event = getEvent(message, muleEndpoint, false); 73 try 74 { 75 event.getSession().dispatchEvent(event); 76 } 77 catch (UMOException e) 78 { 79 throw e; 80 } 81 catch (Exception e) 82 { 83 throw new DispatchException(new Message("client", 1), event.getMessage(), event.getEndpoint(), e); 84 } 85 } 86 87 100 public UMOMessage send(String url, Object payload, Map messageProperties) throws UMOException 101 { 102 UMOEndpointURI muleEndpoint = new MuleEndpointURI(url); 103 UMOMessage message = new MuleMessage(payload, messageProperties); 104 UMOEvent event = getEvent(message, muleEndpoint, true); 105 106 UMOMessage response; 107 try 108 { 109 response = event.getSession().sendEvent(event); 110 } 111 catch (UMOException e) 112 { 113 throw e; 114 } 115 catch (Exception e) 116 { 117 throw new DispatchException(new Message("client", 1), event.getMessage(), event.getEndpoint(), e); 118 } 119 return response; 120 } 121 122 133 public UMOMessage receive(String url, long timeout) throws UMOException 134 { 135 MuleEndpointURI muleEndpoint = new MuleEndpointURI(url); 136 137 UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(muleEndpoint, 138 UMOEndpoint.ENDPOINT_TYPE_SENDER); 139 140 try 141 { 142 return endpoint.getConnector().getDispatcher(endpoint).receive(endpoint, timeout); 143 } 144 catch (Exception e) 145 { 146 throw new ReceiveException(endpoint, timeout, e); 147 } 148 } 149 150 159 protected UMOEvent getEvent(UMOMessage message, UMOEndpointURI uri, boolean synchronous) 160 throws UMOException 161 { 162 UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(uri, UMOEndpoint.ENDPOINT_TYPE_SENDER); 163 UMOConnector connector = endpoint.getConnector(); 164 165 if (!connector.isStarted() && manager.isStarted()) 166 { 167 connector.startConnector(); 168 } 169 170 try 171 { 172 UMOSession session = new MuleSession(message, 173 ((AbstractConnector)endpoint.getConnector()).getSessionHandler()); 174 175 if (credentials != null) 176 { 177 message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken()); 178 } 179 180 return new MuleEvent(message, endpoint, session, synchronous); 181 } 182 catch (Exception e) 183 { 184 throw new DispatchException(new Message(Messages.FAILED_TO_CREATE_X, "Client event"), message, 185 endpoint, e); 186 } 187 } 188 189 195 196 public MuleManagedConnection getManagedConnection() 197 { 198 return managedConnection; 199 } 200 201 204 public void close() throws ResourceException 205 { 206 if (managedConnection == null) 207 { 208 return; } 210 managedConnection.removeConnection(this); 211 212 managedConnection.fireCloseEvent(this); 214 managedConnection = null; 215 } 216 217 222 223 public void associateConnection(MuleManagedConnection newMc) throws ResourceException 224 { 225 checkIfValid(); 226 managedConnection.removeConnection(this); 228 newMc.addConnection(this); 230 managedConnection = newMc; 231 } 232 233 238 239 void checkIfValid() throws ResourceException 240 { 241 if (managedConnection == null) 242 { 243 throw new ResourceException ( 244 new Message(Messages.OBJECT_X_MARKED_INVALID, "muleManagedConnection").toString()); 245 } 246 } 247 248 252 253 void invalidate() 254 { 255 managedConnection = null; 256 } 257 } 258 | Popular Tags |