jaxws/src/java.xml.ws/share/classes/javax/xml/soap/ServiceLoaderUtil.java
author lana
Thu, 11 Aug 2016 15:47:07 +0000
changeset 40243 225732b21228
parent 34467 024f1ce7da02
child 45678 65fdff10664d
permissions -rw-r--r--
Added tag jdk-9+131 for changeset 2bf4f6d6a72f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34467
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     1
/*
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     4
 *
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    10
 *
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    15
 * accompanied this code).
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    16
 *
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    20
 *
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    23
 * questions.
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    24
 */
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    25
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    26
package javax.xml.soap;
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    27
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    28
import java.util.ServiceLoader;
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    29
import java.util.logging.Level;
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    30
import java.util.logging.Logger;
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    31
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    32
/**
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    33
 * Shared ServiceLoader/FactoryFinder Utils shared among SAAJ, JAXB and JAXWS
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    34
 * Class duplicated to all those projects.
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    35
 *
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    36
 * @author Miroslav.Kos@oracle.com
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    37
 */
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    38
class ServiceLoaderUtil {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    39
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    40
    static <P, T extends Exception> P firstByServiceLoader(Class<P> spiClass,
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    41
                                                           Logger logger,
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    42
                                                           ExceptionHandler<T> handler) throws T {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    43
        logger.log(Level.FINE, "Using java.util.ServiceLoader to find {0}", spiClass.getName());
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    44
        // service discovery
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    45
        try {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    46
            ServiceLoader<P> serviceLoader = ServiceLoader.load(spiClass);
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    47
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    48
            for (P impl : serviceLoader) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    49
                logger.fine("ServiceProvider loading Facility used; returning object [" +
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    50
                        impl.getClass().getName() + "]");
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    51
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    52
                return impl;
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    53
            }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    54
        } catch (Throwable t) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    55
            throw handler.createException(t, "Error while searching for service [" + spiClass.getName() + "]");
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    56
        }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    57
        return null;
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    58
    }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    59
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    60
    static void checkPackageAccess(String className) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    61
        // make sure that the current thread has an access to the package of the given name.
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    62
        SecurityManager s = System.getSecurityManager();
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    63
        if (s != null) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    64
            int i = className.lastIndexOf('.');
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    65
            if (i != -1) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    66
                s.checkPackageAccess(className.substring(0, i));
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    67
            }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    68
        }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    69
    }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    70
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    71
    static Class nullSafeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    72
        if (classLoader == null) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    73
            return Class.forName(className);
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    74
        } else {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    75
            return classLoader.loadClass(className);
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    76
        }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    77
    }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    78
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    79
    // Returns instance of required class. It checks package access (security)
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    80
    // unless it is defaultClassname. It means if you are trying to instantiate
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    81
    // default implementation (fallback), pass the class name to both first and second parameter.
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    82
    static <T extends Exception> Object newInstance(String className,
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    83
                                                    String defaultImplClassName, ClassLoader classLoader,
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    84
                                                    final ExceptionHandler<T> handler) throws T {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    85
        try {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    86
            return safeLoadClass(className, defaultImplClassName, classLoader).newInstance();
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    87
        } catch (ClassNotFoundException x) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    88
            throw handler.createException(x, "Provider " + className + " not found");
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    89
        } catch (Exception x) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    90
            throw handler.createException(x, "Provider " + className + " could not be instantiated: " + x);
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    91
        }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    92
    }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    93
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    94
    static Class safeLoadClass(String className,
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    95
                               String defaultImplClassName,
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    96
                               ClassLoader classLoader) throws ClassNotFoundException {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    97
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    98
        try {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
    99
            checkPackageAccess(className);
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   100
        } catch (SecurityException se) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   101
            // anyone can access the platform default factory class without permission
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   102
            if (defaultImplClassName != null && defaultImplClassName.equals(className)) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   103
                return Class.forName(className);
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   104
            }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   105
            // not platform default implementation ...
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   106
            throw se;
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   107
        }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   108
        return nullSafeLoadClass(className, classLoader);
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   109
    }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   110
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   111
    static <T extends Exception> ClassLoader contextClassLoader(ExceptionHandler<T> exceptionHandler) throws T {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   112
        try {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   113
            return Thread.currentThread().getContextClassLoader();
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   114
        } catch (Exception x) {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   115
            throw exceptionHandler.createException(x, x.toString());
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   116
        }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   117
    }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   118
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   119
    static abstract class ExceptionHandler<T extends Exception> {
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   120
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   121
        public abstract T createException(Throwable throwable, String message);
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   122
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   123
    }
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   124
024f1ce7da02 8131334: SAAJ Plugability Layer: using java.util.ServiceLoader
mkos
parents:
diff changeset
   125
}