1 package org.enhydra.shark; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 import java.util.Properties ; 8 9 import org.enhydra.shark.api.ApplicationMappingTransaction; 10 import org.enhydra.shark.api.RootException; 11 import org.enhydra.shark.api.SharkTransaction; 12 import org.enhydra.shark.api.client.wfbase.BaseException; 13 import org.enhydra.shark.api.internal.appmappersistence.ApplicationMap; 14 import org.enhydra.shark.api.internal.appmappersistence.ApplicationMappingManager; 15 import org.enhydra.shark.api.internal.toolagent.AppParameter; 16 import org.enhydra.shark.api.internal.toolagent.ConnectFailed; 17 import org.enhydra.shark.api.internal.toolagent.SessionHandle; 18 import org.enhydra.shark.api.internal.toolagent.ToolAgent; 19 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException; 20 import org.enhydra.shark.api.internal.working.CallbackUtilities; 21 import org.enhydra.shark.api.internal.working.ToolAgentManager; 22 import org.enhydra.shark.api.internal.working.WfActivityInternal; 23 import org.enhydra.shark.api.internal.working.WfProcessInternal; 24 import org.enhydra.shark.xpdl.XMLComplexChoice; 25 import org.enhydra.shark.xpdl.XMLComplexElement; 26 import org.enhydra.shark.xpdl.XPDLConstants; 27 import org.enhydra.shark.xpdl.XMLUtil; 28 import org.enhydra.shark.xpdl.elements.Activity; 29 import org.enhydra.shark.xpdl.elements.ActualParameter; 30 import org.enhydra.shark.xpdl.elements.ActualParameters; 31 import org.enhydra.shark.xpdl.elements.Application; 32 import org.enhydra.shark.xpdl.elements.FormalParameter; 33 import org.enhydra.shark.xpdl.elements.FormalParameters; 34 import org.enhydra.shark.xpdl.elements.Tool; 35 import org.enhydra.shark.xpdl.elements.WorkflowProcess; 36 37 42 public class ToolAgentManagerImpl implements ToolAgentManager { 43 44 private final static long APP_STATUS_INVALID=-1; 45 46 private final static String DEFAULT_TOOL_AGENT="DefaultToolAgent"; 47 48 private String defaultToolAgentClassName; 49 50 private CallbackUtilities cus; 51 52 protected ToolAgentManagerImpl () { 53 this.cus=SharkEngineManager.getInstance().getCallbackUtilities(); 54 Properties props= cus.getProperties(); 55 try { 57 defaultToolAgentClassName=(String )props.get(DEFAULT_TOOL_AGENT); 58 } catch (Throwable ex) { 59 cus.error("ToolAgentManagerImpl -> Can't read default tool agent name - can't work without mappings !!!"); 60 } 61 } 62 63 public void executeActivity (SharkTransaction t,WfActivityInternal act) throws BaseException, ToolAgentGeneralException { 64 ToolRunner tr=new ToolRunner(t,act); 65 tr.run(); 68 } 69 70 protected class ToolRunner { protected WfActivityInternal activity; 72 protected Activity actDef; 73 protected SharkTransaction transaction; 74 75 protected ToolRunner (SharkTransaction t,WfActivityInternal wai) throws BaseException { 76 this.transaction=t; 77 this.activity=wai; 78 WfProcessInternal pr=wai.container(t); 79 WorkflowProcess wp=SharkUtilities. 80 getWorkflowProcess(pr.package_id(t), 81 pr.manager_version(t), 82 pr.process_definition_id(t)); 83 this.actDef=SharkUtilities.getActivityDefinition(t,wai,wp,wai.block_activity(t)); 84 } 85 86 public void run () throws BaseException, ToolAgentGeneralException { 87 Iterator tools = null; 88 tools=actDef.getActivityTypes().getImplementation().getImplementationTypes().getTools().toElements().iterator(); 89 while(tools.hasNext()) { 90 Tool tool = (Tool)tools.next(); 91 cus.info("Activity"+activity.toString()+" - Executing tool [id="+tool.getId()+"]"); 92 95 try { 96 invokeApplication(tool); 97 } catch (Throwable ex) { 99 cus.error("Activity"+activity.toString()+" - failed to execute tool [id="+tool.getId()+"]"); 100 if (ex instanceof ToolAgentGeneralException) { 102 throw (ToolAgentGeneralException)ex; 103 } else { 104 throw new BaseException(ex); 105 } 106 } 107 } 109 } 110 111 protected Map createContextMap (ActualParameters aps,FormalParameters fps) throws Exception { 112 return SharkUtilities.createContextMap(transaction,activity,aps,fps); 113 } 114 115 protected String getAssignmentId (String procId,String actId) throws Exception { 116 String actRes=activity.getResourceRequesterUsername(transaction); 117 String assId=SharkUtilities.createAssignmentKey(actId,actRes); 118 return assId; 119 } 120 121 protected void invokeApplication(Tool tool) throws Throwable { 125 String applicationId = tool.getId(); 126 Application app=SharkUtilities.getApplication(tool, applicationId); 127 128 130 ArrayList parameters = new ArrayList (); 132 133 String appPStr=app.getExtendedAttributes().getExtendedAttributesString(); 135 AppParameter param=new AppParameter("ExtendedAttributes","ExtendedAttributes",XPDLConstants.FORMAL_PARAMETER_MODE_IN,appPStr,String .class); 137 parameters.add(param); 138 139 ActualParameters aps=tool.getActualParameters(); 140 FormalParameters fps=app.getApplicationTypes().getFormalParameters(); 141 Map m=createContextMap(aps,fps); 142 Iterator itFps=fps.toElements().iterator(); 144 Iterator itAps=aps.toElements().iterator(); 145 while (itFps.hasNext() && itAps.hasNext()) { 146 FormalParameter fp=(FormalParameter)itFps.next(); 147 ActualParameter ap=(ActualParameter)itAps.next(); 148 String fpMode=fp.getMode(); 149 String fpId=fp.getId(); 150 Object paramVal=m.get(fpId); 151 152 param=new AppParameter(ap.toValue(),fpId,fpMode,paramVal,SharkUtilities.getJavaClass(fp)); 157 parameters.add(param); 158 } 159 160 ApplicationMappingManager mm=SharkEngineManager.getInstance().getApplicationMapPersistenceManager(); 163 ApplicationMap tad=null; 164 if (mm!=null) { 165 XMLComplexElement cOwn=(XMLComplexElement)app.getParent().getParent(); 166 boolean isProcessApp=(cOwn instanceof WorkflowProcess); 167 ApplicationMappingTransaction t=null; 168 try { 169 t = SharkUtilities.createApplicationMappingTransaction(); 170 tad= 171 SharkEngineManager. 172 getInstance().getApplicationMapPersistenceManager(). 173 getApplicationMap( 174 t, 175 XMLUtil.getPackage(app).getId(), 176 ((isProcessApp)? cOwn.get("Id").toValue() : null), 177 applicationId 178 ); 179 } catch (RootException e) { 181 throw e; 183 } finally { 184 SharkUtilities.releaseMappingTransaction(t); 185 } 186 } 187 SessionHandle shandle=null; 188 String tacn=(tad!=null) ? tad.getToolAgentClassName() : defaultToolAgentClassName; 189 String uname=(tad!=null) ? tad.getUsername() : ""; 190 String pwd=(tad!=null) ? tad.getPassword() : ""; 191 String appN=(tad!=null) ? tad.getApplicationName() : ""; 192 Integer appM=(tad!=null) ? tad.getApplicationMode() : null; 193 ToolAgent ta=SharkEngineManager.getInstance(). 194 getToolAgentFactory(). 195 createToolAgent(transaction,tacn); 196 try { 198 shandle=ta.connect(transaction,uname,pwd,cus.getProperty("enginename","imaobihostrezube"),""); 199 } catch (ConnectFailed cf) { 200 cus.error("Activity"+activity.toString()+" - connection to Tool agent "+tacn+" failed !"); 201 throw cf; 202 } 203 204 String procId=activity.container(transaction).key(transaction); 205 String actKey=activity.key(transaction); 206 String assId=getAssignmentId(procId,actKey); 207 208 AppParameter[] aprs=(AppParameter[])parameters.toArray( 210 new AppParameter[parameters.size()]); 211 ta.invokeApplication(transaction, 212 shandle.getHandle(), 213 appN, 214 procId, 215 assId, 216 aprs, 217 appM); 218 long appStatus; 219 220 221 appStatus=ta.requestAppStatus(transaction, 222 shandle.getHandle(), 223 procId, 224 assId, 225 aprs); 226 if (appStatus==APP_STATUS_INVALID) { 228 ta.disconnect(transaction,shandle); 230 throw new Exception ("Tool agent status is invalid!"); 231 } 232 ta.disconnect(transaction,shandle); 233 234 AppParameter[] returnValues=aprs; 235 236 Map newData=new HashMap (); 238 for(int i = 0; i < returnValues.length; i++){ 239 if (returnValues[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT) || 240 returnValues[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) { 241 String name = returnValues[i].the_actual_name; 242 Object value = returnValues[i].the_value; 243 newData.put(name,value); 244 } 245 } 246 247 activity.set_result(transaction,newData); 248 249 } 250 251 } 252 253 } 254 255 256 | Popular Tags |