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 public class SpiUtils
27 {
28 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 if (type.equals(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE))
37 {
38 return findServiceDescriptor(PROVIDER_SERVICE_PATH, name, TransportFactory.class);
39 }
40 else if (type.equals(ServiceDescriptorFactory.EXCEPTION_SERVICE_TYPE))
41 {
42 return findServiceDescriptor(EXCEPTION_SERVICE_PATH, name, ExceptionHelper.class);
43 }
44 else
45 {
46 logger.warn("Attempt to lookup unrecognized service type: " + type);
47 return null;
48 }
49
50 }
51
52 public static Properties findServiceDescriptor(String path, String name, Class currentClass)
53 {
54
55
56
57 String preferredName = null;
58 String preferredPath = null;
59
60 if (!name.endsWith(".properties"))
61 {
62 name += ".properties";
63
64 preferredName = "preferred-" + name;
65 }
66
67 if (path.startsWith("/"))
68 {
69 path = path.substring(1);
70 }
71 if (!path.endsWith("/"))
72 {
73 path += "/";
74 }
75 if (path.startsWith(SERVICE_ROOT))
76 {
77 path += name;
78 }
79 else
80 {
81 preferredPath = SERVICE_ROOT + path + preferredName;
82 path = SERVICE_ROOT + path + name;
83 }
84 try
85 {
86
87 InputStream is = IOUtils.getResourceAsStream(preferredPath, currentClass, false, false);
88
89
90 if(is == null)
91 {
92 is = IOUtils.getResourceAsStream(path, currentClass, false, false);
93 }
94
95 if (is != null)
96 {
97 Properties props = new Properties();
98 try
99 {
100 props.load(is);
101 return props;
102 }
103 catch (IOException e)
104 {
105 logger.warn("Descriptor found but unable to load properties for service " + name);
106 return null;
107 }
108 }
109 else
110 {
111 return null;
112 }
113 }
114 catch (IOException e)
115 {
116 return null;
117 }
118 }
119 }