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