jaxws/src/java.xml.bind/share/classes/javax/xml/bind/ContextFinder.java
author duke
Wed, 05 Jul 2017 21:00:03 +0200
changeset 33642 d16d6d82a16a
parent 31109 a542f8dcbbf8
child 35413 56a695bbebe8
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     1
/*
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     4
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    10
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    15
 * accompanied this code).
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    16
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    20
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    23
 * questions.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    24
 */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    25
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    26
package javax.xml.bind;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    27
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    28
import java.io.BufferedReader;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    29
import java.io.IOException;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    30
import java.io.InputStream;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    31
import java.io.InputStreamReader;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    32
import java.io.UnsupportedEncodingException;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    34
import java.lang.reflect.Method;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    35
import java.net.URL;
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    36
import java.security.AccessController;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    37
import java.util.Map;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    38
import java.util.Properties;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    39
import java.util.StringTokenizer;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    40
import java.util.logging.ConsoleHandler;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    41
import java.util.logging.Level;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    42
import java.util.logging.Logger;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    43
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    44
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    45
/**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    46
 * This class is package private and therefore is not exposed as part of the
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    47
 * JAXB API.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    48
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    49
 * This code is designed to implement the JAXB 1.0 spec pluggability feature
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    50
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    51
 * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    52
 * @see JAXBContext
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    53
 */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    54
