Coverage Report - org.mule.providers.vm.VMConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
VMConnector
67%
52/78
55%
21/38
2.579
 
 1  
 /*
 2  
  * $Id: VMConnector.java 9436 2007-10-29 14:39:54Z aperepel $
 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.providers.vm;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.config.QueueProfile;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.impl.MuleMessage;
 17  
 import org.mule.impl.endpoint.MuleEndpointURI;
 18  
 import org.mule.providers.AbstractConnector;
 19  
 import org.mule.routing.filters.WildcardFilter;
 20  
 import org.mule.transaction.TransactionCoordination;
 21  
 import org.mule.umo.MessagingException;
 22  
 import org.mule.umo.TransactionException;
 23  
 import org.mule.umo.UMOComponent;
 24  
 import org.mule.umo.UMOException;
 25  
 import org.mule.umo.UMOTransaction;
 26  
 import org.mule.umo.endpoint.EndpointException;
 27  
 import org.mule.umo.endpoint.UMOEndpoint;
 28  
 import org.mule.umo.endpoint.UMOEndpointURI;
 29  
 import org.mule.umo.lifecycle.InitialisationException;
 30  
 import org.mule.umo.provider.MessageTypeNotSupportedException;
 31  
 import org.mule.umo.provider.UMOMessageAdapter;
 32  
 import org.mule.umo.provider.UMOMessageReceiver;
 33  
 import org.mule.util.ClassUtils;
 34  
 import org.mule.util.queue.QueueManager;
 35  
 import org.mule.util.queue.QueueSession;
 36  
 
 37  
 import java.util.Iterator;
 38  
 
 39  
 /**
 40  
  * <code>VMConnector</code> is a simple endpoint wrapper to allow a Mule component
 41  
  * to be accessed from an endpoint
 42  
  */
 43  38
 public class VMConnector extends AbstractConnector
 44  
 {
 45  38
     private boolean queueEvents = false;
 46  
     private QueueProfile queueProfile;
 47  38
     private Class adapterClass = null;
 48  38
     private int queueTimeout = 1000;
 49  
 
 50  
     protected void doInitialise() throws InitialisationException
 51  
     {
 52  36
         if (queueEvents)
 53  
         {
 54  10
             if (queueProfile == null)
 55  
             {
 56  10
                 queueProfile = MuleManager.getConfiguration().getQueueProfile();
 57  
             }
 58  
         }
 59  
 
 60  
         try
 61  
         {
 62  36
             adapterClass = ClassUtils.loadClass(serviceDescriptor.getMessageAdapter(), getClass());
 63  
         }
 64  0
         catch (ClassNotFoundException e)
 65  
         {
 66  0
             throw new InitialisationException(
 67  
                 CoreMessages.failedToLoad("Message Adapter: " + serviceDescriptor.getMessageAdapter()), e);
 68  36
         }
 69  36
     }
 70  
 
 71  
     protected void doDispose()
 72  
     {
 73  
         // template method
 74  38
     }
 75  
 
 76  
     protected void doConnect() throws Exception
 77  
     {
 78  
         // template method
 79  24
     }
 80  
 
 81  
     protected void doDisconnect() throws Exception
 82  
     {
 83  
         // template method
 84  24
     }
 85  
 
 86  
     protected void doStart() throws UMOException
 87  
     {
 88  
         // template method
 89  24
     }
 90  
 
 91  
     protected void doStop() throws UMOException
 92  
     {
 93  
         // template method
 94  24
     }
 95  
 
 96  
     public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception
 97  
     {
 98  42
         if (queueEvents)
 99  
         {
 100  24
             queueProfile.configureQueue(endpoint.getEndpointURI().getAddress());
 101  
         }
 102  42
         return serviceDescriptor.createMessageReceiver(this, component, endpoint);
 103  
     }
 104  
 
 105  
     public UMOMessageAdapter getMessageAdapter(Object message) throws MessagingException
 106  
     {
 107  2
         if (message == null)
 108  
         {
 109  0
             throw new MessageTypeNotSupportedException(null, adapterClass);
 110  
         }
 111  2
         else if (message instanceof MuleMessage)
 112  
         {
 113  2
             return ((MuleMessage)message).getAdapter();
 114  
         }
 115  0
         else if (message instanceof UMOMessageAdapter)
 116  
         {
 117  0
             return (UMOMessageAdapter)message;
 118  
         }
 119  
         else
 120  
         {
 121  0
             throw new MessageTypeNotSupportedException(message, adapterClass);
 122  
         }
 123  
     }
 124  
 
 125  
     public String getProtocol()
 126  
     {
 127  178
         return "VM";
 128  
     }
 129  
 
 130  
     public boolean isQueueEvents()
 131  
     {
 132  178
         return queueEvents;
 133  
     }
 134  
 
 135  
     public void setQueueEvents(boolean queueEvents)
 136  
     {
 137  16
         this.queueEvents = queueEvents;
 138  16
     }
 139  
 
 140  
     public QueueProfile getQueueProfile()
 141  
     {
 142  0
         return queueProfile;
 143  
     }
 144  
 
 145  
     public void setQueueProfile(QueueProfile queueProfile)
 146  
     {
 147  0
         this.queueProfile = queueProfile;
 148  0
     }
 149  
 
 150  
     VMMessageReceiver getReceiver(UMOEndpointURI endpointUri) throws EndpointException
 151  
     {
 152  18
         return (VMMessageReceiver)getReceiverByEndpoint(endpointUri);
 153  
     }
 154  
 
 155  
     QueueSession getQueueSession() throws InitialisationException
 156  
     {
 157  106
         QueueManager qm = MuleManager.getInstance().getQueueManager();
 158  106
         UMOTransaction tx = TransactionCoordination.getInstance().getTransaction();
 159  106
         if (tx != null)
 160  
         {
 161  10
             if (tx.hasResource(qm))
 162  
             {
 163  9
                 final QueueSession queueSession = (QueueSession) tx.getResource(qm);
 164  10
                 if (logger.isDebugEnabled())
 165  
                 {
 166  0
                     logger.debug("Retrieved VM queue session " + queueSession + " from current transaction " + tx);
 167  
                 }
 168  10
                 return queueSession;
 169  
             }
 170  
         }
 171  
 
 172  96
         if (logger.isDebugEnabled())
 173  
         {
 174  0
             logger.debug("Retrieving new VM queue session from queue manager");
 175  
         }
 176  
 
 177  96
         QueueSession session = qm.getQueueSession();
 178  96
         if (tx != null)
 179  
         {
 180  0
             if (logger.isDebugEnabled())
 181  
             {
 182  0
                 logger.debug("Binding VM queue session " + session + " to current transaction " + tx);
 183  
             }
 184  
             try
 185  
             {
 186  0
                 tx.bindResource(qm, session);
 187  
             }
 188  0
             catch (TransactionException e)
 189  
             {
 190  0
                 throw new RuntimeException("Could not bind queue session to current transaction", e);
 191  0
             }
 192  
         }
 193  96
         return session;
 194  
     }
 195  
 
 196  
     protected UMOMessageReceiver getReceiverByEndpoint(UMOEndpointURI endpointUri) throws EndpointException
 197  
     {
 198  18
         if (logger.isDebugEnabled())
 199  
         {
 200  0
             logger.debug("Looking up vm receiver for address: " + endpointUri.toString());
 201  
         }
 202  
 
 203  
         UMOMessageReceiver receiver;
 204  
         // If we have an exact match, use it
 205  18
         receiver = (UMOMessageReceiver)receivers.get(endpointUri.getAddress());
 206  18
         if (receiver != null)
 207  
         {
 208  14
             if (logger.isDebugEnabled())
 209  
             {
 210  0
                 logger.debug("Found exact receiver match on endpointUri: " + endpointUri);
 211  
             }
 212  14
             return receiver;
 213  
         }
 214  
 
 215  
         // otherwise check each one against a wildcard match
 216  4
         for (Iterator iterator = receivers.values().iterator(); iterator.hasNext();)
 217  
         {
 218  8
             receiver = (UMOMessageReceiver)iterator.next();
 219  8
             String filterAddress = receiver.getEndpointURI().getAddress();
 220  8
             WildcardFilter filter = new WildcardFilter(filterAddress);
 221  8
             if (filter.accept(endpointUri.getAddress()))
 222  
             {
 223  0
                 receiver.getEndpoint().setEndpointURI(new MuleEndpointURI(endpointUri, filterAddress));
 224  
 
 225  0
                 if (logger.isDebugEnabled())
 226  
                 {
 227  0
                     logger.debug("Found receiver match on endpointUri: " + receiver.getEndpointURI()
 228  
                                  + " against " + endpointUri);
 229  
                 }
 230  0
                 return receiver;
 231  
             }
 232  8
         }
 233  4
         if (logger.isDebugEnabled())
 234  
         {
 235  0
             logger.debug("No receiver found for endpointUri: " + endpointUri);
 236  
         }
 237  4
         return null;
 238  
     }
 239  
 
 240  
     // @Override
 241  
     public boolean isRemoteSyncEnabled()
 242  
     {
 243  32
         return true;
 244  
     }
 245  
 
 246  
     public int getQueueTimeout()
 247  
     {
 248  44
         return queueTimeout;
 249  
     }
 250  
 
 251  
     public void setQueueTimeout(int queueTimeout)
 252  
     {
 253  0
         this.queueTimeout = queueTimeout;
 254  0
     }
 255  
 
 256  
 }