1 24 25 package com.rift.coad.lib.thread; 26 27 import java.util.Vector ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 32 import org.apache.log4j.Logger; 34 35 import com.rift.coad.lib.security.UserSession; 37 import com.rift.coad.lib.security.user.UserSessionManager; 38 import com.rift.coad.lib.configuration.ConfigurationFactory; 39 import com.rift.coad.lib.configuration.Configuration; 40 import com.rift.coad.lib.security.user.UserStoreManager; 41 42 43 49 public class CoadunationThreadGroup { 50 51 54 public class ThreadList { 55 protected Logger log = 57 Logger.getLogger(ThreadList.class.getName()); 58 59 private boolean terminated = false; 61 private Vector threads = null; 62 63 66 public ThreadList() { 67 threads = new Vector (); 68 } 69 70 71 76 public synchronized boolean addThread(BasicThread thread) { 77 if (terminated == false) { 78 threads.add(thread); 79 return true; 80 } 81 return false; 82 } 83 84 85 90 public synchronized Vector getThreads() { 91 Vector threads = new Vector () ; 92 threads.addAll(this.threads); 93 return threads; 94 } 95 96 97 102 public synchronized void remove(BasicThread thread) { 103 for (int index = 0; index < threads.size(); index++) { 104 if (threads.get(index) == thread) { 105 log.info("Object equal removing [" 106 + thread.getId() + "] id [" + 107 ((BasicThread)threads.get(index)).getId() + "]"); 108 threads.remove(thread); 109 break; 110 } 111 } 112 } 113 114 115 118 public synchronized void terminate() { 119 terminated = true; 120 } 121 122 123 126 public synchronized boolean isTerminated() { 127 return terminated; 128 } 129 } 130 131 private final static String THREAD_TERMINATE_TIMEOUT = "Thread_Terminate_Timeout"; 133 134 protected Logger log = 136 Logger.getLogger(CoadunationThreadGroup.class.getName()); 137 138 private UserSessionManager sessionManager = null; 140 private UserStoreManager userStoreManager = null; 141 private ThreadList threadList = null; 142 private long threadTerminateTimeout = 0; 143 private CoadunationThreadGroup parent = null; 144 145 146 154 public CoadunationThreadGroup(UserSessionManager sessionManager, 155 UserStoreManager userStoreManager) throws ThreadException { 156 this.sessionManager = sessionManager; 157 this.userStoreManager = userStoreManager; 158 this.threadList = new ThreadList(); 159 try { 160 Configuration config = ConfigurationFactory.getInstance().getConfig( 161 this.getClass()); 162 threadTerminateTimeout = config.getLong(THREAD_TERMINATE_TIMEOUT); 163 164 } catch (Exception ex) { 165 throw new ThreadException( 166 "Failed to retrieve default thread terminate timeout.",ex); 167 } 168 } 169 170 171 179 private CoadunationThreadGroup(CoadunationThreadGroup parent, UserSessionManager sessionManager, 180 UserStoreManager userStoreManager) throws ThreadException { 181 if (parent == null) { 182 throw new ThreadException("The parent thread group is invalid"); 183 } 184 this.parent = parent; 185 this.sessionManager = sessionManager; 186 this.userStoreManager = userStoreManager; 187 this.threadList = new ThreadList(); 188 try { 189 Configuration config = ConfigurationFactory.getInstance().getConfig( 190 this.getClass()); 191 threadTerminateTimeout = config.getLong(THREAD_TERMINATE_TIMEOUT); 192 193 } catch (Exception ex) { 194 throw new ThreadException( 195 "Failed to retrieve default thread terminate timeout.",ex); 196 } 197 } 198 199 200 209 public void startThreads(Class classRef, String username, int number) 210 throws ThreadException { 211 try{ 212 validateThreadClass(classRef); 213 for (int count = 0; count < number; count++) { 214 BasicThread threadRef = (BasicThread)classRef.newInstance(); 215 if (threadRef instanceof CoadunationThread) { 216 throw new ThreadException( 217 "Must inherit from Basic Thread and not Coad Thread."); 218 } 219 addThread(threadRef,username); 220 threadRef.setContextClassLoader(Thread.currentThread(). 223 getContextClassLoader()); 224 225 threadRef.start(); 227 } 228 } catch (Exception ex) { 229 throw new ThreadException("Failed to add threads for [" + 230 classRef.getName() + "] because :" + ex.getMessage(),ex); 231 } 232 } 233 234 235 243 public void addThread(BasicThread threadRef, String username) 244 throws ThreadException { 245 try { 246 UserSession user = userStoreManager.getUserInfo(username); 247 threadRef.setSessionManager(sessionManager); 248 threadRef.setUser(user); 249 threadRef.setCoadThreadGroup(this); 250 251 threadRef.setContextClassLoader(Thread.currentThread(). 254 getContextClassLoader()); 255 } catch (Exception ex) { 256 throw new ThreadException( 257 "Failed to add a thread to this group : " + ex.getMessage() 258 ,ex); 259 } 260 } 261 262 263 270 protected boolean addThread(BasicThread threadRef) throws ThreadException { 271 return threadList.addThread(threadRef); 272 } 273 274 275 281 protected void removeThread(BasicThread threadRef) { 282 threadList.remove(threadRef); 283 } 284 285 286 291 public CoadunationThreadGroup getParent() { 292 return parent; 293 } 294 295 296 302 public List getThreadInfo() throws ThreadException { 303 List threadInfoList = new ArrayList (); 304 Vector threads = threadList.getThreads(); 305 for (int i = 0; i < threads.size(); i++) { 306 BasicThread thread = (BasicThread)threads.get(i); 307 ThreadInfo threadInfo = new ThreadInfo(thread.getId(), 308 thread.getClass(), thread.getUser(), thread.getState(), 309 thread.getInfo()); 310 threadInfoList.add(threadInfo); 311 } 312 return threadInfoList; 313 } 314 315 316 319 public boolean isTerminated() { 320 return threadList.isTerminated(); 321 } 322 323 327 public synchronized void terminate() { 328 329 if (threadList.isTerminated()) { 331 return; 332 } 333 334 threadList.terminate(); 336 337 Vector threads = threadList.getThreads(); 338 for (int i = 0; i < threads.size(); i++) { 339 BasicThread thread = (BasicThread)threads.get(i); 340 try { 341 thread.terminate(); 342 } catch(Exception ex) { 343 log.error("Failed to terminate thread [" + thread.getId() + 344 "] class [" + thread.getClass().getName() + 345 "] because : " + ex.getMessage(),ex); 346 } 347 } 348 349 for (int i = 0; i < threads.size(); i++) { 351 BasicThread thread = (BasicThread)threads.get(i); 352 try { 353 thread.join(getThreadTimeout(thread)); 354 } catch(Exception ex) { 355 log.error("Failed to wait for thread [" + thread.getId() + 356 "] class [" + thread.getClass().getName() + 357 "] because : " + ex.getMessage(),ex); 358 } 359 } 360 361 362 for (int i = 0; i < threads.size(); i++) { 365 BasicThread thread = (BasicThread)threads.get(i); 366 try { 367 if (thread.getState() != Thread.State.TERMINATED) { 368 log.error("The thread [" + thread.getId() + 369 "] class [" + thread.getClass().getName() + 370 "] has not been terminated forcing it to stop."); 371 thread.stop(); 372 } 373 } catch(Exception ex) { 374 log.error("Failed to wait for thread [" + thread.getId() + 375 "] class [" + thread.getClass().getName() + 376 "] because : " + ex.getMessage(),ex); 377 } 378 } 379 } 380 381 382 388 private long getThreadTimeout(Object obj) { 389 try { 390 Configuration config = ConfigurationFactory.getInstance().getConfig( 391 obj.getClass()); 392 return config.getLong(THREAD_TERMINATE_TIMEOUT); 393 } catch (Exception ex) { 394 return threadTerminateTimeout; 395 } 396 } 397 398 399 405 public CoadunationThreadGroup createThreadGroup() throws ThreadException { 406 return new CoadunationThreadGroup(this,sessionManager,userStoreManager); 407 } 408 409 410 415 private void validateThreadClass(Class classRef) throws ThreadException { 416 Class tempClass = classRef.getSuperclass(); 417 while (tempClass != null) { 418 System.out.println("Class : " + tempClass.getName()); 419 if (tempClass.getName(). 420 equals("com.rift.coad.lib.thread.BasicThread")) { 421 return; 422 } 423 tempClass = classRef.getSuperclass(); 424 } 425 throw new ThreadException( 426 "This object does not inherit from BasicThread [" + 427 classRef.getName() + "]"); 428 } 429 } 430 | Popular Tags |