jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/api/pipe/ThreadHelper.java
author aefimov
Thu, 16 Feb 2017 13:14:39 +0300
changeset 43852 93a527059d8a
parent 36263 d5333008e409
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:
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
     1
/*
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 36263
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
     4
 *
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    10
 *
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    15
 * accompanied this code).
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    16
 *
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    20
 *
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    23
 * questions.
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    24
 */
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    25
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    26
package com.sun.xml.internal.ws.api.pipe;
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    27
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    28
import java.lang.reflect.Constructor;
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    29
import java.security.AccessController;
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    30
import java.security.PrivilegedAction;
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    31
import java.util.concurrent.ThreadFactory;
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    32
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    33
/**
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    34
 * Simple utility class to instantiate correct Thread instance
36263
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
    35
 * depending on Java version.
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    36
 *
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    37
 * @author miroslav.kos@oracle.com
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    38
 */
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    39
final class ThreadHelper {
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    40
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    41
    private static final String SAFE_THREAD_NAME = "sun.misc.ManagedLocalsThread";
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    42
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    43
    private static final ThreadFactory threadFactory;
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    44
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    45
    // no instantiating wanted
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    46
    private ThreadHelper() {
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    47
    }
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    48
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    49
    static {
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    50
        threadFactory = AccessController.doPrivileged(
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    51
                new PrivilegedAction<ThreadFactory> () {
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    52
                    @Override
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    53
                    public ThreadFactory run() {
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    54
                        // In order of preference
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    55
                        try {
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    56
                            try {
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    57
                                Class<Thread> cls = Thread.class;
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    58
                                Constructor<Thread> ctr = cls.getConstructor(
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    59
                                        ThreadGroup.class,
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    60
                                        Runnable.class,
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    61
                                        String.class,
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    62
                                        long.class,
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    63
                                        boolean.class);
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    64
                                return new JDK9ThreadFactory(ctr);
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    65
                            } catch (NoSuchMethodException ignored) {
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    66
                                // constructor newly added in Java SE 9
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    67
                            }
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    68
                            Class<?> cls = Class.forName(SAFE_THREAD_NAME);
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    69
                            Constructor<?> ctr = cls.getConstructor(Runnable.class);
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    70
                            return new SunMiscThreadFactory(ctr);
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 36263
diff changeset
    71
                        } catch (ClassNotFoundException | NoSuchMethodException ignored) {}
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    72
                        return new LegacyThreadFactory();
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    73
                    }
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    74
                }
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    75
        );
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    76
    }
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    77
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
    78
    static Thread createNewThread(final Runnable r) {
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    79
        return threadFactory.newThread(r);
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    80
    }
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    81
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    82
    // A Thread factory backed by the Thread constructor that
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    83
    // suppresses inheriting of inheritable thread-locals.
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    84
    private static class JDK9ThreadFactory implements ThreadFactory {
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    85
        final Constructor<Thread> ctr;
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    86
        JDK9ThreadFactory(Constructor<Thread> ctr) { this.ctr = ctr; }
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    87
        @Override public Thread newThread(Runnable r) {
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    88
            try {
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    89
                return ctr.newInstance(null, r, "toBeReplaced", 0, false);
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    90
            } catch (ReflectiveOperationException x) {
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 36263
diff changeset
    91
                InternalError ie = new InternalError(x.getMessage());
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 36263
diff changeset
    92
                ie.initCause(ie);
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 36263
diff changeset
    93
                throw ie;
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    94
            }
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    95
        }
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    96
    }
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    97
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    98
    // A Thread factory backed by sun.misc.ManagedLocalsThread
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
    99
    private static class SunMiscThreadFactory implements ThreadFactory {
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
   100
        final Constructor<?> ctr;
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
   101
        SunMiscThreadFactory(Constructor<?> ctr) { this.ctr = ctr; }
43852
93a527059d8a 8174735: Update JAX-WS RI integration to latest version
aefimov
parents: 36263
diff changeset
   102
        @Override public Thread newThread(final Runnable r) {
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
   103
            return AccessController.doPrivileged(
36263
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
   104
                    new PrivilegedAction<Thread>() {
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
   105
                        @Override
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
   106
                        public Thread run() {
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
   107
                            try {
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
   108
                                return (Thread) ctr.newInstance(r);
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
   109
                            } catch (Exception e) {
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
   110
                                return new Thread(r);
d5333008e409 8150174: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents: 35803
diff changeset
   111
                            }
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
   112
                        }
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
   113
                    }
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
   114
            );
35803
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
   115
        }
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
   116
    }
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
   117
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
   118
    // A Thread factory backed by new Thread(Runnable)
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
   119
    private static class LegacyThreadFactory implements ThreadFactory {
093fca558efd 8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor
chegar
parents: 29952
diff changeset
   120
        @Override public Thread newThread(Runnable r) {
29952
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
   121
            return new Thread(r);
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
   122
        }
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
   123
    }
6cccd9075e2f 8048992: Enhance thread contexts in JAXWS
mkos
parents:
diff changeset
   124
}