KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BasicThread.java
20  *
21  * This object is the basic Coadunation thread.
22  */

23
24 // package path
25
package com.rift.coad.lib.thread;
26
27 // logging import
28
import org.apache.log4j.Logger;
29
30 // imports
31
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 /**
37  * This object is the basic Coadunation thread.
38  *
39  * @author Brett Chaldecott
40  */

41 public class BasicThread extends Thread JavaDoc {
42     
43     // The classes member variables
44
private UserSessionManager sessionManager = null;
45     private UserSession user = null;
46     private BasicRunnable runnable = null;
47     private CoadunationThreadGroup threadCoadGroup = null;
48     
49     // the class log variable
50
protected Logger log =
51         Logger.getLogger(BasicThread.class.getName());
52     
53     
54     /**
55      * Creates a new instance of BasicThread
56      */

57     public BasicThread() throws Exception JavaDoc {
58         super(ThreadGroupManager.getInstance().getThreadGroup(),
59                 RandomGuid.getInstance().getGuid());
60     }
61     
62     
63     /**
64      * Creates a new instance of BasicThread setting the runnable reference.
65      *
66      * @param runnable The reference to the runnable object.
67      */

68     public BasicThread(BasicRunnable runnable) throws Exception JavaDoc {
69         super(ThreadGroupManager.getInstance().getThreadGroup(),
70                 RandomGuid.getInstance().getGuid());
71         this.runnable = runnable;
72     }
73     
74     
75     /**
76      * This method will set the session for this thread.
77      *
78      * @param sessionManager The reference to the session manager.
79      * @exception ThreadException
80      */

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     /**
92      * This method returns the user object reference.
93      *
94      * @return The reference to the user object.
95      */

96     public UserSession getUser() {
97         return user;
98     }
99     
100     
101     /**
102      * This method will set the user information for this object.
103      *
104      * @param user The users that this thread will run as.
105      */

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     /**
116      * This method returns the coadanation thread group.
117      *
118      * @return The reference to the coadunation thread group.
119      */

120     protected CoadunationThreadGroup getCoadThreadGroup() {
121         return threadCoadGroup;
122     }
123     
124     
125     /**
126      * This method sets the coadunation thread group.
127      *
128      * @param threadCoadGroup The reference to the coad thread group.
129      */

130     protected void setCoadThreadGroup(CoadunationThreadGroup threadCoadGroup) {
131         this.threadCoadGroup = threadCoadGroup;
132     }
133     
134     
135     /**
136      * The run method
137      */

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 JavaDoc 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     /**
170      * This method replaces the run method in the BasicThread.
171      *
172      * @exception Exception
173      */

174     public void process() throws Exception JavaDoc {
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     /**
187      * This method will be implemented by child objects to terminate the
188      * processing of this thread.
189      */

190     public void terminate() {
191         if (runnable != null) {
192             runnable.terminate();
193         }
194     }
195     
196     
197     /**
198      * This method returns the information about what this thread is processing.
199      *
200      * @return String containing a description of this thread
201      */

202     public String JavaDoc getInfo() {
203         if (runnable != null) {
204             return "Class : " + runnable.getClass().getName();
205         }
206         return "Class : " + this.getClass().getName();
207     }
208 }
209
Popular Tags