1 27 package ch.ethz.prose.tools; 28 29 31 import ch.ethz.prose.query.AspectSurrogate; 32 import ch.ethz.prose.Aspect; 33 import ch.ethz.inf.net.WebServer; 34 import java.io.File ; 35 import java.util.Vector ; 36 37 43 public 44 class CommandlineProseClient 45 { 46 47 private static void usage() 48 { 49 System.err.println("jprosecl -Dinsert=<class> -Dwithdraw=<id> -list -Dprose.address=<host>:<port>/(activeInstance|testInstance)"); 50 } 51 52 private static String doGetScriptPath() 53 { 54 String dotExe=""; 55 if ("x86".equals(System.getProperty("os.arch","NOWINDOWS"))) 56 dotExe=".exe"; 57 String proseTop = System.getProperty("ch.ethz.inf.project.home"); 58 if (proseTop == null) 59 throw new RuntimeException ("expecting the property 'ch.ethz.inf.project.home' to be set;" + 60 "Probable cause: this method is not called from a prose-enabled jvm (jprose)"); 61 File executable = new File (proseTop + 62 System.getProperty("file.separator","/") + 63 "programs" + 64 System.getProperty("file.separator","/") + 65 "clprose" + dotExe); 66 67 if (!executable.exists()) 68 { 69 System.err.println("executable:" + executable); 70 executable = new File (proseTop + 71 System.getProperty("file.separator","/") + 72 "bin" + 73 System.getProperty("file.separator","/") + 74 "clprose" + dotExe); 75 } 76 return executable.toString(); 77 } 78 79 private static Vector doGetCommonArgs(String address, String txid, boolean isReal) 80 { 81 Vector arguments = new Vector (); 82 if (txid != null) 83 { 84 arguments.add("--txid"); 85 arguments.add(txid); 86 } 87 arguments.add("--address"); 88 arguments.add(address); 89 if (!isReal) 90 arguments.add("--test"); 91 92 return arguments; 93 } 94 95 public static String [] insertScriptCommandline(String classpath,String aspectClassName, Object insertId, String address, String txid,boolean isReal) 96 throws java.io.IOException 97 { 98 Vector arguments = new Vector (); 99 arguments.add(doGetScriptPath()); 100 arguments.add("-classpath"); 101 arguments.add(classpath); 102 arguments.add("--insert"); 103 arguments.add(aspectClassName); 104 if (insertId != null) 105 { 106 arguments.add("--insertId"); 107 arguments.add(insertId.toString()); 108 } 109 arguments.addAll(doGetCommonArgs(address,txid,isReal)); 110 return (String [])arguments.toArray(new String []{}); 111 } 112 113 114 public static void main(String [] args) 115 { 116 int exitCode=1; 118 WebServer server=WebServer.allowRMIPeersToRetrieveByteCode(); 119 120 String insertId = System.getProperty("insertId",null); 121 String remoteProse = System.getProperty("prose.address","NONE"); 122 String aspectToInsert = System.getProperty("insert","NONE"); 123 String aspectToWithdraw=System.getProperty("withdraw","NONE"); 124 String transactionId=System.getProperty("txId",null); 125 String finishTxAction=System.getProperty("finishtx","NONE"); 126 String list=System.getProperty("list","NONE"); 127 128 129 130 try 131 { 132 String host = null; 133 int port = -1; 134 try 135 { 136 host = remoteProse.substring(0,remoteProse.indexOf(':')); 137 String portName = remoteProse.substring(remoteProse.indexOf(':') +1, remoteProse.length()); 138 port = Integer.parseInt(portName); 139 } 140 catch (java.lang.StringIndexOutOfBoundsException e) 141 { 142 throw new java.net.MalformedURLException ("Missing ':' separator"); 143 } 144 catch (java.lang.NumberFormatException e) 145 { 146 throw new java.net.MalformedURLException ("Illegal port number"); 147 } 148 149 150 String instance = System.getProperty("prose.instance"); 151 152 153 154 RemoteAspectManager bothRams[] = RemoteProseComponent.doGetRemoteAspectManagers(host,port); 155 RemoteAspectManager ram = null; 156 157 if ("activeInstance".equals(instance)) 158 ram = bothRams[0]; 159 else if ("testInstance".equals(instance)) 160 ram = bothRams[1]; 161 else 162 throw new Error ("instance must be specified by script!"); 163 164 if ("NONE".equals(remoteProse)) 165 { 166 System.err.println("no host:port specified"); 167 usage(); 168 System.exit(1); 169 } 170 171 if ("commit".equals(finishTxAction)) 172 { 173 if (transactionId == null) 174 throw new IllegalArgumentException ("transaction id must be specified"); 175 176 ram.commit(transactionId); 177 } 178 179 if ("abort".equals(finishTxAction)) 180 { 181 if (transactionId == null) 182 throw new IllegalArgumentException ("transaction id must be specified"); 183 184 ram.abort(transactionId); 185 } 186 187 if ( !("NONE".equals(aspectToInsert))) 188 { 189 Class cls = Class.forName(aspectToInsert); 192 Aspect aspect = (Aspect)cls.newInstance(); 193 if (insertId != null) 194 aspect.associateTo(insertId); 195 196 if (transactionId == null) 198 ram.insert(aspect); 199 else 200 ram.insert(aspect,transactionId); 201 } 202 203 if ( !("NONE".equals(aspectToWithdraw)) ) 204 { 205 Object [] array=ram.allAspects().toArray(); 206 int withdrawIdx = Integer.parseInt(aspectToWithdraw); 207 if (transactionId == null) 208 ram.withdraw((AspectSurrogate)array[withdrawIdx]); 209 else 210 ram.withdraw((AspectSurrogate)array[withdrawIdx],transactionId); 211 } 212 213 if ( !("NONE".equals(list)) ) 214 { 215 Object [] array=ram.allAspects().toArray(); 216 for (int i = 0; i < array.length; i++) 217 System.err.println("Aspect[" + i + "]: " + array[i]); 218 } 219 server.stop(); 220 exitCode =0; 221 } 222 catch (java.lang.ClassNotFoundException cannotFoundClass) 223 { 224 System.err.println("prose: *Error* The class '" + aspectToInsert + "' could not be found in the classpath"); 225 } 226 catch (java.lang.IllegalAccessException cannotCallConstructor) 227 { 228 System.err.println("prose: *Error* The class " + aspectToInsert + "' cannot be instantiated. Please check"+ 229 " that the constructor (paramterless) has the correct access rights"); 230 } 231 catch (java.net.UnknownHostException wrongHostName) 232 { 233 System.err.println("prose: *Error* The prose service you specified does not exist at " + remoteProse); 234 } 235 catch (java.rmi.ConnectException serverNotThere) 236 { 237 System.err.println("prose: *Error* The prose service you specified does not exist at " + remoteProse); 238 } 239 catch (java.lang.InstantiationException cannotCreateAspect) 240 { 241 System.err.println("prose: *Error* The aspect class you specified (" + aspectToInsert + ") " + 242 " cannot be instantiated"); 243 } 244 catch (java.rmi.RemoteException cannotConnect) 245 { 246 System.err.println("prose: *Error* The following exception occured while trying to connect to"+ 247 " the service " + cannotConnect+ ":\n"); 248 } 249 catch (java.net.MalformedURLException shitFromUser) 250 { 251 System.err.println("prose: *Error* The address you specified does not have the format <host>:<port>"); 252 } 253 catch (java.lang.IllegalArgumentException missingArgs) 254 { 255 System.err.println("prose: *Error* " + missingArgs.getMessage()); 256 } 257 catch (java.io.IOException cannotTalkToRemoteProse) 258 { 259 System.err.println("prose: *Error* " + cannotTalkToRemoteProse.getMessage()); 260 } 261 finally 262 { 263 server.stop(); 264 } 265 System.exit(exitCode); 266 } 267 } 268 269 270 | Popular Tags |