1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.api.registry; |
12 | |
|
13 | |
import org.mule.MuleServer; |
14 | |
import org.mule.api.config.MuleProperties; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
import org.mule.model.DefaultModelServiceDescriptor; |
17 | |
import org.mule.transport.service.DefaultTransportServiceDescriptor; |
18 | |
import org.mule.transport.service.TransportServiceDescriptor; |
19 | |
import org.mule.util.ClassUtils; |
20 | |
import org.mule.util.SpiUtils; |
21 | |
|
22 | |
import java.util.Properties; |
23 | |
|
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class ServiceDescriptorFactory |
31 | |
{ |
32 | |
|
33 | |
public static final String PROVIDER_SERVICE_TYPE = "transport"; |
34 | |
public static final String MODEL_SERVICE_TYPE = "model"; |
35 | |
public static final String EXCEPTION_SERVICE_TYPE = "exception"; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public static ServiceDescriptor create(String type, String name, Properties props, Properties overrides, Registry registry) throws ServiceException |
41 | |
{ |
42 | |
|
43 | 390 | if (overrides != null) |
44 | |
{ |
45 | 0 | props.putAll(overrides); |
46 | |
} |
47 | |
|
48 | 390 | String serviceFinderClass = (String) props.remove(MuleProperties.SERVICE_FINDER); |
49 | |
|
50 | 390 | ServiceDescriptor sd = null; |
51 | 390 | if (type.equals(PROVIDER_SERVICE_TYPE)) |
52 | |
{ |
53 | |
try |
54 | |
{ |
55 | 390 | sd = new DefaultTransportServiceDescriptor(name, props, registry); |
56 | |
} |
57 | 0 | catch (Exception e) |
58 | |
{ |
59 | 0 | throw (IllegalStateException) new IllegalStateException("Cannot create transport " + name).initCause(e); |
60 | 390 | } |
61 | 390 | Properties exceptionMappingProps = SpiUtils.findServiceDescriptor(EXCEPTION_SERVICE_TYPE, name + "-exception-mappings"); |
62 | 390 | ((TransportServiceDescriptor) sd).setExceptionMappings(exceptionMappingProps); |
63 | 390 | } |
64 | 0 | else if (type.equals(MODEL_SERVICE_TYPE)) |
65 | |
{ |
66 | 0 | sd = new DefaultModelServiceDescriptor(name, props); |
67 | |
} |
68 | |
else |
69 | |
{ |
70 | 0 | throw new ServiceException(CoreMessages.unrecognisedServiceType(type)); |
71 | |
} |
72 | |
|
73 | |
|
74 | 390 | if (StringUtils.isNotBlank(serviceFinderClass)) |
75 | |
{ |
76 | |
ServiceFinder finder; |
77 | |
try |
78 | |
{ |
79 | 0 | finder = (ServiceFinder) ClassUtils.instanciateClass(serviceFinderClass, ClassUtils.NO_ARGS); |
80 | |
} |
81 | 0 | catch (Exception e) |
82 | |
{ |
83 | 0 | throw new ServiceException(CoreMessages.cannotInstanciateFinder(serviceFinderClass), e); |
84 | 0 | } |
85 | 0 | String realService = finder.findService(name, sd, props); |
86 | 0 | if (realService != null) |
87 | |
{ |
88 | |
|
89 | 0 | return MuleServer.getMuleContext().getRegistry().lookupServiceDescriptor( |
90 | |
ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, realService, overrides); |
91 | |
} |
92 | |
else |
93 | |
{ |
94 | 0 | throw new ServiceException(CoreMessages.serviceFinderCantFindService(name)); |
95 | |
} |
96 | |
} |
97 | 390 | return sd; |
98 | |
} |
99 | |
} |
100 | |
|
101 | |
|