Coverage Report - org.mule.ra.DefaultMuleConnection
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMuleConnection
0%
0/57
0%
0/4
3
 
 1  
 /*
 2  
  * $Id: DefaultMuleConnection.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.ra;
 12  
 
 13  
 import org.mule.config.MuleProperties;
 14  
 import org.mule.config.i18n.CoreMessages;
 15  
 import org.mule.extras.client.i18n.ClientMessages;
 16  
 import org.mule.impl.MuleEvent;
 17  
 import org.mule.impl.MuleMessage;
 18  
 import org.mule.impl.MuleSession;
 19  
 import org.mule.impl.endpoint.MuleEndpoint;
 20  
 import org.mule.impl.endpoint.MuleEndpointURI;
 21  
 import org.mule.impl.security.MuleCredentials;
 22  
 import org.mule.providers.AbstractConnector;
 23  
 import org.mule.ra.i18n.JcaMessages;
 24  
 import org.mule.umo.UMOEvent;
 25  
 import org.mule.umo.UMOException;
 26  
 import org.mule.umo.UMOMessage;
 27  
 import org.mule.umo.UMOSession;
 28  
 import org.mule.umo.endpoint.UMOEndpoint;
 29  
 import org.mule.umo.endpoint.UMOEndpointURI;
 30  
 import org.mule.umo.manager.UMOManager;
 31  
 import org.mule.umo.provider.DispatchException;
 32  
 import org.mule.umo.provider.ReceiveException;
 33  
 import org.mule.umo.provider.UMOConnector;
 34  
 
 35  
 import java.util.Map;
 36  
 
 37  
 import javax.resource.ResourceException;
 38  
 
 39  
 /**
 40  
  * <code>MuleConnection</code> TODO
 41  
  */
 42  
 public class DefaultMuleConnection implements MuleConnection
 43  
 {
 44  
     private final MuleCredentials credentials;
 45  
     private final UMOManager manager;
 46  
     private MuleManagedConnection managedConnection;
 47  
 
 48  
     public DefaultMuleConnection(MuleManagedConnection managedConnection,
 49  
                                  UMOManager manager,
 50  
                                  MuleCredentials credentials)
 51  0
     {
 52  0
         this.manager = manager;
 53  0
         this.credentials = credentials;
 54  0
         this.managedConnection = managedConnection;
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Dispatches an event asynchronously to a endpointUri via a mule server. the Url
 59  
      * determines where to dispathc the event to, this can be in the form of
 60  
      * 
 61  
      * @param url the Mule url used to determine the destination and transport of the
 62  
      *            message
 63  
      * @param payload the object that is the payload of the event
 64  
      * @param messageProperties any properties to be associated with the payload. In
 65  
      *            the case of Jms you could set the JMSReplyTo property in these
 66  
      *            properties.
 67  
      * @throws org.mule.umo.UMOException
 68  
      */
 69  
     public void dispatch(String url, Object payload, Map messageProperties) throws UMOException
 70  
     {
 71  0
         UMOEndpointURI muleEndpoint = new MuleEndpointURI(url);
 72  0
         UMOMessage message = new MuleMessage(payload, messageProperties);
 73  0
         UMOEvent event = getEvent(message, muleEndpoint, false);
 74  
         try
 75  
         {
 76  0
             event.getSession().dispatchEvent(event);
 77  
         }
 78  0
         catch (UMOException e)
 79  
         {
 80  0
             throw e;
 81  
         }
 82  0
         catch (Exception e)
 83  
         {
 84  0
             throw new DispatchException(
 85  
                 ClientMessages.failedToDispatchClientEvent(),
 86  
                 event.getMessage(), event.getEndpoint(), e);
 87  0
         }
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Sends an object (payload) synchronous to the given url and returns a
 92  
      * UMOMessage response back.
 93  
      * 
 94  
      * @param url the Mule url used to determine the destination and transport of the
 95  
      *            message
 96  
      * @param payload the object that is the payload of the event
 97  
      * @param messageProperties any properties to be associated with the payload. In
 98  
      *            the case of Jms you could set the JMSReplyTo property in these
 99  
      *            properties.
 100  
      * @return a umomessage response.
 101  
      * @throws org.mule.umo.UMOException
 102  
      */
 103  
     public UMOMessage send(String url, Object payload, Map messageProperties) throws UMOException
 104  
     {
 105  0
         UMOEndpointURI muleEndpoint = new MuleEndpointURI(url);
 106  0
         UMOMessage message = new MuleMessage(payload, messageProperties);
 107  0
         UMOEvent event = getEvent(message, muleEndpoint, true);
 108  
 
 109  
         UMOMessage response;
 110  
         try
 111  
         {
 112  0
             response = event.getSession().sendEvent(event);
 113  
         }
 114  0
         catch (UMOException e)
 115  
         {
 116  0
             throw e;
 117  
         }
 118  0
         catch (Exception e)
 119  
         {
 120  0
             throw new DispatchException(
 121  
                 ClientMessages.failedToDispatchClientEvent(), 
 122  
                 event.getMessage(), event.getEndpoint(), e);
 123  0
         }
 124  0
         return response;
 125  
     }
 126  
 
 127  
     /**
 128  
      * Will receive an event from an endpointUri determined by the url
 129  
      * 
 130  
      * @param url the Mule url used to determine the destination and transport of the
 131  
      *            message
 132  
      * @param timeout how long to block waiting to receive the event, if set to 0 the
 133  
      *            receive will not wait at all and if set to -1 the receive will wait
 134  
      *            forever
 135  
      * @return the message received or null if no message was received
 136  
      * @throws org.mule.umo.UMOException
 137  
      */
 138  
     public UMOMessage receive(String url, long timeout) throws UMOException
 139  
     {
 140  0
         MuleEndpointURI muleEndpoint = new MuleEndpointURI(url);
 141  
 
 142  0
         UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(muleEndpoint,
 143  
             UMOEndpoint.ENDPOINT_TYPE_SENDER);
 144  
 
 145  
         try
 146  
         {
 147  0
             return endpoint.receive(timeout);
 148  
         }
 149  0
         catch (Exception e)
 150  
         {
 151  0
             throw new ReceiveException(endpoint, timeout, e);
 152  
         }
 153  
     }
 154  
 
 155  
     /**
 156  
      * Packages a mule event for the current request
 157  
      * 
 158  
      * @param message the event payload
 159  
      * @param uri the destination endpointUri
 160  
      * @param synchronous whether the event will be synchronously processed
 161  
      * @return the UMOEvent
 162  
      * @throws UMOException in case of Mule error
 163  
      */
 164  
     protected UMOEvent getEvent(UMOMessage message, UMOEndpointURI uri, boolean synchronous)
 165  
         throws UMOException
 166  
     {
 167  0
         UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(uri, UMOEndpoint.ENDPOINT_TYPE_SENDER);
 168  0
         UMOConnector connector = endpoint.getConnector();
 169  
 
 170  0
         if (!connector.isStarted() && manager.isStarted())
 171  
         {
 172  0
             connector.startConnector();
 173  
         }
 174  
 
 175  
         try
 176  
         {
 177  0
             UMOSession session = new MuleSession(message,
 178  
                 ((AbstractConnector)endpoint.getConnector()).getSessionHandler());
 179  
 
 180  0
             if (credentials != null)
 181  
             {
 182  0
                 message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken());
 183  
             }
 184  
 
 185  0
             return new MuleEvent(message, endpoint, session, synchronous);
 186  
         }
 187  0
         catch (Exception e)
 188  
         {
 189  0
             throw new DispatchException(
 190  
                 CoreMessages.failedToCreate("Client event"), message, endpoint, e);
 191  
         }
 192  
     }
 193  
 
 194  
     /**
 195  
      * Retrieves a ManagedConnection.
 196  
      * 
 197  
      * @return a ManagedConnection instance representing the physical connection to
 198  
      *         the EIS
 199  
      */
 200  
 
 201  
     public MuleManagedConnection getManagedConnection()
 202  
     {
 203  0
         return managedConnection;
 204  
     }
 205  
 
 206  
     /**
 207  
      * Closes the connection.
 208  
      */
 209  
     public void close() throws ResourceException
 210  
     {
 211  0
         if (managedConnection == null)
 212  
         {
 213  0
             return; // connection is already closed
 214  
         }
 215  0
         managedConnection.removeConnection(this);
 216  
 
 217  
         // Send a close event to the App Server
 218  0
         managedConnection.fireCloseEvent(this);
 219  0
         managedConnection = null;
 220  0
     }
 221  
 
 222  
     /**
 223  
      * Associates connection handle with new managed connection.
 224  
      * 
 225  
      * @param newMc new managed connection
 226  
      */
 227  
 
 228  
     public void associateConnection(MuleManagedConnection newMc) throws ResourceException
 229  
     {
 230  0
         checkIfValid();
 231  
         // dissociate handle from current managed connection
 232  0
         managedConnection.removeConnection(this);
 233  
         // associate handle with new managed connection
 234  0
         newMc.addConnection(this);
 235  0
         managedConnection = newMc;
 236  0
     }
 237  
 
 238  
     /**
 239  
      * Checks the validity of the physical connection to the EIS.
 240  
      * 
 241  
      * @throws javax.resource.ResourceException in case of any error
 242  
      */
 243  
 
 244  
     void checkIfValid() throws ResourceException
 245  
     {
 246  0
         if (managedConnection == null)
 247  
         {
 248  0
             throw new ResourceException(
 249  
                 JcaMessages.objectMarkedInvalid("muleManagedConnection").toString());
 250  
         }
 251  0
     }
 252  
 
 253  
     /**
 254  
      * Sets the physical connection to the EIS as invalid. The physical connection to
 255  
      * the EIS cannot be used any more.
 256  
      */
 257  
 
 258  
     void invalidate()
 259  
     {
 260  0
         managedConnection = null;
 261  0
     }
 262  
 }