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