jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/model/Injector.java
author aefimov
Thu, 16 Feb 2017 13:14:39 +0300
changeset 43852 93a527059d8a
parent 25871 b80b84e87032
permissions -rw-r--r--
8174735: Update JAX-WS RI integration to latest version Reviewed-by: alanb, mchung, lancea Contributed-by: roman.grigoriadi@oracle.com
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
/*
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
     2
 * Copyright (c) 2008, 2017, 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 com.sun.xml.internal.ws.model;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    27
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    28
import java.lang.reflect.Field;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    29
import javax.xml.ws.WebServiceException;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    31
import java.lang.reflect.Method;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    32
import java.net.URL;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    33
import java.security.AccessController;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    34
import java.security.PrivilegedAction;
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    35
import java.security.PrivilegedActionException;
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    36
import java.security.PrivilegedExceptionAction;
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    37
import java.security.ProtectionDomain;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    38
import java.util.logging.Level;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    39
import java.util.logging.Logger;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    40
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    41
/**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    42
 * A {@link ClassLoader} used to "inject" wrapper and exception bean classes
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    43
 * into the VM.
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
 * @author Jitendra kotamraju
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    46
 */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    47
final class Injector {
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
    private static final Logger LOGGER = Logger.getLogger(Injector.class.getName());
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    50
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    51
    private static Method defineClass;
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    52
    private static Method resolveClass;
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    53
    private static Method getPackage;
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    54
    private static Method definePackage;
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    55
    private static Object U;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    56
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    57
    static {
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    58
        try {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    59
            Method[] m = AccessController.doPrivileged(
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    60
                    new PrivilegedAction<Method[]>() {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    61
                @Override
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    62
                public Method[] run() {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    63
                    return new Method[]{
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    64
                        getMethod(ClassLoader.class, "defineClass", String.class, byte[].class, Integer.TYPE, Integer.TYPE),
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    65
                        getMethod(ClassLoader.class, "resolveClass", Class.class),
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    66
                        getMethod(ClassLoader.class, "getPackage", String.class),
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    67
                        getMethod(ClassLoader.class, "definePackage",
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    68
                            String.class, String.class, String.class, String.class,
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    69
                            String.class, String.class, String.class, URL.class)
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    70
                    };
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    71
                }
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    72
            }
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    73
            );
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    74
            defineClass = m[0];
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    75
            resolveClass = m[1];
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    76
            getPackage = m[2];
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    77
            definePackage = m[3];
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    78
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    79
        } catch (Throwable t) {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    80
            try {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    81
                U = AccessController.doPrivileged(new PrivilegedExceptionAction() {
25685
c4d4cbb36e12 8038970: Deprivilege JAX-WS/JAF code
mkos
parents: 22679
diff changeset
    82
                    @Override
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    83
                    public Object run() throws Exception {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    84
                        Class u = Class.forName("sun.misc.Unsafe");
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    85
                        Field theUnsafe = u.getDeclaredField("theUnsafe");
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    86
                        theUnsafe.setAccessible(true);
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    87
                        return theUnsafe.get(null);
25685
c4d4cbb36e12 8038970: Deprivilege JAX-WS/JAF code
mkos
parents: 22679
diff changeset
    88
                    }
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    89
                });
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    90
                defineClass = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    91
                    @Override
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    92
                    public Method run() throws Exception {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    93
                        try {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    94
                            return U.getClass().getMethod("defineClass",
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    95
                                    new Class[]{String.class,
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    96
                                        byte[].class,
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    97
                                        Integer.TYPE,
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    98
                                        Integer.TYPE,
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
    99
                                        ClassLoader.class,
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   100
                                        ProtectionDomain.class});
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   101
                        } catch (NoSuchMethodException | SecurityException ex) {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   102
                            throw ex;
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   103
                        }
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   104
                    }
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   105
                });
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   106
            } catch (SecurityException | PrivilegedActionException ex) {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   107
                Logger.getLogger(Injector.class.getName()).log(Level.SEVERE, null, ex);
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   108
                WebServiceException we = new WebServiceException(ex);
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   109
                we.addSuppressed(t);
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   110
                throw we;
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   111
            }
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   112
        }
25685
c4d4cbb36e12 8038970: Deprivilege JAX-WS/JAF code
mkos
parents: 22679
diff changeset
   113
    }
c4d4cbb36e12 8038970: Deprivilege JAX-WS/JAF code
mkos
parents: 22679
diff changeset
   114
c4d4cbb36e12 8038970: Deprivilege JAX-WS/JAF code
mkos
parents: 22679
diff changeset
   115
    private static Method getMethod(final Class<?> c, final String methodname, final Class<?>... params) {
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   116
        try {
25685
c4d4cbb36e12 8038970: Deprivilege JAX-WS/JAF code
mkos
parents: 22679
diff changeset
   117
            Method m = c.getDeclaredMethod(methodname, params);
c4d4cbb36e12 8038970: Deprivilege JAX-WS/JAF code
mkos
parents: 22679
diff changeset
   118
            m.setAccessible(true);
c4d4cbb36e12 8038970: Deprivilege JAX-WS/JAF code
mkos
parents: 22679
diff changeset
   119
            return m;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   120
        } catch (NoSuchMethodException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   121
            // impossible
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   122
            throw new NoSuchMethodError(e.getMessage());
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
    static synchronized Class inject(ClassLoader cl, String className, byte[] image) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   127
        // To avoid race conditions let us check if the classloader
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   128
        // already contains the class
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   129
        try {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   130
            return cl.loadClass(className);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   131
        } catch (ClassNotFoundException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   132
            // nothing to do
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   133
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   134
        try {
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   135
            if (definePackage == null) {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   136
                return (Class) defineClass.invoke(U, className.replace('/', '.'), image, 0, image.length, cl, Injector.class.getProtectionDomain());
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   137
            }
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   138
            int packIndex = className.lastIndexOf('.');
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   139
            if (packIndex != -1) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   140
                String pkgname = className.substring(0, packIndex);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   141
                // Check if package already loaded.
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   142
                Package pkg = (Package) getPackage.invoke(cl, pkgname);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   143
                if (pkg == null) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   144
                    definePackage.invoke(cl, pkgname, null, null, null, null, null, null, null);
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
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   148
            Class c = (Class) defineClass.invoke(cl, className.replace('/', '.'), image, 0, image.length);
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   149
            resolveClass.invoke(cl, c);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   150
            return c;
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   151
        } catch (IllegalAccessException | InvocationTargetException e) {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   152
            if (LOGGER.isLoggable(Level.FINE)) {
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   153
                LOGGER.log(Level.FINE, "Unable to inject " + className, e);
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 25871
diff changeset
   154
            }
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   155
            throw new WebServiceException(e);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   156
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   157
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   158
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   159
}