View Javadoc

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