KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > scheduler > Job


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.scheduler;
34
35 import java.lang.ref.SoftReference JavaDoc;
36
37 import java.util.Date JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 import java.sql.Connection JavaDoc;
41 import java.sql.CallableStatement JavaDoc;
42 import java.sql.PreparedStatement JavaDoc;
43 import java.sql.Statement JavaDoc;
44 import java.sql.ResultSet JavaDoc;
45 import java.sql.SQLException JavaDoc;
46 import java.sql.Types JavaDoc;
47 import java.sql.Timestamp JavaDoc;
48
49 import java.io.IOException JavaDoc;
50 import java.io.FileNotFoundException JavaDoc;
51 import java.io.File JavaDoc;
52 import java.io.FileWriter JavaDoc;
53
54 import java.util.Properties JavaDoc;
55
56 import javax.mail.MessagingException JavaDoc;
57
58 import com.knowgate.debug.DebugFile;
59 import com.knowgate.jdc.JDCConnection;
60 import com.knowgate.misc.Environment;
61 import com.knowgate.misc.Gadgets;
62
63 import com.knowgate.dataobjs.DB;
64 import com.knowgate.dataobjs.DBBind;
65 import com.knowgate.dataobjs.DBPersist;
66
67 /**
68  * <p>Abstract base class for Job Commands Implementations</p>
69  * @author Sergio Montoro Ten
70  * @version 2.2
71  */

