1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.component; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.component.InterfaceBinding; |
16 | |
import org.mule.api.component.JavaComponent; |
17 | |
import org.mule.api.component.LifecycleAdapter; |
18 | |
import org.mule.api.component.LifecycleAdapterFactory; |
19 | |
import org.mule.api.construct.FlowConstruct; |
20 | |
import org.mule.api.construct.FlowConstructAware; |
21 | |
import org.mule.api.lifecycle.InitialisationException; |
22 | |
import org.mule.api.model.EntryPointResolver; |
23 | |
import org.mule.api.model.EntryPointResolverSet; |
24 | |
import org.mule.api.object.ObjectFactory; |
25 | |
import org.mule.api.service.Service; |
26 | |
import org.mule.config.i18n.CoreMessages; |
27 | |
import org.mule.model.resolvers.DefaultEntryPointResolverSet; |
28 | |
import org.mule.model.resolvers.LegacyEntryPointResolverSet; |
29 | |
|
30 | |
import java.util.ArrayList; |
31 | |
import java.util.Collection; |
32 | |
import java.util.List; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public abstract class AbstractJavaComponent extends AbstractComponent implements JavaComponent |
42 | |
{ |
43 | |
|
44 | |
protected EntryPointResolverSet entryPointResolverSet; |
45 | |
|
46 | 0 | protected List<InterfaceBinding> bindings = new ArrayList<InterfaceBinding>(); |
47 | |
|
48 | |
protected ObjectFactory objectFactory; |
49 | |
|
50 | |
protected LifecycleAdapterFactory lifecycleAdapterFactory; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public AbstractJavaComponent() |
56 | |
{ |
57 | 0 | super(); |
58 | 0 | } |
59 | |
|
60 | |
public AbstractJavaComponent(ObjectFactory objectFactory) |
61 | |
{ |
62 | 0 | this(objectFactory, null, null); |
63 | 0 | } |
64 | |
|
65 | |
public AbstractJavaComponent(ObjectFactory objectFactory, |
66 | |
EntryPointResolverSet entryPointResolverSet, |
67 | |
List<InterfaceBinding> bindings) |
68 | |
{ |
69 | 0 | super(); |
70 | 0 | this.objectFactory = objectFactory; |
71 | 0 | this.entryPointResolverSet = entryPointResolverSet; |
72 | 0 | if (bindings != null) |
73 | |
{ |
74 | 0 | this.bindings = bindings; |
75 | |
} |
76 | 0 | } |
77 | |
|
78 | |
@Override |
79 | |
protected Object doInvoke(MuleEvent event) throws Exception |
80 | |
{ |
81 | 0 | return invokeComponentInstance(event); |
82 | |
} |
83 | |
|
84 | |
protected Object invokeComponentInstance(MuleEvent event) throws Exception |
85 | |
{ |
86 | 0 | LifecycleAdapter componentLifecycleAdapter = null; |
87 | |
try |
88 | |
{ |
89 | 0 | componentLifecycleAdapter = borrowComponentLifecycleAdaptor(); |
90 | 0 | return componentLifecycleAdapter.invoke(event); |
91 | |
} |
92 | |
finally |
93 | |
{ |
94 | 0 | if (componentLifecycleAdapter != null) |
95 | |
{ |
96 | 0 | returnComponentLifecycleAdaptor(componentLifecycleAdapter); |
97 | |
} |
98 | |
} |
99 | |
} |
100 | |
|
101 | |
public Class<?> getObjectType() |
102 | |
{ |
103 | 0 | return objectFactory.getObjectClass(); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
protected LifecycleAdapter createLifecycleAdaptor() throws Exception |
114 | |
{ |
115 | |
|
116 | 0 | Object object = objectFactory.getInstance(muleContext); |
117 | |
|
118 | |
LifecycleAdapter lifecycleAdapter; |
119 | 0 | if (lifecycleAdapterFactory != null) |
120 | |
{ |
121 | |
|
122 | 0 | lifecycleAdapter = |
123 | |
lifecycleAdapterFactory.create(object, this, flowConstruct, entryPointResolverSet, muleContext); |
124 | |
} |
125 | 0 | else if (objectFactory.isExternallyManagedLifecycle()) |
126 | |
{ |
127 | |
|
128 | |
|
129 | |
|
130 | 0 | lifecycleAdapter = |
131 | |
new NullLifecycleAdapter(object, this, flowConstruct, entryPointResolverSet, muleContext); |
132 | |
} |
133 | 0 | else if (flowConstruct instanceof Service) |
134 | |
{ |
135 | |
|
136 | 0 | lifecycleAdapter = ((Service) flowConstruct).getModel().getLifecycleAdapterFactory().create( |
137 | |
object, this, flowConstruct, entryPointResolverSet, muleContext); |
138 | |
} |
139 | |
else |
140 | |
{ |
141 | 0 | lifecycleAdapter = new DefaultComponentLifecycleAdapterFactory().create(object, this, |
142 | |
flowConstruct, entryPointResolverSet, muleContext); |
143 | |
} |
144 | 0 | lifecycleAdapter.initialise(); |
145 | 0 | return lifecycleAdapter; |
146 | |
} |
147 | |
|
148 | |
protected abstract LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception; |
149 | |
|
150 | |
protected abstract void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) throws Exception; |
151 | |
|
152 | |
@Override |
153 | |
protected void doInitialise() throws InitialisationException |
154 | |
{ |
155 | 0 | if (objectFactory == null) |
156 | |
{ |
157 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("object factory"), this); |
158 | |
} |
159 | 0 | objectFactory.initialise(); |
160 | 0 | } |
161 | |
|
162 | |
@Override |
163 | |
protected void doStart() throws MuleException |
164 | |
{ |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | 0 | if (entryPointResolverSet == null) |
170 | |
{ |
171 | 0 | if (flowConstruct instanceof Service) |
172 | |
{ |
173 | 0 | entryPointResolverSet = ((Service) flowConstruct).getModel().getEntryPointResolverSet(); |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | 0 | entryPointResolverSet = new LegacyEntryPointResolverSet(); |
178 | |
} |
179 | |
} |
180 | 0 | } |
181 | |
|
182 | |
@Override |
183 | |
protected void doDispose() |
184 | |
{ |
185 | 0 | if (objectFactory!=null) |
186 | |
{ |
187 | 0 | objectFactory.dispose(); |
188 | |
} |
189 | 0 | } |
190 | |
|
191 | |
public EntryPointResolverSet getEntryPointResolverSet() |
192 | |
{ |
193 | 0 | return entryPointResolverSet; |
194 | |
} |
195 | |
|
196 | |
public List<InterfaceBinding> getInterfaceBindings() |
197 | |
{ |
198 | 0 | return bindings; |
199 | |
} |
200 | |
|
201 | |
public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet) |
202 | |
{ |
203 | 0 | this.entryPointResolverSet = entryPointResolverSet; |
204 | 0 | } |
205 | |
|
206 | |
public void setInterfaceBindings(List<InterfaceBinding> bindings) |
207 | |
{ |
208 | 0 | this.bindings = bindings; |
209 | 0 | } |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
public void setEntryPointResolvers(Collection<EntryPointResolver> entryPointResolvers) |
218 | |
{ |
219 | 0 | if (null == entryPointResolverSet) |
220 | |
{ |
221 | 0 | entryPointResolverSet = new DefaultEntryPointResolverSet(); |
222 | |
} |
223 | |
|
224 | 0 | for (EntryPointResolver resolver : entryPointResolvers) |
225 | |
{ |
226 | 0 | entryPointResolverSet.addEntryPointResolver(resolver); |
227 | |
} |
228 | 0 | } |
229 | |
|
230 | |
public ObjectFactory getObjectFactory() |
231 | |
{ |
232 | 0 | return objectFactory; |
233 | |
} |
234 | |
|
235 | |
public void setObjectFactory(ObjectFactory objectFactory) |
236 | |
{ |
237 | 0 | this.objectFactory = objectFactory; |
238 | 0 | injectService(); |
239 | 0 | } |
240 | |
|
241 | |
public LifecycleAdapterFactory getLifecycleAdapterFactory() |
242 | |
{ |
243 | 0 | return lifecycleAdapterFactory; |
244 | |
} |
245 | |
|
246 | |
public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory) |
247 | |
{ |
248 | 0 | this.lifecycleAdapterFactory = lifecycleAdapterFactory; |
249 | 0 | } |
250 | |
|
251 | |
@Override |
252 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
253 | |
{ |
254 | 0 | super.setFlowConstruct(flowConstruct); |
255 | 0 | injectService(); |
256 | 0 | } |
257 | |
|
258 | |
protected void injectService() |
259 | |
{ |
260 | 0 | if (objectFactory != null && objectFactory instanceof FlowConstructAware && flowConstruct != null) |
261 | |
{ |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | 0 | ((FlowConstructAware) objectFactory).setFlowConstruct(flowConstruct); |
268 | |
} |
269 | 0 | } |
270 | |
} |