View Javadoc

1   /*
2    * $Id: VMMessageDispatcher.java 11756 2008-05-14 16:43:50Z aguenther $
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.transport.vm;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.EndpointURI;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.api.transaction.TransactionCallback;
18  import org.mule.api.transport.DispatchException;
19  import org.mule.api.transport.NoReceiverForEndpointException;
20  import org.mule.config.i18n.CoreMessages;
21  import org.mule.transaction.TransactionTemplate;
22  import org.mule.transport.AbstractMessageDispatcher;
23  import org.mule.transport.vm.i18n.VMMessages;
24  import org.mule.util.queue.Queue;
25  import org.mule.util.queue.QueueSession;
26  
27  /**
28   * <code>VMMessageDispatcher</code> is used for providing in memory interaction
29   * between components.
30   */
31  public class VMMessageDispatcher extends AbstractMessageDispatcher
32  {
33      private final VMConnector connector;
34  
35      public VMMessageDispatcher(OutboundEndpoint endpoint)
36      {
37          super(endpoint);
38          this.connector = (VMConnector) endpoint.getConnector();
39      }
40  
41      protected void doDispatch(final MuleEvent event) throws Exception
42      {
43          EndpointURI endpointUri = event.getEndpoint().getEndpointURI();
44          //Apply any outbound transformers on this event before we dispatch
45          event.transformMessage();
46  
47          if (endpointUri == null)
48          {
49              throw new DispatchException(
50                      CoreMessages.objectIsNull("Endpoint"), event.getMessage(), event.getEndpoint());
51          }
52          if (connector.isQueueEvents())
53          {
54              QueueSession session = connector.getQueueSession();
55              Queue queue = session.getQueue(endpointUri.getAddress());
56              queue.put(event.getMessage());
57          }
58          else
59          {
60              final VMMessageReceiver receiver = connector.getReceiver(event.getEndpoint().getEndpointURI());
61              if (receiver == null)
62              {
63                  logger.warn("No receiver for endpointUri: " + event.getEndpoint().getEndpointURI());
64                  return;
65              }
66              MuleMessage message = event.getMessage(); 
67              connector.getSessionHandler().storeSessionInfoToMessage(event.getSession(), message);
68              TransactionTemplate tt = new TransactionTemplate(receiver.getEndpoint().getTransactionConfig(), 
69                  connector.getExceptionListener(), event.getMuleContext());
70  
71              TransactionCallback cb = new TransactionCallback()
72              {
73                  public Object doInTransaction() throws Exception
74                  {
75                      receiver.onMessage(event.getMessage());
76                      return null;
77                  }
78              };
79              tt.execute(cb);
80          }
81          if (logger.isDebugEnabled())
82          {
83              logger.debug("dispatched MuleEvent on endpointUri: " + endpointUri);
84          }
85      }
86  
87      protected MuleMessage doSend(final MuleEvent event) throws Exception
88      {
89          MuleMessage retMessage;
90          EndpointURI endpointUri = event.getEndpoint().getEndpointURI();
91          final VMMessageReceiver receiver = connector.getReceiver(endpointUri);
92          //Apply any outbound transformers on this event before we dispatch
93          event.transformMessage();
94          if (receiver == null)
95          {
96              if (connector.isQueueEvents())
97              {
98                  if (logger.isDebugEnabled())
99                  {
100                     logger.debug("Writing to queue as there is no receiver on connector: "
101                             + connector.getName() + ", for endpointUri: "
102                             + event.getEndpoint().getEndpointURI());
103                 }
104                 doDispatch(event);
105                 return null;
106             }
107             else
108             {
109                 throw new NoReceiverForEndpointException(
110                         VMMessages.noReceiverForEndpoint(connector.getName(),
111                                 event.getEndpoint().getEndpointURI()));
112             }
113         }
114 
115         MuleMessage message = event.getMessage(); 
116         connector.getSessionHandler().storeSessionInfoToMessage(event.getSession(), message);
117 
118         TransactionTemplate tt = new TransactionTemplate(receiver.getEndpoint().getTransactionConfig(), 
119             connector.getExceptionListener(), event.getMuleContext());
120         
121         TransactionCallback cb = new TransactionCallback()
122         {
123             public Object doInTransaction() throws Exception
124             {
125                 return receiver.onCall(event.getMessage(), true);
126             }
127         };
128         retMessage = (MuleMessage) tt.execute(cb);
129         
130         if (logger.isDebugEnabled())
131         {
132             logger.debug("sent event on endpointUri: " + event.getEndpoint().getEndpointURI());
133         }
134         return retMessage;
135     }
136 
137     protected void doDispose()
138     {
139         // template method
140     }
141 
142     protected void doConnect() throws Exception
143     {
144         if (connector.isQueueEvents())
145         {
146             // use the default queue profile to configure this queue.
147             connector.getQueueProfile().configureQueue(
148                     endpoint.getEndpointURI().getAddress(), connector.getQueueManager());
149         }
150     }
151 
152     protected void doDisconnect() throws Exception
153     {
154         // template method
155     }
156 
157 }