72 public abstract class Job extends DBPersist {
73   private Properties JavaDoc oParams;
74   private Properties JavaDoc oEnvProps;
75   private File JavaDoc oLogFile;
76   private DBBind oDataBind;
77
78   protected int iPendingAtoms;
79
80   public Job() {
81     super(DB.k_jobs, "Job");
82     oParams = null;
83     oEnvProps = null;
84     iPendingAtoms = 0;
85     oLogFile = null;
86     oDataBind = null;
87   }
88
89   // ----------------------------------------------------------
90

91   /**
92    * <p>Process an atom</p>
93    * Concrete atom processing implementation must be provided by each derived subclass.
94    * @param oAtm Atom to be processed
95    * @return Custom subclass defined Object
96    * @throws SQLException
97    * @throws FileNotFoundException
98    * @throws IOException
99    * @throws MessagingException
100    * @throws NullPointerException
101    */

102   public abstract Object JavaDoc process (Atom oAtm)
103       throws SQLException JavaDoc,FileNotFoundException JavaDoc,IOException JavaDoc,MessagingException JavaDoc,
104       NullPointerException JavaDoc;
105
106   // ----------------------------------------------------------
107

108   /**
109    * <p>This method must free all the resource allocated by a Job</p>
110    */

111   public abstract void free ();
112
113   // ----------------------------------------------------------
114

115   /**
116    * <p>Count of atoms pending of processing for this Job</p>
117    * This count is decremented upon each successfull call to process() method
118    */

119   public int pending() {
120     return iPendingAtoms;
121   }
122
123   // ----------------------------------------------------------
124

125   public void abort(JDCConnection oConn) throws SQLException JavaDoc,IllegalStateException JavaDoc {
126     if (DebugFile.trace) {
127       DebugFile.writeln("Begin Job.abort()");
128       DebugFile.incIdent();
129       DebugFile.writeln("gu_job="+getStringNull(DB.gu_job,"null"));
130     }
131     short iStatus;
132     String JavaDoc sSQL;
133     PreparedStatement JavaDoc oUpdt;
134     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT "+DB.id_status+" FROM "+DB.k_jobs+" WHERE "+DB.gu_job+"=?",
135                                                      ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
136     oStmt.setString(1, getStringNull(DB.gu_job,null));
137     ResultSet JavaDoc oRSet = oStmt.executeQuery();
138     if (oRSet.next())
139       iStatus = oRSet.getShort(1);
140     else
141       iStatus = 100;
142     oRSet.close();
143     oStmt.close();
144     if (100==iStatus)
145       throw new SQLException JavaDoc("Job "+getStringNull(DB.gu_job,"null")+" not found");
146     if (Atom.STATUS_ABORTED==iStatus)
147       throw new IllegalStateException JavaDoc("Job "+getStringNull(DB.gu_job,"null")+" was already aborted");
148     else if (Atom.STATUS_FINISHED==iStatus)
149       throw new IllegalStateException JavaDoc("Job "+getStringNull(DB.gu_job,"null")+" was already finished");
150     sSQL = "UPDATE "+DB.k_job_atoms+" SET "+DB.id_status+"="+String.valueOf(Atom.STATUS_ABORTED)+","+DB.dt_execution+"=NULL WHERE "+DB.gu_job+"=? AND "+DB.id_status+"<>"+String.valueOf(Atom.STATUS_FINISHED);
151     if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement("+sSQL+")");
152     oUpdt = oConn.prepareStatement(sSQL);
153     oUpdt.setString(1, getStringNull(DB.gu_job,null));
154     oUpdt.executeUpdate();
155     oUpdt.close();
156     sSQL = "UPDATE "+DB.k_jobs+" SET "+DB.id_status+"="+String.valueOf(Atom.STATUS_ABORTED)+","+DB.dt_finished+"="+DBBind.Functions.GETDATE+" WHERE "+DB.gu_job+"=?";
157     oUpdt = oConn.prepareStatement(sSQL);
158     oUpdt.setString(1, getStringNull(DB.gu_job,null));
159     oUpdt.executeUpdate();
160     oUpdt.close();
161     if (DebugFile.trace) {
162       DebugFile.decIdent();
163       DebugFile.writeln("End Job.abort()");
164     }
165   } // abort
166

167   // ----------------------------------------------------------
168

169   /**
170    * <p>Load Job</p>
171    * @param oConn Database Connection
172    * @param PKVals An Array with a single element containing the Job GUID
173    * @throws SQLException
174    */

175   public boolean load(JDCConnection oConn, Object JavaDoc[] PKVals) throws SQLException JavaDoc {
176     boolean bRetVal;
177     String JavaDoc sList;
178     String JavaDoc sPageSet;
179     String JavaDoc sAttachImages;
180     Statement JavaDoc oStmt;
181     ResultSet JavaDoc oRSet;
182
183     if (DebugFile.trace) {
184       DebugFile.writeln("Begin Job.load([Connection], Object[])");
185       DebugFile.incIdent();
186     }
187
188     oParams = null;
189
190     bRetVal = super.load(oConn, PKVals);
191
192     if (bRetVal) {
193
194       oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
195
196       try { oStmt.setQueryTimeout(60); } catch (SQLException JavaDoc sqle) { }
197
198       if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT COUNT(*) FROM " + DB.k_job_atoms + " WHERE " + DB.gu_job + "='" + getStringNull(DB.gu_job,"null") + "' AND (" + DB.id_status + "=" + String.valueOf(Atom.STATUS_PENDING) + " OR " + DB.id_status + "=" + String.valueOf(Atom.STATUS_SUSPENDED) + "))");
199
200       oRSet = oStmt.executeQuery("SELECT COUNT(*) FROM " + DB.k_job_atoms + " WHERE " + DB.gu_job + "='" + getString(DB.gu_job) + "' AND (" + DB.id_status + "=" + String.valueOf(Atom.STATUS_PENDING) + " OR " + DB.id_status + "=" + String.valueOf(Atom.STATUS_SUSPENDED) + " OR " + DB.id_status + "=" + String.valueOf(Atom.STATUS_RUNNING) + ")");
201       oRSet.next();
202       iPendingAtoms = oRSet.getInt(1);
203       oRSet.close();
204
205       oStmt.close();
206
207       if (DebugFile.trace) DebugFile.writeln("pending atoms = " + String.valueOf(iPendingAtoms));
208
209       sPageSet = getParameter("gu_pageset");
210
211       if (null!=sPageSet) {
212
213         oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
214
215         oRSet = oStmt.executeQuery("SELECT " + DB.gu_workarea + "," + DB.nm_pageset + " FROM " + DB.k_pagesets + " WHERE " + DB.gu_pageset + "='" + sPageSet + "'");
216
217         if (oRSet.next()) {
218           oParams.put("gu_workarea", oRSet.getString(1));
219           oParams.put("nm_pageset", oRSet.getString(2));
220         }
221         else {
222           bRetVal = false;
223           if (DebugFile.trace)
224             DebugFile.writeln("ERROR: PageSet " + sPageSet +
225                               " referenced by job " + getString(DB.gu_job) +
226                               " was not found");
227         }
228         oRSet.close();
229         oStmt.close();
230
231       } // fi (sPageSet)
232

233       sList = getParameter("gu_list");
234
235       if (null!=sList) {
236
237         oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
238
239         oRSet = oStmt.executeQuery("SELECT " + DB.tx_sender + "," + DB.tx_from + "," + DB.tx_reply + "," + DB.tx_subject + " FROM " + DB.k_lists + " WHERE " + DB.gu_list + "='" + sList + "'");
240
241         if (oRSet.next()) {
242           oParams.put("tx_sender", oRSet.getString(1));
243           oParams.put("tx_from", oRSet.getString(2));
244           oParams.put("tx_reply", oRSet.getString(2));
245           oParams.put("tx_subject", oRSet.getString(2));
246         }
247         else {
248           bRetVal = false;
249           if (DebugFile.trace)
250             DebugFile.writeln("ERROR: List " + sList +
251                               " referenced by job " + getString(DB.gu_job) +
252                               " was not found");
253         }
254         oRSet.close();
255         oStmt.close();
256
257       } // fi (sList)
258

259       sAttachImages = getParameter("bo_attachimages");
260
261       if (null==sAttachImages)
262         oParams.put("bo_attachimages", "1");
263       else
264         oParams.put("bo_attachimages", sAttachImages);
265
266     } // fi (load(oConn, PKVals))
267

268     if (DebugFile.trace) {
269       DebugFile.decIdent();
270       DebugFile.writeln("End Job.load() : " + String.valueOf(bRetVal));
271     }
272
273     return bRetVal;
274   } // load
275

