Merge
authorduke
Wed, 05 Jul 2017 21:16:49 +0200
changeset 35415 c7344a5392a2
parent 35414 59fc48a1a885 (diff)
parent 35411 af04ac0cdea9 (current diff)
child 35428 e0fcf12486c3
Merge
jdk/test/java/net/SocketPermission/policy
--- a/jaxws/.hgtags	Wed Jul 05 21:16:42 2017 +0200
+++ b/jaxws/.hgtags	Wed Jul 05 21:16:49 2017 +0200
@@ -348,3 +348,4 @@
 d0a97e57d2336238edf6a4cd60aafe67deb7258d jdk-9+100
 3e99318616da903e0dc8f07f9f9203dc1bd49921 jdk-9+101
 0868b93587cc99df3a4f4d3817a1aa756bea60ab jdk-9+102
+eb5e005a17e50d7d8340daaf21a5c3c5ae358d68 jdk-9+103
--- a/jaxws/src/java.xml.bind/share/classes/javax/xml/bind/ContextFinder.java	Wed Jul 05 21:16:42 2017 +0200
+++ b/jaxws/src/java.xml.bind/share/classes/javax/xml/bind/ContextFinder.java	Wed Jul 05 21:16:49 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2016, 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
@@ -29,11 +29,12 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
@@ -105,9 +106,9 @@
 
     /**
      * If the {@link InvocationTargetException} wraps an exception that shouldn't be wrapped,
-     * throw the wrapped exception.
+     * throw the wrapped exception. Otherwise returns exception to be wrapped for further processing.
      */
-    private static void handleInvocationTargetException(InvocationTargetException x) throws JAXBException {
+    private static Throwable handleInvocationTargetException(InvocationTargetException x) throws JAXBException {
         Throwable t = x.getTargetException();
         if (t != null) {
             if (t instanceof JAXBException)
@@ -118,7 +119,9 @@
                 throw (RuntimeException) t;
             if (t instanceof Error)
                 throw (Error) t;
+            return t;
         }
+        return x;
     }
 
 
@@ -157,9 +160,10 @@
         } catch (ClassNotFoundException x) {
             throw new JAXBException(Messages.format(Messages.PROVIDER_NOT_FOUND, className), x);
 
-        } catch (RuntimeException x) {
+        } catch (RuntimeException | JAXBException x) {
             // avoid wrapping RuntimeException to JAXBException,
             // because it indicates a bug in this code.
+            // JAXBException re-thrown as is
             throw x;
         } catch (Exception x) {
             // can't catch JAXBException because the method is hidden behind
@@ -189,8 +193,9 @@
             try {
                 Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class, Map.class);
                 // any failure in invoking this method would be considered fatal
-                context = m.invoke(null, contextPath, classLoader, properties);
-            } catch (NoSuchMethodException e) {
+                Object obj = instantiateProviderIfNecessary(m);
+                context = m.invoke(obj, contextPath, classLoader, properties);
+            } catch (NoSuchMethodException ignored) {
                 // it's not an error for the provider not to have this method.
             }
 
@@ -198,8 +203,9 @@
                 // try the old method that doesn't take properties. compatible with 1.0.
                 // it is an error for an implementation not to have both forms of the createContext method.
                 Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class);
+                Object obj = instantiateProviderIfNecessary(m);
                 // any failure in invoking this method would be considered fatal
-                context = m.invoke(null, contextPath, classLoader);
+                context = m.invoke(obj, contextPath, classLoader);
             }
 
             if (!(context instanceof JAXBContext)) {
@@ -208,18 +214,11 @@
             }
             return (JAXBContext) context;
         } catch (InvocationTargetException x) {
-            handleInvocationTargetException(x);
-            // for other exceptions, wrap the internal target exception
-            // with a JAXBException
-            Throwable e = x;
-            if (x.getTargetException() != null)
-                e = x.getTargetException();
+            // throw if it is exception not to be wrapped
+            // otherwise, wrap with a JAXBException
+            Throwable e = handleInvocationTargetException(x);
+            throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
 
-            throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
-        } catch (RuntimeException x) {
-            // avoid wrapping RuntimeException to JAXBException,
-            // because it indicates a bug in this code.
-            throw x;
         } catch (Exception x) {
             // can't catch JAXBException because the method is hidden behind
             // reflection.  Root element collisions detected in the call to
@@ -229,6 +228,23 @@
         }
     }
 