class ContextFinder {
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    55
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    56
    /**
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    57
     * When JAXB is in J2SE, rt.jar has to have a JAXB implementation.
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    58
     * However, rt.jar cannot have META-INF/services/javax.xml.bind.JAXBContext
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    59
     * because if it has, it will take precedence over any file that applications have
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    60
     * in their jar files.
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    61
     *
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    62
     * <p>
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    63
     * When the user bundles his own JAXB implementation, we'd like to use it, and we
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    64
     * want the platform default to be used only when there's no other JAXB provider.
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    65
     *
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    66
     * <p>
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    67
     * For this reason, we have to hard-code the class name into the API.
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    68
     */
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    69
    private static final String PLATFORM_DEFAULT_FACTORY_CLASS = "com.sun.xml.internal.bind.v2.ContextFactory";
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    70
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
    71
    // previous value of JAXBContext.JAXB_CONTEXT_FACTORY, using also this to ensure backwards compatibility
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
    72
    private static final String JAXB_CONTEXT_FACTORY_DEPRECATED = "javax.xml.bind.context.factory";
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
    73
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    74
    private static final Logger logger;
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    75
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    76
    static {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    77
        logger = Logger.getLogger("javax.xml.bind");
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    78
        try {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    79
            if (AccessController.doPrivileged(new GetPropertyAction("jaxb.debug")) != null) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    80
                // disconnect the logger from a bigger framework (if any)
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    81
                // and take the matters into our own hands
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    82
                logger.setUseParentHandlers(false);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    83
                logger.setLevel(Level.ALL);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    84
                ConsoleHandler handler = new ConsoleHandler();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    85
                handler.setLevel(Level.ALL);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    86
                logger.addHandler(handler);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    87
            } else {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    88
                // don't change the setting of this logger
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    89
                // to honor what other frameworks
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    90
                // have done on configurations.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    91
            }
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
    92
        } catch (Throwable t) {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    93
            // just to be extra safe. in particular System.getProperty may throw
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    94
            // SecurityException.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    95
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    96
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    97
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
    98
    private static ServiceLoaderUtil.ExceptionHandler<JAXBException> EXCEPTION_HANDLER =
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
    99
            new ServiceLoaderUtil.ExceptionHandler<JAXBException>() {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   100
                @Override
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   101
                public JAXBException createException(Throwable throwable, String message) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   102
                    return new JAXBException(message, throwable);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   103
                }
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   104
            };
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   105
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   106
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   107
     * If the {@link InvocationTargetException} wraps an exception that shouldn't be wrapped,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   108
     * throw the wrapped exception.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   109
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   110
    private static void handleInvocationTargetException(InvocationTargetException x) throws JAXBException {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   111
        Throwable t = x.getTargetException();
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   112
        if (t != null) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   113
            if (t instanceof JAXBException)
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   114
                // one of our exceptions, just re-throw
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   115
                throw (JAXBException) t;
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   116
            if (t instanceof RuntimeException)
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   117
                // avoid wrapping exceptions unnecessarily
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   118
                throw (RuntimeException) t;
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   119
            if (t instanceof Error)
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   120
                throw (Error) t;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   121
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   122
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   123
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   124
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   125
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   126
     * Determine if two types (JAXBContext in this case) will generate a ClassCastException.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   127
     *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   128
     * For example, (targetType)originalType
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   129
     *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   130
     * @param originalType
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   131
     *          The Class object of the type being cast
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   132
     * @param targetType
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   133
     *          The Class object of the type that is being cast to
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   134
     * @return JAXBException to be thrown.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   135
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   136
    private static JAXBException handleClassCastException(Class originalType, Class targetType) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   137
        final URL targetTypeURL = which(targetType);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   138
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   139
        return new JAXBException(Messages.format(Messages.ILLEGAL_CAST,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   140
                // we don't care where the impl class is, we want to know where JAXBContext lives in the impl
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   141
                // class' ClassLoader
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   142
                getClassClassLoader(originalType).getResource("javax/xml/bind/JAXBContext.class"),
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   143
                targetTypeURL));
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   144
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   145
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   146
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   147
     * Create an instance of a class using the specified ClassLoader
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   148
     */
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   149
    static JAXBContext newInstance(String contextPath,
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   150
                                   String className,
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   151
                                   ClassLoader classLoader,
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   152
                                   Map properties) throws JAXBException {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   153
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   154
        try {
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   155
            Class spFactory = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   156
            return newInstance(contextPath, spFactory, classLoader, properties);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   157
        } catch (ClassNotFoundException x) {
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   158
            throw new JAXBException(Messages.format(Messages.PROVIDER_NOT_FOUND, className), x);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   159
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   160
        } catch (RuntimeException x) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   161
            // avoid wrapping RuntimeException to JAXBException,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   162
            // because it indicates a bug in this code.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   163
            throw x;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   164
        } catch (Exception x) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   165
            // can't catch JAXBException because the method is hidden behind
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   166
            // reflection.  Root element collisions detected in the call to
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   167
            // createContext() are reported as JAXBExceptions - just re-throw it
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   168
            // some other type of exception - just wrap it
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   169
            throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, className, x), x);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   170
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   171
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   172
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   173
    static JAXBContext newInstance(String contextPath,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   174
                                   Class spFactory,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   175
                                   ClassLoader classLoader,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   176
                                   Map properties) throws JAXBException {
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   177
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   178
        try {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   179
            /*
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   180
             * javax.xml.bind.context.factory points to a class which has a
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   181
             * static method called 'createContext' that
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   182
             * returns a javax.xml.JAXBContext.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   183
             */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   184
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   185
            Object context = null;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   186
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   187
            // first check the method that takes Map as the third parameter.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   188
            // this is added in 2.0.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   189
            try {
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   190
                Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class, Map.class);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   191
                // any failure in invoking this method would be considered fatal
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   192
                context = m.invoke(null, contextPath, classLoader, properties);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   193
            } catch (NoSuchMethodException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   194
                // it's not an error for the provider not to have this method.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   195
            }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   196
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   197
            if (context == null) {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   198
                // try the old method that doesn't take properties. compatible with 1.0.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   199
                // it is an error for an implementation not to have both forms of the createContext method.
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   200
                Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   201
                // any failure in invoking this method would be considered fatal
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   202
                context = m.invoke(null, contextPath, classLoader);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   203
            }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   204
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   205
            if (!(context instanceof JAXBContext)) {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   206
                // the cast would fail, so generate an exception with a nice message
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   207
                throw handleClassCastException(context.getClass(), JAXBContext.class);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   208
            }
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   209
            return (JAXBContext) context;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   210
        } catch (InvocationTargetException x) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   211
            handleInvocationTargetException(x);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   212
            // for other exceptions, wrap the internal target exception
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   213
            // with a JAXBException
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   214
            Throwable e = x;
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   215
            if (x.getTargetException() != null)
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   216
                e = x.getTargetException();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   217
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   218
            throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, e), e);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   219
        } catch (RuntimeException x) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   220
            // avoid wrapping RuntimeException to JAXBException,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   221
            // because it indicates a bug in this code.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   222
            throw x;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   223
        } catch (Exception x) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   224
            // can't catch JAXBException because the method is hidden behind
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   225
            // reflection.  Root element collisions detected in the call to
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   226
            // createContext() are reported as JAXBExceptions - just re-throw it
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   227
            // some other type of exception - just wrap it
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   228
            throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, spFactory, x), x);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   229
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   230
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   231
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   232
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   233
     * Create an instance of a class using the thread context ClassLoader
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   234
     */
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   235
    static JAXBContext newInstance(Class[] classes, Map properties, String className) throws JAXBException {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   236
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   237
        Class spi;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   238
        try {
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   239
            spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, getContextClassLoader());
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   240
        } catch (ClassNotFoundException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   241
            throw new JAXBException(e);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   242
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   243
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   244
        if (logger.isLoggable(Level.FINE)) {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   245
            // extra check to avoid costly which operation if not logged
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   246
            logger.log(Level.FINE, "loaded {0} from {1}", new Object[]{className, which(spi)});
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   247
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   248
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   249
        return newInstance(classes, properties, spi);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   250
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   251
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   252
    static JAXBContext newInstance(Class[] classes,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   253
                                   Map properties,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   254
                                   Class spFactory) throws JAXBException {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   255
        try {
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   256
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   257
            Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   258
            Object context = m.invoke(null, classes, properties);
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   259
            if (!(context instanceof JAXBContext)) {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   260
                // the cast would fail, so generate an exception with a nice message
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   261
                throw handleClassCastException(context.getClass(), JAXBContext.class);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   262
            }
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   263
            return (JAXBContext) context;
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   264
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   265
        } catch (NoSuchMethodException | IllegalAccessException e) {
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   266
            throw new JAXBException(e);
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   267
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   268
        } catch (InvocationTargetException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   269
            handleInvocationTargetException(e);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   270
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   271
            Throwable x = e;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   272
            if (e.getTargetException() != null)
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   273
                x = e.getTargetException();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   274
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   275
            throw new JAXBException(x);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   276
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   277
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   278
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   279
    static JAXBContext find(String factoryId,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   280
                            String contextPath,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   281
                            ClassLoader classLoader,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   282
                            Map properties) throws JAXBException {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   283
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   284
        StringTokenizer packages = new StringTokenizer(contextPath, ":");
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   285
        if (!packages.hasMoreTokens()) {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   286
            // no context is specified
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   287
            throw new JAXBException(Messages.format(Messages.NO_PACKAGE_IN_CONTEXTPATH));
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   288
        }
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   289
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   290
        // search for jaxb.properties in the class loader of each class first
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   291
        logger.fine("Searching jaxb.properties");
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   292
        while (packages.hasMoreTokens()) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   293
            // com.acme.foo - > com/acme/foo/jaxb.properties
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   294
            String factoryClassName =
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   295
                    classNameFromPackageProperties(
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   296
                        classLoader,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   297
                        packages.nextToken(":").replace('.', '/'),
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   298
                        factoryId,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   299
                        JAXB_CONTEXT_FACTORY_DEPRECATED);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   300
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   301
            if (factoryClassName != null) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   302
                return newInstance(contextPath, factoryClassName, classLoader, properties);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   303
            }
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   304
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   305
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   306
        String factoryName = classNameFromSystemProperties();
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   307
        if (factoryName != null) return newInstance(contextPath, factoryName, classLoader, properties);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   308
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   309
        JAXBContextFactory obj = ServiceLoaderUtil.firstByServiceLoader(
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   310
                JAXBContextFactory.class, logger, EXCEPTION_HANDLER);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   311
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   312
        if (obj != null) return obj.createContext(contextPath, classLoader, properties);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   313
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   314
        // to ensure backwards compatibility
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   315
        factoryName = firstByServiceLoaderDeprecated(JAXBContext.class, classLoader);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   316
        if (factoryName != null) return newInstance(contextPath, factoryName, classLoader, properties);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   317
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   318
        Class ctxFactory = (Class) ServiceLoaderUtil.lookupUsingOSGiServiceLoader(
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   319
                "javax.xml.bind.JAXBContext", logger);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   320
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   321
        if (ctxFactory != null) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   322
            return newInstance(contextPath, ctxFactory, classLoader, properties);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   323
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   324
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   325
        // else no provider found
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   326
        logger.fine("Trying to create the platform default provider");
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   327
        return newInstance(contextPath, PLATFORM_DEFAULT_FACTORY_CLASS, classLoader, properties);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   328
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   329
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   330
    static JAXBContext find(Class<?>[] classes, Map<String, ?> properties) throws JAXBException {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   331
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   332
        // search for jaxb.properties in the class loader of each class first
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   333
        logger.fine("Searching jaxb.properties");
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   334
        for (final Class c : classes) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   335
            // this classloader is used only to load jaxb.properties, so doing this should be safe.
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   336
            // this is possible for primitives, arrays, and classes that are
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   337
            // loaded by poorly implemented ClassLoaders
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   338
            if (c.getPackage() == null) continue;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   339
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   340
            // TODO: do we want to optimize away searching the same package?  org.Foo, org.Bar, com.Baz
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   341
            // classes from the same package might come from different class loades, so it might be a bad idea
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   342
            // TODO: it's easier to look things up from the class
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   343
            // c.getResourceAsStream("jaxb.properties");
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   344
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   345
            String factoryClassName =
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   346
                    classNameFromPackageProperties(
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   347
                            getClassClassLoader(c),
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   348
                            c.getPackage().getName().replace('.', '/'),
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   349
                            JAXBContext.JAXB_CONTEXT_FACTORY, JAXB_CONTEXT_FACTORY_DEPRECATED);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   350
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   351
            if (factoryClassName != null) return newInstance(classes, properties, factoryClassName);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   352
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   353
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   354
        String factoryClassName = classNameFromSystemProperties();
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   355
        if (factoryClassName != null) return newInstance(classes, properties, factoryClassName);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   356
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   357
        JAXBContextFactory factory =
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   358
                ServiceLoaderUtil.firstByServiceLoader(JAXBContextFactory.class, logger, EXCEPTION_HANDLER);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   359
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   360
        if (factory != null) return factory.createContext(classes, properties);
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   361
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   362
        // to ensure backwards compatibility
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   363
        String className = firstByServiceLoaderDeprecated(JAXBContext.class, getContextClassLoader());
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   364
        if (className != null) return newInstance(classes, properties, className);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   365
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   366
        logger.fine("Trying to create the platform default provider");
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   367
        Class ctxFactoryClass =
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   368
                (Class) ServiceLoaderUtil.lookupUsingOSGiServiceLoader("javax.xml.bind.JAXBContext", logger);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   369
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   370
        if (ctxFactoryClass != null) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   371
            return newInstance(classes, properties, ctxFactoryClass);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   372
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   373
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   374
        // else no provider found
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   375
        logger.fine("Trying to create the platform default provider");
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   376
        return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   377
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   378
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   379
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   380
    /**
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   381
     * first factoryId should be the preffered one,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   382
     * more of those can be provided to support backwards compatibility
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   383
     */
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   384
    private static String classNameFromPackageProperties(ClassLoader classLoader,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   385
                                                         String packageName,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   386
                                                         String ... factoryIds) throws JAXBException {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   387
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   388
        String resourceName = packageName + "/jaxb.properties";
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   389
        logger.log(Level.FINE, "Trying to locate {0}", resourceName);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   390
        Properties props = loadJAXBProperties(classLoader, resourceName);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   391
        if (props != null) {
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   392
            for(String factoryId : factoryIds) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   393
                if (props.containsKey(factoryId)) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   394
                    return props.getProperty(factoryId);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   395
                }
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   396
            }
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   397
            throw new JAXBException(Messages.format(Messages.MISSING_PROPERTY, packageName, factoryIds[0]));
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   398
        }
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   399
        return null;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   400
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   401
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   402
    private static String classNameFromSystemProperties() throws JAXBException {
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   403
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   404
        String factoryClassName = getSystemProperty(JAXBContext.JAXB_CONTEXT_FACTORY);
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   405
        if (factoryClassName != null) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   406
            return factoryClassName;
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   407
        }
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   408
        // leave this here to assure compatibility
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   409
        factoryClassName = getDeprecatedSystemProperty(JAXB_CONTEXT_FACTORY_DEPRECATED);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   410
        if (factoryClassName != null) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   411
            return factoryClassName;
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   412
        }
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   413
        // leave this here to assure compatibility
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   414
        factoryClassName = getDeprecatedSystemProperty(JAXBContext.class.getName());
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   415
        if (factoryClassName != null) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   416
            return factoryClassName;
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   417
        }
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   418
        return null;
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   419
    }
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   420
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   421
    private static String getDeprecatedSystemProperty(String property) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   422
        String value = getSystemProperty(property);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   423
        if (value != null) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   424
            logger.log(Level.WARNING, "Using non-standard property: {0}. Property {1} should be used instead.",
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   425
                    new Object[] {property, JAXBContext.JAXB_CONTEXT_FACTORY});
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   426
        }
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   427
        return value;
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   428
    }
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   429
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   430
    private static String getSystemProperty(String property) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   431
        logger.log(Level.FINE, "Checking system property {0}", property);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   432
        String value = AccessController.doPrivileged(new GetPropertyAction(property));
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   433
        if (value != null) {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   434
            logger.log(Level.FINE, "  found {0}", value);
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   435
        } else {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   436
            logger.log(Level.FINE, "  not found");
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   437
        }
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   438
        return value;
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   439
    }
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   440
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   441
    private static Properties loadJAXBProperties(ClassLoader classLoader,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   442
                                                 String propFileName) throws JAXBException {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   443
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   444
        Properties props = null;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   445
        try {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   446
            URL url;
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   447
            if (classLoader == null)
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   448
                url = ClassLoader.getSystemResource(propFileName);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   449
            else
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   450
                url = classLoader.getResource(propFileName);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   451
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   452
            if (url != null) {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   453
                logger.log(Level.FINE, "loading props from {0}", url);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   454
                props = new Properties();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   455
                InputStream is = url.openStream();
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   456
                props.load(is);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   457
                is.close();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   458
            }
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   459
        } catch (IOException ioe) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   460
            logger.log(Level.FINE, "Unable to load " + propFileName, ioe);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   461
            throw new JAXBException(ioe.toString(), ioe);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   462
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   463
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   464
        return props;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   465
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   466
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   467
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   468
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   469
     * Search the given ClassLoader for an instance of the specified class and
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   470
     * return a string representation of the URL that points to the resource.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   471
     *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   472
     * @param clazz
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   473
     *          The class to search for
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   474
     * @param loader
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   475
     *          The ClassLoader to search.  If this parameter is null, then the
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   476
     *          system class loader will be searched
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   477
     * @return
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   478
     *          the URL for the class or null if it wasn't found
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   479
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   480
    static URL which(Class clazz, ClassLoader loader) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   481
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   482
        String classnameAsResource = clazz.getName().replace('.', '/') + ".class";
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   483
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   484
        if (loader == null) {
16791
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   485
            loader = getSystemClassLoader();
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   486
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   487
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   488
        return loader.getResource(classnameAsResource);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   489
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   490
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   491
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   492
     * Get the URL for the Class from it's ClassLoader.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   493
     *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   494
     * Convenience method for {@link #which(Class, ClassLoader)}.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   495
     *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   496
     * Equivalent to calling: which(clazz, clazz.getClassLoader())
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   497
     *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   498
     * @param clazz
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   499
     *          The class to search for
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   500
     * @return
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   501
     *          the URL for the class or null if it wasn't found
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   502
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   503
    static URL which(Class clazz) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   504
        return which(clazz, getClassClassLoader(clazz));
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   505
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   506
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   507
    @SuppressWarnings("unchecked")
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   508
    private static ClassLoader getContextClassLoader() {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   509
        if (System.getSecurityManager() == null) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   510
            return Thread.currentThread().getContextClassLoader();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   511
        } else {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   512
            return (ClassLoader) java.security.AccessController.doPrivileged(
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   513
                    new java.security.PrivilegedAction() {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   514
                        public java.lang.Object run() {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   515
                            return Thread.currentThread().getContextClassLoader();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   516
                        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   517
                    });
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   518
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   519
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   520
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   521
    @SuppressWarnings("unchecked")
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   522
    private static ClassLoader getClassClassLoader(final Class c) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   523
        if (System.getSecurityManager() == null) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   524
            return c.getClassLoader();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   525
        } else {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   526
            return (ClassLoader) java.security.AccessController.doPrivileged(
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   527
                    new java.security.PrivilegedAction() {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   528
                        public java.lang.Object run() {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   529
                            return c.getClassLoader();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   530
                        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   531
                    });
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   532
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   533
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   534
16791
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   535
    private static ClassLoader getSystemClassLoader() {
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   536
        if (System.getSecurityManager() == null) {
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   537
            return ClassLoader.getSystemClassLoader();
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   538
        } else {
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   539
            return (ClassLoader) java.security.AccessController.doPrivileged(
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   540
                    new java.security.PrivilegedAction() {
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   541
                        public java.lang.Object run() {
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   542
                            return ClassLoader.getSystemClassLoader();
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   543
                        }
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   544
                    });
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   545
        }
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   546
    }
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   547
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   548
    // ServiceLoaderUtil.firstByServiceLoaderDeprecated should be used instead.
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   549
    @Deprecated
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   550
    static String firstByServiceLoaderDeprecated(Class spiClass,
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   551
                                                 ClassLoader classLoader) throws JAXBException {
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   552
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   553
        final String jaxbContextFQCN = spiClass.getName();
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   554
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   555
        logger.fine("Searching META-INF/services");
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   556
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   557
        // search META-INF services next
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   558
        BufferedReader r = null;
31109
a542f8dcbbf8 8072839: JAX-B Plugability Layer: using java.util.ServiceLoader
mkos
parents: 29839
diff changeset
   559
        final String resource = "META-INF/services/" + jaxbContextFQCN;
29839
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   560
        try {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   561
            final InputStream resourceStream =
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   562
                    (classLoader == null) ?
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   563
                            ClassLoader.getSystemResourceAsStream(resource) :
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   564
                            classLoader.getResourceAsStream(resource);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   565
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   566
            if (resourceStream != null) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   567
                r = new BufferedReader(new InputStreamReader(resourceStream, "UTF-8"));
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   568
                String factoryClassName = r.readLine();
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   569
                if (factoryClassName != null) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   570
                    factoryClassName = factoryClassName.trim();
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   571
                }
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   572
                r.close();
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   573
                logger.log(Level.FINE, "Configured factorty class:{0}", factoryClassName);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   574
                return factoryClassName;
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   575
            } else {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   576
                logger.log(Level.FINE, "Unable to load:{0}", resource);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   577
                return null;
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   578
            }
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   579
        } catch (IOException e) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   580
            throw new JAXBException(e);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   581
        } finally {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   582
            try {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   583
                if (r != null) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   584
                    r.close();
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   585
                }
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   586
            } catch (IOException ex) {
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   587
                logger.log(Level.SEVERE, "Unable to close resource: " + resource, ex);
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   588
            }
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   589
        }
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   590
    }
6d5d546e953b 8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents: 25871
diff changeset
   591
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   592
}