< prev index next >

src/java.xml.bind/share/classes/javax/xml/bind/ServiceLoaderUtil.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.bind;
  27 
  28 import java.io.File;
  29 import java.io.FileInputStream;
  30 import java.io.IOException;

  31 import java.lang.reflect.Method;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 import java.util.Iterator;
  35 import java.util.Properties;
  36 import java.util.ServiceLoader;
  37 import java.util.logging.Level;
  38 import java.util.logging.Logger;
  39 
  40 /**
  41  * Shared ServiceLoader/FactoryFinder Utils shared among SAAJ, JAXB and JAXWS
  42  * - this class must be duplicated to all those projects, but it's
  43  * basically generic code and we want to have it everywhere same.
  44  *
  45  * @author Miroslav.Kos@oracle.com
  46  */
  47 class ServiceLoaderUtil {
  48 
  49     private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader";
  50     private static final String OSGI_SERVICE_LOADER_METHOD_NAME = "lookupProviderClasses";
  51 
  52     static <P> P firstByServiceLoader(Class<P> spiClass, Logger logger) {
  53         // service discovery

  54         ServiceLoader<P> serviceLoader = ServiceLoader.load(spiClass);
  55         for (P impl : serviceLoader) {
  56             logger.fine("ServiceProvider loading Facility used; returning object [" + impl.getClass().getName() + "]");
  57             return impl;
  58         }



  59         return null;
  60     }
  61 
  62     static boolean isOsgi(Logger logger) {
  63         try {
  64             Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
  65             return true;
  66         } catch (ClassNotFoundException ignored) {
  67             logger.log(Level.FINE, "OSGi classes not found, OSGi not available.", ignored);
  68         }
  69         return false;
  70     }
  71 
  72     static Object lookupUsingOSGiServiceLoader(String factoryId, Logger logger) {
  73         try {
  74             // Use reflection to avoid having any dependendcy on ServiceLoader class
  75             Class serviceClass = Class.forName(factoryId);
  76             Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
  77             Method m = target.getMethod(OSGI_SERVICE_LOADER_METHOD_NAME, Class.class);
  78             Iterator iter = ((Iterable) m.invoke(null, serviceClass)).iterator();
  79             if (iter.hasNext()) {
  80                 Object next = iter.next();
  81                 logger.fine("Found implementation using OSGi facility; returning object [" + next.getClass().getName() + "].");
  82                 return next;
  83             } else {
  84                 return null;
  85             }
  86         } catch (Exception ignored) {
  87             logger.log(Level.FINE, "Unable to find from OSGi: [" + factoryId + "]", ignored);
  88             return null;
  89         }
  90     }
  91 
  92     static String propertyFileLookup(final String configFullPath, final String factoryId) throws IOException {
  93         File f = new File(configFullPath);
  94         String factoryClassName = null;
  95         if (f.exists()) {
  96             Properties props = new Properties();
  97             FileInputStream stream = null;
  98             try {
  99                 stream = new FileInputStream(f);
 100                 props.load(stream);
 101                 factoryClassName = props.getProperty(factoryId);
 102             } finally {
 103                 if (stream != null) {
 104                     try {
 105                         stream.close();
 106                     } catch (IOException ignored) {




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.bind;
  27 
  28 import java.io.File;
  29 import java.io.FileInputStream;
  30 import java.io.IOException;
  31 import java.lang.reflect.InvocationTargetException;
  32 import java.lang.reflect.Method;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import java.util.Iterator;
  36 import java.util.Properties;
  37 import java.util.ServiceLoader;
  38 import java.util.logging.Level;
  39 import java.util.logging.Logger;
  40 
  41 /**
  42  * Shared ServiceLoader/FactoryFinder Utils shared among SAAJ, JAXB and JAXWS
  43  * - this class must be duplicated to all those projects, but it's
  44  * basically generic code and we want to have it everywhere same.
  45  *
  46  * @author Miroslav.Kos@oracle.com
  47  */
  48 class ServiceLoaderUtil {
  49 
  50     private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader";
  51     private static final String OSGI_SERVICE_LOADER_METHOD_NAME = "lookupProviderClasses";
  52 
  53     static <P, T extends Exception> P firstByServiceLoader(Class<P> spiClass, Logger logger, ExceptionHandler<T> handler) throws T {
  54         // service discovery
  55         try {
  56             ServiceLoader<P> serviceLoader = ServiceLoader.load(spiClass);
  57             for (P impl : serviceLoader) {
  58                 logger.fine("ServiceProvider loading Facility used; returning object [" + impl.getClass().getName() + "]");
  59                 return impl;
  60             }
  61         } catch (Throwable t) {
  62             throw handler.createException(t, "Error while searching for service [" + spiClass.getName() + "]");
  63         }
  64         return null;
  65     }
  66 
  67     static boolean isOsgi(Logger logger) {
  68         try {
  69             Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
  70             return true;
  71         } catch (ClassNotFoundException ignored) {
  72             logger.log(Level.FINE, "OSGi classes not found, OSGi not available.", ignored);
  73         }
  74         return false;
  75     }
  76 
  77     static Object lookupUsingOSGiServiceLoader(String factoryId, Logger logger) {
  78         try {
  79             // Use reflection to avoid having any dependendcy on ServiceLoader class
  80             Class serviceClass = Class.forName(factoryId);
  81             Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
  82             Method m = target.getMethod(OSGI_SERVICE_LOADER_METHOD_NAME, Class.class);
  83             Iterator iter = ((Iterable) m.invoke(null, serviceClass)).iterator();
  84             if (iter.hasNext()) {
  85                 Object next = iter.next();
  86                 logger.fine("Found implementation using OSGi facility; returning object [" + next.getClass().getName() + "].");
  87                 return next;
  88             } else {
  89                 return null;
  90             }
  91         } catch (IllegalAccessException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException ignored) {
  92             logger.log(Level.FINE, "Unable to find from OSGi: [" + factoryId + "]", ignored);
  93             return null;
  94         }
  95     }
  96 
  97     static String propertyFileLookup(final String configFullPath, final String factoryId) throws IOException {
  98         File f = new File(configFullPath);
  99         String factoryClassName = null;
 100         if (f.exists()) {
 101             Properties props = new Properties();
 102             FileInputStream stream = null;
 103             try {
 104                 stream = new FileInputStream(f);
 105                 props.load(stream);
 106                 factoryClassName = props.getProperty(factoryId);
 107             } finally {
 108                 if (stream != null) {
 109                     try {
 110                         stream.close();
 111                     } catch (IOException ignored) {


< prev index next >