001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.dbobj;
066:
067: import com.jcorporate.expresso.core.controller.ControllerRequest;
068: import com.jcorporate.expresso.core.db.DBConnection;
069: import com.jcorporate.expresso.core.db.DBException;
070: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
071: import com.jcorporate.expresso.core.dbobj.ValidValue;
072: import com.jcorporate.expresso.core.misc.ConfigManager;
073: import com.jcorporate.expresso.core.misc.DateTime;
074: import com.jcorporate.expresso.core.security.User;
075: import com.jcorporate.expresso.kernel.util.FastStringBuffer;
076:
077: import java.util.Hashtable;
078: import java.util.Iterator;
079: import java.util.Vector;
080:
081: /**
082: * <p>Copyright 1999, 2000, 2001 Jcorporate Ltd.</p>
083: * <p>Jobqueue is a table that contains all the jobs, or asynchronous tasks, to be
084: * executed. This table works with JobQueueParam table as a detail table so that
085: * multiple parameters per job may be specified. The JobHandler checks the JobQueue
086: * table every so often to see if there's a job there waiting to be used. If it
087: * is, then the JobHandler server takes ownership of that JobQueue entry, constructs
088: * a job object based upon it and executes the appropriate job</p>
089: *
090: * @author Michael Nash, Cron Enhancements by Mike Dubman
091: * @version $Revision: 1.16 $ $Date: 2004/11/17 20:48:18 $
092: */
093: public class JobQueue extends SecuredDBObject {
094: private Hashtable valuesCoded = null;
095: private Vector myParams = null;
096: public static final String FLD_JOBOSNAME = "JobOSName";
097: public static final String FLD_JOBCRON_PARAMS = "JobCronParams";
098: public static final String FLD_UID = "ExpUid";
099: public static final String FLD_JOBCODE = "JobCode";
100: public static final String FLD_STATUS_CODE = "StatusCode";
101: public static final String FLD_PRIORITY = "Priority";
102: public static final String FLD_JOBNUMBER = "JobNumber";
103: public static final String FLD_REQUEST_TIME = "RequestTime";
104: public static final String FLD_SERVERID = "ServerID";
105: public static final String FLD_JOB_PROGRESS_MSG = "JobProgressMsg";
106:
107: //progress(int current, int total, String msg);
108: public static final String JOB_PRIORITY_HIGH = "1";
109: public static final String JOB_PRIORITY_NORMAL = "2";
110: public static final String JOB_PRIORITY_LOW = "3";
111: public static final String JOB_ARCH_UNIX = "Unix";
112: public static final String JOB_ARCH_MSWIN = "MSWIN";
113: public static final String JOB_ARCH_ANY = "any";
114: public static final String JOB_STATUS_NEW = "N";
115: public static final String JOB_STATUS_AVAILABLE = "A";
116: public static final String JOB_STATUS_COMPLETED = "C";
117: public static final String JOB_STATUS_RUNNING = "R";
118: public static final String JOB_STATUS_STOPPED = "S";
119: public static final String JOB_STATUS_KILLED = "K";
120: public static final String JOB_STATUS_SUSPENDED = "P";
121:
122: /**
123: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
124: */
125: public JobQueue() throws DBException {
126: super ();
127: } /* JobQueue() */
128:
129: /**
130: * Connection that provides a DBConnection.
131: *
132: * @param newConnection an already created DBConnection grabbed from the
133: * connection pool that will be used for the lifetime of the object.
134: */
135: public JobQueue(DBConnection newConnection) throws DBException {
136: super (newConnection);
137: }
138:
139: /**
140: * Initializes the JobQueue with the appropriate user permissions.
141: *
142: * @param uid The User's uid
143: * @throws DBException on error
144: */
145: public JobQueue(int uid) throws DBException {
146: super (uid);
147: }
148:
149: /**
150: * For using DBObjects within Controllers. Initializes based upon the current
151: * user and the requested db. [Of course this can be modified later]
152: *
153: * @param request - The controller request handed to you by the framework.
154: */
155: public JobQueue(ControllerRequest request) throws DBException {
156: super (request);
157: }
158:
159: /**
160: * Extends the usual add method to fetch a next number field
161: * and set the Updated flag to Y
162: *
163: * @throws DBException If the next number cannot be determined or
164: * the add fails
165: */
166: public void add() throws DBException {
167:
168: if (getField(FLD_STATUS_CODE).equals("")) {
169: setField(FLD_STATUS_CODE, JOB_STATUS_NEW);
170: }
171:
172: setField(FLD_REQUEST_TIME, DateTime.getDateTimeForDB(this
173: .getDataContext()));
174:
175: if (getField(FLD_PRIORITY).equals("")) {
176: setField(FLD_PRIORITY, JOB_PRIORITY_LOW);
177: }
178:
179: setField(FLD_SERVERID, "0");
180:
181: if (getField(FLD_JOBOSNAME).equals("")) {
182: setField(FLD_JOBOSNAME, JOB_ARCH_ANY);
183: }
184:
185: super .add();
186: } /* add() */
187:
188: /**
189: * Extend the normal find method to read the parameters once the find
190: * is done.
191: *
192: * @return boolean
193: */
194: public boolean find() throws DBException {
195: boolean res = super .find();
196: readParams();
197:
198: return res;
199: } /* find() */
200:
201: /**
202: * Get the job queue parameters associated with this queue entry
203: *
204: * @return Vector A vector of JobQueueParam objects for this entry
205: * @throws DBException If the job queue entry has not yet been retrieved
206: */
207: public synchronized Vector getParams() throws DBException {
208: if (myParams == null) {
209: throw new DBException(
210: "Parameters not available until job queue entry has "
211: + "been retrieved");
212: }
213:
214: return (Vector) myParams.clone();
215: } /* getParams() */
216:
217: /**
218: * Get the parameter value for the named parameter code
219: *
220: * @param paramCode Code for which we want the value
221: * @return String The parameter value
222: * @throws DBException If the paramter value cannot be retrieved
223: */
224: public synchronized String getParamValue(String paramCode)
225: throws DBException {
226: if (valuesCoded == null) {
227: readParams();
228: }
229:
230: return (String) valuesCoded.get(paramCode);
231: } /* getParamValue(String) */
232:
233: /**
234: * Override the method getValidValues to provide specific values for our
235: * multi-valued fields
236: *
237: * @param fieldName Field name for which values are requested
238: * @return Vector The ValidValues field
239: * @throws DBException If the values cannot be retrieved
240: */
241: public Vector getValidValues(String fieldName) throws DBException {
242: if (fieldName.equals(FLD_STATUS_CODE)) {
243: Vector myValues = new Vector(4);
244: myValues.addElement(new ValidValue(JOB_STATUS_NEW, "New"));
245: myValues.addElement(new ValidValue(JOB_STATUS_AVAILABLE,
246: "Available"));
247: myValues.addElement(new ValidValue(JOB_STATUS_RUNNING,
248: "Running"));
249: myValues.addElement(new ValidValue(JOB_STATUS_COMPLETED,
250: "Completed"));
251: myValues.addElement(new ValidValue(JOB_STATUS_STOPPED,
252: "Stopped"));
253: myValues.addElement(new ValidValue(JOB_STATUS_KILLED,
254: "Killed"));
255: myValues.addElement(new ValidValue(JOB_STATUS_SUSPENDED,
256: "Suspended"));
257:
258: return myValues;
259: }
260:
261: return super .getValidValues(fieldName);
262: } /* getValidValues(String) */
263:
264: /**
265: * Read all of the JobQueueParam entries associated with this job
266: *
267: * @throws DBException If the entries cannot be read
268: */
269: private void readParams() throws DBException {
270: myParams = new Vector(3);
271: valuesCoded = new Hashtable(3);
272:
273: JobQueueParam paramList = new JobQueueParam(
274: SecuredDBObject.SYSTEM_ACCOUNT);
275: paramList.setDataContext(getDataContext());
276: paramList.setField(FLD_JOBNUMBER, getField(FLD_JOBNUMBER));
277:
278: for (Iterator e = paramList
279: .searchAndRetrieveList("ParamNumber").iterator(); e
280: .hasNext();) {
281: JobQueueParam oneParam = (JobQueueParam) e.next();
282: myParams.addElement(oneParam);
283: valuesCoded.put(oneParam.getField("ParamCode"), oneParam
284: .getField("ParamValue"));
285: }
286: } /* readParams() */
287:
288: /**
289: * Extend the normal retrieve method to read the parameters
290: * after the record is retrieved
291: *
292: * @throws DBException If the parameters or the entry cannot be retrieved
293: */
294: public void retrieve() throws DBException {
295: super .retrieve();
296: readParams();
297: } /* retrieve() */
298:
299: /**
300: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
301: */
302: protected synchronized void setupFields() throws DBException {
303: setTargetTable("JOBQUEUE");
304: setDescription("DBjobQueue");
305: setCharset("ISO-8859-1");
306: addField(FLD_JOBNUMBER, "auto-inc", 0, false, "jobSerial");
307: addField(FLD_UID, "int", 0, false, "requestedByUser");
308: addVirtualField("LoginName", "char", 30, "loginName");
309: setLookupObject(FLD_UID, ConfigManager
310: .getClassHandler("userInfo"));
311: addField(FLD_REQUEST_TIME, "datetime", 0, true, "requestedAt");
312: addField(FLD_JOBCODE, "varchar", 255, false, "jobCode");
313: addField(FLD_STATUS_CODE, "char", 1, false, "statusCode");
314: addField(FLD_PRIORITY, "int", 0, false, "jobPrio");
315: addField(FLD_SERVERID, "int", 0, false, "handlingServer");
316:
317: // added by mike@schema.com
318: // required OS of job handler, should be one of: UNIX, Windows NT or any
319: addField(FLD_JOBOSNAME, "varchar", 255, false, "requiredOS");
320: addField(FLD_JOBCRON_PARAMS, "varchar", 255, true, "cronParams");
321:
322: //addField(FLD_JOB_PROGRESS_MSG, "varchar", 255, true, "Job progress message");
323: setStringFilter(FLD_JOBCODE, "rawFilter");
324: addKey(FLD_JOBNUMBER);
325: setMultiValued(FLD_STATUS_CODE);
326: setReadOnly(FLD_JOBNUMBER);
327: setReadOnly(FLD_REQUEST_TIME);
328: addDetail(
329: "com.jcorporate.expresso.services.dbobj.JobQueueParam",
330: FLD_JOBNUMBER, FLD_JOBNUMBER);
331: setLookupObject("ExpUid",
332: "com.jcorporate.expresso.core.security.User");
333: } /* setupFields() */
334:
335: public String getField(String fieldName) throws DBException {
336: if (fieldName.equals("LoginName")) {
337: User u = new User();
338: u.setUid(getFieldInt("ExpUid"));
339: u.setDataContext(this .getDataContext());
340:
341: if (u.find()) {
342: return u.getLoginName();
343: }
344:
345: return "No such user";
346: }
347:
348: return super .getField(fieldName);
349: }
350:
351: public void setJobOSName(String os) throws DBException {
352: setField(FLD_JOBOSNAME, os);
353: }
354:
355: public void setUid(String userId) throws DBException {
356: setField(FLD_UID, userId);
357: }
358:
359: public void setJobCode(String jobCode) throws DBException {
360: setField(FLD_JOBCODE, jobCode);
361: }
362:
363: public void setJobStatus(String status) throws DBException {
364: setField(FLD_STATUS_CODE, status);
365: }
366:
367: public String getJobStatus() throws DBException {
368: return getField(FLD_STATUS_CODE);
369: }
370:
371: /**
372: * Convenience method to determine if cron parameters are associated with
373: * this object instance.
374: *
375: * @return true if cron parameters are associated with this job
376: */
377: public boolean useCron() throws DBException {
378: String result = getField(FLD_JOBCRON_PARAMS);
379: if (result == null || result.length() == 0) {
380: return false;
381: } else {
382: return true;
383: }
384: }
385:
386: public String getCronEntry() throws DBException {
387: return getField(FLD_JOBCRON_PARAMS);
388: }
389:
390: /**
391: * <p>Cron-like alarm (minute, hour, day of month, month, day of week, year)
392: * Repetitive when the year is not specified.</p>
393: * <p>Examples:</p>
394: * <p>Every minute:<br />:
395: * <code>setCronParams(-1, -1, -1, -1, -1)</code></p>
396: * <p>Every hour at 5:<br />:
397: * <code>setCronParams(5, -1, -1, -1, -1, -1)</code></p>
398: * <p>Lunch time:<br />
399: * <code> setCronParams(00, 12, -1, -1, -1, -1)</code></p>
400: * <p>On the first of every month at 9:30<br />
401: * <code>setCronParams(30, 9, 1, -1, -1, -1)</code></p>
402: * <p>On every Friday at 18:00<br />
403: * <code>setCronParams(00, 18, -1, -1, Calendar.FRIDAY, -1)</code></p>
404: * <p>2 years that this class was programmed !<br />
405: * <code>setCronParams(00, 13, 1, Calendar.AUGUST, -1, 2001)</code><p>
406: *
407: * @param minute The minute code
408: * @param hour The hour code
409: * @param dayOfMonth The day of Month code
410: * @param month the Month to execute code
411: * @param dayOfWeek The day of week to execute code
412: * @param year The year to execute code
413: * @throws DBException upon error
414: */
415: public void setCronParams(int minute, int hour, int dayOfMonth,
416: int month, int dayOfWeek, int year) throws DBException {
417: FastStringBuffer sb = new FastStringBuffer(48);
418: sb.append(minute).append(",");
419: sb.append(hour).append(",");
420: sb.append(dayOfMonth).append(",");
421: sb.append(month).append(",");
422: sb.append(dayOfWeek).append(",");
423: sb.append(year);
424: setField(FLD_JOBCRON_PARAMS, sb.toString());
425: }
426: }
427:
428: /* JobQueue */
|