276   // ----------------------------------------------------------
277

278   /**
279    * <p>Delete Job</p>
280    * @param oConn Database Connection
281    * @throws SQLException
282    */

283   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
284     return Job.delete(oConn, getString(DB.gu_job));
285   }
286
287   // ----------------------------------------------------------
288

289   /**
290    * <p>Store Job</p>
291    * By default jobs are created with id_status=STATUS_PENDING
292    * @param oConn Database Connection
293    * @throws SQLException
294    */

295   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
296
297     if (!AllVals.containsKey(DB.gu_job))
298       put(DB.gu_job, Gadgets.generateUUID());
299     else
300       put(DB.dt_modified, new Timestamp JavaDoc(new Date JavaDoc().getTime()));
301
302     if (!AllVals.containsKey(DB.id_status))
303       put(DB.id_status, STATUS_PENDING);
304
305     return super.store(oConn);
306   }
307
308   // ----------------------------------------------------------
309

310   /**
311    * <p>Get parameters extracted from tx_parameter field</p>
312    * @return Parameters as a java.util.Properties object
313    */

314
315   public Properties JavaDoc getParameters() {
316     String JavaDoc aParams[];
317     int iParams;
318     int iDot;
319
320     if (null==oParams) {
321       oParams = new Properties JavaDoc();
322       if (!isNull(DB.tx_parameters)) {
323         aParams = Gadgets.split(getString(DB.tx_parameters), ",");
324         iParams = aParams.length;
325         for (int p = 0; p < iParams; p++) {
326           iDot = aParams[p].indexOf(':');
327           if (iDot <= 0)
328             oParams.put(aParams[p], "");
329           else
330             oParams.put(aParams[p].substring(0, iDot),
331                         aParams[p].substring(iDot + 1));
332         } // next (p)
333
} // fi (!isNull(DB.tx_parameters))
334
} // fi (oParams)
335

336     return oParams;
337   } // getParameters
338

339   // ----------------------------------------------------------
340

341   /**
342    * <p>Get parameter extracted from tx_parameter field</p>
343    * @param sParamName Parameter Name
344    * @return Parameter Value or <b>null</b> if not found
345    */

