KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > utility > QueueJob


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 /*
66  * QueueJob.java
67  *
68  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.core.utility;
71
72 import com.jcorporate.expresso.core.db.DBException;
73 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
74 import com.jcorporate.expresso.core.misc.ConfigManager;
75 import com.jcorporate.expresso.kernel.LogManager;
76 import com.jcorporate.expresso.services.dbobj.JobQueue;
77 import com.jcorporate.expresso.services.dbobj.JobQueueParam;
78
79 import java.util.Enumeration JavaDoc;
80 import java.util.Hashtable JavaDoc;
81 import java.util.StringTokenizer JavaDoc;
82
83
84 /**
85  * Command line program to queue a job to the job queue. Can be used
86  * to trigger scheduled jobs, such as report runs, from cron.
87  *
88  * @author Michael Nash
89  */

90 public class QueueJob {
91     private static final String JavaDoc thisClass = QueueJob.class.getName() + ".";
92
93     /**
94      * Constructor: Arguments are passed in from the main method
95      *
96      * @param arg[] Command line arguments
97      */

98     public QueueJob(String JavaDoc[] args) {
99         Hashtable JavaDoc commandArgs = new Hashtable JavaDoc(10);
100         String JavaDoc paramCode = null;
101         String JavaDoc paramValue = null;
102
103         for (int i = 0; i < args.length; i++) {
104             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(args[i], "=");
105
106             if (!stk.hasMoreTokens()) {
107                 usage();
108             }
109
110             paramCode = stk.nextToken();
111
112             if (!stk.hasMoreTokens()) {
113                 usage();
114             }
115
116             paramValue = stk.nextToken();
117         }
118         try {
119
120             String JavaDoc logDir = (String JavaDoc) commandArgs.get("logDir");
121             String JavaDoc logConfig = (String JavaDoc) commandArgs.get("logConfig");
122             if (logConfig == null) {
123                 logConfig = (String JavaDoc) commandArgs.get("configDir") + "/expressoLogging.xml";
124             }
125
126             LogManager lm = new LogManager(logConfig, logDir);
127             // set up ConfigManager first
128
ConfigManager.load((String JavaDoc) commandArgs.get("configDir"));
129
130             //initialize the db pool
131
ConfigManager.dbInitialize();
132
133             //call the LoadLists method and load up the lists used by this application
134
queueJob(commandArgs);
135             System.out.println("Job queued successfully");
136         } catch (Exception JavaDoc de) {
137             System.out.println("QueueJob Error:" + de.getMessage());
138             de.printStackTrace();
139             System.exit(1);
140         }
141     } /* QueueJob(String) */
142
143     /**
144      * Main method
145      * <p>Parameters to this program are:</p>
146      * <ol>
147      * <li>dbDriver: Driver object name for JDBC</li>
148      * <li>dbURL: URL supplied to driver to connect to database</li>
149      * <li>dbConnectFormat: Format of the connect call (1, 2, or 3)</li>
150      * <li>dbLogin: Login name for the database</li>
151      * <li>dbPassword: Password for the database</li>
152      * <li>JobCode: The code for the job to be queued. Ordinarily, this is the
153      * name of the object to be invoked on the server.</li>
154      * </ol>
155      * <p>Remainder of parameters are supplied in pairs: the first is a
156      * job parameter code, the next is the value for that parameter.</p>
157      * <p>For example, if the job takes a parameter code "ReportNumber", then
158      * "ReportNumber 5" would be used to supply the value "5" for this report
159      * number.</p>
160      *
161      * @param args[]
162      * @see com.jcorporate.expresso.core.db.DBConnection#connect
163      */

164     public static void main(String JavaDoc[] args) {
165         Hashtable JavaDoc commandArgs = new Hashtable JavaDoc(10);
166         String JavaDoc paramCode = null;
167         String JavaDoc paramValue = null;
168
169         for (int i = 0; i < args.length; i++) {
170             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(args[i], "=");
171
172             if (!stk.hasMoreTokens()) {
173                 usage();
174             }
175
176             paramCode = stk.nextToken();
177
178             if (!stk.hasMoreTokens()) {
179                 usage();
180             }
181
182             paramValue = stk.nextToken();
183             commandArgs.put(paramCode, paramValue);
184         } /* for each command-line argument */
185
186         try {
187
188             // set up ConfigManager first
189
ConfigManager.load((String JavaDoc) commandArgs.get("configDir"));
190
191             //initialize the db pool
192
ConfigManager.dbInitialize();
193
194             //call the LoadLists method and load up the lists used by this application
195
} catch (Exception JavaDoc de) {
196             System.out.println("Server Error:" + de.getMessage());
197             de.printStackTrace();
198             System.exit(1);
199         }
200
201         new QueueJob(args);
202     } /* main(String) */
203
204     /**
205      * Actually queue the requested job
206      *
207      * @param args Command line arguments
208      * @throws DBException If an error occurrs communicating with the database
209      */

210     public void queueJob(Hashtable JavaDoc args)
211             throws DBException {
212         JobQueue myQueue = new JobQueue(SecuredDBObject.SYSTEM_ACCOUNT);
213         myQueue.setDataContext((String JavaDoc) args.get("db"));
214         myQueue.setField("ExpUid", (String JavaDoc) args.get("uid"));
215         myQueue.setField("JobCode", (String JavaDoc) args.get("job"));
216         myQueue.setField("StatusCode", "N");
217         myQueue.add();
218
219         /* parameters */
220         JobQueueParam myParam = new JobQueueParam(SecuredDBObject.SYSTEM_ACCOUNT);
221         myParam.setDataContext((String JavaDoc) args.get("db"));
222         myParam.setField("JobNumber", myQueue.getField("JobNumber"));
223
224         String JavaDoc currentParam = null;
225         String JavaDoc currentValue = null;
226         int paramNumber = 0;
227
228         for (Enumeration JavaDoc e = args.keys(); e.hasMoreElements();) {
229             currentParam = (String JavaDoc) e.nextElement();
230             currentValue = (String JavaDoc) args.get(currentParam);
231             paramNumber++;
232             myParam.setField("ParamNumber", ("" + paramNumber));
233             myParam.setField("ParamCode", currentParam);
234             myParam.setField("ParamValue", currentValue);
235             myParam.add();
236             System.out.println("Parameter '" + currentParam + "' set to '" +
237                     currentValue + "'");
238         } /* for each argument */
239
240
241         myQueue.setField("StatusCode", "A");
242         myQueue.update();
243     } /* queueJob(Hashtable) */
244
245
246     /**
247      * Display the usage message if the user gives a bad option,
248      * then bail out.
249      */

250     private static void usage() {
251         System.out.println("Usage: QueueJob arg=value ... ");
252         System.out.println("Where arguments are: dbDriver dbURL " +
253                 "dbConnectFormat dbLogin dbPassword ");
254         System.exit(1);
255     } /* usage() */
256
257 } /* QueueJob */
258
Popular Tags