//
// This file is part of the prose package.
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is prose.
//
// The Initial Developer of the Original Code is Andrei Popovici. Portions
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
// All Rights Reserved.
//
// Contributor(s):
// $Id: CommandlineProseClient.java,v 1.4 2008/11/18 11:43:39 anicoara Exp $
// =====================================================================
//
package ch.ethz.prose.tools;
// used packages
import ch.ethz.prose.query.AspectSurrogate;
import ch.ethz.prose.Aspect;
import ch.ethz.inf.net.WebServer;
import java.io.File;
import java.util.Vector;
/**
* Class CommandlineProseClient
*
* @version $Revision: 1.4 $
* @author Andrei Popovici
*/
public class CommandlineProseClient {
private static void usage() {
System.err.println("jprosecl -Dinsert=<class> -Dwithdraw=<id> -list -Dprose.address=<host>:<port>/(activeInstance|testInstance)");
}
private static String doGetScriptPath() {
String dotExe="";
if ("x86".equals(System.getProperty("os.arch","NOWINDOWS")))
dotExe=".exe";
String proseTop = System.getProperty("ch.ethz.inf.project.home");
if (proseTop == null)
throw new RuntimeException("expecting the property 'ch.ethz.inf.project.home' to be set;" +
"Probable cause: this method is not called from a prose-enabled jvm (jprose)");
File executable = new File (proseTop +
System.getProperty("file.separator","/") +
"programs" +
System.getProperty("file.separator","/") +
"clprose" + dotExe);
if (!executable.exists()) {
System.err.println("executable:" + executable);
executable = new File(proseTop +
System.getProperty("file.separator","/") +
"bin" +
System.getProperty("file.separator","/") +
"clprose" + dotExe);
}
return executable.toString();
}
private static Vector doGetCommonArgs(String address, String txid, boolean isReal) {
Vector arguments = new Vector();
if (txid != null) {
arguments.add("--txid");
arguments.add(txid);
}
arguments.add("--address");
arguments.add(address);
if (!isReal)
arguments.add("--test");
return arguments;
}
public static String[] insertScriptCommandline(String classpath,String aspectClassName, Object insertId, String address, String txid,boolean isReal) throws java.io.IOException {
Vector arguments = new Vector();
arguments.add(doGetScriptPath());
arguments.add("-classpath");
arguments.add(classpath);
arguments.add("--insert");
arguments.add(aspectClassName);
if (insertId != null) {
arguments.add("--insertId");
arguments.add(insertId.toString());
}
arguments.addAll(doGetCommonArgs(address,txid,isReal));
return (String[])arguments.toArray(new String[]{});
}
public static void main(String[] args) {
int exitCode=1; // assume there will be an error
WebServer server=WebServer.allowRMIPeersToRetrieveByteCode();
String insertId = System.getProperty("insertId",null);
String remoteProse = System.getProperty("prose.address","NONE");
String aspectToInsert = System.getProperty("insert","NONE");
String aspectToWithdraw=System.getProperty("withdraw","NONE");
String transactionId=System.getProperty("txId",null);
String finishTxAction=System.getProperty("finishtx","NONE");
String list=System.getProperty("list","NONE");
try {
String host = null;
int port = -1;
try {
host = remoteProse.substring(0,remoteProse.indexOf(':'));
String portName = remoteProse.substring(remoteProse.indexOf(':') +1, remoteProse.length());
port = Integer.parseInt(portName);
}
catch (java.lang.StringIndexOutOfBoundsException e) {
throw new java.net.MalformedURLException("Missing ':' separator");
}
catch (java.lang.NumberFormatException e) {
throw new java.net.MalformedURLException("Illegal port number");
}
String instance = System.getProperty("prose.instance");
RemoteAspectManager bothRams[] = RemoteProseComponent.doGetRemoteAspectManagers(host,port);
RemoteAspectManager ram = null;
if ("activeInstance".equals(instance))
ram = bothRams[0];
else if ("testInstance".equals(instance))
ram = bothRams[1];
else
throw new Error("instance must be specified by script!");
if ("NONE".equals(remoteProse)) {
System.err.println("no host:port specified");
usage();
System.exit(1);
}
if ("commit".equals(finishTxAction)) {
if (transactionId == null)
throw new IllegalArgumentException("transaction id must be specified");
ram.commit(transactionId);
}
if ("abort".equals(finishTxAction)) {
if (transactionId == null)
throw new IllegalArgumentException("transaction id must be specified");
ram.abort(transactionId);
}
if ( !("NONE".equals(aspectToInsert))) {
// we have to insert an aspect
// assumption : no constructor
Class cls = Class.forName(aspectToInsert);
Aspect aspect = (Aspect)cls.newInstance();
if (insertId != null)
aspect.associateTo(insertId);
// obtain remote prose reference
if (transactionId == null)
ram.insert(aspect);
else
ram.insert(aspect,transactionId);
}
if ( !("NONE".equals(aspectToWithdraw)) ) {
Object[] array=ram.allAspects().toArray();
int withdrawIdx = Integer.parseInt(aspectToWithdraw);
if (transactionId == null)
ram.withdraw((AspectSurrogate)array[withdrawIdx]);
else
ram.withdraw((AspectSurrogate)array[withdrawIdx],transactionId);
}
if ( !("NONE".equals(list)) ) {
Object[] array=ram.allAspects().toArray();
for (int i = 0; i < array.length; i++)
System.err.println("Aspect[" + i + "]: " + array[i]);
}
server.stop();
exitCode =0;
}
catch (java.lang.ClassNotFoundException cannotFoundClass) {
System.err.println("prose: *Error* The class '" + aspectToInsert + "' could not be found in the classpath");
}
catch (java.lang.IllegalAccessException cannotCallConstructor) {
System.err.println("prose: *Error* The class " + aspectToInsert + "' cannot be instantiated. Please check"+
" that the constructor (paramterless) has the correct access rights");
}
catch (java.net.UnknownHostException wrongHostName) {
System.err.println("prose: *Error* The prose service you specified does not exist at " + remoteProse);
}
catch (java.rmi.ConnectException serverNotThere) {
System.err.println("prose: *Error* The prose service you specified does not exist at " + remoteProse);
}
catch (java.lang.InstantiationException cannotCreateAspect) {
System.err.println("prose: *Error* The aspect class you specified (" + aspectToInsert + ") " +
" cannot be instantiated");
}
catch (java.rmi.RemoteException cannotConnect) {
System.err.println("prose: *Error* The following exception occured while trying to connect to"+
" the service " + cannotConnect+ ":\n");
}
catch (java.net.MalformedURLException shitFromUser) {
System.err.println("prose: *Error* The address you specified does not have the format <host>:<port>");
}
catch (java.lang.IllegalArgumentException missingArgs) {
System.err.println("prose: *Error* " + missingArgs.getMessage());
}
catch (java.io.IOException cannotTalkToRemoteProse) {
System.err.println("prose: *Error* " + cannotTalkToRemoteProse.getMessage());
}
finally {
server.stop();
}
System.exit(exitCode);
}
}