1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.ra; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.ConfigurationBuilder; |
15 | |
import org.mule.config.ConfigurationException; |
16 | |
import org.mule.impl.MuleDescriptor; |
17 | |
import org.mule.impl.endpoint.MuleEndpointURI; |
18 | |
import org.mule.impl.model.ModelFactory; |
19 | |
import org.mule.providers.service.TransportFactory; |
20 | |
import org.mule.umo.UMODescriptor; |
21 | |
import org.mule.umo.UMOException; |
22 | |
import org.mule.umo.endpoint.EndpointException; |
23 | |
import org.mule.umo.endpoint.MalformedEndpointException; |
24 | |
import org.mule.umo.endpoint.UMOEndpoint; |
25 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
26 | |
import org.mule.umo.manager.UMOManager; |
27 | |
import org.mule.umo.model.UMOModel; |
28 | |
import org.mule.util.ClassUtils; |
29 | |
|
30 | |
import java.io.IOException; |
31 | |
import java.io.ObjectInputStream; |
32 | |
import java.io.Serializable; |
33 | |
import java.util.HashMap; |
34 | |
import java.util.Map; |
35 | |
|
36 | |
import javax.resource.NotSupportedException; |
37 | |
import javax.resource.ResourceException; |
38 | |
import javax.resource.spi.ActivationSpec; |
39 | |
import javax.resource.spi.BootstrapContext; |
40 | |
import javax.resource.spi.ResourceAdapter; |
41 | |
import javax.resource.spi.ResourceAdapterInternalException; |
42 | |
import javax.resource.spi.endpoint.MessageEndpointFactory; |
43 | |
import javax.transaction.xa.XAResource; |
44 | |
|
45 | |
import org.apache.commons.logging.Log; |
46 | |
import org.apache.commons.logging.LogFactory; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class MuleResourceAdapter implements ResourceAdapter, Serializable |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
|
56 | |
private static final long serialVersionUID = 5727648958127416509L; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 24 | protected transient Log logger = LogFactory.getLog(this.getClass()); |
62 | |
|
63 | |
protected transient UMOManager manager; |
64 | |
|
65 | |
protected transient BootstrapContext bootstrapContext; |
66 | 24 | protected MuleConnectionRequestInfo info = new MuleConnectionRequestInfo(); |
67 | 24 | protected final Map endpoints = new HashMap(); |
68 | |
protected String defaultJcaModelName; |
69 | |
|
70 | |
public MuleResourceAdapter() |
71 | 24 | { |
72 | 24 | MuleManager.getConfiguration().setModelType(JcaModel.JCA_MODEL_TYPE); |
73 | 24 | } |
74 | |
|
75 | |
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException |
76 | |
{ |
77 | 0 | ois.defaultReadObject(); |
78 | 0 | this.logger = LogFactory.getLog(this.getClass()); |
79 | 0 | this.manager = MuleManager.getInstance(); |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public void start(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException |
86 | |
{ |
87 | 0 | this.bootstrapContext = bootstrapContext; |
88 | 0 | if (info.getConfigurations() != null) |
89 | |
{ |
90 | 0 | if (MuleManager.isInstanciated()) |
91 | |
{ |
92 | 0 | throw new ResourceAdapterInternalException( |
93 | |
"A manager is already configured, cannot configure a new one using the configurations set on the Resource Adapter"); |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | 0 | ConfigurationBuilder builder = null; |
98 | |
try |
99 | |
{ |
100 | 0 | builder = (ConfigurationBuilder) ClassUtils.instanciateClass(info.getConfigurationBuilder(), |
101 | |
ClassUtils.NO_ARGS); |
102 | |
|
103 | |
} |
104 | 0 | catch (Exception e) |
105 | |
{ |
106 | 0 | throw new ResourceAdapterInternalException("Failed to instanciate configurationBuilder class: " |
107 | |
+ info.getConfigurationBuilder(), e); |
108 | 0 | } |
109 | |
|
110 | |
try |
111 | |
{ |
112 | 0 | manager = builder.configure(info.getConfigurations(), null); |
113 | |
} |
114 | 0 | catch (ConfigurationException e) |
115 | |
{ |
116 | 0 | logger.error(e); |
117 | 0 | throw new ResourceAdapterInternalException("Failed to load configurations: " |
118 | |
+ info.getConfigurations(), e); |
119 | 0 | } |
120 | |
} |
121 | |
} |
122 | 0 | manager = MuleManager.getInstance(); |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void stop() |
129 | |
{ |
130 | 0 | manager.dispose(); |
131 | 0 | manager = null; |
132 | 0 | bootstrapContext = null; |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public BootstrapContext getBootstrapContext() |
139 | |
{ |
140 | 0 | return bootstrapContext; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec) |
148 | |
throws ResourceException |
149 | |
{ |
150 | 10 | if (activationSpec.getResourceAdapter() != this) |
151 | |
{ |
152 | 2 | throw new ResourceException("ActivationSpec not initialized with this ResourceAdapter instance"); |
153 | |
} |
154 | |
|
155 | 10 | if (activationSpec.getClass().equals(MuleActivationSpec.class)) |
156 | |
{ |
157 | 8 | MuleActivationSpec muleActivationSpec = (MuleActivationSpec) activationSpec; |
158 | |
try |
159 | |
{ |
160 | |
|
161 | 8 | String modelName = resolveModelName(muleActivationSpec); |
162 | |
|
163 | |
|
164 | 8 | JcaModel model = getJcaModel(modelName); |
165 | |
|
166 | |
|
167 | 8 | UMOEndpoint endpoint = createMessageInflowEndpoint(muleActivationSpec); |
168 | |
|
169 | |
|
170 | 8 | MuleDescriptor descriptor = createJcaComponent(endpointFactory, model, endpoint); |
171 | |
|
172 | |
|
173 | 6 | MuleEndpointKey key = new MuleEndpointKey(endpointFactory, muleActivationSpec); |
174 | 6 | endpoints.put(key, descriptor); |
175 | |
} |
176 | 2 | catch (Exception e) |
177 | |
{ |
178 | 2 | logger.error(e.getMessage(), e); |
179 | 6 | } |
180 | 8 | } |
181 | |
else |
182 | |
{ |
183 | 0 | throw new NotSupportedException("That type of ActicationSpec not supported: " + activationSpec.getClass()); |
184 | |
} |
185 | |
|
186 | 8 | } |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
public void endpointDeactivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec) |
193 | |
{ |
194 | |
|
195 | 2 | if (activationSpec.getClass().equals(MuleActivationSpec.class)) |
196 | |
{ |
197 | 2 | MuleActivationSpec muleActivationSpec = (MuleActivationSpec) activationSpec; |
198 | 2 | MuleEndpointKey key = new MuleEndpointKey(endpointFactory, (MuleActivationSpec) activationSpec); |
199 | 2 | UMODescriptor descriptor = (UMODescriptor) endpoints.remove(key); |
200 | 2 | if (descriptor == null) |
201 | |
{ |
202 | 0 | logger.warn("No endpoint was registered with key: " + key); |
203 | 0 | return; |
204 | |
} |
205 | |
|
206 | |
|
207 | 2 | String modelName = null; |
208 | |
try |
209 | |
{ |
210 | 2 | modelName = resolveModelName(muleActivationSpec); |
211 | |
} |
212 | 0 | catch (ResourceException e) |
213 | |
{ |
214 | 0 | logger.error(e.getMessage(), e); |
215 | 2 | } |
216 | |
|
217 | |
try |
218 | |
{ |
219 | 2 | UMOModel model = MuleManager.getInstance().lookupModel(modelName); |
220 | 2 | model.unregisterComponent(descriptor); |
221 | |
} |
222 | 0 | catch (UMOException e) |
223 | |
{ |
224 | 0 | logger.error(e.getMessage(), e); |
225 | 2 | } |
226 | |
} |
227 | 2 | } |
228 | |
|
229 | |
protected String resolveModelName(MuleActivationSpec activationSpec) throws ResourceException |
230 | |
{ |
231 | |
|
232 | |
|
233 | |
|
234 | 18 | String modelName = activationSpec.getModelName(); |
235 | 18 | if (modelName == null) |
236 | |
{ |
237 | 4 | modelName = defaultJcaModelName; |
238 | |
} |
239 | 18 | if (modelName == null) |
240 | |
{ |
241 | 2 | throw new ResourceException( |
242 | |
"The 'modelName' property has not been configured for either the MuleResourceAdaptor or MuleActicationSpec."); |
243 | |
} |
244 | 16 | return modelName; |
245 | |
} |
246 | |
|
247 | |
protected JcaModel getJcaModel(String modelName) throws UMOException, ResourceException |
248 | |
{ |
249 | 16 | UMOModel model = MuleManager.getInstance().lookupModel(modelName); |
250 | 16 | if (model != null) |
251 | |
{ |
252 | 8 | if (model instanceof JcaModel) |
253 | |
{ |
254 | 6 | ((JcaModel) model).setWorkManager(new DelegateWorkManager(bootstrapContext.getWorkManager())); |
255 | 6 | return (JcaModel) model; |
256 | |
} |
257 | |
else |
258 | |
{ |
259 | 2 | throw new ResourceException("Model:-" + modelName + " is not compatible with JCA type"); |
260 | |
} |
261 | |
} |
262 | |
else |
263 | |
{ |
264 | 8 | JcaModel jcaModel = (JcaModel) ModelFactory.createModel(JcaModel.JCA_MODEL_TYPE); |
265 | 8 | jcaModel.setName(modelName); |
266 | 8 | manager.registerModel(jcaModel); |
267 | 8 | jcaModel.setWorkManager(new DelegateWorkManager(bootstrapContext.getWorkManager())); |
268 | 8 | return jcaModel; |
269 | |
} |
270 | |
} |
271 | |
|
272 | |
protected MuleDescriptor createJcaComponent(MessageEndpointFactory endpointFactory, |
273 | |
JcaModel model, |
274 | |
UMOEndpoint endpoint) throws UMOException |
275 | |
{ |
276 | 10 | String name = "JcaComponent#" + endpointFactory.hashCode(); |
277 | 10 | MuleDescriptor descriptor = new MuleDescriptor(name); |
278 | 10 | descriptor.getInboundRouter().addEndpoint(endpoint); |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | 10 | descriptor.setImplementation(endpointFactory); |
284 | 10 | descriptor.setModelName(model.getName()); |
285 | |
|
286 | 10 | model.registerComponent(descriptor); |
287 | 8 | return descriptor; |
288 | |
} |
289 | |
|
290 | |
protected UMOEndpoint createMessageInflowEndpoint(MuleActivationSpec muleActivationSpec) |
291 | |
throws MalformedEndpointException, EndpointException |
292 | |
{ |
293 | 12 | UMOEndpointURI uri = new MuleEndpointURI(muleActivationSpec.getEndpoint()); |
294 | 12 | UMOEndpoint endpoint = TransportFactory.createEndpoint(uri, UMOEndpoint.ENDPOINT_TYPE_RECEIVER); |
295 | |
|
296 | |
|
297 | |
|
298 | 12 | endpoint.setSynchronous(false); |
299 | 12 | return endpoint; |
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException |
309 | |
{ |
310 | 0 | return new XAResource[]{}; |
311 | |
} |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public String getPassword() |
317 | |
{ |
318 | 0 | return info.getPassword(); |
319 | |
} |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
public String getConfigurations() |
325 | |
{ |
326 | 0 | return info.getConfigurations(); |
327 | |
} |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
public String getUserName() |
333 | |
{ |
334 | 0 | return info.getUserName(); |
335 | |
} |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
public void setPassword(String password) |
341 | |
{ |
342 | 0 | info.setPassword(password); |
343 | 0 | } |
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
public void setConfigurations(String configurations) |
349 | |
{ |
350 | 0 | info.setConfigurations(configurations); |
351 | 0 | } |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
public void setUserName(String userid) |
357 | |
{ |
358 | 0 | info.setUserName(userid); |
359 | 0 | } |
360 | |
|
361 | |
public String getConfigurationBuilder() |
362 | |
{ |
363 | 0 | return info.getConfigurationBuilder(); |
364 | |
} |
365 | |
|
366 | |
public void setConfigurationBuilder(String configbuilder) |
367 | |
{ |
368 | 0 | info.setConfigurationBuilder(configbuilder); |
369 | 0 | } |
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
public MuleConnectionRequestInfo getInfo() |
375 | |
{ |
376 | 0 | return info; |
377 | |
} |
378 | |
|
379 | |
public boolean equals(Object o) |
380 | |
{ |
381 | 0 | if (this == o) |
382 | |
{ |
383 | 0 | return true; |
384 | |
} |
385 | 0 | if (!(o instanceof MuleResourceAdapter)) |
386 | |
{ |
387 | 0 | return false; |
388 | |
} |
389 | |
|
390 | 0 | final MuleResourceAdapter muleResourceAdapter = (MuleResourceAdapter) o; |
391 | |
|
392 | 0 | if (info != null ? !info.equals(muleResourceAdapter.info) : muleResourceAdapter.info != null) |
393 | |
{ |
394 | 0 | return false; |
395 | |
} |
396 | |
|
397 | 0 | return true; |
398 | |
} |
399 | |
|
400 | |
public int hashCode() |
401 | |
{ |
402 | 0 | return (info != null ? info.hashCode() : 0); |
403 | |
} |
404 | |
|
405 | |
public String getModelName() |
406 | |
{ |
407 | 0 | return defaultJcaModelName; |
408 | |
} |
409 | |
|
410 | |
public void setModelName(String modelName) |
411 | |
{ |
412 | 4 | this.defaultJcaModelName = modelName; |
413 | 4 | } |
414 | |
|
415 | |
} |