KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > job > GenericServiceJob


1 /*
2  * $Id: GenericServiceJob.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.job;
26
27 import java.util.Date JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.service.DispatchContext;
32 import org.ofbiz.service.GenericRequester;
33 import org.ofbiz.service.LocalDispatcher;
34 import org.ofbiz.service.ModelService;
35
36 /**
37  * Generic Service Job - A generic async-service Job.
38  *
39  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a> *
40  * @version $Rev: 5462 $
41  * @since 2.0
42  */

43 public class GenericServiceJob extends AbstractJob {
44
45     public static final String JavaDoc module = GenericServiceJob.class.getName();
46
47     protected transient GenericRequester requester = null;
48     protected transient DispatchContext dctx = null;
49
50     private String JavaDoc service = null;
51     private Map JavaDoc context = null;
52
53     public GenericServiceJob(DispatchContext dctx, String JavaDoc jobId, String JavaDoc jobName, String JavaDoc service, Map JavaDoc context, GenericRequester req) {
54         super(jobId, jobName);
55         this.dctx = dctx;
56         this.service = service;
57         this.context = context;
58         this.requester = req;
59         runtime = new Date JavaDoc().getTime();
60     }
61
62     protected GenericServiceJob(String JavaDoc jobId, String JavaDoc jobName) {
63         super(jobId, jobName);
64         this.dctx = null;
65         this.requester = null;
66         this.service = null;
67         this.context = null;
68     }
69
70     /**
71      * Invokes the service.
72      */

73     public void exec() throws InvalidJobException {
74         init();
75
76         // no transaction is necessary since runSync handles this
77
try {
78             // get the dispatcher and invoke the service via runSync -- will run all ECAs
79
LocalDispatcher dispatcher = dctx.getDispatcher();
80             Map JavaDoc result = dispatcher.runSync(getServiceName(), getContext());
81
82             // check for a failure
83
boolean isError = ModelService.RESPOND_ERROR.equals(result.get(ModelService.RESPONSE_MESSAGE));
84             if (isError) {
85                  String JavaDoc errorMessage = (String JavaDoc) result.get(ModelService.ERROR_MESSAGE);
86                  this.failed(new Exception JavaDoc(errorMessage));
87             }
88
89             if (requester != null) {
90                 requester.receiveResult(result);
91             }
92
93         } catch (Throwable JavaDoc t) {
94             // pass the exception back to the requester.
95
if (requester != null) {
96                 requester.receiveThrowable(t);
97             }
98
99             // call the failed method
100
this.failed(t);
101         }
102
103         // call the finish method
104
this.finish();
105     }
106
107     /**
108      * Method is called prior to running the service.
109      */

110     protected void init() throws InvalidJobException {
111         if (Debug.verboseOn()) Debug.logVerbose("Async-Service initializing.", module);
112     }
113
114     /**
115      * Method is called after the service has finished.
116      */

117     protected void finish() throws InvalidJobException {
118         if (Debug.verboseOn()) Debug.logVerbose("Async-Service finished.", module);
119         runtime = 0;
120     }
121
122     /**
123      * Method is called when the service fails.
124      * @param t Throwable
125      */

126     protected void failed(Throwable JavaDoc t) throws InvalidJobException {
127         Debug.logError(t, "Async-Service failed.", module);
128         runtime = 0;
129     }
130
131     /**
132      * Gets the context for the service invocation.
133      * @return Map of name value pairs making up the service context.
134      */

135     protected Map JavaDoc getContext() throws InvalidJobException {
136         return context;
137     }
138
139     /**
140      * Gets the name of the service as defined in the definition file.
141      * @return The name of the service to be invoked.
142      */

143     protected String JavaDoc getServiceName() throws InvalidJobException {
144         return service;
145     }
146 }
147
Popular Tags