1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.jca; |
8 | |
|
9 | |
import org.mule.RequestContext; |
10 | |
import org.mule.api.DefaultMuleException; |
11 | |
import org.mule.api.MessagingException; |
12 | |
import org.mule.api.MuleEvent; |
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.component.LifecycleAdapter; |
15 | |
import org.mule.api.construct.FlowConstruct; |
16 | |
import org.mule.api.lifecycle.InitialisationException; |
17 | |
import org.mule.api.model.EntryPointResolverSet; |
18 | |
import org.mule.component.AbstractJavaComponent; |
19 | |
import org.mule.config.i18n.CoreMessages; |
20 | |
import org.mule.config.i18n.Message; |
21 | |
import org.mule.module.jca.i18n.JcaMessages; |
22 | |
import org.mule.work.AbstractMuleEventWork; |
23 | |
|
24 | |
import javax.resource.spi.UnavailableException; |
25 | |
import javax.resource.spi.endpoint.MessageEndpoint; |
26 | |
import javax.resource.spi.endpoint.MessageEndpointFactory; |
27 | |
import javax.resource.spi.work.WorkEvent; |
28 | |
import javax.resource.spi.work.WorkListener; |
29 | |
import javax.resource.spi.work.WorkManager; |
30 | |
|
31 | 0 | public class JcaComponent extends AbstractJavaComponent implements WorkListener |
32 | |
{ |
33 | |
protected MessageEndpointFactory messageEndpointFactory; |
34 | |
protected WorkManager workManager; |
35 | |
|
36 | |
public JcaComponent(MessageEndpointFactory messageEndpointFactory, |
37 | |
EntryPointResolverSet entryPointResolverSet, |
38 | |
FlowConstruct flowConstruct, |
39 | |
WorkManager workManager) |
40 | 0 | { |
41 | 0 | this.messageEndpointFactory = messageEndpointFactory; |
42 | 0 | this.entryPointResolverSet = entryPointResolverSet; |
43 | 0 | this.flowConstruct = flowConstruct; |
44 | 0 | this.workManager = workManager; |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public Object getManagedInstance() throws UnavailableException, MuleException |
55 | |
{ |
56 | 0 | return messageEndpointFactory.createEndpoint(null); |
57 | |
} |
58 | |
|
59 | |
@Override |
60 | |
public Object doInvoke(MuleEvent event) |
61 | |
{ |
62 | |
try |
63 | |
{ |
64 | 0 | workManager.scheduleWork(new MuleJcaWorker(event), WorkManager.INDEFINITE, null, this); |
65 | |
} |
66 | 0 | catch (Exception e) |
67 | |
{ |
68 | 0 | logger.error(CoreMessages.failedToInvoke("Service: " + flowConstruct.getName())); |
69 | 0 | } |
70 | 0 | return null; |
71 | |
} |
72 | |
|
73 | |
public Class getObjectType() |
74 | |
{ |
75 | 0 | return MessageEndpoint.class; |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception |
80 | |
{ |
81 | |
|
82 | 0 | return null; |
83 | |
} |
84 | |
|
85 | |
@Override |
86 | |
protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) |
87 | |
{ |
88 | |
|
89 | 0 | } |
90 | |
|
91 | |
@Override |
92 | |
protected void doInitialise() throws InitialisationException |
93 | |
{ |
94 | |
|
95 | 0 | } |
96 | |
|
97 | |
public class MuleJcaWorker extends AbstractMuleEventWork |
98 | |
{ |
99 | |
|
100 | |
MuleJcaWorker(MuleEvent event) |
101 | 0 | { |
102 | 0 | super(event); |
103 | 0 | } |
104 | |
|
105 | |
public void doRun() |
106 | |
{ |
107 | |
|
108 | 0 | if (logger.isTraceEnabled()) |
109 | |
{ |
110 | 0 | logger.trace("MuleJcaWorker: async MuleEvent for Mule JCA EndPoint " + flowConstruct.getName()); |
111 | |
} |
112 | |
try |
113 | |
{ |
114 | |
|
115 | 0 | entryPointResolverSet.invoke(getManagedInstance(), RequestContext.getEventContext()); |
116 | |
} |
117 | 0 | catch (Exception e) |
118 | |
{ |
119 | 0 | if (e instanceof UnavailableException) |
120 | |
{ |
121 | 0 | Message message = JcaMessages.cannotAllocateManagedInstance(); |
122 | 0 | logger.error(message); |
123 | 0 | flowConstruct.getExceptionListener().handleException(new DefaultMuleException(message, e), event); |
124 | 0 | } |
125 | 0 | else if (e instanceof MessagingException) |
126 | |
{ |
127 | 0 | logger.error("Failed to execute JCAEndPoint " + e.getMessage(), e); |
128 | 0 | flowConstruct.getExceptionListener().handleException(e, event); |
129 | |
} |
130 | |
else |
131 | |
{ |
132 | 0 | flowConstruct.getExceptionListener().handleException( |
133 | |
new DefaultMuleException(CoreMessages.eventProcessingFailedFor(flowConstruct.getName()), e), event); |
134 | |
} |
135 | 0 | } |
136 | 0 | } |
137 | |
} |
138 | |
|
139 | |
public void workAccepted(WorkEvent arg0) |
140 | |
{ |
141 | |
|
142 | 0 | } |
143 | |
|
144 | |
public void workCompleted(WorkEvent arg0) |
145 | |
{ |
146 | |
|
147 | 0 | } |
148 | |
|
149 | |
public void workRejected(WorkEvent arg0) |
150 | |
{ |
151 | |
|
152 | 0 | } |
153 | |
|
154 | |
public void workStarted(WorkEvent arg0) |
155 | |
{ |
156 | |
|
157 | 0 | } |
158 | |
|
159 | |
} |