1 package org.enhydra.shark.test; 2 3 import org.omg.WfBase.NameValue; 4 import org.omg.WfBase.NameValueInfo; 5 import org.omg.WorkflowModel.WfProcess; 6 import org.omg.WorkflowModel.WfProcessMgr; 7 import org.enhydra.shark.corba.WorkflowService.ConnectFailed; 8 import org.enhydra.shark.corba.WorkflowService.ExecutionAdministration; 9 import org.enhydra.shark.corba.WorkflowService.SharkConnection; 10 import org.enhydra.shark.corba.WorkflowService.SharkInterface; 11 import org.enhydra.shark.corba.WorkflowService.SharkInterfaceHelper; 12 import java.io.ByteArrayOutputStream ; 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.OutputStream ; 16 import java.io.PrintStream ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Properties ; 21 import org.omg.CORBA.Any ; 22 import org.omg.CORBA.ORB ; 23 import org.omg.CosNaming.NameComponent ; 24 import org.omg.CosNaming.NamingContext ; 25 import org.omg.CosNaming.NamingContextHelper ; 26 27 public class CORBAProcStartClient { 28 29 static final String HOST_PROPERTY_NAME="host"; 30 static final String PORT_PROPERTY_NAME="port"; 31 static final String ENGINENAME_PROPERTY_NAME="enginename"; 32 static final String USERNAME_PROPERTY_NAME="username"; 33 static final String PASSWORD_PROPERTY_NAME="password"; 34 35 static String lastStatusString=""; 36 37 private String confFilePath; 38 private ORB orb; 39 private SharkInterface shark; 40 private SharkConnection sc; 41 private ExecutionAdministration ea; 42 private String statusString; 43 44 private Properties props=new Properties (); 45 46 private String host; 47 private String port; 48 private String enginename; 49 private String username; 50 private String password; 51 52 protected CORBAProcStartClient (String confFilePath) { 53 this.confFilePath=confFilePath; 54 } 55 56 protected boolean init () { 57 statusString=""; 58 try { 59 CORBAProcStartClient.configure(props,confFilePath); 60 61 host=props.getProperty(CORBAProcStartClient.HOST_PROPERTY_NAME); 62 port=props.getProperty(CORBAProcStartClient.PORT_PROPERTY_NAME); 63 enginename=props.getProperty(CORBAProcStartClient.ENGINENAME_PROPERTY_NAME); 64 username=props.getProperty(CORBAProcStartClient.USERNAME_PROPERTY_NAME); 65 password=props.getProperty(CORBAProcStartClient.PASSWORD_PROPERTY_NAME); 66 67 String host=props.getProperty(CORBAProcStartClient.HOST_PROPERTY_NAME); 68 String port=props.getProperty(CORBAProcStartClient.PORT_PROPERTY_NAME); 69 String enginename=props.getProperty(CORBAProcStartClient.ENGINENAME_PROPERTY_NAME); 70 String username=props.getProperty(CORBAProcStartClient.USERNAME_PROPERTY_NAME); 71 String password=props.getProperty(CORBAProcStartClient.PASSWORD_PROPERTY_NAME); 72 73 if (host==null) { 74 throw new Exception ("host - property is not specified in configuration file"); 75 } 76 if (port==null) { 77 throw new Exception ("port - property is not specified in configuration file"); 78 } 79 if (enginename==null) { 80 throw new Exception ("enginename - property is not specified in configuration file"); 81 } 82 if (username==null) { 83 throw new Exception ("username - property is not specified in configuration file"); 84 } 85 if (password==null) { 86 throw new Exception ("password - property is not specified in configuration file"); 87 } 88 89 String [] args={"-ORBInitialHost",host,"-ORBInitialPort",port}; 90 orb = ORB.init(args,null); 92 org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); 94 NamingContext ncRef = NamingContextHelper.narrow(objRef); 95 96 NameComponent nc = new NameComponent (enginename,""); 98 NameComponent path[] = {nc}; 99 shark=SharkInterfaceHelper.narrow(ncRef.resolve(path)); 100 101 sc=shark.getSharkConnection(); 102 ea=shark.getExecutionAdministration(); 103 sc.connect(username,password,"",""); 104 ea.connect(username,password,"",""); 105 return true; 106 } catch (Throwable thr) { 107 if (host==null || port==null || enginename==null || username==null || password==null) { 108 statusString=thr.getMessage(); 109 } else if (shark==null) { 110 statusString="Connection to shark server is not established - maybe the server is down, or the connection parameters are wrong!"; 111 } else if (thr instanceof ConnectFailed) { 112 statusString="Connection to shark server is not established - wrong username or password!"; 113 } else { 114 statusString=thr.getMessage(); 115 } 116 return false; 117 } 118 } 119 120 protected boolean startProcess (String pkgId,String pDefId,Map vars) { 121 statusString=""; 122 WfProcessMgr mgr=null; 123 boolean mgrOK=false; 124 125 try { 126 mgr=ea.getProcessMgrByXPDLDefinition(pkgId,pDefId); 127 128 WfProcess proc=sc.createProcess(pkgId,pDefId); 129 130 NameValueInfo[] cs=mgr.context_signature(); 131 mgrOK=true; 132 if (vars!=null && vars.size()>0) { 134 NameValue[] cntxt=new NameValue[vars.size()]; 135 Iterator it=vars.entrySet().iterator(); 136 int i=0; 137 while(it.hasNext()) { 138 Map.Entry me=(Map.Entry )it.next(); 139 String id=(String )me.getKey(); 140 java.lang.Object val=me.getValue(); 141 Any any=orb.create_any(); 142 String clsName=getClassName(cs,id); 143 if (clsName==null) { 144 throw new Exception ("The process definition knows nothing about variable "+id+" - you can't put it into process context"); 145 } 146 if (!clsName.equals(val.getClass().getName())) { 147 throw new Exception ("You are trying to set wrong type "+val.getClass().getName()+" for variable "+id+" which type is "+clsName); 148 } 149 150 if (val instanceof Long ) { 151 any.insert_longlong(((Long )val).longValue()); 152 } else if (val instanceof Double ) { 153 any.insert_double(((Double )val).doubleValue()); 154 } else if (val instanceof Boolean ) { 155 any.insert_boolean(((Boolean )val).booleanValue()); 156 } else if (val instanceof String ) { 157 any.insert_wstring((String )val); 158 } else { 159 throw new Exception ("Unsupported type "+val.getClass().getName()+" for variable "+id); 160 } 161 cntxt[i]=new NameValue(); 162 cntxt[i].the_name=id; 163 cntxt[i].the_value=any; 164 i++; 165 } 166 proc.set_process_context(cntxt); 167 } 168 proc.start(); 169 return true; 170 } catch (Throwable thr) { 171 if (!mgrOK) { 172 lastStatusString="Improper package or process definition Id!"; 173 } else { 174 statusString=thr.getMessage(); 175 if (statusString==null || statusString.trim().equals("")) { 176 OutputStream os=new ByteArrayOutputStream (); 178 PrintStream ps=new PrintStream (os); 179 thr.printStackTrace(ps); 180 ps.close(); 181 statusString=os.toString(); 182 } 183 } 184 } 185 return false; 186 } 187 188 public String getStatusString () { 189 return statusString; 190 } 191 192 public static boolean run (String [] args) { 193 if (args==null || args.length<3) { 194 lastStatusString="Invalid number of parameters - must be at least 3 parameters!"; 195 return false; 196 } 197 198 String pkgId=args[1]; 199 String pDefId=args[2]; 200 201 WfProcessMgr mgr=null; 202 boolean mgrOK=false; 203 try { 204 CORBAProcStartClient cpsc=new CORBAProcStartClient(args[0]); 205 boolean isOK=cpsc.init(); 206 if (!isOK) { 207 lastStatusString=cpsc.getStatusString(); 208 return false; 209 } 210 mgr=cpsc.ea.getProcessMgrByXPDLDefinition(pkgId,pDefId); 211 212 NameValueInfo[] cs=mgr.context_signature(); 213 mgrOK=true; 214 Map cntx=new HashMap (); 216 if (args.length>3) { 217 for (int i=3; i<args.length; i++) { 218 String cnt=args[i]; 219 int ind=cnt.indexOf("="); 220 String id=cnt.substring(0,ind); 221 String val=cnt.substring(ind+1,cnt.length()); 222 if (getClassName(cs,id).equals("java.lang.Boolean")) { 223 if (!val.equalsIgnoreCase("true") && !val.equalsIgnoreCase("false")) { 224 throw new Exception ("The value for Boolean variable "+id+" is not set properly - it must be true or false"); 225 } else { 226 cntx.put(id,new Boolean (val)); 227 } 228 } else if (getClassName(cs,id).equals("java.lang.Long")) { 229 cntx.put(id,new Long (val)); 230 } else if (getClassName(cs,id).equals("java.lang.Double")) { 231 cntx.put(id,new Double (val)); 232 } else if (getClassName(cs,id).equals("java.lang.String")) { 233 cntx.put(id,val); 234 } else { 235 throw new Exception ("Can't init variable which type is "+getClassName(cs,id).equals("java.lang.String")); 236 } 237 } 238 } 239 isOK=cpsc.startProcess(pkgId,pDefId,cntx); 240 if (!isOK) { 241 lastStatusString=cpsc.getStatusString(); 242 return false; 243 } 244 return true; 245 } catch (Throwable ex) { 246 if (!mgrOK) { 247 lastStatusString="Improper package or process definition Id!"; 248 } else { 249 lastStatusString=ex.getMessage(); 250 } 251 return false; 252 } 253 254 } 255 256 public static String getLastStatusString () { 257 return lastStatusString; 258 } 259 260 public static void main (String [] args) { 261 if (args==null || args.length<3) { 262 printUsage(); 263 return; 264 } 265 266 if (!CORBAProcStartClient.run(args)) { 267 System.out.println("Error message: "+CORBAProcStartClient.getLastStatusString()+"\n"); 268 printUsage(); 269 } 270 271 } 272 273 static void configure (Properties props,String filePath) throws Exception { 274 if (filePath==null) { 275 throw new Exception ("Client need to be configured properly - given path to configuration file is null!!!"); 276 } 277 File configFile=new File (filePath); 278 if (!configFile.isAbsolute()) { 279 configFile=configFile.getAbsoluteFile(); 280 } 281 if (configFile.exists()) { 282 FileInputStream fis=null; 283 fis=new FileInputStream (configFile); 284 props.load(fis); 285 fis.close(); 286 } else { 287 throw new Exception ("Client need to be configured properly - configuration file "+configFile+" does not exist!!!"); 288 } 289 } 290 291 static String getClassName (NameValueInfo[] nvia,String varId) { 292 if (nvia!=null && nvia.length>0) { 293 for (int i=0; i<nvia.length; i++) { 294 NameValueInfo nvi=nvia[i]; 295 if (nvi.attribute_name.equals(varId)) { 296 return nvi.type_name; 297 } 298 } 299 } 300 return null; 301 } 302 303 static void printUsage () { 304 System.err.println("This application is used to start shark's process through the CORBA."); 305 System.err.println("It is supposed that CORBA server is up, and that corresponding xpdl is loaded into shark."); 306 System.err.println(); 307 System.err.println("usage: java org.enhydra.shark.test.CORBAProcStartClient configFilePath pkgId pDefId [var1=val1 [var2=val2 ... ]]"); 308 System.err.println(); 309 System.err.println("arguments:"); 310 System.err.println(" configFilePath the path to the configuration file."); 311 System.err.println(" pkgId the id of xpdl package."); 312 System.err.println(" pDefId the id of xpdl process definition."); 313 System.err.println(" vari=vali the process variable id and its value."); 314 System.err.println(); 315 System.err.println("NOTE: the configuration file should contain name-value pairs, and following should be specified:"); 316 System.err.println(" - host the name of CORBA name server host."); 317 System.err.println(" - port the port number for the CORBA name server."); 318 System.err.println(" - enginename the name of shark server instance (the one registerd with nameserver)."); 319 System.err.println(" - username username credential to connect to shark server."); 320 System.err.println(" - password password credential to connect to shark server."); 321 System.err.println(); 322 System.err.println("NOTE: variable value will be interpreted as:"); 323 System.err.println(" Boolean - if you enter 'true' or 'false',"); 324 System.err.println(" Long - if you enter only digits,"); 325 System.err.println(" Double - if you enter only digits and full-stop,"); 326 System.err.println(" String - otherwise."); 327 } 328 } 329 330 | Popular Tags |