KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > thread > ThreadGroupManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ThreadGroupManager.java
20  *
21  * This object is responsible for managing the thread groups within Coadunation.
22  */

23
24 // package path
25
package com.rift.coad.lib.thread;
26
27 // java imports
28
import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 // logging import
32
import org.apache.log4j.Logger;
33
34
35 // coadunation imports
36
import com.rift.coad.lib.common.RandomGuid;
37 import com.rift.coad.lib.configuration.Configuration;
38 import com.rift.coad.lib.configuration.ConfigurationFactory;
39 import com.rift.coad.lib.security.Validator;
40
41 /**
42  * This object is responsible for managing the thread groups within Coadunation.
43  *
44  * @author Brett Chaldecott
45  */

46 public class ThreadGroupManager {
47     
48     /**
49      * The class loader thread group.
50      */

51     public class LoaderThreadGroupManager {
52         // private member variables
53
private ThreadGroup JavaDoc threadGroup = null;
54         private CoadunationThreadGroup coadThreadGroup = null;
55         
56         /**
57          * The class loader thread group manager
58          */

59         public LoaderThreadGroupManager(CoadunationThreadGroup coadThreadGroup)
60                 throws ThreadException {
61             try {
62                 threadGroup = new ThreadGroup JavaDoc(
63                     RandomGuid.getInstance().getGuid());
64                 this.coadThreadGroup = coadThreadGroup.createThreadGroup();
65             } catch (Exception JavaDoc ex) {
66                 throw new ThreadException("Failed to create the " +
67                         "LoaderThreadGroupManager : " + ex.getMessage(),ex);
68             }
69         }
70         
71         
72         /**
73          * This method returns the JAVA thread group.
74          */

75         public ThreadGroup JavaDoc getThreadGroup() {
76             return threadGroup;
77         }
78         
79         
80         /**
81          * This method returns the reference to the coad thread group.
82          */

83         public CoadunationThreadGroup getCoadThreadGroup() {
84             return coadThreadGroup;
85         }
86         
87         
88         /**
89          * This method terminates the thread groups
90          */

91         public void terminate() {
92             try {
93                 coadThreadGroup.terminate();
94                 if (threadGroup.activeCount() > 0) {
95                     log.warn("There may still be threads in daemon that has " +
96                             "undeployed.");
97                 }
98                 try {
99                     threadGroup.destroy();
100                 } catch (Exception JavaDoc ex) {
101                     log.debug("Failed to destroy the thread group " +
102                         ex.getMessage(),ex);
103                 }
104             } catch (Exception JavaDoc ex) {
105                 log.error("Failed to terminate the thread group cleanly " +
106                         ex.getMessage(),ex);
107             }
108         }
109     }
110     
111     // class constants
112
private final static String JavaDoc ROLE = "role";
113     
114     // the classes singletons
115
private static ThreadGroupManager threadGroupManager = null;
116     protected static Logger log =
117         Logger.getLogger(ThreadGroupManager.class.getName());
118     
119     // classes private member variables
120
private Map JavaDoc managedGroups = new HashMap JavaDoc();
121     
122     // static member variables
123
private static String JavaDoc role = null;
124     
125     // setup the role
126
static {
127         try {
128             Configuration configuration =
129                     ConfigurationFactory.getInstance().getConfig(
130                     ThreadGroupManager.class);
131             role = configuration.getString(ROLE);
132         } catch (Exception JavaDoc ex) {
133             log.error("Failed to retrieve the thread group manager role : "
134                     + ex.getMessage(),ex);
135             throw new RuntimeException JavaDoc(
136                     "Failed to retrieve the thread group manager role : "
137                     + ex.getMessage(),ex);
138         }
139     }
140     
141     
142     /** Creates a new instance of ThreadGroupManager */
143     private ThreadGroupManager() {
144     }
145     
146     
147     /**
148      * This method is responsible for getting and instance of the thread group
149      * manager.
150      *
151      * @return The reference to the in memory thread group.
152      */

153     public static synchronized ThreadGroupManager getInstance() {
154         if (threadGroupManager == null) {
155             threadGroupManager = new ThreadGroupManager();
156         }
157         return threadGroupManager;
158     }
159     
160     
161     /**
162      * This method inits the thread group for the given class loader.
163      *
164      * @param coadThreadGroup The thread group to start.
165      */

166     public void initThreadGroup (CoadunationThreadGroup coadThreadGroup) throws
167             ThreadException {
168         LoaderThreadGroupManager loaderThreadGroupManager = new
169                 LoaderThreadGroupManager(coadThreadGroup);
170         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
171         synchronized(managedGroups) {
172             managedGroups.put(classLoader,loaderThreadGroupManager);
173         }
174     }
175     
176     
177     /**
178      * This method returns the reference to the thread group
179      */

180     public ThreadGroup JavaDoc getThreadGroup() {
181         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
182         LoaderThreadGroupManager loaderThreadGroupManager = null;
183         synchronized(managedGroups) {
184             if (!managedGroups.containsKey(classLoader)) {
185                 return null;
186             }
187             loaderThreadGroupManager = (LoaderThreadGroupManager)managedGroups.
188                     get(classLoader);
189         }
190         return loaderThreadGroupManager.getThreadGroup();
191     }
192     
193     
194     /**
195      * This method returns the reference to the thread group
196      */

197     public void terminateThreadGroup() {
198         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
199         LoaderThreadGroupManager loaderThreadGroupManager = null;
200         synchronized(managedGroups) {
201             if (!managedGroups.containsKey(classLoader)) {
202                 return;
203             }
204             loaderThreadGroupManager = (LoaderThreadGroupManager)managedGroups.
205                     get(classLoader);
206             managedGroups.remove(classLoader);
207         }
208         loaderThreadGroupManager.terminate();
209     }
210     
211     
212     /**
213      * This method returns the reference to the thread group
214      */

215     public void addThreadToGroup(BasicThread thread, String JavaDoc username) throws
216             ThreadException,Exception JavaDoc {
217         Validator.validate(ThreadGroupManager.class,role);
218         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
219         LoaderThreadGroupManager loaderThreadGroupManager = null;
220         synchronized(managedGroups) {
221             if (!managedGroups.containsKey(classLoader)) {
222                 return;
223             }
224             loaderThreadGroupManager = (LoaderThreadGroupManager)managedGroups.
225                     get(classLoader);
226         }
227         loaderThreadGroupManager.getCoadThreadGroup().addThread(thread,
228                 username);
229     }
230 }
231
Popular Tags