346   public String JavaDoc getParameter(String JavaDoc sParamName) {
347     return getParameters().getProperty(sParamName);
348   }
349
350
351   /**
352    * <p>Get Environment Property</p>
353    * Environment properties are readed from hipergate.cnf.
354    * @param sPropertyName
355    * @return
356    */

357   public String JavaDoc getProperty(String JavaDoc sPropertyName) {
358     return oEnvProps.getProperty(sPropertyName);
359   }
360
361   // ----------------------------------------------------------
362

363   /**
364    * Get Environment Properties Collection
365    */

366   public Properties JavaDoc getProperties() {
367     return oEnvProps;
368   }
369
370   // ----------------------------------------------------------
371

372   /**
373    * <p>Get reference to Job log file</p>
374    * Job log file is placed at /storage/jobs/gu_workarea/
375    * @return Reference to Job Log File Object
376    */

377   public File JavaDoc logFile() {
378     return oLogFile;
379   }
380
381   // ----------------------------------------------------------
382

383   /**
384    * <p>Write Line to Job Log File</p>
385    * @param sStr Line to be written
386    */

387   public void log (String JavaDoc sStr) {
388     FileWriter JavaDoc oWriter;
389
390     if (oLogFile!=null) {
391       oWriter = null;
392       try {
393         oWriter = new FileWriter JavaDoc(oLogFile, true);
394         oWriter.write(sStr);
395         oWriter.close();
396         oWriter = null;
397       }
398       catch (IOException JavaDoc ioe) {
399         if (null!=oWriter) { try {oWriter.close();} catch (IOException JavaDoc e) {} }
400       }
401     } // fi (oLogFile)
402
} // log
403

404   // ----------------------------------------------------------
405

406   /**
407    * Get database binding for this Job
408    * @return DBBind
409    */

410   public DBBind getDataBaseBind() {
411     return oDataBind;
412   }
413
414   // ----------------------------------------------------------
415

416   /**
417    * Assign a database binding to the Job
418    * @param oDbb DBBind
419    */

420   public void setDataBaseBind(DBBind oDbb) {
421     oDataBind = oDbb;
422   }
423
424   // ----------------------------------------------------------
425

426   /**
427    * <p>Set Job Status</p>
428    * If Status if set to Job.STATUS_FINISHED then dt_finished is set to current
429    * system date.
430    * @param oConn Database Connection
431    * @param iStatus Job Status
432    * <table border=1 cellpaddng=4>
433    * <tr><td>Status</td></tr>
434    * <tr><td align=middle>STATUS_ABORTED (-1)</td></tr>
435    * <tr><td align=middle>STATUS_FINISHED (0)</td></tr>
436    * <tr><td align=middle>STATUS_PENDING (1)</td></tr>
437    * <tr><td align=middle>STATUS_SUSPENDED (2)</td></tr>
438    * <tr><td align=middle>STATUS_RUNNING (3)</td></tr>
439    * </table>
440    * @throws SQLException
441    */

442   public void setStatus(JDCConnection oConn, int iStatus) throws SQLException JavaDoc {
443
444     PreparedStatement JavaDoc oStmt;
445
446     if (DebugFile.trace) {
447       DebugFile.writeln("Begin Job.setStatus([Connection], " + String.valueOf(iStatus) + ")");
448       DebugFile.incIdent();
449     }
450
451     if (Job.STATUS_FINISHED==iStatus) {
452
453       oStmt = oConn.prepareStatement("UPDATE " + DB.k_jobs + " SET " + DB.id_status + "=" + String.valueOf(iStatus) + "," + DB.dt_finished + "=? WHERE " + DB.gu_job + "='" + getString(DB.gu_job) + "'");
454
455       try {oStmt.setQueryTimeout(10);} catch (SQLException JavaDoc sqle) {}
456
457       oStmt.setTimestamp(1, new Timestamp JavaDoc(new java.util.Date JavaDoc().getTime()));
458       oStmt.executeUpdate();
459       oStmt.close();
460
461     }
462
463     else {
464
465       oStmt = oConn.prepareStatement("UPDATE " + DB.k_jobs + " SET " + DB.id_status + "=" + String.valueOf(iStatus) + " WHERE " + DB.gu_job + "='" + getString(DB.gu_job) + "'");
466
467       try {oStmt.setQueryTimeout(10);} catch (SQLException JavaDoc sqle) {}
468
469       oStmt.executeUpdate();
470       oStmt.close();
471
472     }
473
474     if (DebugFile.trace) {
475       DebugFile.decIdent();
476       DebugFile.writeln("End Job.setStatus()");
477     }
478   } // setStatus
479

