1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.component; |
8 | |
|
9 | |
import org.mule.DefaultMuleEventContext; |
10 | |
import org.mule.VoidResult; |
11 | |
import org.mule.api.DefaultMuleException; |
12 | |
import org.mule.api.MuleEvent; |
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.MuleRuntimeException; |
16 | |
import org.mule.api.component.JavaComponent; |
17 | |
import org.mule.api.component.LifecycleAdapter; |
18 | |
import org.mule.api.lifecycle.Callable; |
19 | |
import org.mule.api.lifecycle.Disposable; |
20 | |
import org.mule.api.lifecycle.Startable; |
21 | |
import org.mule.api.lifecycle.Stoppable; |
22 | |
import org.mule.api.object.ObjectFactory; |
23 | |
import org.mule.api.registry.ServiceException; |
24 | |
import org.mule.config.i18n.CoreMessages; |
25 | |
import org.mule.object.SingletonObjectFactory; |
26 | |
import org.mule.transformer.TransformerTemplate; |
27 | |
|
28 | |
import edu.emory.mathcs.backport.java.util.Collections; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class SimpleCallableJavaComponent extends AbstractJavaComponent |
41 | |
{ |
42 | |
|
43 | 0 | private boolean started = false; |
44 | |
|
45 | |
public SimpleCallableJavaComponent() |
46 | 0 | { |
47 | |
|
48 | 0 | } |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public SimpleCallableJavaComponent(Callable callable) |
57 | 0 | { |
58 | 0 | objectFactory = new SingletonObjectFactory(callable); |
59 | 0 | } |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public SimpleCallableJavaComponent(Class callable) throws DefaultMuleException |
70 | 0 | { |
71 | 0 | if (!(Callable.class.isAssignableFrom(callable))) |
72 | |
{ |
73 | 0 | throw new DefaultMuleException(CoreMessages.objectNotOfCorrectType(callable, Callable.class)); |
74 | |
} |
75 | 0 | objectFactory = new SingletonObjectFactory(callable); |
76 | 0 | } |
77 | |
|
78 | |
public SimpleCallableJavaComponent(ObjectFactory objectFactory) throws DefaultMuleException |
79 | 0 | { |
80 | 0 | if (!(Callable.class.isAssignableFrom(objectFactory.getObjectClass()))) |
81 | |
{ |
82 | 0 | throw new DefaultMuleException(CoreMessages.objectNotOfCorrectType(objectFactory.getObjectClass(), |
83 | |
Callable.class)); |
84 | |
} |
85 | 0 | this.objectFactory = objectFactory; |
86 | 0 | } |
87 | |
|
88 | |
@Override |
89 | |
protected void doStart() throws MuleException |
90 | |
{ |
91 | 0 | super.doStart(); |
92 | 0 | if (Startable.class.isAssignableFrom(objectFactory.getObjectClass())) |
93 | |
{ |
94 | |
try |
95 | |
{ |
96 | 0 | ((Startable) objectFactory.getInstance(muleContext)).start(); |
97 | |
} |
98 | 0 | catch (Exception e) |
99 | |
{ |
100 | 0 | throw new ServiceException(CoreMessages.failedToStart("Service '" + flowConstruct.getName() + "'"), e); |
101 | 0 | } |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
@Override |
106 | |
protected void doStop() throws MuleException |
107 | |
{ |
108 | 0 | super.doStop(); |
109 | 0 | if (started && Stoppable.class.isAssignableFrom(objectFactory.getObjectClass())) |
110 | |
{ |
111 | |
try |
112 | |
{ |
113 | 0 | ((Stoppable) objectFactory.getInstance(muleContext)).stop(); |
114 | |
} |
115 | 0 | catch (Exception e) |
116 | |
{ |
117 | 0 | throw new ServiceException(CoreMessages.failedToStop("Service '" + flowConstruct.getName() + "'"), e); |
118 | 0 | } |
119 | |
} |
120 | 0 | } |
121 | |
|
122 | |
@Override |
123 | |
protected void doDispose() |
124 | |
{ |
125 | 0 | super.doDispose(); |
126 | 0 | if (Disposable.class.isAssignableFrom(objectFactory.getObjectClass())) |
127 | |
{ |
128 | |
try |
129 | |
{ |
130 | 0 | ((Disposable) objectFactory.getInstance(muleContext)).dispose(); |
131 | |
} |
132 | 0 | catch (Exception e) |
133 | |
{ |
134 | 0 | logger.error("Unable to dispose component instance", e); |
135 | 0 | } |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
@Override |
140 | |
public Class getObjectType() |
141 | |
{ |
142 | 0 | if (objectFactory != null) |
143 | |
{ |
144 | 0 | return objectFactory.getObjectClass(); |
145 | |
} |
146 | |
else |
147 | |
{ |
148 | 0 | return Callable.class; |
149 | |
} |
150 | |
} |
151 | |
|
152 | |
@Override |
153 | |
protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception |
154 | |
{ |
155 | |
|
156 | 0 | return null; |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) |
161 | |
{ |
162 | |
|
163 | 0 | } |
164 | |
|
165 | |
@Override |
166 | |
protected Object invokeComponentInstance(MuleEvent event) throws Exception |
167 | |
{ |
168 | 0 | Object result = ((Callable) objectFactory.getInstance(muleContext)).onCall(new DefaultMuleEventContext(event)); |
169 | 0 | if (result instanceof VoidResult) |
170 | |
{ |
171 | |
|
172 | 0 | return event.getMessage(); |
173 | |
} |
174 | 0 | else if (result != null) |
175 | |
{ |
176 | 0 | if (result instanceof MuleMessage) |
177 | |
{ |
178 | 0 | return result; |
179 | |
} |
180 | |
else |
181 | |
{ |
182 | 0 | event.getMessage().applyTransformers( |
183 | |
event, Collections.singletonList(new TransformerTemplate(new TransformerTemplate.OverwitePayloadCallback( |
184 | |
result)))); |
185 | 0 | return event.getMessage(); |
186 | |
} |
187 | |
} |
188 | |
else |
189 | |
{ |
190 | 0 | return null; |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
@Override |
195 | |
public void setObjectFactory(ObjectFactory objectFactory) |
196 | |
{ |
197 | 0 | if (!(Callable.class.isAssignableFrom(objectFactory.getObjectClass()))) |
198 | |
{ |
199 | 0 | throw new MuleRuntimeException(CoreMessages.objectNotOfCorrectType(objectFactory.getObjectClass(), |
200 | |
Callable.class)); |
201 | |
} |
202 | 0 | super.setObjectFactory(objectFactory); |
203 | 0 | } |
204 | |
} |