KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: FilteringOutboundRouter.java 4259 2006-12-14 03:12:07Z 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 java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19 import org.mule.impl.MuleMessage;
20 import org.mule.impl.endpoint.MuleEndpointURI;
21 import org.mule.umo.UMOException;
22 import org.mule.umo.UMOFilter;
23 import org.mule.umo.UMOMessage;
24 import org.mule.umo.UMOSession;
25 import org.mule.umo.endpoint.EndpointException;
26 import org.mule.umo.endpoint.UMOEndpoint;
27 import org.mule.umo.endpoint.UMOEndpointURI;
28 import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
29 import org.mule.umo.routing.RoutePathNotFoundException;
30 import org.mule.umo.routing.RoutingException;
31 import org.mule.umo.transformer.TransformerException;
32 import org.mule.umo.transformer.UMOTransformer;
33 import org.mule.util.TemplateParser;
34
35 /**
36  * <code>FilteringRouter</code> is a router that accepts events based on a filter
37  * set.
38  */

39
40 public class FilteringOutboundRouter extends AbstractOutboundRouter
41 {
42     private UMOTransformer transformer;
43
44     private UMOFilter filter;
45
46     private boolean useTemplates = false;
47
48     // We used Square templates as they can exist as part of an URI.
49
private TemplateParser parser = TemplateParser.createSquareBracesStyleParser();
50
51     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
52         throws RoutingException
53     {
54         UMOMessage result = null;
55
56         if (endpoints == null || endpoints.size() == 0)
57         {
58             throw new RoutePathNotFoundException(new Message(Messages.NO_ENDPOINTS_FOR_ROUTER), message, null);
59         }
60
61         UMOEndpoint ep = getEndpoint(0, message);
62
63         try
64         {
65             if (synchronous)
66             {
67                 result = send(session, message, ep);
68             }
69             else
70             {
71                 dispatch(session, message, ep);
72             }
73         }
74         catch (UMOException e)
75         {
76             throw new CouldNotRouteOutboundMessageException(message, ep, e);
77         }
78         return result;
79     }
80
81     public UMOFilter getFilter()
82     {
83         return filter;
84     }
85
86     public void setFilter(UMOFilter filter)
87     {
88         this.filter = filter;
89     }
90
91     public boolean isMatch(UMOMessage message) throws RoutingException
92     {
93         if (getFilter() == null)
94         {
95             return true;
96         }
97         if (transformer != null)
98         {
99             try
100             {
101                 Object JavaDoc payload = transformer.transform(message.getPayload());
102                 message = new MuleMessage(payload, message);
103             }
104             catch (TransformerException e)
105             {
106                 throw new RoutingException(new Message(Messages.TRANSFORM_FAILED_BEFORE_FILTER), message,
107                     (UMOEndpoint)endpoints.get(0), e);
108             }
109         }
110         return getFilter().accept(message);
111     }
112
113     public UMOTransformer getTransformer()
114     {
115         return transformer;
116     }
117
118     public void setTransformer(UMOTransformer transformer)
119     {
120         this.transformer = transformer;
121     }
122
123     public void addEndpoint(UMOEndpoint endpoint)
124     {
125         if (!useTemplates && parser.isContainsTemplate(endpoint.getEndpointURI().toString()))
126         {
127             useTemplates = true;
128         }
129         super.addEndpoint(endpoint);
130     }
131
132     /**
133      * Will Return the endpont at the given index and will resolve any template tags
134      * on the Endpoint URI if necessary
135      *
136      * @param index the index of the endpoint to get
137      * @param message the current message. This is required if template matching is
138      * being used
139      * @return the endpoint at the index, with any template tags resolved
140      * @throws CouldNotRouteOutboundMessageException if the template causs the
141      * endpoint to become illegal or malformed
142      */

143     public UMOEndpoint getEndpoint(int index, UMOMessage message)
144         throws CouldNotRouteOutboundMessageException
145     {
146         if (!useTemplates)
147         {
148             return (UMOEndpoint)endpoints.get(index);
149         }
150         else
151         {
152             UMOEndpoint ep = (UMOEndpoint)endpoints.get(index);
153             String JavaDoc uri = ep.getEndpointURI().toString();
154             if (logger.isDebugEnabled())
155             {
156                 logger.debug("Uri before parsing is: " + uri);
157             }
158             Map JavaDoc props = new HashMap JavaDoc();
159             // Also add the endpoint propertie so that users can set fallback values
160
// when the property is not set on the event
161
props.putAll(ep.getProperties());
162             for (Iterator JavaDoc iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
163             {
164                 String JavaDoc propertyKey = (String JavaDoc)iterator.next();
165                 props.put(propertyKey, message.getProperty(propertyKey));
166             }
167             String JavaDoc newUriString = parser.parse(props, uri);
168             if (logger.isDebugEnabled())
169             {
170                 logger.debug("Uri after parsing is: " + uri);
171             }
172             try
173             {
174                 UMOEndpointURI newUri = new MuleEndpointURI(newUriString);
175                 if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme()))
176                 {
177                     throw new CouldNotRouteOutboundMessageException(new Message(
178                         Messages.SCHEME_CANT_CHANGE_FOR_ROUTER_X_X, ep.getEndpointURI().getScheme(),
179                         newUri.getScheme()), message, ep);
180                 }
181                 ep.setEndpointURI(newUri);
182             }
183             catch (EndpointException e)
184             {
185                 throw new CouldNotRouteOutboundMessageException(new Message(
186                     Messages.TEMPLATE_X_CAUSED_MALFORMED_ENDPOINT_X, uri, newUriString), message, ep, e);
187             }
188
189             return ep;
190         }
191     }
192
193     public boolean isUseTemplates()
194     {
195         return useTemplates;
196     }
197
198     public void setUseTemplates(boolean useTemplates)
199     {
200         this.useTemplates = useTemplates;
201     }
202 }
203
Popular Tags