View Javadoc

1   /*
2    * $Id: TemplateEndpointRouter.java 10961 2008-02-22 19:01:02Z dfeist $
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.udp.functional;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.endpoint.EndpointURI;
17  import org.mule.api.endpoint.OutboundEndpoint;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.routing.CouldNotRouteOutboundMessageException;
20  import org.mule.api.routing.RoutePathNotFoundException;
21  import org.mule.api.routing.RoutingException;
22  import org.mule.config.i18n.CoreMessages;
23  import org.mule.endpoint.DynamicURIOutboundEndpoint;
24  import org.mule.endpoint.MuleEndpointURI;
25  import org.mule.routing.outbound.FilteringOutboundRouter;
26  
27  public class DynamicEndpointRouter extends FilteringOutboundRouter
28  {
29      @Override
30      public MuleEvent route(MuleEvent event) throws RoutingException
31      {
32          MuleMessage message = event.getMessage();
33          MuleEvent result;
34  
35          if (routes == null || routes.size() == 0)
36          {
37              throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), event, null);
38          }
39  
40          try
41          {
42              MessageProcessor ep = routes.get(0);
43              EndpointURI newUri;
44  
45              if (ep instanceof OutboundEndpoint)
46              {
47                  for (String propertyKey : message.getOutboundPropertyNames())
48                  {
49                      Object propertyValue = message.getOutboundProperty(propertyKey);
50                      if (propertyKey.equalsIgnoreCase("packet.port"))
51                      {
52                          int port = (Integer) propertyValue;
53                          newUri = new MuleEndpointURI("udp://localhost:" + port, muleContext);
54                          if (logger.isDebugEnabled())
55                          {
56                              logger.info("Uri after parsing is: " + newUri.getAddress());
57                          }
58                          ep = new DynamicURIOutboundEndpoint((OutboundEndpoint) ep, newUri);
59                          break;
60                      }
61                  }
62              }
63              result = sendRequest(event, message, ep, true);
64          }
65          catch (MuleException e)
66          {
67              throw new CouldNotRouteOutboundMessageException(event, routes.get(0), e);
68          }
69  
70          return result;
71      }
72  }