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.MuleMessage; |
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.lifecycle.InitialisationException; |
20 | |
import org.mule.api.model.EntryPointResolver; |
21 | |
import org.mule.api.model.EntryPointResolverSet; |
22 | |
import org.mule.api.object.ObjectFactory; |
23 | |
import org.mule.api.routing.NestedRouterCollection; |
24 | |
import org.mule.config.i18n.CoreMessages; |
25 | |
import org.mule.model.resolvers.DefaultEntryPointResolverSet; |
26 | |
import org.mule.routing.nested.DefaultNestedRouterCollection; |
27 | |
|
28 | |
import java.util.Collection; |
29 | |
import java.util.Iterator; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public abstract class AbstractJavaComponent extends AbstractComponent implements JavaComponent |
39 | |
{ |
40 | |
|
41 | |
protected EntryPointResolverSet entryPointResolverSet; |
42 | |
|
43 | 442 | protected NestedRouterCollection nestedRouter = new DefaultNestedRouterCollection(); |
44 | |
|
45 | |
protected ObjectFactory objectFactory; |
46 | |
|
47 | |
protected LifecycleAdapterFactory lifecycleAdapterFactory; |
48 | |
|
49 | |
public AbstractJavaComponent() |
50 | |
{ |
51 | |
|
52 | 14 | super(); |
53 | 14 | } |
54 | |
|
55 | |
public AbstractJavaComponent(ObjectFactory objectFactory) |
56 | |
{ |
57 | 428 | this(objectFactory, null, null); |
58 | 428 | } |
59 | |
|
60 | |
public AbstractJavaComponent(ObjectFactory objectFactory, |
61 | |
EntryPointResolverSet entryPointResolverSet, |
62 | |
NestedRouterCollection nestedRouterCollection) |
63 | |
{ |
64 | 428 | super(); |
65 | 428 | this.objectFactory = objectFactory; |
66 | 428 | this.entryPointResolverSet = entryPointResolverSet; |
67 | 428 | if (nestedRouterCollection != null) |
68 | |
{ |
69 | 0 | this.nestedRouter = nestedRouterCollection; |
70 | |
} |
71 | 428 | } |
72 | |
|
73 | |
protected MuleMessage doOnCall(MuleEvent event) throws Exception |
74 | |
{ |
75 | 0 | return invokeComponentInstance(event); |
76 | |
} |
77 | |
|
78 | |
protected MuleMessage invokeComponentInstance(MuleEvent event) throws Exception |
79 | |
{ |
80 | 0 | LifecycleAdapter componentLifecycleAdapter = null; |
81 | |
try |
82 | |
{ |
83 | 0 | componentLifecycleAdapter = borrowComponentLifecycleAdaptor(); |
84 | 0 | return componentLifecycleAdapter.intercept(null); |
85 | |
} |
86 | |
finally |
87 | |
{ |
88 | 0 | returnComponentLifecycleAdaptor(componentLifecycleAdapter); |
89 | |
} |
90 | |
} |
91 | |
|
92 | |
public Class getObjectType() |
93 | |
{ |
94 | 4 | return objectFactory.getObjectClass(); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
protected LifecycleAdapter createLifeCycleAdaptor() throws MuleException, Exception |
106 | |
{ |
107 | |
LifecycleAdapter lifecycleAdapter; |
108 | 76 | if (lifecycleAdapterFactory != null) |
109 | |
{ |
110 | |
|
111 | 0 | lifecycleAdapter = lifecycleAdapterFactory.create(objectFactory.getInstance(), this, entryPointResolverSet); |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | |
|
116 | 76 | lifecycleAdapter = service.getModel().getLifecycleAdapterFactory().create(objectFactory.getInstance(), |
117 | |
this, entryPointResolverSet); |
118 | |
} |
119 | 76 | lifecycleAdapter.initialise(); |
120 | 76 | return lifecycleAdapter; |
121 | |
} |
122 | |
|
123 | |
protected abstract LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception; |
124 | |
|
125 | |
protected abstract void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) throws Exception; |
126 | |
|
127 | |
|
128 | |
protected void doInitialise() throws InitialisationException |
129 | |
{ |
130 | 422 | if (objectFactory == null) |
131 | |
{ |
132 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("object factory"), this); |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | 422 | objectFactory.initialise(); |
139 | 422 | } |
140 | |
|
141 | |
|
142 | |
protected void doStart() throws MuleException |
143 | |
{ |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | 50 | if (entryPointResolverSet == null) |
149 | |
{ |
150 | 44 | entryPointResolverSet = service.getModel().getEntryPointResolverSet(); |
151 | |
} |
152 | 50 | } |
153 | |
|
154 | |
|
155 | |
protected void doDispose() |
156 | |
{ |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | 18 | } |
162 | |
|
163 | |
public EntryPointResolverSet getEntryPointResolverSet() |
164 | |
{ |
165 | 0 | return entryPointResolverSet; |
166 | |
} |
167 | |
|
168 | |
public NestedRouterCollection getNestedRouter() |
169 | |
{ |
170 | 152 | return nestedRouter; |
171 | |
} |
172 | |
|
173 | |
public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet) |
174 | |
{ |
175 | 0 | this.entryPointResolverSet = entryPointResolverSet; |
176 | 0 | } |
177 | |
|
178 | |
public void setNestedRouter(NestedRouterCollection nestedRouter) |
179 | |
{ |
180 | 0 | this.nestedRouter = nestedRouter; |
181 | 0 | } |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public void setEntryPointResolvers(Collection entryPointResolvers) |
190 | |
{ |
191 | 0 | if (null == entryPointResolverSet) |
192 | |
{ |
193 | 0 | entryPointResolverSet = new DefaultEntryPointResolverSet(); |
194 | |
} |
195 | 0 | for (Iterator resolvers = entryPointResolvers.iterator(); resolvers.hasNext();) |
196 | |
{ |
197 | 0 | entryPointResolverSet.addEntryPointResolver((EntryPointResolver) resolvers.next()); |
198 | |
} |
199 | 0 | } |
200 | |
|
201 | |
public ObjectFactory getObjectFactory() |
202 | |
{ |
203 | 30 | return objectFactory; |
204 | |
} |
205 | |
|
206 | |
public void setObjectFactory(ObjectFactory objectFactory) |
207 | |
{ |
208 | 0 | this.objectFactory = objectFactory; |
209 | 0 | } |
210 | |
|
211 | |
public LifecycleAdapterFactory getLifecycleAdapterFactory() |
212 | |
{ |
213 | 0 | return lifecycleAdapterFactory; |
214 | |
} |
215 | |
|
216 | |
public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory) |
217 | |
{ |
218 | 0 | this.lifecycleAdapterFactory = lifecycleAdapterFactory; |
219 | 0 | } |
220 | |
|
221 | |
} |