1 /* 2 * Copyright (c) 2003, 2004, 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.corba.se.impl.orbutil.threadpool; 27 28 import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException; 29 import com.sun.corba.se.spi.orbutil.threadpool.ThreadPool; 30 import com.sun.corba.se.spi.orbutil.threadpool.ThreadPoolManager; 31 import com.sun.corba.se.spi.orbutil.threadpool.ThreadPoolChooser; 32 33 import com.sun.corba.se.impl.orbutil.threadpool.ThreadPoolImpl; 34 import com.sun.corba.se.impl.orbutil.ORBConstants; 35 36 public class ThreadPoolManagerImpl implements ThreadPoolManager 37 { 38 private ThreadPool threadPool ; 39 40 public ThreadPoolManagerImpl( ThreadGroup tg ) 41 { 42 // Use unbounded threadpool in J2SE ORB 43 // ThreadPoolManager from s1as appserver code base can be set in the 44 // ORB. ThreadPools in the appserver are bounded. In that situation 45 // the ThreadPool in this ThreadPoolManager will have its threads 46 // die after the idle timeout. 47 // XXX Should there be cleanup when ORB.shutdown is called if the 48 // ORB owns the ThreadPool? 49 threadPool = new ThreadPoolImpl( tg, 50 ORBConstants.THREADPOOL_DEFAULT_NAME ) ; 51 } 52 53 /** 54 * This method will return an instance of the threadpool given a threadpoolId, 55 * that can be used by any component in the app. server. 56 * 57 * @throws NoSuchThreadPoolException thrown when invalid threadpoolId is passed 58 * as a parameter 59 */ 60 public ThreadPool getThreadPool(String threadpoolId) 61 throws NoSuchThreadPoolException { 62 63 return threadPool; 64 } 65 66 /** 67 * This method will return an instance of the threadpool given a numeric threadpoolId. 68 * This method will be used by the ORB to support the functionality of 69 * dedicated threadpool for EJB beans 70 * 71 * @throws NoSuchThreadPoolException thrown when invalidnumericIdForThreadpool is passed 72 * as a parameter | 1 /* 2 * Copyright (c) 2003, 2012, 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.corba.se.impl.orbutil.threadpool; 27 28 import java.io.IOException; 29 30 import java.security.PrivilegedAction; 31 import java.security.AccessController; 32 33 import java.util.concurrent.atomic.AtomicInteger; 34 35 import com.sun.corba.se.spi.orb.ORB; 36 37 import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException; 38 import com.sun.corba.se.spi.orbutil.threadpool.ThreadPool; 39 import com.sun.corba.se.spi.orbutil.threadpool.ThreadPoolManager; 40 import com.sun.corba.se.spi.orbutil.threadpool.ThreadPoolChooser; 41 42 import com.sun.corba.se.impl.orbutil.threadpool.ThreadPoolImpl; 43 import com.sun.corba.se.impl.orbutil.ORBConstants; 44 45 import com.sun.corba.se.impl.logging.ORBUtilSystemException; 46 import com.sun.corba.se.impl.orbutil.ORBConstants; 47 import com.sun.corba.se.spi.logging.CORBALogDomains; 48 49 50 public class ThreadPoolManagerImpl implements ThreadPoolManager 51 { 52 private ThreadPool threadPool; 53 private ThreadGroup threadGroup; 54 55 private static final ORBUtilSystemException wrapper = 56 ORBUtilSystemException.get(CORBALogDomains.RPC_TRANSPORT); 57 58 public ThreadPoolManagerImpl() { 59 threadGroup = getThreadGroup(); 60 threadPool = new ThreadPoolImpl(threadGroup, 61 ORBConstants.THREADPOOL_DEFAULT_NAME); 62 } 63 64 private static AtomicInteger tgCount = new AtomicInteger(); 65 66 67 private ThreadGroup getThreadGroup() { 68 ThreadGroup tg; 69 70 // See bugs 4916766 and 4936203 71 // We intend to create new threads in a reliable thread group. 72 // This avoids problems if the application/applet 73 // creates a thread group, makes JavaIDL calls which create a new 74 // connection and ReaderThread, and then destroys the thread 75 // group. If our ReaderThreads were to be part of such destroyed thread 76 // group then it might get killed and cause other invoking threads 77 // sharing the same connection to get a non-restartable 78 // CommunicationFailure. We'd like to avoid that. 79 // 80 // Our solution is to create all of our threads in the highest thread 81 // group that we have access to, given our own security clearance. 82 // 83 try { 84 // try to get a thread group that's as high in the threadgroup 85 // parent-child hierarchy, as we can get to. 86 // this will prevent an ORB thread created during applet-init from 87 // being killed when an applet dies. 88 tg = AccessController.doPrivileged( 89 new PrivilegedAction<ThreadGroup>() { 90 public ThreadGroup run() { 91 ThreadGroup tg = Thread.currentThread().getThreadGroup(); 92 ThreadGroup ptg = tg; 93 try { 94 while (ptg != null) { 95 tg = ptg; 96 ptg = tg.getParent(); 97 } 98 } catch (SecurityException se) { 99 // Discontinue going higher on a security exception. 100 } 101 return new ThreadGroup(tg, "ORB ThreadGroup " + tgCount.getAndIncrement()); 102 } 103 } 104 ); 105 } catch (SecurityException e) { 106 // something wrong, we go back to the original code 107 tg = Thread.currentThread().getThreadGroup(); 108 } 109 110 return tg; 111 } 112 113 public void close() { 114 try { 115 threadPool.close(); 116 } catch (IOException exc) { 117 wrapper.threadPoolCloseError(); 118 } 119 120 try { 121 boolean isDestroyed = threadGroup.isDestroyed(); 122 int numThreads = threadGroup.activeCount(); 123 int numGroups = threadGroup.activeGroupCount(); 124 125 if (isDestroyed) { 126 wrapper.threadGroupIsDestroyed(threadGroup); 127 } else { 128 if (numThreads > 0) 129 wrapper.threadGroupHasActiveThreadsInClose(threadGroup, numThreads); 130 131 if (numGroups > 0) 132 wrapper.threadGroupHasSubGroupsInClose(threadGroup, numGroups); 133 134 threadGroup.destroy(); 135 } 136 } catch (IllegalThreadStateException exc) { 137 wrapper.threadGroupDestroyFailed(exc, threadGroup); 138 } 139 140 threadGroup = null; 141 } 142 143 /** 144 * This method will return an instance of the threadpool given a threadpoolId, 145 * that can be used by any component in the app. server. 146 * 147 * @throws NoSuchThreadPoolException thrown when invalid threadpoolId is passed 148 * as a parameter 149 */ 150 public ThreadPool getThreadPool(String threadpoolId) 151 throws NoSuchThreadPoolException { 152 153 return threadPool; 154 } 155 156 /** 157 * This method will return an instance of the threadpool given a numeric threadpoolId. 158 * This method will be used by the ORB to support the functionality of 159 * dedicated threadpool for EJB beans 160 * 161 * @throws NoSuchThreadPoolException thrown when invalidnumericIdForThreadpool is passed 162 * as a parameter |