KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > job > ValidationJob


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.job;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.job.Job;
69 import com.jcorporate.expresso.services.dbobj.Event;
70 import com.jcorporate.expresso.services.dbobj.JobQueue;
71 import com.jcorporate.expresso.services.validation.AuthValidationException;
72 import com.jcorporate.expresso.services.validation.ValidationEntry;
73 import com.jcorporate.expresso.services.validation.ValidationHandler;
74 import org.apache.log4j.Logger;
75
76 import java.util.List JavaDoc;
77
78
79 /**
80  * This class implements an Expresso Job, which is a background task, to
81  * decouple the user selecting some action that requires validation, from
82  * the mechanics of storing the user/application-selected parameters and
83  * starting the processing. The primary reason for using a Job here is
84  * to allow load-balancing if required, and also to make the UI appear
85  * much faster (since otherwise the user would have to wait until the
86  * entire function finished).
87  * <p/>
88  * The submission of the job is done using the ValidationEntry class.
89  *
90  * @author Shash Chatterjee
91  * @version $Revision: 1.13 $ $Date: 2004/11/17 20:48:18 $
92  */

93 public class ValidationJob
94         extends Job {
95
96     //Handle to the logging class
97
private static Logger log = Logger.getLogger(ValidationJob.class);
98
99     /**
100      * The default constructor for instantiating a new job.
101      * <p/>
102      * Creation date: (9/23/2001 9:25:23 AM)
103      * Author: Shash Chatterjee
104      */

105     public ValidationJob() {
106         super();
107         setSchema("com.jcorporate.expresso.core.ExpressoSchema");
108     }
109
110     /**
111      * Notification of job failires
112      * Creation date: (9/23/2001 9:29:24 AM)
113      * Author: Shash Chatterjee
114      *
115      * @param msg The message to send in an Expresso SYSERROR event in case this job fails
116      */

117     private void errorNotify(String JavaDoc msg) {
118         try {
119
120             // will email
121
new Event(getJobQueueEntry().getDataContext(),
122                     "SYSERROR",
123                     getClass().getName() +
124                     ":Unable to run validation job:" + msg +
125                     " Job:" + getJobNumber() + " User:" +
126                     getUser(), false);
127         } catch (Exception JavaDoc e) {
128             log.error(e);
129         }
130     } /* errorNotify */
131
132     /**
133      * The name of this Job, used typically in UI screens
134      * Creation date: (9/23/2001 9:30:54 AM)
135      * Author: Shash Chatterjee
136      *
137      * @return The title
138      */

139     public String JavaDoc getTitle() {
140         return "Validation Job";
141     }
142
143     /**
144      * This method initializes the job, currently all it does is to log a message.
145      * Creation date: (9/23/2001 9:31:39 AM)
146      * Author: Shash Chatterjee
147      *
148      * @throws AuthValidationException If there is a DB error getting job queue entry id
149      */

150     protected void initialize()
151             throws AuthValidationException {
152         if (log.isDebugEnabled()) {
153             try {
154                 log.debug("Running validation job, job id=" +
155                         getJobQueueEntry().getField("ServerID"));
156             } catch (DBException dbe) {
157                 throw new AuthValidationException("DB error getting job queue entry",
158                         dbe);
159             }
160         }
161     }
162
163     /**
164      * This method actually implements the "meat" of the Job.
165      * It's purpose is to take the parameters given the job when
166      * it was submitted and then store them into the validation
167      * subsystem. It also invokes a application-specific
168      * "validation handler" to allow the application to do all its
169      * unique things for whatever validation task it is requesting.
170      * <p/>
171      * Creation date: (9/23/2001 9:33:35 AM)
172      * Author: Shash Chatterjee
173      */

174     public void run() {
175         super.run();
176
177         //This is the default description of the job, we try to get a more specific one later
178
String JavaDoc desc = getTitle();
179
180         try {
181
182             // Guess what this does....
183
initialize();
184
185             // These are the params passed to the job from the ValidationEntry.submit()
186
List JavaDoc paramsVector = this.getJobParameterList();
187
188             // All the information about this job
189
JobQueue jq = getJobQueueEntry();
190
191             // Create a more specific description for this job
192
desc = jq.getParamValue(ValidationEntry.PRM_VAL_TITLE) + "(" +
193                     jq.getParamValue(ValidationEntry.PRM_VAL_DESC) + ")";
194
195             // Create a new ValidationEntry using the Job parameters provided, this
196
// actually permamnently stores the validation parameters.
197
ValidationEntry ve = new ValidationEntry(getDataContext(), paramsVector,
198                     jq);
199
200             // The application-specific "job handler"
201
ValidationHandler vh = ve.instantiateHandler();
202
203             // Notify the "validator" of what he needs to do...
204
vh.notify(ve.getParams(), ve.validationURL());
205             ve.setStatus("W");
206
207             // Done with this job ... success
208
finish(desc + ":succeeded");
209         } catch (Exception JavaDoc e) {
210
211             // Something went wrong....
212
finish(desc + ":failed", e);
213         } finally {
214
215             // Done with this job, mark it so that it can be cleaned up
216
try {
217                 getJobQueueEntry().delete();
218             } catch (DBException de2) {
219                 errorNotify(de2.getMessage());
220             }
221         }
222     }
223
224 }
225
Popular Tags