1 23 24 package com.rift.coad.lib.thread; 26 27 import org.apache.log4j.Logger; 29 30 import com.rift.coad.lib.common.RandomGuid; 32 import com.rift.coad.lib.security.UserSession; 33 import com.rift.coad.lib.security.user.UserSessionManager; 34 35 36 41 public class BasicThread extends Thread { 42 43 private UserSessionManager sessionManager = null; 45 private UserSession user = null; 46 private BasicRunnable runnable = null; 47 private CoadunationThreadGroup threadCoadGroup = null; 48 49 protected Logger log = 51 Logger.getLogger(BasicThread.class.getName()); 52 53 54 57 public BasicThread() throws Exception { 58 super(ThreadGroupManager.getInstance().getThreadGroup(), 59 RandomGuid.getInstance().getGuid()); 60 } 61 62 63 68 public BasicThread(BasicRunnable runnable) throws Exception { 69 super(ThreadGroupManager.getInstance().getThreadGroup(), 70 RandomGuid.getInstance().getGuid()); 71 this.runnable = runnable; 72 } 73 74 75 81 protected void setSessionManager(UserSessionManager sessionManager) 82 throws ThreadException { 83 if (null == sessionManager) { 84 throw new ThreadException( 85 "Cannot set the session manager reference to null."); 86 } 87 this.sessionManager = sessionManager; 88 } 89 90 91 96 public UserSession getUser() { 97 return user; 98 } 99 100 101 106 public void setUser(UserSession user) throws ThreadException { 107 if (user == null) { 108 throw new ThreadException( 109 "Cannot set the User to null."); 110 } 111 this.user = user; 112 } 113 114 115 120 protected CoadunationThreadGroup getCoadThreadGroup() { 121 return threadCoadGroup; 122 } 123 124 125 130 protected void setCoadThreadGroup(CoadunationThreadGroup threadCoadGroup) { 131 this.threadCoadGroup = threadCoadGroup; 132 } 133 134 135 138 public final void run () { 139 try { 140 if ((user == null) || (sessionManager == null)) { 141 log.error( 142 "Cannot run this thread as it has not been correctly initialized."); 143 return ; 144 } 145 if (threadCoadGroup == null) { 146 log.error( 147 "The thread group has not been set thread cannot run."); 148 return; 149 } 150 else if (false == threadCoadGroup.addThread(this)) 151 { 152 log.error( 153 "The thread group has been terminated will exit now."); 154 return; 155 } 156 sessionManager.initSessionForUser(user); 157 process(); 158 } catch (Exception ex) { 159 log.error("Thread failed to processes :" + ex.getMessage(),ex); 160 } finally { 161 if (threadCoadGroup != null) { 162 threadCoadGroup.removeThread(this); 163 } 164 } 165 166 } 167 168 169 174 public void process() throws Exception { 175 if (runnable == null) { 176 throw new ThreadException( 177 "This object has not been setup correctly. Either the process " + 178 "method must be over ridden or a BasicRunnable object must be used."); 179 } 180 System.out.println("Call the runnable object"); 181 runnable.process(); 182 System.out.println("After calling the runnable object"); 183 } 184 185 186 190 public void terminate() { 191 if (runnable != null) { 192 runnable.terminate(); 193 } 194 } 195 196 197 202 public String getInfo() { 203 if (runnable != null) { 204 return "Class : " + runnable.getClass().getName(); 205 } 206 return "Class : " + this.getClass().getName(); 207 } 208 } 209 | Popular Tags |