KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > outbound > TemplateEndpointRouter


1 /*
2  * $Id: TemplateEndpointRouter.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.routing.outbound;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.impl.endpoint.MuleEndpointURI;
16 import org.mule.umo.UMOException;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.UMOSession;
19 import org.mule.umo.endpoint.UMOEndpoint;
20 import org.mule.umo.endpoint.UMOEndpointURI;
21 import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
22 import org.mule.umo.routing.RoutePathNotFoundException;
23 import org.mule.umo.routing.RoutingException;
24 import org.mule.util.TemplateParser;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * The template endpoint router allows endpoints to be alered at runtime based on
32  * properties set on the current event of fallback values set on the endpoint
33  * properties. Templated values are expressed using square braces around a property
34  * name i.e. axis:http://localhost:8082/MyService?method=[SOAP_METHOD] Note that Ant
35  * style property templates cannot be used in valid URI strings, so we must use
36  * Square braces instead
37  *
38  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
39  * @version $Revision: 3798 $
40  */

41 public class TemplateEndpointRouter extends FilteringOutboundRouter
42 {
43
44     // We used Square templates as they can exist as part of an uri.
45
private TemplateParser parser = TemplateParser.createSquareBracesStyleParser();
46
47     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
48         throws RoutingException
49     {
50         UMOMessage result = null;
51         if (endpoints == null || endpoints.size() == 0)
52         {
53             throw new RoutePathNotFoundException(new Message(Messages.NO_ENDPOINTS_FOR_ROUTER), message, null);
54         }
55         try
56         {
57             UMOEndpoint ep = (UMOEndpoint)endpoints.get(0);
58             String JavaDoc uri = ep.getEndpointURI().toString();
59             if (logger.isDebugEnabled())
60             {
61                 logger.debug("Uri before parsing is: " + uri);
62             }
63             Map JavaDoc props = new HashMap JavaDoc();
64             // Also add the endpoint propertie so that users can set fallback values
65
// when the property is not set on the event
66
props.putAll(ep.getProperties());
67             for (Iterator JavaDoc iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
68             {
69                 String JavaDoc propertyKey = (String JavaDoc)iterator.next();
70                 props.put(propertyKey, message.getProperty(propertyKey));
71             }
72             uri = parser.parse(props, uri);
73             if (logger.isDebugEnabled())
74             {
75                 logger.debug("Uri after parsing is: " + uri);
76             }
77             UMOEndpointURI newUri = new MuleEndpointURI(uri);
78             if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme()))
79             {
80                 throw new CouldNotRouteOutboundMessageException(new Message(
81                     Messages.SCHEME_CANT_CHANGE_FOR_ROUTER_X_X, ep.getEndpointURI().getScheme(),
82                     newUri.getScheme()), message, ep);
83             }
84             ep.setEndpointURI(new MuleEndpointURI(uri));
85             if (synchronous)
86             {
87                 result = send(session, message, ep);
88             }
89             else
90             {
91                 dispatch(session, message, ep);
92             }
93         }
94         catch (UMOException e)
95         {
96             throw new CouldNotRouteOutboundMessageException(message, (UMOEndpoint)endpoints.get(0), e);
97         }
98         return result;
99     }
100
101 }
102
Popular Tags