001: package org.enhydra.shark.toolagent;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008: import java.util.Properties;
009:
010: import org.enhydra.shark.api.RootError;
011: import org.enhydra.shark.api.client.wfmc.wapi.WMConnectInfo;
012: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
013: import org.enhydra.shark.api.client.wfservice.WMEntity;
014: import org.enhydra.shark.api.internal.appmappersistence.ApplicationMap;
015: import org.enhydra.shark.api.internal.appmappersistence.ApplicationMappingManager;
016: import org.enhydra.shark.api.internal.toolagent.AppParameter;
017: import org.enhydra.shark.api.internal.toolagent.ConnectFailed;
018: import org.enhydra.shark.api.internal.toolagent.ToolAgent;
019: import org.enhydra.shark.api.internal.toolagent.ToolAgentManager;
020: import org.enhydra.shark.api.internal.working.CallbackUtilities;
021:
022: /**
023: * Standard manager for creating and invoking tool agents.
024: *
025: * @author Sasa Bojanic
026: * @version 1.0
027: */
028: public class StandardToolAgentManager implements ToolAgentManager {
029: public static final String APPLICATION_MAPPING_CLASS_NAME_PROPERTY = "StandardToolAgentManager.ApplicationMapPersistenceManagerClassName";
030:
031: public static final String DEFAULT_TOOL_AGENT = "DefaultToolAgent";
032:
033: private ApplicationMappingManager applicationMappings;
034:
035: private final static String TOOL_AGENT_PREFIX = "ToolAgent.";
036:
037: private List toolAgents = new ArrayList();
038:
039: private CallbackUtilities cus;
040:
041: private String defaultToolAgentClassName;
042:
043: public void configure(CallbackUtilities cus) throws Exception {
044: this .cus = cus;
045: defaultToolAgentClassName = cus.getProperty(DEFAULT_TOOL_AGENT,
046: "org.enhydra.shark.toolagent.DefaultToolAgent");
047:
048: createToolAgentList();
049:
050: String ammClassName = cus
051: .getProperty(APPLICATION_MAPPING_CLASS_NAME_PROPERTY);
052:
053: ClassLoader cl = getClass().getClassLoader();
054:
055: try {
056: applicationMappings = (ApplicationMappingManager) cl
057: .loadClass(ammClassName).newInstance();
058: applicationMappings.configure(cus);
059: cus
060: .info(
061: null,
062: "StandardToolAgentManager -> Working with '"
063: + ammClassName
064: + "' implementation of ApplicationMapPersistence API");
065: } catch (Exception ex) {
066: boolean throwError = true;
067: String msg = "StandardToolAgentManager -> Can't work - ";
068: if (ammClassName == null || ammClassName.trim().equals("")) {
069: msg = "StandardToolAgentManager -> Working without ApplicationMapPersistence API implementation - ";
070: msg += "ApplicationMapPersistenceManager is not specified.";
071: throwError = false;
072: } else if (applicationMappings == null) {
073: msg += "Can't find ApplicationMapPersistenceManager class '"
074: + ammClassName + "' in classpath!";
075: } else {
076: msg += "Problems while configuring ApplicationMapPersistenceManager!";
077: }
078: cus.info(null, msg);
079: if (throwError) {
080: throw new RootError(msg, ex);
081: }
082: }
083: }
084:
085: /**
086: * Returns all tool agents specified in shark's properties.
087: */
088: public List getDefinedToolAgents(WMSessionHandle shandle)
089: throws Exception {
090: return Collections.unmodifiableList(toolAgents);
091: }
092:
093: public ToolAgent createToolAgent(WMSessionHandle shandle,
094: String className) throws Exception {
095:
096: Class cls = null;
097:
098: try {
099: cls = Class.forName(className);
100: } catch (ClassNotFoundException cnfe1) {
101: cls = ToolAgentLoader.load(cus, className);
102: }
103:
104: ToolAgent ta = (ToolAgent) cls.newInstance();
105: ta.configure(cus);
106: return ta;
107: }
108:
109: public void invokeToolAgent(WMSessionHandle shandle,
110: WMEntity appInfo, WMEntity toolInfo, AppParameter[] aprs,
111: String procId, String actId, String assId) throws Exception {
112: // find mapped procedure - but we can also live without mapping
113: // manager (but we can't without ToolAgentFactory
114: ApplicationMappingManager mm = getApplicationMapPersistenceManager();
115: ApplicationMap tad = null;
116: if (mm != null) {
117: tad = mm.getApplicationMap(shandle, appInfo.getPkgId(),
118: appInfo.getPkgVer(), appInfo.getWpId(), appInfo
119: .getId());
120: }
121: WMSessionHandle tashandle = null;
122: String tacn = (tad != null) ? tad.getToolAgentClassName()
123: : defaultToolAgentClassName;
124: String uname = (tad != null) ? tad.getUsername() : "";
125: String pwd = (tad != null) ? tad.getPassword() : "";
126: String appN = (tad != null) ? tad.getApplicationName() : "";
127: Integer appM = (tad != null) ? tad.getApplicationMode() : null;
128:
129: ToolAgent ta = createToolAgent(shandle, tacn);
130:
131: WMConnectInfo wmci = new WMConnectInfo(uname, pwd, cus
132: .getProperty("enginename", "imaobihostrezube"), "");
133: // try to connect to the tool agent
134: try {
135: tashandle = ta.connect(wmci);
136: } catch (ConnectFailed cf) {
137: cus.error(shandle, "Activity" + actId
138: + " - connection to Tool agent " + tacn
139: + " failed !");
140: throw cf;
141: }
142:
143: long appStatus;
144: ta.invokeApplication(shandle, tashandle.getId(), appInfo,
145: toolInfo, appN, procId, assId, aprs, appM);
146:
147: appStatus = ta.requestAppStatus(shandle, tashandle.getId(),
148: toolInfo, procId, assId, aprs);
149: // System.err.println("TAM get status "+appStatus+" for underlying TA");
150: if (appStatus == ToolAgent.APP_STATUS_INVALID) {
151: // System.err.println("TAM catched invalid APPS and throwing exc");
152: ta.disconnect(tashandle);
153: throw new Exception("Tool agent status is invalid!");
154: }
155: ta.disconnect(tashandle);
156:
157: }
158:
159: private void createToolAgentList() {
160: String taName = null;
161: String className = null;
162: Properties props = cus.getProperties();
163:
164: Iterator it = props.entrySet().iterator();
165: while (it.hasNext()) {
166: try {
167: Map.Entry me = (Map.Entry) it.next();
168: taName = me.getKey().toString();
169: if (taName.startsWith(TOOL_AGENT_PREFIX)) {
170: taName = taName.substring(TOOL_AGENT_PREFIX
171: .length());
172: className = me.getValue().toString();
173: toolAgents.add(className);
174: }
175: } catch (Throwable ex) {
176: // ex.printStackTrace();
177: cus
178: .error(
179: null,
180: "StandardToolAgentManager -> Error when retrieving list of tool agents from properties!!!");
181: }
182: }
183: }
184:
185: public ApplicationMappingManager getApplicationMapPersistenceManager() {
186: return applicationMappings;
187: }
188:
189: public String[][] getToolAgentsInfo(WMSessionHandle shandle)
190: throws Exception {
191: List l = getDefinedToolAgents(shandle);
192: String[][] ret = new String[l.size()][2];
193: for (int i = 0; i < l.size(); i++) {
194: String tan = (String) l.get(i);
195: ret[i][0] = new String(tan);
196: ret[i][1] = new String(createToolAgent(shandle, tan)
197: .getInfo(shandle));
198: }
199:
200: return ret;
201: }
202:
203: }
|