KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > threads > AbstractThreadManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.cornerstone.blocks.threads;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.cornerstone.services.threads.ThreadManager;
24
25 import org.apache.excalibur.thread.ThreadPool;
26
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.logger.AbstractLogEnabled;
31
32 /**
33  * Abstract implementation of ThreadManager.
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  */

37 public abstract class AbstractThreadManager
38     extends AbstractLogEnabled
39     implements ThreadManager, Configurable
40 {
41     ///Map of thread pools for application
42
private HashMap JavaDoc m_threadPools = new HashMap JavaDoc();
43
44     /**
45      * Setup thread pools based on configuration data.
46      *
47      * @param configuration the configuration data
48      * @exception ConfigurationException if an error occurs
49      * @avalon.configuration schema="http://relaxng.org/ns/structure/1.0"
50      */

51     public void configure( final Configuration configuration )
52         throws ConfigurationException
53     {
54         final Configuration[] groups = configuration.getChildren( "thread-group" );
55         for( int i = 0; i < groups.length; i++ )
56         {
57             configureThreadPool( m_threadPools, groups[ i ] );
58         }
59     }
60
61     protected abstract void configureThreadPool( final Map JavaDoc threadPools,
62                                                  final Configuration configuration )
63         throws ConfigurationException;
64
65     /**
66      * Retrieve a thread pool by name.
67      *
68      * @param name the name of thread pool
69      * @return the threadpool
70      * @exception IllegalArgumentException if the name of thread pool is
71      * invalid or named pool does not exist
72      */

73     public ThreadPool getThreadPool( final String JavaDoc name )
74         throws IllegalArgumentException JavaDoc
75     {
76         final ThreadPool threadPool = (ThreadPool)m_threadPools.get( name );
77
78         if( null == threadPool )
79         {
80             final String JavaDoc message = "Unable to locate ThreadPool named " + name;
81             throw new IllegalArgumentException JavaDoc( message );
82         }
83
84         return threadPool;
85     }
86
87     /**
88      * Retrieve the default thread pool.
89      *
90      * @return the thread pool
91      */

92     public ThreadPool getDefaultThreadPool()
93     {
94         return getThreadPool( "default" );
95     }
96 }
97
Popular Tags