1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   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;
  31 
  32 /**
  33  * Simple utility class to instantiate correct Thread instance
  34  * depending on runtime context (jdk/non-jdk usage)
  35  *
  36  * @author miroslav.kos@oracle.com
  37  */
  38 final class ThreadHelper {
  39 
  40     private static final String SAFE_THREAD_NAME = "sun.misc.ManagedLocalsThread";
  41     private static final Constructor THREAD_CONSTRUCTOR;
  42 
  43     // no instantiating wanted
  44     private ThreadHelper() {
  45     }
  46 
  47     static {
  48         THREAD_CONSTRUCTOR = AccessController.doPrivileged(
  49                 new PrivilegedAction<Constructor> () {
  50                     @Override
  51                     public Constructor run() {
  52                         try {
  53                             Class cls = Class.forName(SAFE_THREAD_NAME);
  54                             if (cls != null) {
  55                                 return cls.getConstructor(Runnable.class);
  56                             }
  57                         } catch (ClassNotFoundException ignored) {
  58                         } catch (NoSuchMethodException ignored) {
  59                         }
  60                         return null;
  61                     }
  62                 }
  63         );
  64     }
  65 
  66     static Thread createNewThread(final Runnable r) {
  67         if (isJDKInternal()) {
  68             return AccessController.doPrivileged(
  69                     new PrivilegedAction<Thread>() {
  70                         @Override
  71                         public Thread run() {
  72                             try {
  73                                 return (Thread) THREAD_CONSTRUCTOR.newInstance(r);
  74                             } catch (Exception e) {
  75                                 return new Thread(r);
  76                             }
  77                         }
  78                     }
  79             );
  80         } else {
  81             return new Thread(r);
  82         }
  83     }
  84 
  85     private static boolean isJDKInternal() {
  86         String className = ThreadHelper.class.getName();
  87         return className.contains(".internal.");
  88     }
  89 }