< prev index next >

jaxws/src/java.xml.bind/share/classes/javax/xml/bind/ContextFinder.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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


 141 
 142         return new JAXBException(Messages.format(Messages.ILLEGAL_CAST,
 143                 // we don't care where the impl class is, we want to know where JAXBContext lives in the impl
 144                 // class' ClassLoader
 145                 getClassClassLoader(originalType).getResource("javax/xml/bind/JAXBContext.class"),
 146                 targetTypeURL));
 147     }
 148 
 149     /**
 150      * Create an instance of a class using the specified ClassLoader
 151      */
 152     static JAXBContext newInstance(String contextPath,
 153                                    String className,
 154                                    ClassLoader classLoader,
 155                                    Map properties) throws JAXBException {
 156 
 157         try {
 158             Class spFactory = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader);
 159             return newInstance(contextPath, spFactory, classLoader, properties);
 160         } catch (ClassNotFoundException x) {
 161             throw new JAXBException(Messages.format(Messages.PROVIDER_NOT_FOUND, className), x);
 162 
 163         } catch (RuntimeException | JAXBException x) {
 164             // avoid wrapping RuntimeException to JAXBException,
 165             // because it indicates a bug in this code.
 166             // JAXBException re-thrown as is
 167             throw x;
 168         } catch (Exception x) {
 169             // can't catch JAXBException because the method is hidden behind
 170             // reflection.  Root element collisions detected in the call to
 171             // createContext() are reported as JAXBExceptions - just re-throw it
 172             // some other type of exception - just wrap it
 173             throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, className, x), x);
 174         }
 175     }
 176 
 177     static JAXBContext newInstance(String contextPath,
 178                                    Class spFactory,
 179                                    ClassLoader classLoader,
 180                                    Map properties) throws JAXBException {
 181 


 211             if (!(context instanceof JAXBContext)) {
 212                 // the cast would fail, so generate an exception with a nice message
 213                 throw handleClassCastException(context.getClass(), JAXBContext.class);
 214             }
 215             return (JAXBContext) context;
 216         } catch (InvocationTargetException x) {
 217             // throw if it is exception not to be wrapped
 218             // otherwise, wrap with a JAXBException
 219             Throwable e = handleInvocationTargetException(x);
 220             throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
 221 
 222         } catch (Exception x) {
 223             // can't catch JAXBException because the method is hidden behind
 224             // reflection.  Root element collisions detected in the call to
 225             // createContext() are reported as JAXBExceptions - just re-throw it
 226             // some other type of exception - just wrap it
 227             throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, x), x);
 228         }
 229     }
 230 
 231     private static Object instantiateProviderIfNecessary(Class<?> implClass) throws JAXBException {
 232         try {
 233             if (JAXBContextFactory.class.isAssignableFrom(implClass)) {
 234                 return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
 235                     @Override
 236                     public Object run() throws Exception {
 237                         return implClass.newInstance();
 238                     }
 239                 });
 240             }
 241             return null;
 242         } catch (PrivilegedActionException x) {
 243             Throwable e = (x.getCause() == null) ? x : x.getCause();
 244             throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, implClass, e), e);
 245         }
 246     }
 247 
 248     /**
 249      * Create an instance of a class using the thread context ClassLoader
 250      */
 251     static JAXBContext newInstance(Class[] classes, Map properties, String className) throws JAXBException {
 252 
 253         Class spi;
 254         try {
 255             spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, getContextClassLoader());
 256         } catch (ClassNotFoundException e) {
 257             throw new JAXBException(e);
 258         }
 259 
 260         if (logger.isLoggable(Level.FINE)) {
 261             // extra check to avoid costly which operation if not logged
 262             logger.log(Level.FINE, "loaded {0} from {1}", new Object[]{className, which(spi)});
 263         }
 264 
 265         return newInstance(classes, properties, spi);
 266     }
 267 
 268     static JAXBContext newInstance(Class[] classes,
 269                                    Map properties,
 270                                    Class spFactory) throws JAXBException {
 271         try {
 272 
 273             Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
 274             Object obj = instantiateProviderIfNecessary(spFactory);
 275             Object context = m.invoke(obj, classes, properties);
 276             if (!(context instanceof JAXBContext)) {
 277                 // the cast would fail, so generate an exception with a nice message


 508      * Convenience method for {@link #which(Class, ClassLoader)}.
 509      *
 510      * Equivalent to calling: which(clazz, clazz.getClassLoader())
 511      *
 512      * @param clazz
 513      *          The class to search for
 514      * @return
 515      *          the URL for the class or null if it wasn't found
 516      */
 517     static URL which(Class clazz) {
 518         return which(clazz, getClassClassLoader(clazz));
 519     }
 520 
 521     @SuppressWarnings("unchecked")
 522     private static ClassLoader getContextClassLoader() {
 523         if (System.getSecurityManager() == null) {
 524             return Thread.currentThread().getContextClassLoader();
 525         } else {
 526             return (ClassLoader) java.security.AccessController.doPrivileged(
 527                     new java.security.PrivilegedAction() {

 528                         public java.lang.Object run() {
 529                             return Thread.currentThread().getContextClassLoader();
 530                         }
 531                     });
 532         }
 533     }
 534 
 535     @SuppressWarnings("unchecked")
 536     private static ClassLoader getClassClassLoader(final Class c) {
 537         if (System.getSecurityManager() == null) {
 538             return c.getClassLoader();
 539         } else {
 540             return (ClassLoader) java.security.AccessController.doPrivileged(
 541                     new java.security.PrivilegedAction() {

 542                         public java.lang.Object run() {
 543                             return c.getClassLoader();
 544                         }
 545                     });
 546         }
 547     }
 548 
 549     private static ClassLoader getSystemClassLoader() {
 550         if (System.getSecurityManager() == null) {
 551             return ClassLoader.getSystemClassLoader();
 552         } else {
 553             return (ClassLoader) java.security.AccessController.doPrivileged(
 554                     new java.security.PrivilegedAction() {

 555                         public java.lang.Object run() {
 556                             return ClassLoader.getSystemClassLoader();
 557                         }
 558                     });
 559         }
 560     }
 561 
 562     // ServiceLoaderUtil.firstByServiceLoaderDeprecated should be used instead.
 563     @Deprecated
 564     static String firstByServiceLoaderDeprecated(Class spiClass,
 565                                                  ClassLoader classLoader) throws JAXBException {
 566 
 567         final String jaxbContextFQCN = spiClass.getName();
 568 
 569         logger.fine("Searching META-INF/services");
 570 
 571         // search META-INF services next
 572         BufferedReader r = null;
 573         final String resource = "META-INF/services/" + jaxbContextFQCN;
 574         try {


   1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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


 141 
 142         return new JAXBException(Messages.format(Messages.ILLEGAL_CAST,
 143                 // we don't care where the impl class is, we want to know where JAXBContext lives in the impl
 144                 // class' ClassLoader
 145                 getClassClassLoader(originalType).getResource("javax/xml/bind/JAXBContext.class"),
 146                 targetTypeURL));
 147     }
 148 
 149     /**
 150      * Create an instance of a class using the specified ClassLoader
 151      */
 152     static JAXBContext newInstance(String contextPath,
 153                                    String className,
 154                                    ClassLoader classLoader,
 155                                    Map properties) throws JAXBException {
 156 
 157         try {
 158             Class spFactory = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader);
 159             return newInstance(contextPath, spFactory, classLoader, properties);
 160         } catch (ClassNotFoundException x) {
 161             throw new JAXBException(Messages.format(Messages.DEFAULT_PROVIDER_NOT_FOUND), x);
 162 
 163         } catch (RuntimeException | JAXBException x) {
 164             // avoid wrapping RuntimeException to JAXBException,
 165             // because it indicates a bug in this code.
 166             // JAXBException re-thrown as is
 167             throw x;
 168         } catch (Exception x) {
 169             // can't catch JAXBException because the method is hidden behind
 170             // reflection.  Root element collisions detected in the call to
 171             // createContext() are reported as JAXBExceptions - just re-throw it
 172             // some other type of exception - just wrap it
 173             throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, className, x), x);
 174         }
 175     }
 176 
 177     static JAXBContext newInstance(String contextPath,
 178                                    Class spFactory,
 179                                    ClassLoader classLoader,
 180                                    Map properties) throws JAXBException {
 181 


 211             if (!(context instanceof JAXBContext)) {
 212                 // the cast would fail, so generate an exception with a nice message
 213                 throw handleClassCastException(context.getClass(), JAXBContext.class);
 214             }
 215             return (JAXBContext) context;
 216         } catch (InvocationTargetException x) {
 217             // throw if it is exception not to be wrapped
 218             // otherwise, wrap with a JAXBException
 219             Throwable e = handleInvocationTargetException(x);
 220             throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
 221 
 222         } catch (Exception x) {
 223             // can't catch JAXBException because the method is hidden behind
 224             // reflection.  Root element collisions detected in the call to
 225             // createContext() are reported as JAXBExceptions - just re-throw it
 226             // some other type of exception - just wrap it
 227             throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, x), x);
 228         }
 229     }
 230 
 231     private static Object instantiateProviderIfNecessary(final Class<?> implClass) throws JAXBException {
 232         try {
 233             if (JAXBContextFactory.class.isAssignableFrom(implClass)) {
 234                 return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
 235                     @Override
 236                     public Object run() throws Exception {
 237                         return implClass.newInstance();
 238                     }
 239                 });
 240             }
 241             return null;
 242         } catch (PrivilegedActionException x) {
 243             Throwable e = (x.getCause() == null) ? x : x.getCause();
 244             throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, implClass, e), e);
 245         }
 246     }
 247 
 248     /**
 249      * Create an instance of a class using the thread context ClassLoader
 250      */
 251     static JAXBContext newInstance(Class[] classes, Map properties, String className) throws JAXBException {
 252 
 253         Class spi;
 254         try {
 255             spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, getContextClassLoader());
 256         } catch (ClassNotFoundException e) {
 257             throw new JAXBException(Messages.format(Messages.DEFAULT_PROVIDER_NOT_FOUND), e);
 258         }
 259 
 260         if (logger.isLoggable(Level.FINE)) {
 261             // extra check to avoid costly which operation if not logged
 262             logger.log(Level.FINE, "loaded {0} from {1}", new Object[]{className, which(spi)});
 263         }
 264 
 265         return newInstance(classes, properties, spi);
 266     }
 267 
 268     static JAXBContext newInstance(Class[] classes,
 269                                    Map properties,
 270                                    Class spFactory) throws JAXBException {
 271         try {
 272 
 273             Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
 274             Object obj = instantiateProviderIfNecessary(spFactory);
 275             Object context = m.invoke(obj, classes, properties);
 276             if (!(context instanceof JAXBContext)) {
 277                 // the cast would fail, so generate an exception with a nice message


 508      * Convenience method for {@link #which(Class, ClassLoader)}.
 509      *
 510      * Equivalent to calling: which(clazz, clazz.getClassLoader())
 511      *
 512      * @param clazz
 513      *          The class to search for
 514      * @return
 515      *          the URL for the class or null if it wasn't found
 516      */
 517     static URL which(Class clazz) {
 518         return which(clazz, getClassClassLoader(clazz));
 519     }
 520 
 521     @SuppressWarnings("unchecked")
 522     private static ClassLoader getContextClassLoader() {
 523         if (System.getSecurityManager() == null) {
 524             return Thread.currentThread().getContextClassLoader();
 525         } else {
 526             return (ClassLoader) java.security.AccessController.doPrivileged(
 527                     new java.security.PrivilegedAction() {
 528                         @Override
 529                         public java.lang.Object run() {
 530                             return Thread.currentThread().getContextClassLoader();
 531                         }
 532                     });
 533         }
 534     }
 535 
 536     @SuppressWarnings("unchecked")
 537     private static ClassLoader getClassClassLoader(final Class c) {
 538         if (System.getSecurityManager() == null) {
 539             return c.getClassLoader();
 540         } else {
 541             return (ClassLoader) java.security.AccessController.doPrivileged(
 542                     new java.security.PrivilegedAction() {
 543                         @Override
 544                         public java.lang.Object run() {
 545                             return c.getClassLoader();
 546                         }
 547                     });
 548         }
 549     }
 550 
 551     private static ClassLoader getSystemClassLoader() {
 552         if (System.getSecurityManager() == null) {
 553             return ClassLoader.getSystemClassLoader();
 554         } else {
 555             return (ClassLoader) java.security.AccessController.doPrivileged(
 556                     new java.security.PrivilegedAction() {
 557                         @Override
 558                         public java.lang.Object run() {
 559                             return ClassLoader.getSystemClassLoader();
 560                         }
 561                     });
 562         }
 563     }
 564 
 565     // ServiceLoaderUtil.firstByServiceLoaderDeprecated should be used instead.
 566     @Deprecated
 567     static String firstByServiceLoaderDeprecated(Class spiClass,
 568                                                  ClassLoader classLoader) throws JAXBException {
 569 
 570         final String jaxbContextFQCN = spiClass.getName();
 571 
 572         logger.fine("Searching META-INF/services");
 573 
 574         // search META-INF services next
 575         BufferedReader r = null;
 576         final String resource = "META-INF/services/" + jaxbContextFQCN;
 577         try {


< prev index next >