ThreadPoolManagerImpl.java revision 608:7e06bf1dcb09
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
26package com.sun.corba.se.impl.orbutil.threadpool;
27
28import java.io.IOException;
29
30import java.security.PrivilegedAction;
31import java.security.AccessController;
32
33import java.util.concurrent.atomic.AtomicInteger;
34
35import com.sun.corba.se.spi.orb.ORB;
36
37import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException;
38import com.sun.corba.se.spi.orbutil.threadpool.ThreadPool;
39import com.sun.corba.se.spi.orbutil.threadpool.ThreadPoolManager;
40import com.sun.corba.se.spi.orbutil.threadpool.ThreadPoolChooser;
41
42import com.sun.corba.se.impl.orbutil.threadpool.ThreadPoolImpl;
43import com.sun.corba.se.impl.orbutil.ORBConstants;
44
45import com.sun.corba.se.impl.logging.ORBUtilSystemException;
46import com.sun.corba.se.impl.orbutil.ORBConstants;
47import com.sun.corba.se.spi.logging.CORBALogDomains;
48
49
50public 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
163    */
164    public ThreadPool getThreadPool(int numericIdForThreadpool)
165        throws NoSuchThreadPoolException {
166
167        return threadPool;
168    }
169
170    /**
171    * This method is used to return the numeric id of the threadpool, given a String
172    * threadpoolId. This is used by the POA interceptors to add the numeric threadpool
173    * Id, as a tagged component in the IOR. This is used to provide the functionality of
174    * dedicated threadpool for EJB beans
175    */
176    public int  getThreadPoolNumericId(String threadpoolId) {
177        return 0;
178    }
179
180    /**
181    * Return a String Id for a numericId of a threadpool managed by the threadpool
182    * manager
183    */
184    public String getThreadPoolStringId(int numericIdForThreadpool) {
185       return "";
186    }
187
188    /**
189    * Returns the first instance of ThreadPool in the ThreadPoolManager
190    */
191    public ThreadPool getDefaultThreadPool() {
192        return threadPool;
193    }
194
195    /**
196     * Return an instance of ThreadPoolChooser based on the componentId that was
197     * passed as argument
198     */
199    public ThreadPoolChooser getThreadPoolChooser(String componentId) {
200        //FIXME: This method is not used, but should be fixed once
201        //nio select starts working and we start using ThreadPoolChooser
202        return null;
203    }
204    /**
205     * Return an instance of ThreadPoolChooser based on the componentIndex that was
206     * passed as argument. This is added for improved performance so that the caller
207     * does not have to pay the cost of computing hashcode for the componentId
208     */
209    public ThreadPoolChooser getThreadPoolChooser(int componentIndex) {
210        //FIXME: This method is not used, but should be fixed once
211        //nio select starts working and we start using ThreadPoolChooser
212        return null;
213    }
214
215    /**
216     * Sets a ThreadPoolChooser for a particular componentId in the ThreadPoolManager. This
217     * would enable any component to add a ThreadPoolChooser for their specific use
218     */
219    public void setThreadPoolChooser(String componentId, ThreadPoolChooser aThreadPoolChooser) {
220        //FIXME: This method is not used, but should be fixed once
221        //nio select starts working and we start using ThreadPoolChooser
222    }
223
224    /**
225     * Gets the numeric index associated with the componentId specified for a
226     * ThreadPoolChooser. This method would help the component call the more
227     * efficient implementation i.e. getThreadPoolChooser(int componentIndex)
228     */
229    public int getThreadPoolChooserNumericId(String componentId) {
230        //FIXME: This method is not used, but should be fixed once
231        //nio select starts working and we start using ThreadPoolChooser
232        return 0;
233    }
234
235}
236
237// End of file.
238