+    private static Object instantiateProviderIfNecessary(Method m) throws JAXBException {
+        Class<?> declaringClass = m.getDeclaringClass();
+        try {
+            if (JAXBContextFactory.class.isAssignableFrom(declaringClass)) {
+                return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
+                    @Override
+                    public Object run() throws Exception {
+                        return declaringClass.newInstance();
+                    }
+                });
+            }
+            return null;
+        } catch (PrivilegedActionException e) {
+            throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, declaringClass, e), e);
+        }
+    }
+
     /**
      * Create an instance of a class using the thread context ClassLoader
      */
@@ -255,7 +271,8 @@
         try {
 
             Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
-            Object context = m.invoke(null, classes, properties);
+            Object obj = instantiateProviderIfNecessary(m);
+            Object context = m.invoke(obj, classes, properties);
             if (!(context instanceof JAXBContext)) {
                 // the cast would fail, so generate an exception with a nice message
                 throw handleClassCastException(context.getClass(), JAXBContext.class);
@@ -264,13 +281,10 @@
 
         } catch (NoSuchMethodException | IllegalAccessException e) {
             throw new JAXBException(e);
-
         } catch (InvocationTargetException e) {
-            handleInvocationTargetException(e);
-
-            Throwable x = e;
-            if (e.getTargetException() != null)
-                x = e.getTargetException();
+            // throw if it is exception not to be wrapped
+            // otherwise, wrap with a JAXBException
+            Throwable x = handleInvocationTargetException(e);
 
             throw new JAXBException(x);
         }
--- a/jaxws/src/java.xml.bind/share/classes/javax/xml/bind/JAXBContext.java	Wed Jul 05 21:16:42 2017 +0200
+++ b/jaxws/src/java.xml.bind/share/classes/javax/xml/bind/JAXBContext.java	Wed Jul 05 21:16:49 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2016, 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
@@ -94,7 +94,7 @@
  * allows the merging of global elements and type definitions across a set of schemas (listed
  * in the {@code contextPath}). Since each schema in the schema set can belong
  * to distinct namespaces, the unification of schemas to an unmarshalling
- * context should be namespace independent.  This means that a client
+ * context must be namespace independent.  This means that a client
  * application is able to unmarshal XML documents that are instances of
  * any of the schemas listed in the {@code contextPath}.  For example:
  *
@@ -200,21 +200,28 @@
  *
  * <h3>Discovery of JAXB implementation</h3>
  * <p>
- * When one of the {@code newInstance} methods is called, a JAXB implementation is discovered
- * by the following steps.
+ * To create an instance of {@link JAXBContext}, one of {@code JAXBContext.newInstance(...)} methods is invoked. After
+ * JAX-B implementation is discovered, call is delegated to appropriate provider's method {@code createContext(...)}
+ * passing parameters from the original call.
+ * <p>
+ * JAX-B implementation discovery happens each time {@code JAXBContext.newInstance} is invoked. If there is no user
+ * specific configuration provided, default JAX-B provider must be returned.
+ * <p>
+ * Implementation discovery consists of following steps:
  *
  * <ol>
  *
  * <li>
- * For each package/class explicitly passed in to the {@link #newInstance} method, in the order they are specified,
- * {@code jaxb.properties} file is looked up in its package, by using the associated classloader &mdash;
+ * Packages/classes explicitly passed in to the {@link #newInstance} method are processed in the order they are
+ * specified, until {@code jaxb.properties} file is looked up in its package, by using the associated classloader &mdash;
  * this is {@link Class#getClassLoader() the owner class loader} for a {@link Class} argument, and for a package
  * the specified {@link ClassLoader}.
  *
  * <p>
- * If such a file is discovered, it is {@link Properties#load(InputStream) loaded} as a property file, and
- * the value of the {@link #JAXB_CONTEXT_FACTORY} key will be assumed to be the provider factory class.
- * This class is then loaded by the associated class loader discussed above.
+ * If such a resource is discovered, it is {@link Properties#load(InputStream) loaded} as a property file, and
+ * the value of the {@link #JAXB_CONTEXT_FACTORY} key will be assumed to be the provider factory class. If no value
+ * found, {@code "javax.xml.bind.context.factory"} is used as a key for backwards compatibility reasons. This class is
+ * then loaded by the associated class loader discussed above.
  *
  * <p>
  * This phase of the look up allows some packages to force the use of a certain JAXB implementation.
@@ -222,7 +229,9 @@
  *
  * <li>
  * If the system property {@link #JAXB_CONTEXT_FACTORY} exists, then its value is assumed to be the provider
- * factory class. This phase of the look up enables per-JVM override of the JAXB implementation.
+ * factory class. If no such property exists, properties {@code "javax.xml.bind.context.factory"} and
+ * {@code "javax.xml.bind.JAXBContext"} are checked too (in this order), for backwards compatibility reasons. This phase
+ * of the look up enables per-JVM override of the JAXB implementation.
  *
  * <li>
  * Provider of {@link javax.xml.bind.JAXBContextFactory} is loaded using the service-provider loading
@@ -235,43 +244,58 @@
  * <br>
  * In case of {@link java.util.ServiceConfigurationError service
  * configuration error} a {@link javax.xml.bind.JAXBException} will be thrown.
- * </li>
  *
  * <li>
  * Look for resource {@code /META-INF/services/javax.xml.bind.JAXBContext} using provided class loader.
  * Methods without class loader parameter use {@code Thread.currentThread().getContextClassLoader()}.
- * If such a resource exists, its content is assumed to be the provider factory class and must supply
- * an implementation class containing the following method signatures:
+ * If such a resource exists, its content is assumed to be the provider factory class.
+ *
+ * This configuration method is deprecated.
+ *
+ * <li>
+ * Finally, if all the steps above fail, then the rest of the look up is unspecified. That said,
+ * the recommended behavior is to simply look for some hard-coded platform default JAXB implementation.
+ * This phase of the look up is so that Java SE can have its own JAXB implementation as the last resort.
+ * </ol>
  *
+ * <p>
+ * Once the provider factory class is discovered, context creation is delegated to one of its
+ * {@code createContext(...)} methods.
+ *
+ * For backward compatibility reasons, there are two ways how to implement provider factory class:
+ * <ol>
+ *     <li>the class is implementation of {@link javax.xml.bind.JAXBContextFactory}. It must also implement no-arg
+ *     constructor. If discovered in other step then 3, new instance using no-arg constructor is created first.
+ *     After that, appropriate instance method is invoked on this instance.
+ *     <li>the class is not implementation of interface above and then it is mandated to implement the following
+ *     static method signatures:
  * <pre>
  *
  * public static JAXBContext createContext(
  *                                      String contextPath,
  *                                      ClassLoader classLoader,
- *                                      Map&lt;String,Object&gt; properties throws JAXBException
+ *                                      Map&lt;String,Object&gt; properties ) throws JAXBException
  *
  * public static JAXBContext createContext(
  *                                      Class[] classes,
  *                                      Map&lt;String,Object&gt; properties ) throws JAXBException
  * </pre>
- * This configuration method is deprecated.
- *
- * <li>
- * Finally, if all the steps above fail, then the rest of the look up is unspecified. That said,
- * the recommended behavior is to simply look for some hard-coded platform default JAXB implementation.
- * This phase of the look up is so that JavaSE can have its own JAXB implementation as the last resort.
+ *      In this scenario, appropriate static method is used instead of instance method. This approach is incompatible
+ *      with {@link java.util.ServiceLoader} so it can't be used with step 3.
  * </ol>
- *
  * <p>
- * Once the provider factory class {@link javax.xml.bind.JAXBContextFactory} is discovered, one of its methods
- * {@link javax.xml.bind.JAXBContextFactory#createContext(String, ClassLoader, java.util.Map)} or
- * {@link javax.xml.bind.JAXBContextFactory#createContext(Class[], java.util.Map)} is invoked
- * to create a {@link JAXBContext}.
+ * There is no difference in behavior of given method {@code createContext(...)} regardless of whether it uses approach
+ * 1 (JAXBContextFactory) or 2 (no interface, static methods).
  *
  * @apiNote
- * <p>Service discovery method using file /META-INF/services/javax.xml.bind.JAXBContext (described in step 4)
- * and leveraging provider's static methods is supported only to allow backwards compatibility, but it is strongly
- * recommended to migrate to standard ServiceLoader mechanism (described in step 3).
+ * Service discovery method using resource {@code /META-INF/services/javax.xml.bind.JAXBContext} (described in step 4)
+ * is supported only to allow backwards compatibility, it is strongly recommended to migrate to standard
+ * {@link java.util.ServiceLoader} mechanism (described in step 3). The difference here is the resource name, which
+ * doesn't match service's type name.
+ * <p>
+ * Also using providers implementing interface {@link JAXBContextFactory} is preferred over using ones defining
+ * static methods, same as {@link JAXBContext#JAXB_CONTEXT_FACTORY} property is preferred over property
+ * {@code "javax.xml.bind.context.factory"}
  *
  * @implNote
  * Within the last step, if Glassfish AS environment detected, its specific service loader is used to find factory class.
@@ -308,16 +332,10 @@
      * the context class loader of the current thread.
      *
      * @throws JAXBException if an error was encountered while creating the
-     *                       {@code JAXBContext} such as
-     * <ol>
-     *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
-     *   <li>an ambiguity among global elements contained in the contextPath</li>
-     *   <li>failure to locate a value for the context factory provider property</li>
-     *   <li>mixing schema derived packages from different providers on the same contextPath</li>
-     * </ol>
+     *               {@code JAXBContext}. See {@link JAXBContext#newInstance(String, ClassLoader, Map)} for details.
      */
     public static JAXBContext newInstance( String contextPath )
-        throws JAXBException {
+            throws JAXBException {
 
         //return newInstance( contextPath, JAXBContext.class.getClassLoader() );
         return newInstance( contextPath, getContextClassLoader());
@@ -405,13 +423,7 @@
      *
      * @return a new instance of a {@code JAXBContext}
      * @throws JAXBException if an error was encountered while creating the
-     *                       {@code JAXBContext} such as
-     * <ol>
-     *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
-     *   <li>an ambiguity among global elements contained in the contextPath</li>
-     *   <li>failure to locate a value for the context factory provider property</li>
-     *   <li>mixing schema derived packages from different providers on the same contextPath</li>
-     * </ol>
+     *               {@code JAXBContext}. See {@link JAXBContext#newInstance(String, ClassLoader, Map)} for details.
      */
     public static JAXBContext newInstance( String contextPath, ClassLoader classLoader ) throws JAXBException {
 
@@ -427,7 +439,7 @@
      * the instantiation of {@link JAXBContext}.
      *
      * <p>
-     * The interpretation of properties is up to implementations. Implementations should
+     * The interpretation of properties is up to implementations. Implementations must
      * throw {@code JAXBException} if it finds properties that it doesn't understand.
      *
      * @param contextPath list of java package names that contain schema derived classes
@@ -439,13 +451,7 @@
      *
      * @return a new instance of a {@code JAXBContext}
      * @throws JAXBException if an error was encountered while creating the
-     *                       {@code JAXBContext} such as
-     * <ol>
-     *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
-     *   <li>an ambiguity among global elements contained in the contextPath</li>
-     *   <li>failure to locate a value for the context factory provider property</li>
-     *   <li>mixing schema derived packages from different providers on the same contextPath</li>
-     * </ol>
+     *                       {@code JAXBContext}. See {@link #newInstance(String, ClassLoader)} for details.
      * @since 1.6, JAXB 2.0
      */
     public static JAXBContext newInstance( String contextPath,
@@ -454,14 +460,14 @@
 
         return ContextFinder.find(
                         /* The default property name according to the JAXB spec */
-                        JAXB_CONTEXT_FACTORY,
+                JAXB_CONTEXT_FACTORY,
 
                         /* the context path supplied by the client app */
-                        contextPath,
+                contextPath,
 
                         /* class loader to be used */
-                        classLoader,
-                        properties );
+                classLoader,
+                properties );
     }
 
 // TODO: resurrect this once we introduce external annotations
@@ -583,17 +589,8 @@
      * @return
      *      A new instance of a {@code JAXBContext}.
      *
-     * @throws JAXBException
-     *      if an error was encountered while creating the
-     *      {@code JAXBContext}, such as (but not limited to):
-     * <ol>
-     *  <li>No JAXB implementation was discovered
-     *  <li>Classes use JAXB annotations incorrectly
-     *  <li>Classes have colliding annotations (i.e., two classes with the same type name)
-     *  <li>The JAXB implementation was unable to locate
-     *      provider-specific out-of-band information (such as additional
-     *      files generated at the development time.)
-     * </ol>
+     * @throws JAXBException if an error was encountered while creating the
+     *               {@code JAXBContext}. See {@link JAXBContext#newInstance(Class[], Map)} for details.
      *
      * @throws IllegalArgumentException
      *      if the parameter contains {@code null} (i.e., {@code newInstance(null);})
@@ -601,7 +598,7 @@
      * @since 1.6, JAXB 2.0
      */
     public static JAXBContext newInstance( Class<?> ... classesToBeBound )
-        throws JAXBException {
+            throws JAXBException {
 
         return newInstance(classesToBeBound,Collections.<String,Object>emptyMap());
     }
@@ -614,7 +611,7 @@
      * to configure 'properties' for this instantiation of {@link JAXBContext}.
      *
      * <p>
-     * The interpretation of properties is up to implementations. Implementations should
+     * The interpretation of properties is up to implementations. Implementations must
      * throw {@code JAXBException} if it finds properties that it doesn't understand.
      *
      * @param classesToBeBound
@@ -646,10 +643,10 @@
      * @since 1.6, JAXB 2.0
      */
     public static JAXBContext newInstance( Class<?>[] classesToBeBound, Map<String,?> properties )
-        throws JAXBException {
+            throws JAXBException {
 
         if (classesToBeBound == null) {
-                throw new IllegalArgumentException();
+            throw new IllegalArgumentException();
         }
 
         // but it is an error to have nulls in it.
--- a/jaxws/src/java.xml.bind/share/classes/javax/xml/bind/JAXBContextFactory.java	Wed Jul 05 21:16:42 2017 +0200
+++ b/jaxws/src/java.xml.bind/share/classes/javax/xml/bind/JAXBContextFactory.java	Wed Jul 05 21:16:49 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, 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
@@ -56,14 +56,7 @@
      *
      * @throws JAXBException
      *      if an error was encountered while creating the
-     *      {@code JAXBContext}, such as (but not limited to):
-     * <ol>
-     *  <li>Classes use JAXB annotations incorrectly
-     *  <li>Classes have colliding annotations (i.e., two classes with the same type name)
-     *  <li>The JAXB implementation was unable to locate
-     *      provider-specific out-of-band information (such as additional
-     *      files generated at the development time.)
-     * </ol>
+     *      {@code JAXBContext}. See {@link JAXBContext#newInstance(Class[], Map)} for details.
      *
      * @throws IllegalArgumentException
      *      if the parameter contains {@code null} (i.e., {@code newInstance(null,someMap);})
@@ -81,7 +74,7 @@
      * For semantics see {@link javax.xml.bind.JAXBContext#newInstance(String, ClassLoader, java.util.Map)}
      *
      * <p>
-     * The interpretation of properties is up to implementations. Implementations should
+     * The interpretation of properties is up to implementations. Implementations must
      * throw {@code JAXBException} if it finds properties that it doesn't understand.
      *
      * @param contextPath list of java package names that contain schema derived classes
@@ -93,13 +86,8 @@
      *
      * @return a new instance of a {@code JAXBContext}
      * @throws JAXBException if an error was encountered while creating the
-     *                       {@code JAXBContext} such as
-     * <ol>
-     *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
-     *   <li>an ambiguity among global elements contained in the contextPath</li>
-     *   <li>failure to locate a value for the context factory provider property</li>
-     *   <li>mixing schema derived packages from different providers on the same contextPath</li>
-     * </ol>
+     *      {@code JAXBContext}. See {@link JAXBContext#newInstance(String, ClassLoader, Map)} for details.
+     *
      * @since 9, JAXB 2.3
      */
     JAXBContext createContext(String contextPath,