src/share/jaxws_classes/javax/xml/bind/ContextFinder.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -308,39 +308,49 @@
             if(  factoryClassName != null ) {
                 return newInstance( contextPath, factoryClassName, classLoader, properties );
             }
         }
 
-        if (getContextClassLoader() == classLoader) {
-            Class factory = lookupUsingOSGiServiceLoader("javax.xml.bind.JAXBContext");
-            if (factory != null) {
+        // OSGi search
+        Class jaxbContext = lookupJaxbContextUsingOsgiServiceLoader();
+        if (jaxbContext != null) {
                 logger.fine("OSGi environment detected");
-                return newInstance(contextPath, factory, classLoader, properties);
-            }
+            return newInstance(contextPath, jaxbContext, classLoader, properties);
         }
 
         logger.fine("Searching META-INF/services");
         // search META-INF services next
-        BufferedReader r;
+        BufferedReader r = null;
         try {
             final StringBuilder resource = new StringBuilder().append("META-INF/services/").append(jaxbContextFQCN);
             final InputStream resourceStream =
                     classLoader.getResourceAsStream(resource.toString());
 
             if (resourceStream != null) {
                 r = new BufferedReader(new InputStreamReader(resourceStream, "UTF-8"));
-                factoryClassName = r.readLine().trim();
+                factoryClassName = r.readLine();
+                if (factoryClassName != null) {
+                    factoryClassName = factoryClassName.trim();
+                }
                 r.close();
                 return newInstance(contextPath, factoryClassName, classLoader, properties);
             } else {
                 logger.log(Level.FINE, "Unable to load:{0}", resource.toString());
             }
         } catch (UnsupportedEncodingException e) {
             // should never happen
             throw new JAXBException(e);
         } catch (IOException e) {
             throw new JAXBException(e);
+        } finally {
+            try {
+                if (r != null) {
+                    r.close();
+                }
+            } catch (IOException ex) {
+                Logger.getLogger(ContextFinder.class.getName()).log(Level.SEVERE, null, ex);
+            }
         }
 
         // else no provider found
         logger.fine("Trying to create the platform default provider");
         return newInstance(contextPath, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader, properties);

@@ -400,19 +410,20 @@
             } else {
                 logger.fine("  not found");
             }
         }
 
-        Class factory = lookupUsingOSGiServiceLoader("javax.xml.bind.JAXBContext");
-        if (factory != null) {
+        // OSGi search
+        Class jaxbContext = lookupJaxbContextUsingOsgiServiceLoader();
+        if (jaxbContext != null) {
             logger.fine("OSGi environment detected");
-            return newInstance(classes, properties, factory);
+            return newInstance(classes, properties, jaxbContext);
         }
 
         // search META-INF services next
         logger.fine("Checking META-INF/services");
-        BufferedReader r;
+        BufferedReader r = null;
         try {
             final String resource = new StringBuilder("META-INF/services/").append(jaxbContextFQCN).toString();
             ClassLoader classLoader = getContextClassLoader();
             URL resourceURL;
             if(classLoader==null)

@@ -421,37 +432,47 @@
                 resourceURL = classLoader.getResource(resource);
 
             if (resourceURL != null) {
                 logger.log(Level.FINE, "Reading {0}", resourceURL);
                 r = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "UTF-8"));
-                factoryClassName = r.readLine().trim();
+                factoryClassName = r.readLine();
+                if (factoryClassName != null) {
+                    factoryClassName = factoryClassName.trim();
+                }
                 return newInstance(classes, properties, factoryClassName);
             } else {
                 logger.log(Level.FINE, "Unable to find: {0}", resource);
             }
         } catch (UnsupportedEncodingException e) {
             // should never happen
             throw new JAXBException(e);
         } catch (IOException e) {
             throw new JAXBException(e);
+        } finally {
+            if (r != null) {
+                try {
+                    r.close();
+                } catch (IOException ex) {
+                    logger.log(Level.FINE, "Unable to close stream", ex);
+                }
+            }
         }
 
         // else no provider found
         logger.fine("Trying to create the platform default provider");
         return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS);
     }
 
-    private static Class lookupUsingOSGiServiceLoader(String factoryId) {
+    private static Class lookupJaxbContextUsingOsgiServiceLoader() {
         try {
-            // Use reflection to avoid having any dependendcy on ServiceLoader class
-            Class serviceClass = Class.forName(factoryId);
+            // Use reflection to avoid having any dependency on ServiceLoader class
             Class target = Class.forName("com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader");
             Method m = target.getMethod("lookupProviderClasses", Class.class);
-            Iterator iter = ((Iterable) m.invoke(null, serviceClass)).iterator();
+            Iterator iter = ((Iterable) m.invoke(null, JAXBContext.class)).iterator();
             return iter.hasNext() ? (Class)iter.next() : null;
         } catch(Exception e) {
-            logger.log(Level.FINE, "Unable to find from OSGi: {0}", factoryId);
+            logger.log(Level.FINE, "Unable to find from OSGi: javax.xml.bind.JAXBContext");
             return null;
         }
     }
 
     private static Properties loadJAXBProperties( ClassLoader classLoader,