Coverage Report - org.mule.module.jca.DefaultMuleConnection
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMuleConnection
0%
0/51
0%
0/6
2.9
 
 1  
 /*
 2  
  * $Id: DefaultMuleConnection.java 11343 2008-03-13 10:58:26Z tcarlson $
 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.module.jca;
 12  
 
 13  
 import org.mule.DefaultMuleEvent;
 14  
 import org.mule.DefaultMuleMessage;
 15  
 import org.mule.DefaultMuleSession;
 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.ImmutableEndpoint;
 23  
 import org.mule.api.endpoint.InboundEndpoint;
 24  
 import org.mule.api.transport.DispatchException;
 25  
 import org.mule.api.transport.ReceiveException;
 26  
 import org.mule.config.i18n.CoreMessages;
 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.transport.AbstractConnector;
 31  
 
 32  
 import java.util.Map;
 33  
 
 34  
 import javax.resource.ResourceException;
 35  
 
 36  
 /**
 37  
  * <code>MuleConnection</code> TODO
 38  
  */
 39  
 public class DefaultMuleConnection implements MuleConnection
 40  
 {
 41  
     private final MuleCredentials credentials;
 42  
     private final MuleContext muleContext;
 43  
     private MuleManagedConnection managedConnection;
 44  
 
 45  
     public DefaultMuleConnection(MuleManagedConnection managedConnection,
 46  
                                  MuleContext muleContext,
 47  
                                  MuleCredentials credentials)
 48  0
     {
 49  0
         this.muleContext = muleContext;
 50  0
         this.credentials = credentials;
 51  0
         this.managedConnection = managedConnection;
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Dispatches an event asynchronously to a endpointUri via a mule server. the Url
 56  
      * determines where to dispathc the event to, this can be in the form of
 57  
      * 
 58  
      * @param url the Mule url used to determine the destination and transport of the
 59  
      *            message
 60  
      * @param payload the object that is the payload of the event
 61  
      * @param messageProperties any properties to be associated with the payload. In
 62  
      *            the case of Jms you could set the JMSReplyTo property in these
 63  
      *            properties.
 64  
      * @throws org.mule.api.MuleException
 65  
      */
 66  
     public void dispatch(String url, Object payload, Map messageProperties) throws MuleException
 67  
     {
 68  0
         MuleMessage message = new DefaultMuleMessage(payload, messageProperties);
 69  0
         MuleEvent event = getEvent(message, url, false);
 70  
         try
 71  
         {
 72  0
             event.getSession().dispatchEvent(event);
 73  
         }
 74  0
         catch (MuleException e)
 75  
         {
 76  0
             throw e;
 77  
         }
 78  0
         catch (Exception e)
 79  
         {
 80  0
             throw new DispatchException(
 81  
                 ClientMessages.failedToDispatchClientEvent(),
 82  
                 event.getMessage(), event.getEndpoint(), e);
 83  0
         }
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Sends an object (payload) synchronous to the given url and returns a
 88  
      * MuleMessage response back.
 89  
      * 
 90  
      * @param url the Mule url used to determine the destination and transport of the
 91  
      *            message
 92  
      * @param payload the object that is the payload of the event
 93  
      * @param messageProperties any properties to be associated with the payload. In
 94  
      *            the case of Jms you could set the JMSReplyTo property in these
 95  
      *            properties.
 96  
      * @return a umomessage response.
 97  
      * @throws org.mule.api.MuleException
 98  
      */
 99  
     public MuleMessage send(String url, Object payload, Map messageProperties) throws MuleException
 100  
     {
 101  0
         MuleMessage message = new DefaultMuleMessage(payload, messageProperties);
 102  0
         MuleEvent event = getEvent(message, url, true);
 103  
 
 104  
         MuleMessage response;
 105  
         try
 106  
         {
 107  0
             response = event.getSession().sendEvent(event);
 108  
         }
 109  0
         catch (MuleException e)
 110  
         {
 111  0
             throw e;
 112  
         }
 113  0
         catch (Exception e)
 114  
         {
 115  0
             throw new DispatchException(
 116  
                 ClientMessages.failedToDispatchClientEvent(), 
 117  
                 event.getMessage(), event.getEndpoint(), e);
 118  0
         }
 119  0
         return response;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Will receive an event from an endpointUri determined by the url
 124  
      * 
 125  
      * @param url the Mule url used to determine the destination and transport of the
 126  
      *            message
 127  
      * @param timeout how long to block waiting to receive the event, if set to 0 the
 128  
      *            receive will not wait at all and if set to -1 the receive will wait
 129  
      *            forever
 130  
      * @return the message received or null if no message was received
 131  
      * @throws org.mule.api.MuleException
 132  
      */
 133  
     public MuleMessage request(String url, long timeout) throws MuleException
 134  
     {
 135  0
         InboundEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(url);
 136  
 
 137  
         try
 138  
         {
 139  0
             return endpoint.request(timeout);
 140  
         }
 141  0
         catch (Exception e)
 142  
         {
 143  0
             throw new ReceiveException(endpoint, timeout, e);
 144  
         }
 145  
     }
 146  
 
 147  
     /**
 148  
      * Packages a mule event for the current request
 149  
      * 
 150  
      * @param message the event payload
 151  
      * @param uri the destination endpointUri
 152  
      * @param synchronous whether the event will be synchronously processed
 153  
      * @return the MuleEvent
 154  
      * @throws MuleException in case of Mule error
 155  
      */
 156  
     protected MuleEvent getEvent(MuleMessage message, String uri, boolean synchronous)
 157  
         throws MuleException
 158  
     {
 159  0
         ImmutableEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(uri);
 160  
         //Connector connector = endpoint.getConnector();
 161  
 
 162  
 //        if (!connector.isStarted() && manager.isStarted())
 163  
 //        {
 164  
 //            connector.start();
 165  
 //        }
 166  
 
 167  
         try
 168  
         {
 169  0
             MuleSession session = new DefaultMuleSession(message,
 170  
                 ((AbstractConnector)endpoint.getConnector()).getSessionHandler(), muleContext);
 171  
 
 172  0
             if (credentials != null)
 173  
             {
 174  0
                 message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken());
 175  
             }
 176  
 
 177  0
             return new DefaultMuleEvent(message, endpoint, session, synchronous);
 178  
         }
 179  0
         catch (Exception e)
 180  
         {
 181  0
             throw new DispatchException(
 182  
                 CoreMessages.failedToCreate("Client event"), message, endpoint, e);
 183  
         }
 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  
 }