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