001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.enhydra.shark;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Properties;
025:
026: //import org.enhydra.jawe.xml.elements.Implementation;
027: //import org.enhydra.jawe.xml.elements.Tools;
028: import org.enhydra.shark.SharkEngineManager;
029: import org.enhydra.shark.api.ApplicationMappingTransaction;
030: import org.enhydra.shark.api.RootException;
031: import org.enhydra.shark.api.SharkTransaction;
032: import org.enhydra.shark.api.client.wfbase.BaseException;
033: import org.enhydra.shark.api.internal.appmappersistence.ApplicationMap;
034: import org.enhydra.shark.api.internal.appmappersistence.ApplicationMappingManager;
035: import org.enhydra.shark.api.internal.toolagent.AppParameter;
036: import org.enhydra.shark.api.internal.toolagent.ConnectFailed;
037: import org.enhydra.shark.api.internal.toolagent.SessionHandle;
038: import org.enhydra.shark.api.internal.toolagent.ToolAgent;
039:
040: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
041: import org.enhydra.shark.api.internal.working.CallbackUtilities;
042: import org.enhydra.shark.api.internal.working.ToolAgentManager;
043: import org.enhydra.shark.api.internal.working.WfActivityInternal;
044: import org.enhydra.shark.api.internal.working.WfProcessInternal;
045:
046: import org.enhydra.shark.xpdl.XMLComplexElement;
047: import org.enhydra.shark.xpdl.XPDLConstants;
048: import org.enhydra.shark.xpdl.XMLUtil;
049: import org.enhydra.shark.xpdl.elements.Activity;
050: import org.enhydra.shark.xpdl.elements.ActivityTypes;
051: import org.enhydra.shark.xpdl.elements.ActualParameter;
052: import org.enhydra.shark.xpdl.elements.ActualParameters;
053: import org.enhydra.shark.xpdl.elements.Application;
054: import org.enhydra.shark.xpdl.elements.FormalParameter;
055: import org.enhydra.shark.xpdl.elements.FormalParameters;
056: import org.enhydra.shark.xpdl.elements.ImplementationTypes;
057: import org.enhydra.shark.xpdl.elements.Tool;
058: import org.enhydra.shark.xpdl.elements.WorkflowProcess;
059:
060: /**
061: * Executes tool agents for Tool activities.
062: */
063: public class ThreadedToolAgentManager implements ToolAgentManager {
064:
065: private final static long APP_STATUS_INVALID = -1;
066:
067: private final static String DEFAULT_TOOL_AGENT = "DefaultToolAgent";
068:
069: private String defaultToolAgentClassName;
070:
071: private CallbackUtilities cus;
072:
073: protected ThreadedToolAgentManager() {
074: this .cus = SharkEngineManager.getInstance()
075: .getCallbackUtilities();
076: Properties props = cus.getProperties();
077: // setting default tool agent
078: try {
079: defaultToolAgentClassName = (String) props
080: .get(DEFAULT_TOOL_AGENT);
081: } catch (Throwable ex) {
082: cus
083: .error("ToolAgentManagerImpl -> Can't read default tool agent name - can't work without mappings !!!");
084: }
085: }
086:
087: public void executeActivity(SharkTransaction t,
088: WfActivityInternal act) throws BaseException,
089: ToolAgentGeneralException {
090: ToolRunner tr = new ToolRunner(t, act);
091: tr.run();
092: }
093:
094: protected class ToolRunner {// implements Runnable {
095: protected WfActivityInternal activity;
096: protected Activity actDef;
097: protected SharkTransaction transaction;
098:
099: protected ToolRunner(SharkTransaction t, WfActivityInternal wai)
100: throws BaseException {
101: this .transaction = t;
102: this .activity = wai;
103: WfProcessInternal pr = wai.container(t);
104: WorkflowProcess wp = SharkUtilities.getWorkflowProcess(pr
105: .package_id(t), pr.manager_version(t), pr
106: .process_definition_id(t));
107: this .actDef = SharkUtilities.getActivityDefinition(t, wai,
108: wp, wai.block_activity(t));
109: }
110:
111: public void run() throws BaseException,
112: ToolAgentGeneralException {
113: Iterator tools = null;
114: try {
115: ActivityTypes acTypes = actDef.getActivityTypes();
116: org.enhydra.shark.xpdl.elements.Implementation impl = acTypes
117: .getImplementation();
118: ImplementationTypes implt = impl
119: .getImplementationTypes();
120: org.enhydra.shark.xpdl.elements.Tools tolls = implt
121: .getTools();
122: ArrayList al = tolls.toElements();
123: tools = al.iterator();
124: } catch (Exception e) {
125: e.printStackTrace();
126: throw new BaseException(e);
127: }
128:
129: while (tools.hasNext()) {
130: Tool tool = (Tool) tools.next();
131: cus
132: .info("Activity" + activity.toString()
133: + " - Executing tool [id="
134: + tool.getId() + "]");
135: // implement me
136: /*if (tool.get("Type").toValue().toString().equals("APPLICATION")){
137: } else {*/
138: try {
139: invokeApplication(tool);
140: // if some application is not executed, throw an exception
141: } catch (Throwable ex) {
142: cus.error("Activity" + activity.toString()
143: + " - failed to execute tool [id="
144: + tool.getId() + "]");
145: if (ex instanceof ToolAgentGeneralException) {
146: throw (ToolAgentGeneralException) ex;
147: } else {
148: throw new BaseException(ex);
149: }
150: }
151: //}
152: }
153: }
154:
155: protected Map createContextMap(ActualParameters aps,
156: FormalParameters fps) throws Exception {
157: return SharkUtilities.createContextMap(transaction,
158: activity, aps, fps);
159: }
160:
161: protected String getAssignmentId(String procId, String actId)
162: throws Exception {
163: String actRes = activity
164: .getResourceRequesterUsername(transaction);
165: String assId = SharkUtilities.createAssignmentKey(actId,
166: actRes);
167: return assId;
168: }
169:
170: protected void invokeApplication(Tool tool) throws Throwable {
171: String applicationId = tool.getId();
172: Application app = SharkUtilities.getApplication(tool,
173: applicationId);
174:
175: ArrayList parameters = new ArrayList();
176:
177: // the extended attributes are always the first parameter passed to tool agent
178: String appPStr = app.getExtendedAttributes()
179: .getExtendedAttributesString();
180: AppParameter param = new AppParameter("ExtendedAttributes",
181: "ExtendedAttributes",
182: XPDLConstants.FORMAL_PARAMETER_MODE_IN, appPStr,
183: String.class);
184: parameters.add(param);
185:
186: ActualParameters aps = tool.getActualParameters();
187: FormalParameters fps = app.getApplicationTypes()
188: .getFormalParameters();
189: Map m = createContextMap(aps, fps);
190: Iterator itFps = fps.toElements().iterator();
191: Iterator itAps = aps.toElements().iterator();
192: while (itFps.hasNext() && itAps.hasNext()) {
193: FormalParameter fp = (FormalParameter) itFps.next();
194: ActualParameter ap = (ActualParameter) itAps.next();
195: String fpMode = fp.getMode();
196: String fpId = fp.getId();
197: Object paramVal = m.get(fpId);
198:
199: param = new AppParameter(ap.toValue(), fpId, fpMode,
200: paramVal, SharkUtilities.getJavaClass(fp));
201: parameters.add(param);
202: }
203: ApplicationMappingManager mm = SharkEngineManager
204: .getInstance()
205: .getApplicationMapPersistenceManager();
206: ApplicationMap tad = null;
207: if (mm != null) {
208: XMLComplexElement cOwn = (XMLComplexElement) app
209: .getParent().getParent();
210: boolean isProcessApp = (cOwn instanceof WorkflowProcess);
211: ApplicationMappingTransaction t = null;
212: try {
213: t = SharkUtilities
214: .createApplicationMappingTransaction();
215: tad = mm.getApplicationMap(t, XMLUtil.getPackage(
216: app).getId(), ((isProcessApp) ? cOwn.get(
217: "Id").toValue() : null), applicationId);
218: SharkUtilities.commitMappingTransaction(t);
219: } catch (RootException e) {
220: SharkUtilities.rollbackMappingTransaction(t, e);
221: throw e;
222: } finally {
223: SharkUtilities.releaseMappingTransaction(t);
224: }
225: }
226: SessionHandle shandle = null;
227: String tacn = (tad != null) ? tad.getToolAgentClassName()
228: : defaultToolAgentClassName;
229: String uname = (tad != null) ? tad.getUsername() : "";
230: String pwd = (tad != null) ? tad.getPassword() : "";
231: String appN = (tad != null) ? tad.getApplicationName() : "";
232: Integer appM = (tad != null) ? tad.getApplicationMode()
233: : null;
234: ToolAgent ta = SharkEngineManager.getInstance()
235: .getToolAgentFactory().createToolAgent(transaction,
236: tacn);
237: // try to connect to the tool agent
238: try {
239: shandle = ta.connect(transaction, uname, pwd, cus
240: .getProperty("enginename", "imaobihostrezube"),
241: "");
242: } catch (ConnectFailed cf) {
243: cus.error("Activity" + activity.toString()
244: + " - connection to Tool agent " + tacn
245: + " failed !");
246: throw cf;
247: }
248:
249: String procId = activity.container(transaction).key(
250: transaction);
251: String actKey = activity.key(transaction);
252: String assId = getAssignmentId(procId, actKey);
253:
254: // invoke the procedure with the specified parameters
255: AppParameter[] aprs = (AppParameter[]) parameters
256: .toArray(new AppParameter[parameters.size()]);
257: ta.invokeApplication(transaction, shandle.getHandle(),
258: appN, procId, assId, aprs, appM);
259: long appStatus;
260:
261: appStatus = ta.requestAppStatus(transaction, shandle
262: .getHandle(), procId, assId, aprs);
263: if (appStatus == APP_STATUS_INVALID) {
264: ta.disconnect(transaction, shandle);
265: throw new Exception("Tool agent status is invalid!");
266: }
267: ta.disconnect(transaction, shandle);
268:
269: AppParameter[] returnValues = aprs;
270:
271: // copy the return values into the workflow data
272: Map newData = new HashMap();
273: for (int i = 0; i < returnValues.length; i++) {
274: if (returnValues[i].the_mode
275: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT)
276: || returnValues[i].the_mode
277: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
278: String name = returnValues[i].the_actual_name;
279: Object value = returnValues[i].the_value;
280: newData.put(name, value);
281: }
282: }
283: activity.set_result(transaction, newData);
284: }
285: }
286: }
|