1 5 package org.enhydra.shark; 6 7 import java.util.List ; 8 import java.util.ArrayList ; 9 import java.util.Properties ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 import java.util.HashMap ; 13 import java.util.Collection ; 14 15 import org.enhydra.shark.api.internal.working.ToolAgentManager; 16 import org.enhydra.shark.api.internal.working.WfActivityInternal; 17 import org.enhydra.shark.api.internal.working.CallbackUtilities; 18 import org.enhydra.shark.api.internal.working.WfProcessInternal; 19 import org.enhydra.shark.api.internal.toolagent.AppParameter; 20 import org.enhydra.shark.api.internal.toolagent.SessionHandle; 21 import org.enhydra.shark.api.internal.toolagent.ToolAgent; 22 import org.enhydra.shark.api.internal.toolagent.ConnectFailed; 23 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException; 24 import org.enhydra.shark.api.internal.appmappersistence.ApplicationMap; 25 import org.enhydra.shark.api.internal.appmappersistence.ApplicationMappingManager; 26 import org.enhydra.shark.api.SharkTransaction; 27 import org.enhydra.shark.api.RootException; 28 import org.enhydra.shark.api.TransactionException; 29 import org.enhydra.shark.api.ApplicationMappingTransaction; 30 import org.enhydra.shark.api.client.wfbase.BaseException; 31 import org.enhydra.shark.api.client.wfmodel.InvalidData; 32 import org.enhydra.shark.api.client.wfmodel.UpdateNotAllowed; 33 import org.enhydra.shark.api.client.wfmodel.CannotComplete; 34 import org.enhydra.shark.xpdl.elements.Tool; 35 import org.enhydra.shark.xpdl.elements.WorkflowProcess; 36 import org.enhydra.shark.xpdl.elements.Application; 37 import org.enhydra.shark.xpdl.elements.ActualParameters; 38 import org.enhydra.shark.xpdl.elements.FormalParameters; 39 import org.enhydra.shark.xpdl.elements.FormalParameter; 40 import org.enhydra.shark.xpdl.elements.ActualParameter; 41 import org.enhydra.shark.xpdl.XMLComplexChoice; 42 import org.enhydra.shark.xpdl.XMLComplexElement; 43 44 49 public class ThreadedToolAgentManager implements ToolAgentManager { 50 51 private final static String TOOL_AGENT_PREFIX = "ToolAgent."; 52 private final static String DEFAULT_TOOL_AGENT = "DefaultToolAgent"; 53 private final static long APP_STATUS_INVALID = -1; 54 private static List toolMonitors = new ArrayList (); 55 56 private String defaultToolAgentClassName = null; 57 private List toolAgents = null; 58 private CallbackUtilities cus = null; 59 60 ThreadedToolAgentManager() { 61 this.cus = SharkEngineManager.getInstance().getCallbackUtilities(); 62 createToolAgentList(); 63 } 64 65 public void executeActivity(SharkTransaction t, WfActivityInternal act) throws BaseException, ToolAgentGeneralException { 66 ThreadedToolAgentManager.toolMonitors.add(new ToolRunnerManager(t, act)); 67 } 68 69 72 public String [] getDefinedToolAgents() { 73 String [] ata = new String [toolAgents.size()]; 74 toolAgents.toArray(ata); 75 return ata; 76 } 77 78 private void createToolAgentList() { 79 this.toolAgents = new ArrayList (); 80 String taName = null; 81 String className = null; 82 Properties props = cus.getProperties(); 83 84 Iterator it = props.entrySet().iterator(); 85 while (it.hasNext()) { 86 try { 87 Map.Entry me = (Map.Entry ) it.next(); 88 taName = me.getKey().toString(); 89 if (taName.startsWith(TOOL_AGENT_PREFIX)) { 90 taName = taName.substring(TOOL_AGENT_PREFIX.length()); 91 className = me.getValue().toString(); 92 toolAgents.add(className); 93 } 94 } catch (Throwable ex) { 95 cus.error("ToolAgentManager -> Creation of Tool Agent " + taName + " from clas " + className + " failed !!!"); 97 } 98 } 99 100 try { 102 defaultToolAgentClassName = (String ) props.get(DEFAULT_TOOL_AGENT); 103 104 } catch (Throwable ex) { 105 cus.error("ToolAgentManager -> Creation of Default Tool Agent failed !!!"); 106 } 107 } 108 109 public static int howManyActivitiesRunning() { 110 return ThreadedToolAgentManager.toolMonitors.size(); 111 } 112 113 private synchronized static void removeToolMonitor(ToolRunnerManager monitor) { 114 ThreadedToolAgentManager.toolMonitors.remove(monitor); 115 } 116 117 class ToolContext { 118 119 private Tool tool; 120 private Application app; 121 private ApplicationMap appMap; 122 private String packageId; 123 private String processId; 124 private String activityId; 125 private String resource; 126 private AppParameter[] params; 127 128 public ToolContext(Tool tool, Application app, ApplicationMap appMap, String pkgId, 129 String procId, String actId, String resource, AppParameter[] params) { 130 this.tool = tool; 131 this.app = app; 132 this.appMap = appMap; 133 this.packageId = pkgId; 134 this.processId = procId; 135 this.activityId = actId; 136 this.resource = resource; 137 this.params = params; 138 } 139 140 public Tool getTool() { 141 return tool; 142 } 143 144 public Application getApplication() { 145 return app; 146 } 147 148 public ApplicationMap getApplicationMap() { 149 return appMap; 150 } 151 152 public String getPackageId() { 153 return packageId; 154 } 155 156 public String getProcessId() { 157 return processId; 158 } 159 160 public String getActivityId() { 161 return activityId; 162 } 163 164 public String getActivityResource() { 165 return resource; 166 } 167 168 public AppParameter[] getParameters() { 169 return params; 170 } 171 } 172 173 class ToolRunnerManager implements Runnable { 174 175 private String packageKey; 176 private String packageVer; 177 private String processKey; 178 private String activityKey; 179 private String resource; 180 private List tools; 181 private Map context; 182 183 private Map toolResults; 184 private List runners; 185 private Thread thread; 186 private boolean isRunning = false; 187 188 public ToolRunnerManager(SharkTransaction trans, WfActivityInternal activity) throws BaseException { 189 this.packageKey = activity.container(trans).package_id(trans); 190 this.packageVer = activity.container(trans).manager_version(trans); 191 this.processKey = activity.container(trans).key(trans); 192 this.packageKey = activity.container(trans).package_id(trans); 193 this.activityKey = activity.key(trans); 194 this.resource = activity.getResourceUsername(trans); 195 this.context = activity.process_context(trans); 196 this.tools = this.getTools(trans, activity); 197 this.toolResults = new HashMap (); 198 this.runners = new ArrayList (); 199 200 thread = new Thread (this); 202 thread.setDaemon(false); 203 thread.setName(this.getClass().getName()); 204 thread.start(); 205 } 206 207 private Collection getToolObjs(SharkTransaction trans, WfActivityInternal activity) throws BaseException { 208 WfProcessInternal pr = activity.container(trans); 209 WorkflowProcess wp = SharkUtilities.getWorkflowProcess(pr.manager(trans).package_id(trans), 210 pr.manager_version(trans), pr.manager(trans).process_definition_id(trans)); 211 212 return SharkUtilities.getActivityDefinition(trans, activity, wp, 213 activity.block_activity(trans)).getTools().toCollection(); 214 } 215 216 private List getTools(SharkTransaction trans, WfActivityInternal activity) throws BaseException { 217 Collection tools = getToolObjs(trans, activity); 218 219 List toolList = null; 220 if (tools != null) { 221 toolList = new ArrayList (); 222 Iterator i = tools.iterator(); 223 while (i.hasNext()) { 224 Tool tool = (Tool) i.next(); 225 String toolId = tool.getID(); 226 227 Application app = (Application) ((XMLComplexChoice) tool.get("Application")).getChoosen(); 228 ApplicationMap appMap = null; 229 try { 230 appMap = getApplicationMap(app, toolId); 231 } catch (Exception e) { 232 throw new BaseException(e); 233 } 234 235 AppParameter[] params = null; 236 try { 237 params = makeParameters(trans, tool, app); 238 } catch (Exception e) { 239 throw new BaseException(e); 240 } 241 ToolContext ctx = new ToolContext(tool, app, appMap, packageKey, processKey, activityKey, resource, params); 242 toolList.add(ctx); 243 } 244 } 245 return toolList; 246 } 247 248 private AppParameter[] makeParameters(SharkTransaction transaction, Tool tool, Application app) throws Exception { 249 List parameters = new ArrayList (); 251 252 String appPStr = app.getExtendedAttributesString(); 254 AppParameter param = new AppParameter("ExtendedAttributes", "ExtendedAttributes", AppParameter.MODE_IN, appPStr, String .class); 255 parameters.add(param); 256 257 ActualParameters aps = (ActualParameters) tool.get("ActualParameters"); 258 FormalParameters fps = (FormalParameters) ((XMLComplexChoice) app.get("Choice")).getChoosen(); 259 Map ctxMap = SharkUtilities.createContextMap(transaction, context, aps, fps, packageKey, packageVer); 260 261 Iterator itFps = fps.toCollection().iterator(); 262 Iterator itAps = aps.toCollection().iterator(); 263 while (itFps.hasNext() && itAps.hasNext()) { 264 FormalParameter fp = (FormalParameter) itFps.next(); 265 ActualParameter ap = (ActualParameter) itAps.next(); 266 String fpMode = fp.get("Mode").toValue().toString(); 267 String fpId = fp.getID(); 268 Object paramVal = ctxMap.get(fpId); 269 270 param = new AppParameter(ap.toValue().toString(), fpId, fpMode, paramVal, SharkUtilities.getJavaClass(fp)); 275 parameters.add(param); 276 } 277 278 return (AppParameter[]) parameters.toArray(new AppParameter[parameters.size()]); 279 } 280 281 private ApplicationMap getApplicationMap(Application app, String applicationId) throws Exception { 282 ApplicationMappingManager mm = SharkEngineManager.getInstance().getApplicationMapPersistenceManager(); 285 ApplicationMap tad = null; 286 if (mm != null) { 287 XMLComplexElement cOwn = app.getCollection().getOwner(); 288 boolean isProcessApp = (cOwn instanceof WorkflowProcess); 289 ApplicationMappingTransaction t = null; 290 try { 291 t = SharkUtilities.createApplicationMappingTransaction(); 292 tad = SharkEngineManager.getInstance().getApplicationMapPersistenceManager(). 293 getApplicationMap(t, app.getPackage().get("Id").toString(), 294 ((isProcessApp) ? cOwn.get("Id").toString() : null), applicationId); 295 } catch (RootException e) { 296 throw e; 297 } finally { 298 SharkUtilities.releaseMappingTransaction(t); 299 } 300 } 301 302 return tad; 303 } 304 305 public void run() { 306 this.isRunning = true; 307 308 Iterator ti = tools.iterator(); 310 while (ti.hasNext()) { 311 ToolContext tool = (ToolContext) ti.next(); 312 runners.add(new ToolRunner(tool)); 313 } 314 315 while (isRunning) { 317 Iterator ri = runners.iterator(); 319 while (ri.hasNext()) { 320 ToolRunner runner = (ToolRunner) ri.next(); 321 if (!runner.isRunning()) { 322 Throwable toolError = runner.getError(); 324 if (toolError != null) { 325 } else { 327 Map thisResult = runner.getResults(); 329 if (thisResult != null) { 330 toolResults.putAll(thisResult); 331 } 332 } 333 ri.remove(); 335 } 336 } 337 338 try { 340 Thread.sleep(5000); 341 } catch (InterruptedException e) { 342 } 343 } 344 345 SharkTransaction trans = null; 347 WfActivityInternal act = null; 348 349 try { 350 trans = SharkUtilities.createTransaction(); 351 act = SharkUtilities.getActivity(trans, processKey, activityKey); 352 act.set_process_context(trans, toolResults); 353 act.finish(trans); 354 } catch (TransactionException e) { 355 e.printStackTrace(); } catch (BaseException e) { 357 e.printStackTrace(); } catch (UpdateNotAllowed e) { 359 e.printStackTrace(); } catch (InvalidData e) { 361 e.printStackTrace(); } catch (CannotComplete e) { 363 e.printStackTrace(); } 365 366 ThreadedToolAgentManager.removeToolMonitor(this); 368 } 369 370 class ToolRunner implements Runnable { 371 372 private Thread thread; 373 private ToolContext tool; 374 375 private boolean isRunning = false; 376 private Throwable error = null; 377 private Map results = null; 378 379 ToolRunner(ToolContext tool) { 380 this.tool = tool; 381 382 thread = new Thread (this); 384 thread.setDaemon(false); 385 thread.setName(this.getClass().getName()); 386 thread.start(); 387 } 388 389 public void run() { 390 this.isRunning = true; 391 try { 392 this.results = this.runTool(null); 393 } catch (Exception e) { 394 this.error = e; 395 } 396 this.isRunning = false; 397 } 398 399 public boolean isRunning() { 400 return this.isRunning; 401 } 402 403 public Throwable getError() { 404 return this.error; 405 } 406 407 public Map getResults() { 408 return this.results; 409 } 410 411 private Map runTool(SharkTransaction transaction) throws Exception { 412 ApplicationMap tad = tool.getApplicationMap(); 414 415 String tacn = (tad != null) ? tad.getToolAgentClassName() : defaultToolAgentClassName; 417 String uname = (tad != null) ? tad.getUsername() : ""; 418 String pwd = (tad != null) ? tad.getPassword() : ""; 419 String appN = (tad != null) ? tad.getApplicationName() : ""; 420 Integer appM = (tad != null) ? tad.getApplicationMode() : null; 421 ToolAgent ta = SharkEngineManager.getInstance().getToolAgentFactory().createToolAgent(transaction, tacn); 422 423 SessionHandle shandle = null; 424 try { 426 shandle = ta.connect(transaction, uname, pwd, cus.getProperty("enginename", "imaobihostrezube"), ""); 427 } catch (ConnectFailed cf) { 428 cus.error("Activity[" + tool.getActivityId() + "] - connection to Tool agent " + tacn + " failed !"); 429 throw cf; 430 } 431 432 String assId = SharkUtilities.createAssignmentKey(tool.getActivityId(), resource); 433 434 ta.invokeApplication(transaction, shandle.getHandle(), appN, tool.getProcessId(), assId, tool.getParameters(), appM); 436 437 long appStatus = ta.requestAppStatus(transaction, shandle.getHandle(), tool.getProcessId(), assId, tool.getParameters()); 439 if (appStatus == APP_STATUS_INVALID) { 440 ta.disconnect(transaction, shandle); 441 throw new Exception (); 442 } 443 ta.disconnect(transaction, shandle); 444 445 AppParameter[] returnValues = tool.getParameters(); 447 Map newData = new HashMap (); 448 for (int i = 0; i < returnValues.length; i++) { 449 if (returnValues[i].the_mode.equals(AppParameter.MODE_OUT) || 450 returnValues[i].the_mode.equals(AppParameter.MODE_INOUT)) { 451 String name = returnValues[i].the_actual_name; 452 Object value = returnValues[i].the_value; 453 newData.put(name, value); 454 } 455 } 456 457 return newData; 458 } 459 } 460 } 461 } 462 | Popular Tags |