480   /**
481    * <p>Fills atoms data from their e-mails</p>
482    * This method call k_sp_resolve_atoms stored procedure which takes each atom
483    * mail address and looks it up at k_member_address table for completing name
484    * surname and other personalization data embedded into each atom's record
485    * @param oConn JDCConnection
486    * @throws SQLException
487    */

488   public void resolveAtomsEMails(JDCConnection oConn)
489     throws SQLException JavaDoc {
490     Statement JavaDoc oStmt;
491     CallableStatement JavaDoc oCall;
492     if (DebugFile.trace) {
493       DebugFile.writeln("Begin Job.resolveAtomsEMails()");
494       DebugFile.incIdent();
495     }
496     switch (oConn.getDataBaseProduct()) {
497       case JDCConnection.DBMS_POSTGRESQL:
498         oStmt = oConn.createStatement();
499         oStmt.executeQuery("SELECT k_sp_resolve_atoms('"+getStringNull(DB.gu_job,null)+"')");
500         oStmt.close();
501         break;
502       default:
503         oCall = oConn.prepareCall("{ call k_sp_resolve_atoms('"+getStringNull(DB.gu_job,null)+"') }");
504         oCall.execute();
505         oCall.close();
506     }
507     if (DebugFile.trace) {
508       DebugFile.decIdent();
509       DebugFile.writeln("End Job.resolveAtomsEMails()");
510     }
511   }
512
513   // **********************************************************
514
// Static Methods
515

516   /**
517    * <p>Delete Job</p>
518    * Call k_sp_del_job stored procedure
519    * @param oConn Database Connection
520    * @param sJobId GUID of Job to be deleted
521    * @throws SQLException
522    */

523   public static boolean delete(JDCConnection oConn, String JavaDoc sJobId) throws SQLException JavaDoc {
524     Statement JavaDoc oStmt;
525     CallableStatement JavaDoc oCall;
526     boolean bRetVal;
527     if (DebugFile.trace) {
528       DebugFile.writeln("Begin Job.delete([Connection]," + sJobId + ")");
529       DebugFile.incIdent();
530     }
531
532     switch (oConn.getDataBaseProduct()) {
533       case JDCConnection.DBMS_POSTGRESQL:
534         oStmt = oConn.createStatement();
535         if (DebugFile.trace) DebugFile.writeln("Connection.executeQuery(SELECT k_sp_del_job ('" + sJobId + "')");
536         oStmt.executeQuery("SELECT k_sp_del_job ('" + sJobId + "')");
537         oStmt.close();
538         bRetVal = true;
539         break;
540       default:
541         if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_del_job ('" + sJobId + "') }");
542         oCall = oConn.prepareCall("{ call k_sp_del_job ('" + sJobId + "') }");
543         bRetVal = oCall.execute();
544         oCall.close();
545     }
546     if (DebugFile.trace) {
547       DebugFile.decIdent();
548       DebugFile.writeln("End Job.delete() : " + String.valueOf(bRetVal));
549     }
550     return bRetVal;
551   } // delete
552

553   // ----------------------------------------------------------
554

555   /**
556    * <p>Create an instance of a Job subclass</p>
557    * <p>The new object class name will be readed form k_jobs.nm_class field.</p>
558    * @param oConn Database Connection
559    * @param sJobId GUID of Job to be deleted
560    * @param oEnvironmentProps Environment properties taken from hipergate.cnf
561    * @return Reference to Instantiated Object
562    * @throws FileNotFoundException If any directory for Job log file could not be created
563    * @throws ClassNotFoundException If no class with name k_jobs.nm_class was found
564    * @throws IllegalAccessException
565    * @throws InstantiationException
566    * @throws SQLException
567    */

