001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * BasicThread.java
020: *
021: * This object is the basic Coadunation thread.
022: */
023:
024: // package path
025: package com.rift.coad.lib.thread;
026:
027: // logging import
028: import org.apache.log4j.Logger;
029:
030: // imports
031: import com.rift.coad.lib.common.RandomGuid;
032: import com.rift.coad.lib.security.UserSession;
033: import com.rift.coad.lib.security.user.UserSessionManager;
034:
035: /**
036: * This object is the basic Coadunation thread.
037: *
038: * @author Brett Chaldecott
039: */
040: public class BasicThread extends Thread {
041:
042: // The classes member variables
043: private UserSessionManager sessionManager = null;
044: private UserSession user = null;
045: private BasicRunnable runnable = null;
046: private CoadunationThreadGroup threadCoadGroup = null;
047:
048: // the class log variable
049: protected Logger log = Logger
050: .getLogger(BasicThread.class.getName());
051:
052: /**
053: * Creates a new instance of BasicThread
054: */
055: public BasicThread() throws Exception {
056: super (ThreadGroupManager.getInstance().getThreadGroup(),
057: RandomGuid.getInstance().getGuid());
058: }
059:
060: /**
061: * Creates a new instance of BasicThread setting the runnable reference.
062: *
063: * @param runnable The reference to the runnable object.
064: */
065: public BasicThread(BasicRunnable runnable) throws Exception {
066: super (ThreadGroupManager.getInstance().getThreadGroup(),
067: RandomGuid.getInstance().getGuid());
068: this .runnable = runnable;
069: }
070:
071: /**
072: * This method will set the session for this thread.
073: *
074: * @param sessionManager The reference to the session manager.
075: * @exception ThreadException
076: */
077: protected void setSessionManager(UserSessionManager sessionManager)
078: throws ThreadException {
079: if (null == sessionManager) {
080: throw new ThreadException(
081: "Cannot set the session manager reference to null.");
082: }
083: this .sessionManager = sessionManager;
084: }
085:
086: /**
087: * This method returns the user object reference.
088: *
089: * @return The reference to the user object.
090: */
091: public UserSession getUser() {
092: return user;
093: }
094:
095: /**
096: * This method will set the user information for this object.
097: *
098: * @param user The users that this thread will run as.
099: */
100: public void setUser(UserSession user) throws ThreadException {
101: if (user == null) {
102: throw new ThreadException("Cannot set the User to null.");
103: }
104: this .user = user;
105: }
106:
107: /**
108: * This method returns the coadanation thread group.
109: *
110: * @return The reference to the coadunation thread group.
111: */
112: protected CoadunationThreadGroup getCoadThreadGroup() {
113: return threadCoadGroup;
114: }
115:
116: /**
117: * This method sets the coadunation thread group.
118: *
119: * @param threadCoadGroup The reference to the coad thread group.
120: */
121: protected void setCoadThreadGroup(
122: CoadunationThreadGroup threadCoadGroup) {
123: this .threadCoadGroup = threadCoadGroup;
124: }
125:
126: /**
127: * The run method
128: */
129: public final void run() {
130: try {
131: if ((user == null) || (sessionManager == null)) {
132: log
133: .error("Cannot run this thread as it has not been correctly initialized.");
134: return;
135: }
136: if (threadCoadGroup == null) {
137: log
138: .error("The thread group has not been set thread cannot run.");
139: return;
140: } else if (false == threadCoadGroup.addThread(this )) {
141: log
142: .error("The thread group has been terminated will exit now.");
143: return;
144: }
145: sessionManager.initSessionForUser(user);
146: process();
147: } catch (Exception ex) {
148: log.error("Thread failed to processes :" + ex.getMessage(),
149: ex);
150: } finally {
151: if (threadCoadGroup != null) {
152: threadCoadGroup.removeThread(this );
153: }
154: }
155:
156: }
157:
158: /**
159: * This method replaces the run method in the BasicThread.
160: *
161: * @exception Exception
162: */
163: public void process() throws Exception {
164: if (runnable == null) {
165: throw new ThreadException(
166: "This object has not been setup correctly. Either the process "
167: + "method must be over ridden or a BasicRunnable object must be used.");
168: }
169: System.out.println("Call the runnable object");
170: runnable.process();
171: System.out.println("After calling the runnable object");
172: }
173:
174: /**
175: * This method will be implemented by child objects to terminate the
176: * processing of this thread.
177: */
178: public void terminate() {
179: if (runnable != null) {
180: runnable.terminate();
181: }
182: }
183:
184: /**
185: * This method returns the information about what this thread is processing.
186: *
187: * @return String containing a description of this thread
188: */
189: public String getInfo() {
190: if (runnable != null) {
191: return "Class : " + runnable.getClass().getName();
192: }
193: return "Class : " + this.getClass().getName();
194: }
195: }
|