KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > tool > ServiceEngineAgent


1 package org.ofbiz.shark.tool;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import org.ofbiz.shark.container.SharkContainer;
7 import org.ofbiz.service.LocalDispatcher;
8 import org.ofbiz.service.GenericServiceException;
9 import org.ofbiz.service.ServiceUtil;
10 import org.ofbiz.base.util.Debug;
11
12 import org.enhydra.shark.toolagent.AbstractToolAgent;
13 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
14 import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
15 import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
16 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
17 import org.enhydra.shark.api.internal.toolagent.AppParameter;
18 import org.enhydra.shark.api.SharkTransaction;
19 import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
20
21 /**
22  * Shark Service Engine Agent Tool API
23  *
24  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
25  * @version $Rev: 5462 $
26  * @since 3.1
27  */

28 public class ServiceEngineAgent extends AbstractToolAgent {
29
30     public static final String JavaDoc module = ServiceEngineAgent.class.getName();
31
32     public void invokeApplication (SharkTransaction trans, long handle, String JavaDoc applicationName, String JavaDoc procInstId, String JavaDoc actInstId,
33             AppParameter[] parameters, Integer JavaDoc appMode) throws ApplicationNotStarted,
34             ApplicationNotDefined, ApplicationBusy, ToolAgentGeneralException {
35
36         super.invokeApplication(trans, handle, applicationName, procInstId, actInstId, parameters, appMode);
37
38         // set the status
39
status = APP_STATUS_RUNNING;
40
41         // prepare the service
42
LocalDispatcher dispatcher = SharkContainer.getDispatcher();
43         Map JavaDoc serviceContext = new HashMap JavaDoc();
44         this.getServiceContext(parameters, serviceContext);
45
46         // invoke the service
47
Map JavaDoc serviceResult = null;
48         try {
49             serviceResult = dispatcher.runSync(appName, serviceContext);
50         } catch (GenericServiceException e) {
51             status = APP_STATUS_INVALID;
52             Debug.logError(e, module);
53             throw new ToolAgentGeneralException(e);
54         }
55
56         // process the result
57
this.getServiceResults(parameters, serviceResult);
58
59         // check for errors
60
if (ServiceUtil.isError(serviceResult)) {
61             status = APP_STATUS_INVALID;
62         } else {
63             status = APP_STATUS_FINISHED;
64         }
65     }
66
67     private void getServiceContext(AppParameter[] params, Map JavaDoc context) {
68         if (params != null && context != null) {
69             for (int i = 1; i < params.length; i++) {
70                 if (params[i].the_mode.equals(AppParameter.MODE_IN) || params[i].the_mode.equals(AppParameter.MODE_INOUT)) {
71                     context.put(params[i].the_formal_name, params[i].the_value);
72                 }
73             }
74         }
75     }
76
77     private void getServiceResults(AppParameter[] params, Map JavaDoc result) {
78         if (params != null && result != null) {
79             for (int i = 1; i < params.length; i++) {
80                 if (params[i].the_mode.equals(AppParameter.MODE_OUT) || params[i].the_mode.equals(AppParameter.MODE_INOUT)) {
81                     params[i].the_value = result.get(params[i].the_formal_name);
82                 }
83             }
84         }
85     }
86
87     protected ExtendedAttributes readParamsFromExtAttributes (String JavaDoc extAttribs) throws Exception JavaDoc {
88         ExtendedAttributes eas = super.readParamsFromExtAttributes(extAttribs);
89         return eas;
90     }
91 }
92
Popular Tags