001: /*
002: * $Id: GenericServiceJob.java,v 1.3 2003/11/25 23:56:07 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.job;
026:
027: import java.util.Date;
028: import java.util.Map;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.service.DispatchContext;
032: import org.ofbiz.service.GenericRequester;
033: import org.ofbiz.service.LocalDispatcher;
034: import org.ofbiz.service.ModelService;
035:
036: /**
037: * Generic Service Job - A generic async-service Job.
038: *
039: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> *
040: * @version $Revision: 1.3 $
041: * @since 2.0
042: */
043: public class GenericServiceJob extends AbstractJob {
044:
045: public static final String module = GenericServiceJob.class
046: .getName();
047:
048: protected transient GenericRequester requester = null;
049: protected transient DispatchContext dctx = null;
050:
051: private String service = null;
052: private Map context = null;
053:
054: public GenericServiceJob(DispatchContext dctx, String jobName,
055: String service, Map context, GenericRequester req) {
056: super (jobName);
057: this .dctx = dctx;
058: this .service = service;
059: this .context = context;
060: this .requester = req;
061: runtime = new Date().getTime();
062: }
063:
064: protected GenericServiceJob(String jobName) {
065: super (jobName);
066: this .dctx = null;
067: this .requester = null;
068: this .service = null;
069: this .context = null;
070: }
071:
072: /**
073: * Invokes the service.
074: */
075: public void exec() {
076: init();
077:
078: // no transaction is necessary since runSync handles this
079: try {
080: // get the dispatcher and invoke the service via runSync -- will run all ECAs
081: LocalDispatcher dispatcher = dctx.getDispatcher();
082: Map result = dispatcher.runSync(getServiceName(),
083: getContext());
084:
085: // check for a failure
086: boolean isError = ModelService.RESPOND_ERROR.equals(result
087: .get(ModelService.RESPONSE_MESSAGE));
088: if (isError) {
089: String errorMessage = (String) result
090: .get(ModelService.ERROR_MESSAGE);
091: this .failed(new Exception(errorMessage));
092: }
093:
094: if (requester != null) {
095: requester.receiveResult(result);
096: }
097:
098: } catch (Throwable t) {
099: // pass the exception back to the requester.
100: if (requester != null) {
101: requester.receiveThrowable(t);
102: }
103:
104: // call the failed method
105: this .failed(t);
106: }
107:
108: // call the finish method
109: this .finish();
110: }
111:
112: /**
113: * Method is called prior to running the service.
114: */
115: protected void init() {
116: if (Debug.verboseOn())
117: Debug.logVerbose("Async-Service initializing.", module);
118: }
119:
120: /**
121: * Method is called after the service has finished.
122: */
123: protected void finish() {
124: if (Debug.verboseOn())
125: Debug.logVerbose("Async-Service finished.", module);
126: runtime = 0;
127: }
128:
129: /**
130: * Method is called when the service fails.
131: * @param t Throwable
132: */
133: protected void failed(Throwable t) {
134: Debug.logError(t, "Async-Service failed.", module);
135: runtime = 0;
136: }
137:
138: /**
139: * Gets the context for the service invocation.
140: * @return Map of name value pairs making up the service context.
141: */
142: protected Map getContext() {
143: return context;
144: }
145:
146: /**
147: * Gets the name of the service as defined in the definition file.
148: * @return The name of the service to be invoked.
149: */
150: protected String getServiceName() {
151: return service;
152: }
153: }
|