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