1 23 24 package com.rift.coad.lib.thread; 26 27 import java.util.Map ; 29 import java.util.HashMap ; 30 31 import org.apache.log4j.Logger; 33 34 35 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 46 public class ThreadGroupManager { 47 48 51 public class LoaderThreadGroupManager { 52 private ThreadGroup threadGroup = null; 54 private CoadunationThreadGroup coadThreadGroup = null; 55 56 59 public LoaderThreadGroupManager(CoadunationThreadGroup coadThreadGroup) 60 throws ThreadException { 61 try { 62 threadGroup = new ThreadGroup ( 63 RandomGuid.getInstance().getGuid()); 64 this.coadThreadGroup = coadThreadGroup.createThreadGroup(); 65 } catch (Exception ex) { 66 throw new ThreadException("Failed to create the " + 67 "LoaderThreadGroupManager : " + ex.getMessage(),ex); 68 } 69 } 70 71 72 75 public ThreadGroup getThreadGroup() { 76 return threadGroup; 77 } 78 79 80 83 public CoadunationThreadGroup getCoadThreadGroup() { 84 return coadThreadGroup; 85 } 86 87 88 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 ex) { 101 log.debug("Failed to destroy the thread group " + 102 ex.getMessage(),ex); 103 } 104 } catch (Exception ex) { 105 log.error("Failed to terminate the thread group cleanly " + 106 ex.getMessage(),ex); 107 } 108 } 109 } 110 111 private final static String ROLE = "role"; 113 114 private static ThreadGroupManager threadGroupManager = null; 116 protected static Logger log = 117 Logger.getLogger(ThreadGroupManager.class.getName()); 118 119 private Map managedGroups = new HashMap (); 121 122 private static String role = null; 124 125 static { 127 try { 128 Configuration configuration = 129 ConfigurationFactory.getInstance().getConfig( 130 ThreadGroupManager.class); 131 role = configuration.getString(ROLE); 132 } catch (Exception ex) { 133 log.error("Failed to retrieve the thread group manager role : " 134 + ex.getMessage(),ex); 135 throw new RuntimeException ( 136 "Failed to retrieve the thread group manager role : " 137 + ex.getMessage(),ex); 138 } 139 } 140 141 142 143 private ThreadGroupManager() { 144 } 145 146 147 153 public static synchronized ThreadGroupManager getInstance() { 154 if (threadGroupManager == null) { 155 threadGroupManager = new ThreadGroupManager(); 156 } 157 return threadGroupManager; 158 } 159 160 161 166 public void initThreadGroup (CoadunationThreadGroup coadThreadGroup) throws 167 ThreadException { 168 LoaderThreadGroupManager loaderThreadGroupManager = new 169 LoaderThreadGroupManager(coadThreadGroup); 170 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 171 synchronized(managedGroups) { 172 managedGroups.put(classLoader,loaderThreadGroupManager); 173 } 174 } 175 176 177 180 public ThreadGroup getThreadGroup() { 181 ClassLoader 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 197 public void terminateThreadGroup() { 198 ClassLoader 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 215 public void addThreadToGroup(BasicThread thread, String username) throws 216 ThreadException,Exception { 217 Validator.validate(ThreadGroupManager.class,role); 218 ClassLoader 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 |