View Javadoc

1   /*
2    * $Id: VMConnector.java 19191 2010-08-25 21:05:23Z 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.transport.vm;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.construct.FlowConstruct;
16  import org.mule.api.endpoint.EndpointException;
17  import org.mule.api.endpoint.EndpointURI;
18  import org.mule.api.endpoint.InboundEndpoint;
19  import org.mule.api.lifecycle.InitialisationException;
20  import org.mule.api.transaction.Transaction;
21  import org.mule.api.transaction.TransactionException;
22  import org.mule.api.transport.MessageReceiver;
23  import org.mule.config.QueueProfile;
24  import org.mule.endpoint.DynamicURIInboundEndpoint;
25  import org.mule.endpoint.MuleEndpointURI;
26  import org.mule.routing.filters.WildcardFilter;
27  import org.mule.transaction.TransactionCoordination;
28  import org.mule.transport.AbstractConnector;
29  import org.mule.util.queue.QueueManager;
30  import org.mule.util.queue.QueueSession;
31  
32  import java.util.Iterator;
33  
34  /**
35   * <code>VMConnector</code> A simple endpoint wrapper to allow a Mule service to
36   * <p/> be accessed from an endpoint
37   * 
38   */
39  public class VMConnector extends AbstractConnector
40  {
41  
42      public static final String VM = "vm";
43      private QueueProfile queueProfile;
44      private Integer queueTimeout;
45      /** The queue manager to use for vm queues only */
46      private QueueManager queueManager;
47      
48      public VMConnector(MuleContext context)
49      {
50          super(context);
51      }
52  
53      @Override
54      protected void doInitialise() throws InitialisationException
55      {
56          if (queueTimeout == null)
57          {
58              queueTimeout = muleContext.getConfiguration().getDefaultQueueTimeout();
59          }
60          if (queueManager == null)
61          {
62              queueManager = getMuleContext().getQueueManager();
63          }
64          if (queueProfile == null)
65          {
66              // create a default QueueProfile
67              queueProfile = new QueueProfile();
68              if (logger.isDebugEnabled())
69              {
70                  logger.debug("created default QueueProfile for VM connector: " + queueProfile);
71              }
72          }
73      }
74  
75      @Override
76      protected void doDispose()
77      {
78          // template method
79      }
80  
81      @Override
82      protected void doConnect() throws Exception
83      {
84          // template method
85      }
86  
87      @Override
88      protected void doDisconnect() throws Exception
89      {
90          // template method
91      }
92  
93      @Override
94      protected void doStart() throws MuleException
95      {
96          // template method
97      }
98  
99      @Override
100     protected void doStop() throws MuleException
101     {
102         // template method
103     }
104 
105     @Override
106     public MessageReceiver createReceiver(FlowConstruct flowConstruct, InboundEndpoint endpoint) throws Exception
107     {
108         if (!endpoint.getExchangePattern().hasResponse())
109         {
110             queueProfile.configureQueue(endpoint.getEndpointURI().getAddress(), queueManager);
111         }
112         return serviceDescriptor.createMessageReceiver(this, flowConstruct, endpoint);
113     }
114 
115     public String getProtocol()
116     {
117         return "VM";
118     }
119 
120     public QueueProfile getQueueProfile()
121     {
122         return queueProfile;
123     }
124 
125     public void setQueueProfile(QueueProfile queueProfile)
126     {
127         this.queueProfile = queueProfile;
128     }
129 
130     VMMessageReceiver getReceiver(EndpointURI endpointUri) throws EndpointException
131     {
132         return (VMMessageReceiver)getReceiverByEndpoint(endpointUri);
133     }
134 
135     QueueSession getQueueSession() throws InitialisationException
136     {
137         Transaction tx = TransactionCoordination.getInstance().getTransaction();
138         if (tx != null)
139         {
140             if (tx.hasResource(queueManager))
141             {
142                 final QueueSession queueSession = (QueueSession) tx.getResource(queueManager);
143                 if (logger.isDebugEnabled())
144                 {
145                     logger.debug("Retrieved VM queue session " + queueSession + " from current transaction " + tx);
146                 }
147                 return queueSession;
148             }
149         }
150 
151         //This get printed every second for every thread
152 //        if (logger.isDebugEnabled())
153 //        {
154 //            logger.debug("Retrieving new VM queue session from queue manager");
155 //        }
156 
157         QueueSession session = queueManager.getQueueSession();
158         if (tx != null)
159         {
160             //This get printed every second for every thread
161 //            if (logger.isDebugEnabled())
162 //            {
163 //                logger.debug("Binding VM queue session " + session + " to current transaction " + tx);
164 //            }
165             try
166             {
167                 tx.bindResource(queueManager, session);
168             }
169             catch (TransactionException e)
170             {
171                 throw new RuntimeException("Could not bind queue session to current transaction", e);
172             }
173         }
174         return session;
175     }
176 
177     protected MessageReceiver getReceiverByEndpoint(EndpointURI endpointUri) throws EndpointException
178     {
179         if (logger.isDebugEnabled())
180         {
181             logger.debug("Looking up vm receiver for address: " + endpointUri.toString());
182         }
183 
184         MessageReceiver receiver;
185         // If we have an exact match, use it
186         receiver = receivers.get(endpointUri.getAddress());
187         if (receiver != null)
188         {
189             if (logger.isDebugEnabled())
190             {
191                 logger.debug("Found exact receiver match on endpointUri: " + endpointUri);
192             }
193             return receiver;
194         }
195 
196         // otherwise check each one against a wildcard match
197         for (Iterator iterator = receivers.values().iterator(); iterator.hasNext();)
198         {
199             receiver = (MessageReceiver)iterator.next();
200             String filterAddress = receiver.getEndpointURI().getAddress();
201             WildcardFilter filter = new WildcardFilter(filterAddress);
202             if (filter.accept(endpointUri.getAddress()))
203             {
204                 InboundEndpoint endpoint = receiver.getEndpoint();
205                 EndpointURI newEndpointURI = new MuleEndpointURI(endpointUri, filterAddress);
206                 receiver.setEndpoint(new DynamicURIInboundEndpoint(endpoint, newEndpointURI));
207 
208                 if (logger.isDebugEnabled())
209                 {
210                     logger.debug("Found receiver match on endpointUri: " + receiver.getEndpointURI()
211                                  + " against " + endpointUri);
212                 }
213                 return receiver;
214             }
215         }
216         if (logger.isDebugEnabled())
217         {
218             logger.debug("No receiver found for endpointUri: " + endpointUri);
219         }
220         return null;
221     }
222 
223     @Override
224     public boolean isResponseEnabled()
225     {
226         return true;
227     }                                                      
228 
229     public int getQueueTimeout()
230     {
231         return queueTimeout;
232     }
233 
234     public void setQueueTimeout(int queueTimeout)
235     {
236         this.queueTimeout = queueTimeout;
237     }
238 
239     public QueueManager getQueueManager()
240     {
241         return queueManager;
242     }
243 
244 }