1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.registry; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.NamedObject; |
16 | |
import org.mule.api.agent.Agent; |
17 | |
import org.mule.api.config.MuleProperties; |
18 | |
import org.mule.api.endpoint.EndpointBuilder; |
19 | |
import org.mule.api.endpoint.EndpointFactory; |
20 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
21 | |
import org.mule.api.lifecycle.Disposable; |
22 | |
import org.mule.api.lifecycle.Initialisable; |
23 | |
import org.mule.api.lifecycle.InitialisationException; |
24 | |
import org.mule.api.lifecycle.LifecycleException; |
25 | |
import org.mule.api.model.Model; |
26 | |
import org.mule.api.registry.AbstractServiceDescriptor; |
27 | |
import org.mule.api.registry.MuleRegistry; |
28 | |
import org.mule.api.registry.RegistrationException; |
29 | |
import org.mule.api.registry.ResolverException; |
30 | |
import org.mule.api.registry.ServiceDescriptor; |
31 | |
import org.mule.api.registry.ServiceDescriptorFactory; |
32 | |
import org.mule.api.registry.ServiceException; |
33 | |
import org.mule.api.registry.ServiceType; |
34 | |
import org.mule.api.registry.TransformerResolver; |
35 | |
import org.mule.api.service.Service; |
36 | |
import org.mule.api.transformer.DataType; |
37 | |
import org.mule.api.transformer.DiscoverableTransformer; |
38 | |
import org.mule.api.transformer.Transformer; |
39 | |
import org.mule.api.transformer.TransformerException; |
40 | |
import org.mule.api.transport.Connector; |
41 | |
import org.mule.config.i18n.CoreMessages; |
42 | |
import org.mule.transformer.types.SimpleDataType; |
43 | |
import org.mule.util.SpiUtils; |
44 | |
import org.mule.util.StringUtils; |
45 | |
import org.mule.util.UUID; |
46 | |
|
47 | |
import java.util.ArrayList; |
48 | |
import java.util.Collection; |
49 | |
import java.util.Collections; |
50 | |
import java.util.Comparator; |
51 | |
import java.util.Iterator; |
52 | |
import java.util.List; |
53 | |
import java.util.Map; |
54 | |
import java.util.Properties; |
55 | |
|
56 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
57 | |
|
58 | |
import org.apache.commons.logging.Log; |
59 | |
import org.apache.commons.logging.LogFactory; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public class MuleRegistryHelper implements MuleRegistry |
66 | |
{ |
67 | 0 | protected transient Log logger = LogFactory.getLog(MuleRegistryHelper.class); |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private DefaultRegistryBroker registry; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 0 | protected Map<String, List<Transformer>> transformerListCache = new ConcurrentHashMap(8); |
78 | |
|
79 | |
private MuleContext muleContext; |
80 | |
|
81 | |
public MuleRegistryHelper(DefaultRegistryBroker registry, MuleContext muleContext) |
82 | 0 | { |
83 | 0 | this.registry = registry; |
84 | 0 | this.muleContext = muleContext; |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void initialise() throws InitialisationException |
91 | |
{ |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public void dispose() |
102 | |
{ |
103 | 0 | transformerListCache.clear(); |
104 | 0 | } |
105 | |
|
106 | |
public void fireLifecycle(String phase) throws LifecycleException |
107 | |
{ |
108 | 0 | if(Initialisable.PHASE_NAME.equals(phase)) |
109 | |
{ |
110 | 0 | registry.initialise(); |
111 | |
} |
112 | 0 | else if(Disposable.PHASE_NAME.equals(phase)) |
113 | |
{ |
114 | 0 | registry.dispose(); |
115 | |
} |
116 | |
else |
117 | |
{ |
118 | 0 | registry.fireLifecycle(phase); |
119 | |
} |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public Connector lookupConnector(String name) |
126 | |
{ |
127 | 0 | return (Connector) registry.lookupObject(name); |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public EndpointBuilder lookupEndpointBuilder(String name) |
165 | |
{ |
166 | 0 | Object o = registry.lookupObject(name); |
167 | 0 | if (o instanceof EndpointBuilder) |
168 | |
{ |
169 | 0 | logger.debug("Global endpoint EndpointBuilder for name: " + name + " found"); |
170 | 0 | return (EndpointBuilder) o; |
171 | |
} |
172 | |
else |
173 | |
{ |
174 | 0 | logger.debug("No endpoint builder with the name: " + name + " found."); |
175 | 0 | return null; |
176 | |
} |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
public EndpointFactory lookupEndpointFactory() |
183 | |
{ |
184 | 0 | return (EndpointFactory) registry.lookupObject(MuleProperties.OBJECT_MULE_ENDPOINT_FACTORY); |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public Transformer lookupTransformer(String name) |
191 | |
{ |
192 | 0 | return (Transformer) registry.lookupObject(name); |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
@Deprecated |
203 | |
public Transformer lookupTransformer(Class inputType, Class outputType) throws TransformerException |
204 | |
{ |
205 | 0 | return lookupTransformer(new SimpleDataType(inputType), new SimpleDataType(outputType)); |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
@Deprecated |
216 | |
public List<Transformer> lookupTransformers(Class input, Class output) |
217 | |
{ |
218 | 0 | return lookupTransformers(new SimpleDataType(input), new SimpleDataType(output)); |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
public Transformer lookupTransformer(DataType source, DataType result) throws TransformerException |
225 | |
{ |
226 | |
|
227 | |
Transformer trans; |
228 | 0 | List<TransformerResolver> resolvers = (List<TransformerResolver>) lookupObjects(TransformerResolver.class); |
229 | 0 | Collections.sort(resolvers, new TransformerResolverComparator()); |
230 | 0 | for (TransformerResolver resolver : resolvers) |
231 | |
{ |
232 | |
try |
233 | |
{ |
234 | 0 | trans = resolver.resolve(source, result); |
235 | 0 | if (trans != null) |
236 | |
{ |
237 | 0 | return trans; |
238 | |
} |
239 | |
} |
240 | 0 | catch (ResolverException e) |
241 | |
{ |
242 | 0 | throw new TransformerException(CoreMessages.noTransformerFoundForMessage(source, result), e); |
243 | 0 | } |
244 | |
} |
245 | 0 | throw new TransformerException(CoreMessages.noTransformerFoundForMessage(source, result)); |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public List<Transformer> lookupTransformers(DataType source, DataType result) |
252 | |
{ |
253 | 0 | List<Transformer> results = transformerListCache.get(source.toString() + result.toString()); |
254 | 0 | if (results != null) |
255 | |
{ |
256 | 0 | return results; |
257 | |
} |
258 | |
|
259 | 0 | results = new ArrayList<Transformer>(2); |
260 | 0 | Collection<Transformer> transformers = getTransformers(); |
261 | 0 | for (Transformer t : transformers) |
262 | |
{ |
263 | |
|
264 | |
|
265 | 0 | if (!(t instanceof DiscoverableTransformer)) |
266 | |
{ |
267 | 0 | continue; |
268 | |
} |
269 | 0 | DataType dt = t.getReturnDataType(); |
270 | 0 | if (result.isCompatibleWith(dt) && t.isSourceDataTypeSupported(source)) |
271 | |
{ |
272 | 0 | results.add(t); |
273 | |
} |
274 | 0 | } |
275 | |
|
276 | 0 | transformerListCache.put(source.toString() + result.toString(), results); |
277 | 0 | return results; |
278 | |
} |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public Model lookupModel(String name) |
284 | |
{ |
285 | 0 | return (Model) registry.lookupObject(name); |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
public Model lookupSystemModel() |
292 | |
{ |
293 | 0 | return lookupModel(MuleProperties.OBJECT_SYSTEM_MODEL); |
294 | |
} |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
public Collection<Model> getModels() |
300 | |
{ |
301 | 0 | return registry.lookupObjects(Model.class); |
302 | |
} |
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
public Collection<Connector> getConnectors() |
308 | |
{ |
309 | 0 | return registry.lookupObjects(Connector.class); |
310 | |
} |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public Collection<Agent> getAgents() |
316 | |
{ |
317 | 0 | return registry.lookupObjects(Agent.class); |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
public Collection<ImmutableEndpoint> getEndpoints() |
324 | |
{ |
325 | 0 | return registry.lookupObjects(ImmutableEndpoint.class); |
326 | |
} |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public Collection<Transformer> getTransformers() |
332 | |
{ |
333 | 0 | return registry.lookupObjects(Transformer.class); |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
public Agent lookupAgent(String name) |
340 | |
{ |
341 | 0 | return (Agent) registry.lookupObject(name); |
342 | |
} |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public Service lookupService(String name) |
348 | |
{ |
349 | 0 | return (Service) registry.lookupObject(name); |
350 | |
} |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public Collection<Service> lookupServices() |
356 | |
{ |
357 | 0 | return lookupObjects(Service.class); |
358 | |
} |
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public Collection<Service> lookupServices(String model) |
364 | |
{ |
365 | 0 | Collection<Service> services = lookupServices(); |
366 | 0 | List<Service> modelServices = new ArrayList<Service>(); |
367 | 0 | Iterator it = services.iterator(); |
368 | |
Service service; |
369 | 0 | while (it.hasNext()) |
370 | |
{ |
371 | 0 | service = (Service) it.next(); |
372 | 0 | if (model.equals(service.getModel().getName())) |
373 | |
{ |
374 | 0 | modelServices.add(service); |
375 | |
} |
376 | |
} |
377 | 0 | return modelServices; |
378 | |
} |
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
public final void registerTransformer(Transformer transformer) throws MuleException |
384 | |
{ |
385 | 0 | registry.registerObject(getName(transformer), transformer, Transformer.class); |
386 | 0 | notifyTransformerResolvers(transformer, TransformerResolver.RegistryAction.ADDED); |
387 | 0 | } |
388 | |
|
389 | |
protected void notifyTransformerResolvers(Transformer t, TransformerResolver.RegistryAction action) |
390 | |
{ |
391 | 0 | if (t instanceof DiscoverableTransformer) |
392 | |
{ |
393 | 0 | Collection<TransformerResolver> resolvers = lookupObjects(TransformerResolver.class); |
394 | 0 | for (TransformerResolver resolver : resolvers) |
395 | |
{ |
396 | 0 | resolver.transformerChange(t, action); |
397 | |
} |
398 | 0 | transformerListCache.clear(); |
399 | |
} |
400 | 0 | } |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
public ServiceDescriptor lookupServiceDescriptor(ServiceType type, String name, Properties overrides) throws ServiceException |
406 | |
{ |
407 | 0 | String key = new AbstractServiceDescriptor.Key(name, overrides).getKey(); |
408 | |
|
409 | |
|
410 | 0 | ServiceDescriptor sd = (ServiceDescriptor) registry.lookupObject(key); |
411 | |
|
412 | 0 | synchronized (this) |
413 | |
{ |
414 | 0 | if (sd == null) |
415 | |
{ |
416 | 0 | sd = createServiceDescriptor(type, name, overrides); |
417 | |
try |
418 | |
{ |
419 | 0 | registry.registerObject(key, sd, ServiceDescriptor.class); |
420 | |
} |
421 | 0 | catch (RegistrationException e) |
422 | |
{ |
423 | 0 | throw new ServiceException(e.getI18nMessage(), e); |
424 | 0 | } |
425 | |
} |
426 | 0 | } |
427 | 0 | return sd; |
428 | |
} |
429 | |
|
430 | |
protected ServiceDescriptor createServiceDescriptor(ServiceType type, String name, Properties overrides) throws ServiceException |
431 | |
{ |
432 | |
|
433 | 0 | String scheme = name; |
434 | 0 | if (name.contains(":")) |
435 | |
{ |
436 | 0 | scheme = name.substring(0, name.indexOf(":")); |
437 | |
} |
438 | |
|
439 | 0 | Properties props = SpiUtils.findServiceDescriptor(type, scheme); |
440 | 0 | if (props == null) |
441 | |
{ |
442 | 0 | throw new ServiceException(CoreMessages.failedToLoad(type + " " + scheme)); |
443 | |
} |
444 | |
|
445 | 0 | return ServiceDescriptorFactory.create(type, name, props, overrides, muleContext, null); |
446 | |
} |
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
public void registerAgent(Agent agent) throws MuleException |
452 | |
{ |
453 | 0 | registry.registerObject(getName(agent), agent, Agent.class); |
454 | 0 | } |
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
public void registerConnector(Connector connector) throws MuleException |
460 | |
{ |
461 | 0 | registry.registerObject(getName(connector), connector, Connector.class); |
462 | 0 | } |
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
public void registerEndpoint(ImmutableEndpoint endpoint) throws MuleException |
468 | |
{ |
469 | 0 | registry.registerObject(getName(endpoint), endpoint, ImmutableEndpoint.class); |
470 | 0 | } |
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
public void registerEndpointBuilder(String name, EndpointBuilder builder) throws MuleException |
476 | |
{ |
477 | 0 | registry.registerObject(name, builder, EndpointBuilder.class); |
478 | 0 | } |
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
public void registerModel(Model model) throws MuleException |
484 | |
{ |
485 | 0 | registry.registerObject(getName(model), model, Model.class); |
486 | 0 | } |
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
public void registerService(Service service) throws MuleException |
492 | |
{ |
493 | 0 | registry.registerObject(getName(service), service, Service.class); |
494 | 0 | } |
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
public void unregisterService(String serviceName) throws MuleException |
500 | |
{ |
501 | 0 | registry.unregisterObject(serviceName, Service.class); |
502 | 0 | } |
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
public void unregisterAgent(String agentName) throws MuleException |
508 | |
{ |
509 | 0 | registry.unregisterObject(agentName, Agent.class); |
510 | 0 | } |
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
public void unregisterConnector(String connectorName) throws MuleException |
516 | |
{ |
517 | 0 | registry.unregisterObject(connectorName, Connector.class); |
518 | 0 | } |
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
public void unregisterEndpoint(String endpointName) throws MuleException |
524 | |
{ |
525 | 0 | registry.unregisterObject(endpointName, ImmutableEndpoint.class); |
526 | 0 | } |
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
public void unregisterModel(String modelName) throws MuleException |
532 | |
{ |
533 | 0 | registry.unregisterObject(modelName, Model.class); |
534 | 0 | } |
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
public void unregisterTransformer(String transformerName) throws MuleException |
540 | |
{ |
541 | 0 | Transformer transformer = lookupTransformer(transformerName); |
542 | 0 | notifyTransformerResolvers(transformer, TransformerResolver.RegistryAction.REMOVED); |
543 | 0 | registry.unregisterObject(transformerName, Transformer.class); |
544 | |
|
545 | 0 | } |
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
public Object applyProcessorsAndLifecycle(Object object) throws MuleException |
551 | |
{ |
552 | 0 | object = applyProcessors(object); |
553 | 0 | object = applyLifecycle(object); |
554 | 0 | return object; |
555 | |
} |
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
public Object applyProcessors(Object object) throws MuleException |
561 | |
{ |
562 | 0 | return registry.getTransientRegistry().applyProcessors(object, null); |
563 | |
} |
564 | |
|
565 | |
|
566 | |
|
567 | |
|
568 | |
public Object applyProcessors(Object object, int flags) throws MuleException |
569 | |
{ |
570 | 0 | return registry.getTransientRegistry().applyProcessors(object, flags); |
571 | |
} |
572 | |
|
573 | |
|
574 | |
|
575 | |
|
576 | |
public Object applyLifecycle(Object object) throws MuleException |
577 | |
{ |
578 | 0 | return registry.getTransientRegistry().applyLifecycle(object); |
579 | |
} |
580 | |
|
581 | |
public Object applyLifecycle(Object object, String phase) throws MuleException |
582 | |
{ |
583 | 0 | return registry.getTransientRegistry().applyLifecycle(object, phase); |
584 | |
} |
585 | |
|
586 | |
|
587 | |
|
588 | |
|
589 | |
|
590 | |
|
591 | |
|
592 | |
|
593 | |
public <T> T lookupObject(Class<T> type) throws RegistrationException |
594 | |
{ |
595 | 0 | return registry.lookupObject(type); |
596 | |
} |
597 | |
|
598 | |
@SuppressWarnings("unchecked") |
599 | |
public <T> T lookupObject(String key) |
600 | |
{ |
601 | 0 | return (T) registry.lookupObject(key); |
602 | |
} |
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
public <T> Collection<T> lookupObjects(Class<T> type) |
608 | |
{ |
609 | 0 | return registry.lookupObjects(type); |
610 | |
} |
611 | |
|
612 | |
@SuppressWarnings("unchecked") |
613 | |
public <T> T get(String key) |
614 | |
{ |
615 | 0 | return (T)registry.get(key); |
616 | |
} |
617 | |
|
618 | |
public <T> Map<String, T> lookupByType(Class<T> type) |
619 | |
{ |
620 | 0 | return registry.lookupByType(type); |
621 | |
} |
622 | |
|
623 | |
|
624 | |
|
625 | |
|
626 | |
public void registerObject(String key, Object value, Object metadata) throws RegistrationException |
627 | |
{ |
628 | 0 | registry.registerObject(key, value, metadata); |
629 | 0 | } |
630 | |
|
631 | |
|
632 | |
|
633 | |
|
634 | |
public void registerObject(String key, Object value) throws RegistrationException |
635 | |
{ |
636 | 0 | registry.registerObject(key, value); |
637 | 0 | } |
638 | |
|
639 | |
|
640 | |
|
641 | |
|
642 | |
public void registerObjects(Map objects) throws RegistrationException |
643 | |
{ |
644 | 0 | registry.registerObjects(objects); |
645 | 0 | } |
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
public void unregisterObject(String key, Object metadata) throws RegistrationException |
651 | |
{ |
652 | 0 | registry.unregisterObject(key, metadata); |
653 | 0 | } |
654 | |
|
655 | |
|
656 | |
|
657 | |
|
658 | |
public void unregisterObject(String key) throws RegistrationException |
659 | |
{ |
660 | 0 | registry.unregisterObject(key); |
661 | 0 | } |
662 | |
|
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | |
|
668 | |
|
669 | |
|
670 | |
protected String getName(Object obj) |
671 | |
{ |
672 | 0 | String name = null; |
673 | 0 | if (obj instanceof NamedObject) |
674 | |
{ |
675 | 0 | name = ((NamedObject) obj).getName(); |
676 | |
} |
677 | 0 | if (StringUtils.isBlank(name)) |
678 | |
{ |
679 | 0 | name = obj.getClass().getName() + ":" + UUID.getUUID(); |
680 | |
} |
681 | 0 | return name; |
682 | |
} |
683 | |
|
684 | |
|
685 | |
|
686 | |
|
687 | |
|
688 | |
|
689 | |
|
690 | |
|
691 | |
public String getRegistryId() |
692 | |
{ |
693 | 0 | return this.toString(); |
694 | |
} |
695 | |
|
696 | |
|
697 | |
|
698 | |
|
699 | |
public boolean isReadOnly() |
700 | |
{ |
701 | 0 | return false; |
702 | |
} |
703 | |
|
704 | |
|
705 | |
|
706 | |
|
707 | |
public boolean isRemote() |
708 | |
{ |
709 | 0 | return false; |
710 | |
} |
711 | |
|
712 | |
|
713 | 0 | private class TransformerResolverComparator implements Comparator<TransformerResolver> |
714 | |
{ |
715 | |
public int compare(TransformerResolver transformerResolver, TransformerResolver transformerResolver1) |
716 | |
{ |
717 | 0 | if (transformerResolver.getClass().equals(TypeBasedTransformerResolver.class)) |
718 | |
{ |
719 | 0 | return 1; |
720 | |
} |
721 | |
|
722 | 0 | if (transformerResolver1.getClass().equals(TypeBasedTransformerResolver.class)) |
723 | |
{ |
724 | 0 | return -1; |
725 | |
} |
726 | 0 | return 0; |
727 | |
} |
728 | |
} |
729 | |
} |
730 | |
|
731 | |
|