KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > dbobj > JobQueue


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.dbobj;
66
67 import com.jcorporate.expresso.core.controller.ControllerRequest;
68 import com.jcorporate.expresso.core.db.DBConnection;
69 import com.jcorporate.expresso.core.db.DBException;
70 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
71 import com.jcorporate.expresso.core.dbobj.ValidValue;
72 import com.jcorporate.expresso.core.misc.ConfigManager;
73 import com.jcorporate.expresso.core.misc.DateTime;
74 import com.jcorporate.expresso.core.security.User;
75 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
76
77 import java.util.Hashtable JavaDoc;
78 import java.util.Iterator JavaDoc;
79 import java.util.Vector JavaDoc;
80
81
82 /**
83  * <p>Copyright 1999, 2000, 2001 Jcorporate Ltd.</p>
84  * <p>Jobqueue is a table that contains all the jobs, or asynchronous tasks, to be
85  * executed. This table works with JobQueueParam table as a detail table so that
86  * multiple parameters per job may be specified. The JobHandler checks the JobQueue
87  * table every so often to see if there's a job there waiting to be used. If it
88  * is, then the JobHandler server takes ownership of that JobQueue entry, constructs
89  * a job object based upon it and executes the appropriate job</p>
90  *
91  * @author Michael Nash, Cron Enhancements by Mike Dubman
92  * @version $Revision: 1.16 $ $Date: 2004/11/17 20:48:18 $
93  */

