1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import org.mule.api.registry.ServiceDescriptorFactory; |
14 | |
import org.mule.config.ExceptionHelper; |
15 | |
import org.mule.transport.service.TransportFactory; |
16 | |
|
17 | |
import java.io.IOException; |
18 | |
import java.io.InputStream; |
19 | |
import java.util.Properties; |
20 | |
|
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class SpiUtils |
27 | |
{ |
28 | 2 | private static final Log logger = LogFactory.getLog(SpiUtils.class); |
29 | |
|
30 | |
public static final String SERVICE_ROOT = "META-INF/services/"; |
31 | |
public static final String PROVIDER_SERVICE_PATH = "org/mule/providers/"; |
32 | |
public static final String EXCEPTION_SERVICE_PATH = "org/mule/config/"; |
33 | |
|
34 | |
public static Properties findServiceDescriptor(String type, String name) |
35 | |
{ |
36 | 780 | if (type.equals(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE)) |
37 | |
{ |
38 | 390 | return findServiceDescriptor(PROVIDER_SERVICE_PATH, name, TransportFactory.class); |
39 | |
} |
40 | 390 | else if (type.equals(ServiceDescriptorFactory.EXCEPTION_SERVICE_TYPE)) |
41 | |
{ |
42 | 390 | return findServiceDescriptor(EXCEPTION_SERVICE_PATH, name, ExceptionHelper.class); |
43 | |
} |
44 | |
else |
45 | |
{ |
46 | 0 | logger.warn("Attempt to lookup unrecognized service type: " + type); |
47 | 0 | return null; |
48 | |
} |
49 | |
|
50 | |
} |
51 | |
|
52 | |
public static Properties findServiceDescriptor(String path, String name, Class currentClass) |
53 | |
{ |
54 | |
|
55 | |
|
56 | |
|
57 | 780 | String preferredName = null; |
58 | 780 | String preferredPath = null; |
59 | |
|
60 | 780 | if (!name.endsWith(".properties")) |
61 | |
{ |
62 | 780 | name += ".properties"; |
63 | |
|
64 | 780 | preferredName = "preferred-" + name; |
65 | |
} |
66 | |
|
67 | 780 | if (path.startsWith("/")) |
68 | |
{ |
69 | 0 | path = path.substring(1); |
70 | |
} |
71 | 780 | if (!path.endsWith("/")) |
72 | |
{ |
73 | 0 | path += "/"; |
74 | |
} |
75 | 780 | if (path.startsWith(SERVICE_ROOT)) |
76 | |
{ |
77 | 0 | path += name; |
78 | |
} |
79 | |
else |
80 | |
{ |
81 | 780 | preferredPath = SERVICE_ROOT + path + preferredName; |
82 | 780 | path = SERVICE_ROOT + path + name; |
83 | |
} |
84 | |
try |
85 | |
{ |
86 | |
|
87 | 780 | InputStream is = IOUtils.getResourceAsStream(preferredPath, currentClass, false, false); |
88 | |
|
89 | |
|
90 | 780 | if(is == null) |
91 | |
{ |
92 | 780 | is = IOUtils.getResourceAsStream(path, currentClass, false, false); |
93 | |
} |
94 | |
|
95 | 780 | if (is != null) |
96 | |
{ |
97 | 390 | Properties props = new Properties(); |
98 | |
try |
99 | |
{ |
100 | 390 | props.load(is); |
101 | 390 | return props; |
102 | |
} |
103 | 0 | catch (IOException e) |
104 | |
{ |
105 | 0 | logger.warn("Descriptor found but unable to load properties for service " + name); |
106 | 0 | return null; |
107 | |
} |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 390 | return null; |
112 | |
} |
113 | |
} |
114 | 0 | catch (IOException e) |
115 | |
{ |
116 | 0 | return null; |
117 | |
} |
118 | |
} |
119 | |
} |