1   /*
2    * $Id: InboundTransformingForwardingCatchAllStrategy.java 10961 2008-02-22 19:01:02Z dfeist $
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.test.usecases.routing;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.RequestContext;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.MuleMessage;
18  import org.mule.api.MuleSession;
19  import org.mule.api.endpoint.OutboundEndpoint;
20  import org.mule.api.routing.RoutingException;
21  import org.mule.api.routing.ServiceRoutingException;
22  import org.mule.config.i18n.CoreMessages;
23  import org.mule.routing.AbstractCatchAllStrategy;
24  
25  public class InboundTransformingForwardingCatchAllStrategy extends AbstractCatchAllStrategy
26  {
27      public MuleMessage catchMessage(MuleMessage message, MuleSession session, boolean synchronous)
28          throws RoutingException
29      {
30          OutboundEndpoint endpoint = this.getEndpoint();
31  
32          if (endpoint == null)
33          {
34              throw new ServiceRoutingException(
35                  CoreMessages.noCatchAllEndpointSet(), message, this.getEndpoint(), session.getService());
36          }
37          try
38          {
39              message = new DefaultMuleMessage(RequestContext.getEventContext().transformMessage(), message);
40              MuleEvent newEvent = new DefaultMuleEvent(message, endpoint, session, synchronous);
41  
42              if (synchronous)
43              {
44                  MuleMessage result = endpoint.send(newEvent);
45                  if (statistics != null)
46                  {
47                      statistics.incrementRoutedMessage(getEndpoint());
48                  }
49                  return result;
50              }
51              else
52              {
53                  endpoint.dispatch(newEvent);
54                  if (statistics != null)
55                  {
56                      statistics.incrementRoutedMessage(getEndpoint());
57                  }
58                  return null;
59              }
60  
61          }
62          catch (Exception e)
63          {
64              throw new RoutingException(message, endpoint, e);
65          }
66      }
67  }