1 10 11 package org.mule.util; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.security.AccessController ; 16 import java.security.PrivilegedAction ; 17 import java.util.Properties ; 18 19 import org.apache.commons.discovery.DiscoveryException; 20 import org.apache.commons.discovery.resource.ClassLoaders; 21 import org.apache.commons.discovery.tools.DiscoverClass; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 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 32 43 public static Class findService(Class spi, String propertyFileName, String defaultImpl, Class currentClass) 44 { 45 ClassLoaders loaders = ClassLoaders.getAppLoaders(spi, currentClass, false); 46 DiscoverClass discover = new DiscoverClass(loaders); 47 try 48 { 49 return discover.find(spi, propertyFileName, defaultImpl); 50 } 51 catch (DiscoveryException e) 52 { 53 logger.warn("Failed to find service for spi: " + spi.getName()); 54 return null; 55 } 56 } 57 58 69 public static Class findService(final Class spi, final String defaultImpl, final Class currentClass) 70 { 71 ClassLoaders loaders = (ClassLoaders)AccessController.doPrivileged(new PrivilegedAction () 72 { 73 public Object run() 74 { 75 return ClassLoaders.getAppLoaders(spi, currentClass, false); 76 } 77 }); 78 DiscoverClass discover = new DiscoverClass(loaders); 79 try 80 { 81 return discover.find(spi, System.getProperties(), defaultImpl); 82 } 83 catch (DiscoveryException e) 84 { 85 logger.warn("Failed to find service for spi: " + spi.getName()); 86 return null; 87 } 88 } 89 90 99 public static Class findService(Class spi, Class currentClass) 100 { 101 ClassLoaders loaders = ClassLoaders.getAppLoaders(spi, currentClass, false); 102 DiscoverClass discover = new DiscoverClass(loaders); 103 try 104 { 105 return discover.find(spi, System.getProperties()); 106 } 107 catch (DiscoveryException e) 108 { 109 logger.warn("Failed to find service for spi: " + spi.getName()); 110 return null; 111 } 112 } 113 114 125 public static Class findService(Class spi, Properties props, Class currentClass) 126 { 127 ClassLoaders loaders = ClassLoaders.getAppLoaders(spi, currentClass, false); 128 DiscoverClass discover = new DiscoverClass(loaders); 129 try 130 { 131 return discover.find(spi, props); 132 } 133 catch (DiscoveryException e) 134 { 135 logger.warn("Failed to find service for spi: " + spi.getName()); 136 return null; 137 } 138 } 139 140 public static InputStream findServiceDescriptor(String path, String name, Class currentClass) 141 { 142 if (path.startsWith("/")) 143 { 144 path = path.substring(1); 145 } 146 if (!path.endsWith("/")) 147 { 148 path += "/"; 149 } 150 if (path.startsWith(SERVICE_ROOT)) 151 { 152 path += name; 153 } 154 else 155 { 156 path = SERVICE_ROOT + path + name; 157 } 158 try 159 { 160 return IOUtils.getResourceAsStream(path, currentClass, false, false); 161 } 162 catch (IOException e) 163 { 164 return null; 165 } 166 } 167 } 168 | Popular Tags |