KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > SpiUtils


1 /*
2  * $Id: SpiUtils.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.security.AccessController JavaDoc;
16 import java.security.PrivilegedAction JavaDoc;
17 import java.util.Properties JavaDoc;
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 // @ThreadSafe
26
public class SpiUtils
27 {
28     private static final Log logger = LogFactory.getLog(SpiUtils.class);
29
30     public static final String JavaDoc SERVICE_ROOT = "META-INF/services/";
31
32     /**
33      * Find class implementing a specified SPI.
34      *
35      * @param spi Service Provider Interface Class.
36      * @param propertyFileName is a location of a property file that contains the SPI
37      * property value
38      * @param defaultImpl Default implementation class name.
39      * @param currentClass is used to include the classloader of the calling class in
40      * the search. All system classloaders will be checked as well.
41      * @return Class implementing the SPI or null if a service was not found
42      */

43     public static Class JavaDoc findService(Class JavaDoc spi, String JavaDoc propertyFileName, String JavaDoc defaultImpl, Class JavaDoc 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     /**
59      * Find class implementing a specified SPI. The system properties will be checked
60      * for an SPI property to use. this will be the fully qualified SPI class name.
61      *
62      * @param spi Service Provider Interface Class.
63      * @param defaultImpl Default implementation class name.
64      * @param currentClass is used to include the classloader of the calling class in
65      * the search. All system classloaders will be checked as well.
66      * @return Class implementing the SPI or the default implementation class if
67      * nothing has been found
68      */

69     public static Class JavaDoc findService(final Class JavaDoc spi, final String JavaDoc defaultImpl, final Class JavaDoc currentClass)
70     {
71         ClassLoaders loaders = (ClassLoaders)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
72         {
73             public Object JavaDoc 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     /**
91      * Find class implementing a specified SPI. The system properties will be checked
92      * for an SPI property to use. this will be the fully qualified SPI class name.
93      *
94      * @param spi Service Provider Interface Class.
95      * @param currentClass is used to include the classloader of the calling class in
96      * the search. All system classloaders will be checked as well.
97      * @return Class implementing the SPI or null if a service was not found
98      */

99     public static Class JavaDoc findService(Class JavaDoc spi, Class JavaDoc 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     /**
115      * Find class implementing a specified SPI.
116      *
117      * @param spi Service Provider Interface Class.
118      * @param currentClass is used to include the classloader of the calling class in
119      * the search.
120      * @param props The properties will be checked for an SPI property to use. this
121      * will be the fully qualified SPI class name. All system classloaders
122      * will be checked as well.
123      * @return Class implementing the SPI or null if a service was not found
124      */

125     public static Class JavaDoc findService(Class JavaDoc spi, Properties JavaDoc props, Class JavaDoc 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 JavaDoc findServiceDescriptor(String JavaDoc path, String JavaDoc name, Class JavaDoc 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 JavaDoc e)
163         {
164             return null;
165         }
166     }
167 }
168
Popular Tags