1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.spring; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.agent.Agent; |
15 | |
import org.mule.api.config.MuleConfiguration; |
16 | |
import org.mule.api.endpoint.EndpointBuilder; |
17 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
18 | |
import org.mule.api.lifecycle.Disposable; |
19 | |
import org.mule.api.lifecycle.Initialisable; |
20 | |
import org.mule.api.lifecycle.LifecycleManager; |
21 | |
import org.mule.api.model.Model; |
22 | |
import org.mule.api.registry.RegistrationException; |
23 | |
import org.mule.api.registry.ServiceDescriptor; |
24 | |
import org.mule.api.registry.ServiceDescriptorFactory; |
25 | |
import org.mule.api.registry.ServiceException; |
26 | |
import org.mule.api.service.Service; |
27 | |
import org.mule.api.transformer.Transformer; |
28 | |
import org.mule.api.transport.Connector; |
29 | |
import org.mule.config.i18n.CoreMessages; |
30 | |
import org.mule.config.i18n.MessageFactory; |
31 | |
import org.mule.lifecycle.ContainerManagedLifecyclePhase; |
32 | |
import org.mule.lifecycle.GenericLifecycleManager; |
33 | |
import org.mule.registry.AbstractRegistry; |
34 | |
import org.mule.util.SpiUtils; |
35 | |
import org.mule.util.StringUtils; |
36 | |
|
37 | |
import java.util.Collection; |
38 | |
import java.util.Map; |
39 | |
import java.util.Properties; |
40 | |
|
41 | |
import javax.transaction.TransactionManager; |
42 | |
|
43 | |
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
44 | |
import org.springframework.context.ConfigurableApplicationContext; |
45 | |
|
46 | |
public class SpringRegistry extends AbstractRegistry |
47 | |
{ |
48 | |
public static final String REGISTRY_ID = "org.mule.Registry.Spring"; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static final String SPRING_APPLICATION_CONTEXT = "springApplicationContext"; |
55 | |
|
56 | |
protected ConfigurableApplicationContext applicationContext; |
57 | |
|
58 | |
public SpringRegistry() |
59 | |
{ |
60 | 0 | super(REGISTRY_ID); |
61 | 0 | } |
62 | |
|
63 | |
public SpringRegistry(String id) |
64 | |
{ |
65 | 0 | super(id); |
66 | 0 | } |
67 | |
|
68 | |
public SpringRegistry(ConfigurableApplicationContext applicationContext) |
69 | |
{ |
70 | 0 | super(REGISTRY_ID); |
71 | 0 | this.applicationContext = applicationContext; |
72 | 0 | } |
73 | |
|
74 | |
public SpringRegistry(String id, ConfigurableApplicationContext applicationContext) |
75 | |
{ |
76 | 0 | super(id); |
77 | 0 | this.applicationContext = applicationContext; |
78 | 0 | } |
79 | |
|
80 | |
protected LifecycleManager createLifecycleManager() |
81 | |
{ |
82 | 0 | GenericLifecycleManager lcm = new GenericLifecycleManager(); |
83 | 0 | lcm.registerLifecycle(new ContainerManagedLifecyclePhase(Initialisable.PHASE_NAME, |
84 | |
Initialisable.class, Disposable.PHASE_NAME)); |
85 | 0 | lcm.registerLifecycle(new ContainerManagedLifecyclePhase(Disposable.PHASE_NAME, Disposable.class, |
86 | |
Initialisable.PHASE_NAME)); |
87 | 0 | return lcm; |
88 | |
} |
89 | |
|
90 | |
protected Object doLookupObject(String key) |
91 | |
{ |
92 | 0 | if (StringUtils.isBlank(key)) |
93 | |
{ |
94 | 0 | logger.warn( |
95 | |
MessageFactory.createStaticMessage("Detected a lookup attempt with an empty or null key"), |
96 | |
new Throwable().fillInStackTrace()); |
97 | 0 | return null; |
98 | |
} |
99 | |
|
100 | 0 | if (key.equals(SPRING_APPLICATION_CONTEXT) && applicationContext != null) |
101 | |
{ |
102 | 0 | return applicationContext; |
103 | |
} |
104 | |
else |
105 | |
{ |
106 | |
try |
107 | |
{ |
108 | 0 | return applicationContext.getBean(key); |
109 | |
} |
110 | 0 | catch (NoSuchBeanDefinitionException e) |
111 | |
{ |
112 | 0 | logger.debug(e); |
113 | 0 | return null; |
114 | |
} |
115 | |
} |
116 | |
} |
117 | |
|
118 | |
protected Collection doLookupObjects(Class type) |
119 | |
{ |
120 | 0 | Map map = applicationContext.getBeansOfType(type); |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | 0 | return map.values(); |
127 | |
} |
128 | |
|
129 | |
public ServiceDescriptor lookupServiceDescriptor(String type, String name, Properties overrides) |
130 | |
throws ServiceException |
131 | |
{ |
132 | 0 | Properties props = SpiUtils.findServiceDescriptor(type, name); |
133 | 0 | if (props == null) |
134 | |
{ |
135 | 0 | throw new ServiceException(CoreMessages.failedToLoad(type + " " + name)); |
136 | |
} |
137 | 0 | return ServiceDescriptorFactory.create(type, name, props, overrides, this); |
138 | |
} |
139 | |
|
140 | |
|
141 | |
public TransactionManager getTransactionManager() |
142 | |
{ |
143 | |
try |
144 | |
{ |
145 | 0 | return (TransactionManager) lookupObject(TransactionManager.class); |
146 | |
} |
147 | 0 | catch (RegistrationException e) |
148 | |
{ |
149 | 0 | throw new RuntimeException(e); |
150 | |
} |
151 | |
} |
152 | |
|
153 | |
public Collection getModels() |
154 | |
{ |
155 | 0 | return applicationContext.getBeansOfType(Model.class).values(); |
156 | |
} |
157 | |
|
158 | |
|
159 | |
public Collection getConnectors() |
160 | |
{ |
161 | 0 | return applicationContext.getBeansOfType(Connector.class).values(); |
162 | |
} |
163 | |
|
164 | |
public Collection getAgents() |
165 | |
{ |
166 | 0 | return applicationContext.getBeansOfType(Agent.class).values(); |
167 | |
} |
168 | |
|
169 | |
|
170 | |
public Collection getEndpoints() |
171 | |
{ |
172 | 0 | return applicationContext.getBeansOfType(ImmutableEndpoint.class).values(); |
173 | |
} |
174 | |
|
175 | |
|
176 | |
public Collection getTransformers() |
177 | |
{ |
178 | 0 | return applicationContext.getBeansOfType(Transformer.class).values(); |
179 | |
} |
180 | |
|
181 | |
public boolean isReadOnly() |
182 | |
{ |
183 | 0 | return true; |
184 | |
} |
185 | |
|
186 | |
public boolean isRemote() |
187 | |
{ |
188 | 0 | return false; |
189 | |
} |
190 | |
|
191 | |
public void registerConnector(Connector connector) |
192 | |
throws MuleException |
193 | |
{ |
194 | 0 | unsupportedOperation("registerConnector", connector); |
195 | 0 | } |
196 | |
|
197 | |
public void unregisterConnector(String connectorName) throws MuleException |
198 | |
{ |
199 | 0 | unsupportedOperation("unregisterConnector", connectorName); |
200 | 0 | } |
201 | |
|
202 | |
public void registerEndpoint(ImmutableEndpoint endpoint) |
203 | |
throws MuleException |
204 | |
{ |
205 | 0 | unsupportedOperation("registerEndpoint", endpoint); |
206 | 0 | } |
207 | |
|
208 | |
public void unregisterEndpoint(String endpointName) |
209 | |
{ |
210 | 0 | unsupportedOperation("unregisterEndpoint", endpointName); |
211 | 0 | } |
212 | |
|
213 | |
protected void doRegisterTransformer(Transformer transformer) throws MuleException |
214 | |
{ |
215 | 0 | unsupportedOperation("registerTransformer", transformer); |
216 | 0 | } |
217 | |
|
218 | |
public void unregisterTransformer(String transformerName) |
219 | |
{ |
220 | 0 | unsupportedOperation("unregistertransformer", transformerName); |
221 | 0 | } |
222 | |
|
223 | |
|
224 | |
public void registerService(Service service) |
225 | |
throws MuleException |
226 | |
{ |
227 | 0 | unsupportedOperation("registerService", service); |
228 | 0 | } |
229 | |
|
230 | |
public void unregisterService(String serviceName) |
231 | |
{ |
232 | 0 | unsupportedOperation("unregisterService", serviceName); |
233 | 0 | } |
234 | |
|
235 | |
public void registerModel(Model model) throws MuleException |
236 | |
{ |
237 | 0 | unsupportedOperation("registerModel", model); |
238 | 0 | } |
239 | |
|
240 | |
public void unregisterModel(String modelName) |
241 | |
{ |
242 | 0 | unsupportedOperation("unregisterModel", modelName); |
243 | 0 | } |
244 | |
|
245 | |
public void registerAgent(Agent agent) throws MuleException |
246 | |
{ |
247 | 0 | unsupportedOperation("registerAgent", agent); |
248 | 0 | } |
249 | |
|
250 | |
public void unregisterAgent(String agentName) throws MuleException |
251 | |
{ |
252 | 0 | unsupportedOperation("unregisterAgent", agentName); |
253 | 0 | } |
254 | |
|
255 | |
protected void doRegisterObject(String key, |
256 | |
Object value, |
257 | |
Object metadata) throws RegistrationException |
258 | |
{ |
259 | 0 | unsupportedOperation("doRegisterObject", key); |
260 | 0 | } |
261 | |
|
262 | |
public void unregisterObject(String key) |
263 | |
{ |
264 | 0 | unsupportedOperation("unregisterObject", key); |
265 | 0 | } |
266 | |
|
267 | |
public void registerObjects(Map objects) throws RegistrationException |
268 | |
{ |
269 | 0 | unsupportedOperation("registryObjects", objects); |
270 | 0 | } |
271 | |
|
272 | |
public void setConfiguration(MuleConfiguration config) |
273 | |
{ |
274 | 0 | unsupportedOperation("setConfiguration", config); |
275 | 0 | } |
276 | |
|
277 | |
public void registerEndpointBuilder(String name, |
278 | |
EndpointBuilder builder) throws MuleException |
279 | |
{ |
280 | 0 | unsupportedOperation("registerEndpointBuilder", builder); |
281 | 0 | } |
282 | |
|
283 | |
protected void doDispose() |
284 | |
{ |
285 | 0 | super.doDispose(); |
286 | 0 | applicationContext.close(); |
287 | 0 | } |
288 | |
} |