author | aefimov |
Thu, 16 Feb 2017 13:14:39 +0300 | |
changeset 43852 | 93a527059d8a |
parent 36263 | d5333008e409 |
permissions | -rw-r--r-- |
29952 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.ws.api.pipe; |
|
27 |
||
28 |
import java.lang.reflect.Constructor; |
|
29 |
import java.security.AccessController; |
|
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 | 32 |
|
33 |
/** |
|
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 | 36 |
* |
37 |
* @author miroslav.kos@oracle.com |
|
38 |
*/ |
|
39 |
final class ThreadHelper { |
|
40 |
||
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 | 44 |
|
45 |
// no instantiating wanted |
|
46 |
private ThreadHelper() { |
|
47 |
} |
|
48 |
||
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 | 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 | 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 | 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 | 73 |
} |
74 |
} |
|
75 |
); |
|
76 |
} |
|
77 |
||
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 | 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 | 112 |
} |
113 |
} |
|
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 | 121 |
return new Thread(r); |
122 |
} |
|
123 |
} |
|
124 |
} |