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