1
2
3
4
5
6
7
8
9
10
11 package org.mule.routing.response;
12
13 import org.mule.config.MuleConfiguration;
14 import org.mule.management.stats.RouterStatistics;
15 import org.mule.routing.AbstractRouterCollection;
16 import org.mule.umo.UMOEvent;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.endpoint.UMOEndpoint;
19 import org.mule.umo.routing.RoutingException;
20 import org.mule.umo.routing.UMOResponseRouter;
21 import org.mule.umo.routing.UMOResponseRouterCollection;
22 import org.mule.umo.routing.UMORouter;
23
24 import java.util.Iterator;
25 import java.util.List;
26
27 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
28
29
30
31
32
33
34 public class ResponseRouterCollection extends AbstractRouterCollection implements UMOResponseRouterCollection
35 {
36 private volatile List endpoints = new CopyOnWriteArrayList();
37 private volatile int timeout = MuleConfiguration.DEFAULT_TIMEOUT;
38 private volatile boolean failOnTimeout = true;
39
40 public ResponseRouterCollection()
41 {
42 super(RouterStatistics.TYPE_RESPONSE);
43 }
44
45 public void route(UMOEvent event) throws RoutingException
46 {
47 UMOResponseRouter router;
48 for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
49 {
50 router = (UMOResponseRouter) iterator.next();
51 router.process(event);
52
53 if (getStatistics().isEnabled())
54 {
55 getStatistics().incrementRoutedMessage(event.getEndpoint());
56 }
57 }
58 }
59
60 public UMOMessage getResponse(UMOMessage message) throws RoutingException
61 {
62 UMOMessage result = null;
63 if (routers.size() == 0)
64 {
65 logger.warn("There are no routers configured on the response router. Returning the current message");
66 result = message;
67 }
68 else
69 {
70 UMOResponseRouter router;
71 for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
72 {
73 router = (UMOResponseRouter) iterator.next();
74 result = router.getResponse(message);
75 }
76
77 if (result == null)
78 {
79
80 if (getStatistics().isEnabled())
81 {
82 getStatistics().incrementNoRoutedMessage();
83 }
84 }
85 }
86
87
88
89
90
91
92
93
94
95 return result;
96
97 }
98
99 public void addRouter(UMORouter router)
100 {
101 ((UMOResponseRouter) router).setTimeout(getTimeout());
102 ((UMOResponseRouter) router).setFailOnTimeout(isFailOnTimeout());
103 routers.add(router);
104 }
105
106 public UMOResponseRouter removeRouter(UMOResponseRouter router)
107 {
108 if (routers.remove(router))
109 {
110 return router;
111 }
112 else
113 {
114 return null;
115 }
116 }
117
118 public void addEndpoint(UMOEndpoint endpoint)
119 {
120 if (endpoint != null)
121 {
122 endpoint.setType(UMOEndpoint.ENDPOINT_TYPE_RESPONSE);
123 endpoints.add(endpoint);
124 }
125 else
126 {
127 throw new IllegalArgumentException("endpoint = null");
128 }
129 }
130
131 public boolean removeEndpoint(UMOEndpoint endpoint)
132 {
133 return endpoints.remove(endpoint);
134 }
135
136 public List getEndpoints()
137 {
138 return endpoints;
139 }
140
141 public void setEndpoints(List endpoints)
142 {
143 if (endpoints != null)
144 {
145 this.endpoints.clear();
146 this.endpoints.addAll(endpoints);
147
148
149 for (Iterator it = this.endpoints.iterator(); it.hasNext();)
150 {
151 ((UMOEndpoint) it.next()).setType(UMOEndpoint.ENDPOINT_TYPE_RESPONSE);
152 }
153 }
154 else
155 {
156 throw new IllegalArgumentException("List of endpoints = null");
157 }
158 }
159
160
161
162
163
164
165 public UMOEndpoint getEndpoint(String name)
166 {
167 UMOEndpoint endpointDescriptor;
168 for (Iterator iterator = endpoints.iterator(); iterator.hasNext();)
169 {
170 endpointDescriptor = (UMOEndpoint) iterator.next();
171 if (endpointDescriptor.getName().equals(name))
172 {
173 return endpointDescriptor;
174 }
175 }
176 return null;
177 }
178
179 public int getTimeout()
180 {
181 return timeout;
182 }
183
184 public void setTimeout(int timeout)
185 {
186 this.timeout = timeout;
187 }
188
189
190 public boolean isFailOnTimeout()
191 {
192 return failOnTimeout;
193 }
194
195 public void setFailOnTimeout(boolean failOnTimeout)
196 {
197 this.failOnTimeout = failOnTimeout;
198 }
199 }