1 package org.enhydra.shark.toolagent; 2 3 import java.util.ArrayList ; 4 5 import org.enhydra.shark.api.RootException; 6 import org.enhydra.shark.api.SharkTransaction; 7 import org.enhydra.shark.api.internal.toolagent.AppParameter; 8 import org.enhydra.shark.api.internal.toolagent.ApplicationBusy; 9 import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined; 10 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted; 11 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStopped; 12 import org.enhydra.shark.api.internal.toolagent.ConnectFailed; 13 import org.enhydra.shark.api.internal.toolagent.InvalidProcessInstance; 14 import org.enhydra.shark.api.internal.toolagent.InvalidSessionHandle; 15 import org.enhydra.shark.api.internal.toolagent.InvalidToolAgentHandle; 16 import org.enhydra.shark.api.internal.toolagent.InvalidWorkitem; 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.utilities.SizeLimitedToolAgentMap; 22 import org.enhydra.shark.xpdl.XMLUtil; 23 import org.enhydra.shark.xpdl.XPDLConstants; 24 import org.enhydra.shark.xpdl.elements.ExtendedAttribute; 25 import org.enhydra.shark.xpdl.elements.ExtendedAttributes; 26 27 34 public abstract class AbstractToolAgent implements ToolAgent { 35 36 public static final long APP_STATUS_INVALID=-1; 37 public static final long APP_STATUS_RUNNING=0; 38 public static final long APP_STATUS_ACTIVE=1; 39 public static final long APP_STATUS_WAITING=2; 40 public static final long APP_STATUS_TERMINATED=3; 41 public static final long APP_STATUS_FINISHED=4; 42 43 public static final String APP_NAME_EXT_ATTR_NAME="AppName"; 44 public static final String APP_MODE_EXT_ATTR_NAME="AppMode"; 45 46 protected String username; 47 protected String password; 48 protected String engineName; 49 protected String scope; 50 protected SessionHandle shandle; 51 52 protected long handle; 53 protected String appName; 54 protected String procInstId; 55 protected String assId; 56 protected AppParameter[] parameters; 57 protected Integer appMode; 58 protected long status=AbstractToolAgent.APP_STATUS_ACTIVE; 59 60 protected CallbackUtilities cus; 61 62 protected static SizeLimitedToolAgentMap extAttributes=null; 63 64 public void configure (CallbackUtilities cus) throws RootException { 65 this.cus=cus; 66 67 if (AbstractToolAgent.extAttributes==null) { 68 int eacs=-1; 69 try { 70 eacs=Integer.parseInt(cus.getProperty("AbstractToolAgent.extAttribsCacheSize","-1")); 71 } catch (Exception ex) {} 72 AbstractToolAgent.extAttributes=new SizeLimitedToolAgentMap(eacs); 73 } 74 } 75 76 77 public SessionHandle connect (SharkTransaction t, 78 String userId, 79 String password, 80 String engineName, 81 String scope) 82 throws ConnectFailed, ToolAgentGeneralException { 83 84 this.username=userId; 85 this.password=password; 86 this.engineName=engineName; 87 this.scope=scope; 88 shandle=new SessionHandleImpl(0,""); 89 return shandle; 90 } 91 92 public void disconnect (SharkTransaction t,SessionHandle shandle) 93 throws InvalidSessionHandle, ToolAgentGeneralException { 94 95 if (!this.shandle.equals(shandle)) throw new InvalidSessionHandle(); 96 } 97 98 public void invokeApplication (SharkTransaction t, 99 long handle, 100 String applicationName, 101 String procInstId, 102 String assId, 103 AppParameter[] parameters, 104 Integer appMode) 105 throws ApplicationNotStarted, ApplicationNotDefined, 106 ApplicationBusy, ToolAgentGeneralException { 107 108 this.handle=handle; 109 this.appName=applicationName; 110 this.procInstId=procInstId; 111 this.assId=assId; 112 this.parameters=parameters; 113 this.appMode=appMode; 114 this.status=AbstractToolAgent.APP_STATUS_ACTIVE; 115 116 } 117 118 public long requestAppStatus (SharkTransaction t, 119 long handle, 120 String procInstId, 121 String assId, 122 AppParameter[] parameters) 123 throws ApplicationBusy, InvalidToolAgentHandle, 124 InvalidWorkitem, InvalidProcessInstance, ToolAgentGeneralException { 125 126 if (this.handle==handle) { 127 if (!(status==APP_STATUS_FINISHED || status==APP_STATUS_TERMINATED || 129 status==APP_STATUS_INVALID)) { 130 throw new ApplicationBusy(); 132 } 133 if (status!=APP_STATUS_INVALID) { 134 copyParams(getReturnParameters(),parameters); 135 } 137 return status; 138 } else { 139 throw new InvalidToolAgentHandle("TA "+getClass().getName()+" - Can't find app for [handle="+handle+",pId="+procInstId+",assId="+assId+"]"); 140 } 141 } 142 143 public void terminateApp (SharkTransaction t, 144 long handle, 145 String procInstId, 146 String assId) 147 throws ApplicationNotStopped, InvalidWorkitem, 148 InvalidProcessInstance, ApplicationBusy, ToolAgentGeneralException { 149 cus.info("Terminating tool agent applications not implemented !!!"); 150 throw new ApplicationNotStopped(); 151 } 152 153 public String getInfo (SharkTransaction t) throws ToolAgentGeneralException { 154 return ""; 155 } 156 157 protected ExtendedAttributes readParamsFromExtAttributes (String extAttribs) throws Exception { 158 ExtendedAttributes eas=(ExtendedAttributes)AbstractToolAgent.extAttributes.get(extAttribs); 159 if (eas==null) { 160 if (extAttribs!=null && !extAttribs.trim().equals("")) { 161 try { 162 eas=XMLUtil.destringyfyExtendedAttributes(extAttribs); 163 eas.setReadOnly(true); 164 eas.initCaches(); 165 } catch (Throwable ex) { 166 System.out.println("FAiled to destr"); 167 cus.warn("AbstractToolAgent -> "+ex.getMessage()); 168 } 169 } 170 171 AbstractToolAgent.extAttributes.put(extAttribs,eas); 173 } 175 ExtendedAttribute ea=eas.getFirstExtendedAttributeForName(AbstractToolAgent.APP_NAME_EXT_ATTR_NAME); 176 if (ea!=null) { 177 appName=ea.getVValue(); 178 } 179 ea=eas.getFirstExtendedAttributeForName(AbstractToolAgent.APP_MODE_EXT_ATTR_NAME); 180 if (ea!=null) { 181 try { 182 appMode=new Integer (Integer.parseInt(ea.getVValue())); 183 } catch (Exception ex){} 184 } 185 return eas; 186 } 187 188 protected AppParameter[] getReturnParameters () { 189 ArrayList returnValues = new ArrayList (); 191 if (parameters!=null) { 192 for(int i = 0; i < parameters.length; i++){ 193 if(parameters[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT) || 194 parameters[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) { 195 returnValues.add(parameters[i]); 196 } 197 } 198 } 199 AppParameter[] retParameters=(AppParameter[])returnValues.toArray(new AppParameter[returnValues.size()]); 200 201 return retParameters; 202 } 203 204 public static void copyParams (AppParameter[] taApps,AppParameter[] apps) { 205 if (taApps!=null) { 206 for (int i=0; i<taApps.length; i++) { 207 AppParameter taApp=taApps[i]; 208 for (int j=0; j<apps.length; j++) { 209 if (apps[j].the_actual_name.equals(taApp.the_actual_name)) { 210 apps[j].the_value=taApp.the_value; 211 } 212 } 213 } 214 } 215 } 216 217 } 218 | Popular Tags |