568   public static synchronized Job instantiate(JDCConnection oConn, String JavaDoc sJobId, Properties JavaDoc oEnvironmentProps)
569
570     throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, SQLException JavaDoc, InstantiationException JavaDoc, FileNotFoundException JavaDoc {
571
572     Class JavaDoc oJobImplementation;
573     PreparedStatement JavaDoc oStmt;
574     ResultSet JavaDoc oRSet;
575     String JavaDoc sStorage;
576     String JavaDoc sCmmdId, sClassNm;
577     Job oRetObj;
578
579     if (DebugFile.trace) {
580       DebugFile.writeln("Begin Job.instantiate([Connection]," + sJobId + ")");
581       DebugFile.incIdent();
582       DebugFile.writeln("Connection.prepareStatement(SELECT id_command,tx_command,nm_class FROM k_lu_job_commands WHERE id_command=(SELECT id_command FROM k_jobs WHERE gu_job='" + sJobId + "'))");
583     }
584
585     oStmt = oConn.prepareStatement("SELECT id_command,tx_command,nm_class FROM k_lu_job_commands WHERE id_command=(SELECT id_command FROM k_jobs WHERE gu_job=?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
586     oStmt.setString(1, sJobId);
587
588     if (DebugFile.trace) DebugFile.writeln("PreparedStatementStatement.executeQuery()");
589
590     oRSet = oStmt.executeQuery();
591
592     if (oRSet.next()) {
593
594       sCmmdId = oRSet.getString(1);
595
596       if (DebugFile.trace) DebugFile.writeln("Class Id =" + sCmmdId);
597
598       sClassNm = oRSet.getString(3);
599
600       if (DebugFile.trace) DebugFile.writeln("Class Name =" + sClassNm);
601     } // fi (next())
602

603     else {
604       sCmmdId = null;
605       sClassNm = "null";
606
607       if (DebugFile.trace) DebugFile.writeln("ERROR: job command id. not found");
608     }
609
610     oRSet.close();
611     oStmt.close();
612
613     if (null==sCmmdId)
614       oRetObj = null;
615     else {
616       if (DebugFile.trace) DebugFile.writeln("Class.forName(" + sClassNm + ");");
617
618       oJobImplementation = Class.forName(sClassNm);
619       oRetObj = (Job) oJobImplementation.newInstance();
620
621       if (oRetObj.load(oConn, new Object JavaDoc[] {sJobId})) {
622         if (null!=oConn.getPool()) {
623           if (DebugFile.trace) DebugFile.writeln("setting Job database binding");
624           oRetObj.setDataBaseBind( (DBBind) oConn.getPool().getDatabaseBinding());
625         } else {
626           if (DebugFile.trace) DebugFile.writeln("Job has no database binding");
627         }
628         oRetObj.oEnvProps = oEnvironmentProps;
629       } else {
630         oRetObj = null;
631       }
632     } // fi (sCmmdId)
633

634     if (null!=oRetObj) {
635       sStorage = oEnvironmentProps.getProperty("storage");
636
637       if (null != sStorage) {
638         if (!sStorage.endsWith(System.getProperty("file.separator"))) sStorage += System.getProperty("file.separator");
639
640         sStorage += "jobs";
641
642         // Create directory storage/jobs
643

644         oRetObj.oLogFile = new File JavaDoc(sStorage);
645         if (!oRetObj.oLogFile.exists()) oRetObj.oLogFile.mkdir();
646         if (!oRetObj.oLogFile.exists()) throw new FileNotFoundException JavaDoc(sStorage);
647
648         // Create directory storage/jobs/gu_workarea
649

650         sStorage += System.getProperty("file.separator") + oRetObj.getString(DB.gu_workarea);
651         oRetObj.oLogFile = new File JavaDoc(sStorage);
652         if (!oRetObj.oLogFile.exists()) oRetObj.oLogFile.mkdir();
653         if (!oRetObj.oLogFile.exists()) throw new FileNotFoundException JavaDoc(sStorage);
654
655         // Create directory storage/jobs/gu_workarea/gu_job
656
oRetObj.oLogFile = new File JavaDoc(sStorage + System.getProperty("file.separator") + sJobId);
657         if (!oRetObj.oLogFile.exists()) oRetObj.oLogFile.mkdir();
658         if (!oRetObj.oLogFile.exists()) throw new FileNotFoundException JavaDoc(sStorage);
659
660         // Set File Object to storage/jobs/gu_workarea/gu_job.txt
661
oRetObj.oLogFile = new File JavaDoc(sStorage + System.getProperty("file.separator") + sJobId + ".txt");
662
663       } // fi (sStorage)
664
} // fi(oRetObj)
665

666     if (DebugFile.trace) {
667       DebugFile.decIdent();
668       DebugFile.writeln("End Job.instantiate()");
669     }
670
671     return oRetObj;
672   } // instantiate
673

674   /**
675    * <p>Create an instance of a Job subclass</p>
676    * <p>The new object class name will be readed form k_jobs.nm_class field.</p>
677    * @param oConn Database Connection
678    * @param sJobId GUID of Job to be deleted
679    * @param sProfileName Name without .cnf extension of the properties file to use
680    * @return Reference to Instantiated Object
681    * @throws FileNotFoundException If any directory for Job log file could not be created
682    * @throws ClassNotFoundException If no class with name k_jobs.nm_class was found
683    * @throws IllegalAccessException
684    * @throws InstantiationException
685    * @throws SQLException
686    */

687   public static synchronized Job instantiate(JDCConnection oConn, String JavaDoc sJobId, String JavaDoc sProfileName)
688     throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, SQLException JavaDoc, InstantiationException JavaDoc, FileNotFoundException JavaDoc {
689     return instantiate(oConn,sJobId,Environment.getProfile(sProfileName));
690   }
691
692   // **********************************************************
693
// Static Methods
694

695    private static void printUsage() {
696      System.out.println("");
697      System.out.println("Usage:");
698      System.out.println("Job create job_type cnf_file_path xml_file_path [gu_job]");
699      System.out.println("job_type is one of {MAIL | MIME | FAX | SAVE | FTP}");
700    }
701
702    public static void main(String JavaDoc[] argv)
703      throws SQLException JavaDoc, org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc,
704      ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
705
706      DBPersist oJob;
707      DBBind oDBB;
708      JDCConnection oCon;
709
710      if (argv.length!=4 && argv.length!=5)
711        printUsage();
712      else
713        if (!argv[0].equals("create"))
714          printUsage();
715
716       else if (!argv[1].equalsIgnoreCase("MAIL") && !argv[1].equalsIgnoreCase("FAX") &&
717                 !argv[1].equalsIgnoreCase("SAVE") && !argv[1].equalsIgnoreCase("FTP") &&
718                 !argv[1].equalsIgnoreCase("MIME") )
719         printUsage();
720
721       else {
722          oDBB = new DBBind(argv[2]);
723
724          oCon = oDBB.getConnection("job_main");
725
726          oJob = new DBPersist(DB.k_jobs, argv[1]);
727
728          oJob.parseXML(argv[3]);
729
730          if (argv.length==5)
731            oJob.replace (DB.gu_job, argv[4]);
732
733          else if (!oJob.getItemMap().containsKey(DB.gu_job))
734            oJob.put (DB.gu_job, com.knowgate.misc.Gadgets.generateUUID());
735
736          oJob.store(oCon);
737
738          oCon.close();
739
740          oDBB.close();
741
742          System.out.println("gu_job:"+oJob.getString(DB.gu_job));
743       } // fi
744

745    } // main
746

747   // **********************************************************
748
// Public Constants
749

750   public static final short STATUS_ABORTED = -1;
751   public static final short STATUS_FINISHED = 0;
752   public static final short STATUS_PENDING = 1;
753   public static final short STATUS_SUSPENDED = 2;
754   public static final short STATUS_RUNNING = 3;
755   public static final short STATUS_INTERRUPTED = 4;
756
757 }
758
Popular Tags