8048992: Enhance thread contexts in JAXWS
authormkos
Tue, 07 Apr 2015 13:08:57 +0200
changeset 29952 6cccd9075e2f
parent 29845 38f98cb6b335
child 29953 d5894097fe65
8048992: Enhance thread contexts in JAXWS Summary: fix also reviewed by skoivu,asaha,mgrebac,Alexander Fomin,Bill Shannon Reviewed-by: chegar
jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/api/pipe/Engine.java
jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/api/pipe/ThreadHelper.java
jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/client/WSServiceDelegate.java
--- a/jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/api/pipe/Engine.java	Wed Jul 05 20:28:21 2017 +0200
+++ b/jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/api/pipe/Engine.java	Tue Apr 07 13:08:57 2015 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -108,7 +108,8 @@
         }
 
         public Thread newThread(Runnable r) {
-            Thread t = new Thread(null, r, namePrefix + threadNumber.getAndIncrement(), 0);
+            Thread t = ThreadHelper.createNewThread(r);
+            t.setName(namePrefix + threadNumber.getAndIncrement());
             if (!t.isDaemon()) {
                 t.setDaemon(true);
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/api/pipe/ThreadHelper.java	Tue Apr 07 13:08:57 2015 +0200
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.xml.internal.ws.api.pipe;
+
+import java.lang.reflect.Constructor;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * Simple utility class to instantiate correct Thread instance
+ * depending on runtime context (jdk/non-jdk usage)
+ *
+ * @author miroslav.kos@oracle.com
+ */
+final class ThreadHelper {
+
+    private static final String SAFE_THREAD_NAME = "sun.misc.ManagedLocalsThread";
+    private static final Constructor THREAD_CONSTRUCTOR;
+
+    // no instantiating wanted
+    private ThreadHelper() {
+    }
+
+    static {
+        THREAD_CONSTRUCTOR = AccessController.doPrivileged(
+                new PrivilegedAction<Constructor> () {
+                    @Override
+                    public Constructor run() {
+                        try {
+                            Class cls = Class.forName(SAFE_THREAD_NAME);
+                            if (cls != null) {
+                                return cls.getConstructor(Runnable.class);
+                            }
+                        } catch (ClassNotFoundException ignored) {
+                        } catch (NoSuchMethodException ignored) {
+                        }
+                        return null;
+                    }
+                }
+        );
+    }
+
+    static Thread createNewThread(final Runnable r) {
+        if (isJDKInternal()) {
+            return AccessController.doPrivileged(
+                    new PrivilegedAction<Thread>() {
+                        @Override
+                        public Thread run() {
+                            try {
+                                return (Thread) THREAD_CONSTRUCTOR.newInstance(r);
+                            } catch (Exception e) {
+                                return new Thread(r);
+                            }
+                        }
+                    }
+            );
+        } else {
+            return new Thread(r);
+        }
+    }
+
+    private static boolean isJDKInternal() {
+        String className = ThreadHelper.class.getName();
+        return className.contains(".internal.");
+    }
+}
--- a/jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/client/WSServiceDelegate.java	Wed Jul 05 20:28:21 2017 +0200
+++ b/jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/ws/client/WSServiceDelegate.java	Tue Apr 07 13:08:57 2015 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -894,15 +894,6 @@
         return wsdlService;
     }
 
-    static class DaemonThreadFactory implements ThreadFactory {
-        @Override
-        public Thread newThread(Runnable r) {
-            Thread daemonThread = new Thread(r);
-            daemonThread.setDaemon(Boolean.TRUE);
-            return daemonThread;
-        }
-    }
-
     protected static final WebServiceFeature[] EMPTY_FEATURES = new WebServiceFeature[0];
 
     private static ClassLoader getDelegatingLoader(ClassLoader loader1, ClassLoader loader2) {