KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > S1ASThreadPoolManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.util;
24
25 import com.sun.corba.ee.spi.orbutil.threadpool.ThreadPoolManager;
26 import com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool;
27 import com.sun.corba.ee.spi.orbutil.threadpool.NoSuchThreadPoolException;
28 import com.sun.corba.ee.spi.orbutil.threadpool.ThreadPoolChooser;
29
30 import com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl;
31 import com.sun.corba.ee.impl.orbutil.ORBConstants;
32
33 import com.sun.enterprise.server.ApplicationServer;
34 import com.sun.enterprise.server.ServerContext;
35
36 import com.sun.enterprise.config.ConfigContext;
37 import com.sun.enterprise.config.ConfigException;
38 import com.sun.enterprise.config.ConfigBean;
39 import com.sun.enterprise.config.serverbeans.Server;
40 import com.sun.enterprise.config.serverbeans.IiopService;
41 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
42 import com.sun.enterprise.config.serverbeans.Orb;
43 import com.sun.enterprise.config.serverbeans.Config;
44 import com.sun.enterprise.config.serverbeans.ThreadPools;
45
46 import java.util.HashMap JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.logging.*;
49 import com.sun.logging.*;
50
51 public class S1ASThreadPoolManager implements ThreadPoolManager {
52
53     static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
54
55     private static final int DEFAULT_NUMBER_OF_QUEUES = 0;
56     private static final int DEFAULT_MIN_THREAD_COUNT = 10;
57     private static final int DEFAULT_MAX_THREAD_COUNT = 200;
58
59     private static HashMap JavaDoc idToIndexTable = new HashMap JavaDoc();
60     private static HashMap JavaDoc indexToIdTable = new HashMap JavaDoc();
61     private static ArrayList JavaDoc threadpoolList = new ArrayList JavaDoc();
62     private static String JavaDoc defaultID;
63
64     private static ThreadPoolManager s1asThreadPoolMgr = new S1ASThreadPoolManager();
65
66     public static ThreadPoolManager getThreadPoolManager() {
67     return s1asThreadPoolMgr;
68     }
69
70
71     S1ASThreadPoolManager() {
72
73     Orb orbBean = null;
74     
75         try {
76             ServerContext serverContext = ApplicationServer.getServerContext();
77              
78             ConfigContext configContext = serverContext.getConfigContext();
79             assert(configContext != null);
80         
81             // IiopService iiopServiceBean = ServerBeansFactory.getIiopServiceBean(configContext);
82
Config config = ServerBeansFactory.getConfigBean(configContext);
83
84             assert(config != null);
85
86             // orbBean = iiopServiceBean.getOrb();
87
com.sun.enterprise.config.serverbeans.ThreadPools
88                 threadpoolsElement = config.getThreadPools();
89             assert (threadpoolsElement != null);
90
91         com.sun.enterprise.config.serverbeans.ThreadPool[]
92             allThreadpools = threadpoolsElement.getThreadPool();
93         for (int i = 0; i < allThreadpools.length; i++) {
94         createThreadPools(allThreadpools[i], i);
95         }
96         defaultID = (String JavaDoc)indexToIdTable.get(new Integer JavaDoc(0));
97         } catch (ConfigException cfe) {
98             _logger.log(Level.SEVERE,"enterprise_util.orbmgr_config_excep", cfe);
99         } catch (NullPointerException JavaDoc npe) {
100             _logger.log(Level.FINE,"Server Context is NULL. Ignoring and proceeding.");
101         }
102
103     
104     }
105
106
107     private void createThreadPools(com.sun.enterprise.config.serverbeans.ThreadPool
108             threadpoolBean, int index) {
109     String JavaDoc threadpoolId = null;
110     String JavaDoc minThreadsValue, maxThreadsValue, timeoutValue, numberOfQueuesValue;
111     int minThreads = DEFAULT_MIN_THREAD_COUNT;
112     int maxThreads = DEFAULT_MAX_THREAD_COUNT;
113     int idleTimeoutInSeconds = ORBConstants.DEFAULT_INACTIVITY_TIMEOUT;
114     int numberOfQueues = DEFAULT_NUMBER_OF_QUEUES;
115
116     try {
117         threadpoolId = threadpoolBean.getThreadPoolId();
118     } catch (NullPointerException JavaDoc npe) {
119         if(_logger.isLoggable(Level.WARNING)) {
120         _logger.log(Level.WARNING, "ThreadPoolBean may be null ", npe);
121             }
122     }
123     try {
124         minThreadsValue = threadpoolBean.getMinThreadPoolSize();
125         minThreads = Integer.parseInt(minThreadsValue);
126     } catch (NullPointerException JavaDoc npe) {
127         if(_logger.isLoggable(Level.WARNING)) {
128         _logger.log(Level.WARNING, "ThreadPoolBean may be null ", npe);
129         _logger.log(Level.WARNING,
130             "Using default value for steady-threadpool-size = " + minThreads);
131             }
132     } catch (NumberFormatException JavaDoc nfe) {
133         if(_logger.isLoggable(Level.WARNING)) {
134         _logger.log(Level.WARNING,"enterprise_util.excep_orbmgr_numfmt",nfe);
135         _logger.log(Level.WARNING,
136             "Using default value for min-threadpool-size = " + minThreads);
137             }
138     }
139     try {
140         maxThreadsValue = threadpoolBean.getMaxThreadPoolSize();
141         maxThreads = Integer.parseInt(maxThreadsValue);
142     } catch (NullPointerException JavaDoc npe) {
143         if(_logger.isLoggable(Level.WARNING)) {
144         _logger.log(Level.WARNING, "ThreadPoolBean may be null ", npe);
145         _logger.log(Level.WARNING,
146             "Using default value for max-threadpool-size = " + maxThreads);
147             }
148     } catch (NumberFormatException JavaDoc nfe) {
149         if(_logger.isLoggable(Level.WARNING)) {
150         _logger.log(Level.WARNING,"enterprise_util.excep_orbmgr_numfmt",nfe);
151         _logger.log(Level.WARNING,
152             "Using default value for max-threadpool-size = " + maxThreads);
153             }
154     }
155     try {
156         timeoutValue = threadpoolBean.getIdleThreadTimeoutInSeconds();
157         idleTimeoutInSeconds = Integer.parseInt(timeoutValue);
158     } catch (NullPointerException JavaDoc npe) {
159         if(_logger.isLoggable(Level.WARNING)) {
160         _logger.log(Level.WARNING, "ThreadPoolBean may be null ", npe);
161         _logger.log(Level.WARNING,
162             "Using default value for idle-thread-timeout-in-seconds = " +
163             idleTimeoutInSeconds);
164             }
165     } catch (NumberFormatException JavaDoc nfe) {
166         if(_logger.isLoggable(Level.WARNING)) {
167         _logger.log(Level.WARNING,"enterprise_util.excep_orbmgr_numfmt",nfe);
168         _logger.log(Level.WARNING,
169             "Using default value for idle-thread-timeout-in-seconds = " +
170             idleTimeoutInSeconds);
171             }
172     }
173
174     // Currently this value is not used but when multi-queue threadpools are
175
// implemented this could be used to decide which one to instantiate and
176
// number of queues in the multi-queue threadpool
177
try {
178         numberOfQueuesValue = threadpoolBean.getNumWorkQueues();
179         numberOfQueues = Integer.parseInt(numberOfQueuesValue);
180     } catch (NullPointerException JavaDoc npe) {
181         if(_logger.isLoggable(Level.WARNING)) {
182         _logger.log(Level.WARNING, "ThreadPoolBean may be null ", npe);
183         _logger.log(Level.WARNING,
184             "Using default value for num-work-queues = " +
185             numberOfQueues);
186             }
187     } catch (NumberFormatException JavaDoc nfe) {
188         if(_logger.isLoggable(Level.WARNING)) {
189         _logger.log(Level.WARNING,"enterprise_util.excep_orbmgr_numfmt",nfe);
190         _logger.log(Level.WARNING,
191             "Using default value for num-work-queues = " +
192             numberOfQueues);
193             }
194     }
195     
196     // Mutiplied the idleTimeoutInSeconds by 1000 to convert to milliseconds
197
com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool threadpool =
198         new ThreadPoolImpl(minThreads, maxThreads,
199             idleTimeoutInSeconds * 1000, threadpoolId);
200
201
202     // Add the threadpool instance to the threadpoolList
203
threadpoolList.add(threadpool);
204
205     // Associate the threadpoolId to the index passed
206
idToIndexTable.put(threadpoolId, new Integer JavaDoc(index));
207
208     // Associate the threadpoolId to the index passed
209
indexToIdTable.put(new Integer JavaDoc(index), threadpoolId);
210     
211     }
212
213     /**
214     * This method will return an instance of the threadpool given a threadpoolId,
215     * that can be used by any component in the app. server.
216     *
217     * @throws NoSuchThreadPoolException thrown when invalid threadpoolId is passed
218     * as a parameter
219     */

220     public com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool
221                 getThreadPool(String JavaDoc id)
222         throws NoSuchThreadPoolException {
223
224     Integer JavaDoc i = (Integer JavaDoc)idToIndexTable.get(id);
225     if (i == null) {
226         throw new NoSuchThreadPoolException();
227     }
228     try {
229         com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool threadpool =
230         (com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool)
231         threadpoolList.get(i.intValue());
232         return threadpool;
233     } catch (IndexOutOfBoundsException JavaDoc iobe) {
234         throw new NoSuchThreadPoolException();
235     }
236     }
237
238     /**
239     * This method will return an instance of the threadpool given a numeric threadpoolId.
240     * This method will be used by the ORB to support the functionality of
241     * dedicated threadpool for EJB beans
242     *
243     * @throws NoSuchThreadPoolException thrown when invalidnumericIdForThreadpool is passed
244     * as a parameter
245     */

246     public com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool
247             getThreadPool(int numericIdForThreadpool)
248         throws NoSuchThreadPoolException {
249
250     try {
251         com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool threadpool =
252         (com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool)
253         threadpoolList.get(numericIdForThreadpool);
254         return threadpool;
255     } catch (IndexOutOfBoundsException JavaDoc iobe) {
256         throw new NoSuchThreadPoolException();
257     }
258     }
259
260     /**
261     * This method is used to return the numeric id of the threadpool, given a String
262     * threadpoolId. This is used by the POA interceptors to add the numeric threadpool
263     * Id, as a tagged component in the IOR. This is used to provide the functionality of
264     * dedicated threadpool for EJB beans
265     */

266     public int getThreadPoolNumericId(String JavaDoc id) {
267     Integer JavaDoc i = (Integer JavaDoc)idToIndexTable.get(id);
268     return ((i == null) ? 0 : i.intValue());
269     }
270
271     /**
272     * Return a String Id for a numericId of a threadpool managed by the threadpool
273     * manager
274     */

275     public String JavaDoc getThreadPoolStringId(int numericIdForThreadpool) {
276     String JavaDoc id = (String JavaDoc)indexToIdTable.get(new Integer JavaDoc(numericIdForThreadpool));
277     return ((id == null) ? defaultID : id);
278     }
279
280     /**
281     * Returns the first instance of ThreadPool in the ThreadPoolManager
282     */

283     public com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool
284                     getDefaultThreadPool() {
285     try {
286         return getThreadPool(0);
287     } catch (NoSuchThreadPoolException nstpe) {
288         if(_logger.isLoggable(Level.WARNING)) {
289         _logger.log(Level.WARNING, "No default ThreadPool defined ", nstpe);
290             }
291     }
292     return null;
293     }
294
295     /**
296      * Return an instance of ThreadPoolChooser based on the componentId that was
297      * passed as argument
298      */

299     public ThreadPoolChooser getThreadPoolChooser(String JavaDoc componentId) {
300     //FIXME: This method is not used, but should be fixed once
301
//ORB's nio select starts working and we start using ThreadPoolChooser
302
//This will be mostly used by the ORB
303
return null;
304     }
305
306     /**
307      * Return an instance of ThreadPoolChooser based on the componentIndex that was
308      * passed as argument. This is added for improved performance so that the caller
309      * does not have to pay the cost of computing hashcode for the componentId
310      */

311     public ThreadPoolChooser getThreadPoolChooser(int componentIndex) {
312     //FIXME: This method is not used, but should be fixed once
313
//ORB's nio select starts working and we start using ThreadPoolChooser
314
//This will be mostly used by the ORB
315
return null;
316     }
317
318     /**
319      * Sets a ThreadPoolChooser for a particular componentId in the ThreadPoolManager. This
320      * would enable any component to add a ThreadPoolChooser for their specific use
321      */

322     public void setThreadPoolChooser(String JavaDoc componentId, ThreadPoolChooser aThreadPoolChooser) {
323     //FIXME: This method is not used, but should be fixed once
324
//ORB's nio select starts working and we start using ThreadPoolChooser
325
//This will be mostly used by the ORB
326
}
327
328     /**
329      * Gets the numeric index associated with the componentId specified for a
330      * ThreadPoolChooser. This method would help the component call the more
331      * efficient implementation i.e. getThreadPoolChooser(int componentIndex)
332      */

333     public int getThreadPoolChooserNumericId(String JavaDoc componentId) {
334     //FIXME: This method is not used, but should be fixed once
335
//ORB's nio select starts working and we start using ThreadPoolChooser
336
//This will be mostly used by the ORB
337
return 0;
338     }
339
340
341 }
342
343
344
Popular Tags