94 public class JobQueue
95         extends SecuredDBObject {
96     private Hashtable JavaDoc valuesCoded = null;
97     private Vector JavaDoc myParams = null;
98     public static final String JavaDoc FLD_JOBOSNAME = "JobOSName";
99     public static final String JavaDoc FLD_JOBCRON_PARAMS = "JobCronParams";
100     public static final String JavaDoc FLD_UID = "ExpUid";
101     public static final String JavaDoc FLD_JOBCODE = "JobCode";
102     public static final String JavaDoc FLD_STATUS_CODE = "StatusCode";
103     public static final String JavaDoc FLD_PRIORITY = "Priority";
104     public static final String JavaDoc FLD_JOBNUMBER = "JobNumber";
105     public static final String JavaDoc FLD_REQUEST_TIME = "RequestTime";
106     public static final String JavaDoc FLD_SERVERID = "ServerID";
107     public static final String JavaDoc FLD_JOB_PROGRESS_MSG = "JobProgressMsg";
108
109     //progress(int current, int total, String msg);
110
public static final String JavaDoc JOB_PRIORITY_HIGH = "1";
111     public static final String JavaDoc JOB_PRIORITY_NORMAL = "2";
112     public static final String JavaDoc JOB_PRIORITY_LOW = "3";
113     public static final String JavaDoc JOB_ARCH_UNIX = "Unix";
114     public static final String JavaDoc JOB_ARCH_MSWIN = "MSWIN";
115     public static final String JavaDoc JOB_ARCH_ANY = "any";
116     public static final String JavaDoc JOB_STATUS_NEW = "N";
117     public static final String JavaDoc JOB_STATUS_AVAILABLE = "A";
118     public static final String JavaDoc JOB_STATUS_COMPLETED = "C";
119     public static final String JavaDoc JOB_STATUS_RUNNING = "R";
120     public static final String JavaDoc JOB_STATUS_STOPPED = "S";
121     public static final String JavaDoc JOB_STATUS_KILLED = "K";
122     public static final String JavaDoc JOB_STATUS_SUSPENDED = "P";
123
124     /**
125      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
126      */

127     public JobQueue()
128             throws DBException {
129         super();
130     } /* JobQueue() */
131
132     /**
133      * Connection that provides a DBConnection.
134      *
135      * @param newConnection an already created DBConnection grabbed from the
136      * connection pool that will be used for the lifetime of the object.
137      */

138     public JobQueue(DBConnection newConnection) throws DBException {
139         super(newConnection);
140     }
141
142     /**
143      * Initializes the JobQueue with the appropriate user permissions.
144      *
145      * @param uid The User's uid
146      * @throws DBException on error
147      */

148     public JobQueue(int uid)
149             throws DBException {
150         super(uid);
151     }
152
153     /**
154      * For using DBObjects within Controllers. Initializes based upon the current
155      * user and the requested db. [Of course this can be modified later]
156      *
157      * @param request - The controller request handed to you by the framework.
158      */

159     public JobQueue(ControllerRequest request)
160             throws DBException {
161         super(request);
162     }
163
164     /**
165      * Extends the usual add method to fetch a next number field
166      * and set the Updated flag to Y
167      *
168      * @throws DBException If the next number cannot be determined or
169      * the add fails
170      */

171     public void add()
172             throws DBException {
173
174         if (getField(FLD_STATUS_CODE).equals("")) {
175             setField(FLD_STATUS_CODE, JOB_STATUS_NEW);
176         }
177
178         setField(FLD_REQUEST_TIME, DateTime.getDateTimeForDB(this.getDataContext()));
179
180         if (getField(FLD_PRIORITY).equals("")) {
181             setField(FLD_PRIORITY, JOB_PRIORITY_LOW);
182         }
183
184         setField(FLD_SERVERID, "0");
185
186         if (getField(FLD_JOBOSNAME).equals("")) {
187             setField(FLD_JOBOSNAME, JOB_ARCH_ANY);
188         }
189
190         super.add();
191     } /* add() */
192
193
194     /**
195      * Extend the normal find method to read the parameters once the find
196      * is done.
197      *
198      * @return boolean
199      */

200     public boolean find()
201             throws DBException {
202         boolean res = super.find();
203         readParams();
204
205         return res;
206     } /* find() */
207
208
209     /**
210      * Get the job queue parameters associated with this queue entry
211      *
212      * @return Vector A vector of JobQueueParam objects for this entry
213      * @throws DBException If the job queue entry has not yet been retrieved
214      */

215     public synchronized Vector JavaDoc getParams()
216             throws DBException {
217         if (myParams == null) {
218             throw new DBException("Parameters not available until job queue entry has " +
219                     "been retrieved");
220         }
221
222         return (Vector JavaDoc) myParams.clone();
223     } /* getParams() */
224
225
226     /**
227      * Get the parameter value for the named parameter code
228      *
229      * @param paramCode Code for which we want the value
230      * @return String The parameter value
231      * @throws DBException If the paramter value cannot be retrieved
232      */

233     public synchronized String JavaDoc getParamValue(String JavaDoc paramCode)
234             throws DBException {
235         if (valuesCoded == null) {
236             readParams();
237         }
238
239         return (String JavaDoc) valuesCoded.get(paramCode);
240     } /* getParamValue(String) */
241
242
243     /**
244      * Override the method getValidValues to provide specific values for our
245      * multi-valued fields
246      *
247      * @param fieldName Field name for which values are requested
248      * @return Vector The ValidValues field
249      * @throws DBException If the values cannot be retrieved
250      */

251     public Vector JavaDoc getValidValues(String JavaDoc fieldName)
252             throws DBException {
253         if (fieldName.equals(FLD_STATUS_CODE)) {
254             Vector JavaDoc myValues = new Vector JavaDoc(4);
255             myValues.addElement(new ValidValue(JOB_STATUS_NEW, "New"));
256             myValues.addElement(new ValidValue(JOB_STATUS_AVAILABLE,
257                     "Available"));
258             myValues.addElement(new ValidValue(JOB_STATUS_RUNNING, "Running"));
259             myValues.addElement(new ValidValue(JOB_STATUS_COMPLETED,
260                     "Completed"));
261             myValues.addElement(new ValidValue(JOB_STATUS_STOPPED, "Stopped"));
262             myValues.addElement(new ValidValue(JOB_STATUS_KILLED, "Killed"));
263             myValues.addElement(new ValidValue(JOB_STATUS_SUSPENDED,
264                     "Suspended"));
265
266             return myValues;
267         }
268
269         return super.getValidValues(fieldName);
270     } /* getValidValues(String) */
271
272
273     /**
274      * Read all of the JobQueueParam entries associated with this job
275      *
276      * @throws DBException If the entries cannot be read
277      */

278     private void readParams()
279             throws DBException {
280         myParams = new Vector JavaDoc(3);
281         valuesCoded = new Hashtable JavaDoc(3);
282
283         JobQueueParam paramList = new JobQueueParam(SecuredDBObject.SYSTEM_ACCOUNT);
284         paramList.setDataContext(getDataContext());
285         paramList.setField(FLD_JOBNUMBER, getField(FLD_JOBNUMBER));
286
287         for (Iterator JavaDoc e = paramList.searchAndRetrieveList("ParamNumber").iterator();
288              e.hasNext();) {
289             JobQueueParam oneParam = (JobQueueParam) e.next();
290             myParams.addElement(oneParam);
291             valuesCoded.put(oneParam.getField("ParamCode"),
292                     oneParam.getField("ParamValue"));
293         }
294     } /* readParams() */
295
296
297     /**
298      * Extend the normal retrieve method to read the parameters
299      * after the record is retrieved
300      *
301      * @throws DBException If the parameters or the entry cannot be retrieved
302      */

303     public void retrieve()
304             throws DBException {
305         super.retrieve();
306         readParams();
307     } /* retrieve() */
308
309
310     /**
311      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
312      */

313     protected synchronized void setupFields()
314             throws DBException {
315         setTargetTable("JOBQUEUE");
316         setDescription("DBjobQueue");
317         setCharset("ISO-8859-1");
318         addField(FLD_JOBNUMBER, "auto-inc", 0, false, "jobSerial");
319         addField(FLD_UID, "int", 0, false, "requestedByUser");
320         addVirtualField("LoginName", "char", 30, "loginName");
321         setLookupObject(FLD_UID, ConfigManager.getClassHandler("userInfo"));
322         addField(FLD_REQUEST_TIME, "datetime", 0, true, "requestedAt");
323         addField(FLD_JOBCODE, "varchar", 255, false, "jobCode");
324         addField(FLD_STATUS_CODE, "char", 1, false, "statusCode");
325         addField(FLD_PRIORITY, "int", 0, false, "jobPrio");
326         addField(FLD_SERVERID, "int", 0, false, "handlingServer");
327
328         // added by mike@schema.com
329
// required OS of job handler, should be one of: UNIX, Windows NT or any
330
addField(FLD_JOBOSNAME, "varchar", 255, false, "requiredOS");
331         addField(FLD_JOBCRON_PARAMS, "varchar", 255, true, "cronParams");
332
333         //addField(FLD_JOB_PROGRESS_MSG, "varchar", 255, true, "Job progress message");
334
setStringFilter(FLD_JOBCODE, "rawFilter");
335         addKey(FLD_JOBNUMBER);
336         setMultiValued(FLD_STATUS_CODE);
337         setReadOnly(FLD_JOBNUMBER);
338         setReadOnly(FLD_REQUEST_TIME);
339         addDetail("com.jcorporate.expresso.services.dbobj.JobQueueParam",
340                 FLD_JOBNUMBER, FLD_JOBNUMBER);
341         setLookupObject("ExpUid", "com.jcorporate.expresso.core.security.User");
342     } /* setupFields() */
343
344
345     public String JavaDoc getField(String JavaDoc fieldName)
346             throws DBException {
347         if (fieldName.equals("LoginName")) {
348             User u = new User();
349             u.setUid(getFieldInt("ExpUid"));
350             u.setDataContext(this.getDataContext());
351
352             if (u.find()) {
353                 return u.getLoginName();
354             }
355
356             return "No such user";
357         }
358
359         return super.getField(fieldName);
360     }
361
362     public void setJobOSName(String JavaDoc os)
363             throws DBException {
364         setField(FLD_JOBOSNAME, os);
365     }
366
367     public void setUid(String JavaDoc userId)
368             throws DBException {
369         setField(FLD_UID, userId);
370     }
371
372     public void setJobCode(String JavaDoc jobCode)
373             throws DBException {
374         setField(FLD_JOBCODE, jobCode);
375     }
376
377     public void setJobStatus(String JavaDoc status)
378             throws DBException {
379         setField(FLD_STATUS_CODE, status);
380     }
381
382     public String JavaDoc getJobStatus()
383             throws DBException {
384         return getField(FLD_STATUS_CODE);
385     }
386
387     /**
388      * Convenience method to determine if cron parameters are associated with
389      * this object instance.
390      *
391      * @return true if cron parameters are associated with this job
392      */

393     public boolean useCron()
394             throws DBException {
395         String JavaDoc result = getField(FLD_JOBCRON_PARAMS);
396         if (result == null || result.length() == 0) {
397             return false;
398         } else {
399             return true;
400         }
401     }
402
403     public String JavaDoc getCronEntry()
404             throws DBException {
405         return getField(FLD_JOBCRON_PARAMS);
406     }
407
408     /**
409      * <p>Cron-like alarm (minute, hour, day of month, month, day of week, year)
410      * Repetitive when the year is not specified.</p>
411      * <p>Examples:</p>
412      * <p>Every minute:<br />:
413      * <code>setCronParams(-1, -1, -1, -1, -1)</code></p>
414      * <p>Every hour at 5:<br />:
415      * <code>setCronParams(5, -1, -1, -1, -1, -1)</code></p>
416      * <p>Lunch time:<br />
417      * <code> setCronParams(00, 12, -1, -1, -1, -1)</code></p>
418      * <p>On the first of every month at 9:30<br />
419      * <code>setCronParams(30, 9, 1, -1, -1, -1)</code></p>
420      * <p>On every Friday at 18:00<br />
421      * <code>setCronParams(00, 18, -1, -1, Calendar.FRIDAY, -1)</code></p>
422      * <p>2 years that this class was programmed !<br />
423      * <code>setCronParams(00, 13, 1, Calendar.AUGUST, -1, 2001)</code><p>
424      *
425      * @param minute The minute code
426      * @param hour The hour code
427      * @param dayOfMonth The day of Month code
428      * @param month the Month to execute code
429      * @param dayOfWeek The day of week to execute code
430      * @param year The year to execute code
431      * @throws DBException upon error
432      */

433     public void setCronParams(int minute, int hour, int dayOfMonth, int month,
434                               int dayOfWeek, int year)
435             throws DBException {
436         FastStringBuffer sb = new FastStringBuffer(48);
437         sb.append(minute).append(",");
438         sb.append(hour).append(",");
439         sb.append(dayOfMonth).append(",");
440         sb.append(month).append(",");
441         sb.append(dayOfWeek).append(",");
442         sb.append(year);
443         setField(FLD_JOBCRON_PARAMS, sb.toString());
444     }
445 }
446
447 /* JobQueue */
Popular Tags