001: //
002: // This file is part of the prose package.
003: //
004: // The contents of this file are subject to the Mozilla Public License
005: // Version 1.1 (the "License"); you may not use this file except in
006: // compliance with the License. You may obtain a copy of the License at
007: // http://www.mozilla.org/MPL/
008: //
009: // Software distributed under the License is distributed on an "AS IS" basis,
010: // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: // for the specific language governing rights and limitations under the
012: // License.
013: //
014: // The Original Code is prose.
015: //
016: // The Initial Developer of the Original Code is Andrei Popovici. Portions
017: // created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
018: // All Rights Reserved.
019: //
020: // Contributor(s):
021: // $Id: CommandlineProseClient.java,v 1.2 2003/07/10 14:59:19 apopovic Exp $
022: // =====================================================================
023: //
024: // (history at end)
025: //
026:
027: package ch.ethz.prose.tools;
028:
029: // used packages
030:
031: import ch.ethz.prose.query.AspectSurrogate;
032: import ch.ethz.prose.Aspect;
033: import ch.ethz.inf.net.WebServer;
034: import java.io.File;
035: import java.util.Vector;
036:
037: /**
038: * Class CommandlineProseClient XXX
039: *
040: * @version $Revision: 1.2 $
041: * @author Andrei Popovici
042: */
043: public class CommandlineProseClient {
044:
045: private static void usage() {
046: System.err
047: .println("jprosecl -Dinsert=<class> -Dwithdraw=<id> -list -Dprose.address=<host>:<port>/(activeInstance|testInstance)");
048: }
049:
050: private static String doGetScriptPath() {
051: String dotExe = "";
052: if ("x86".equals(System.getProperty("os.arch", "NOWINDOWS")))
053: dotExe = ".exe";
054: String proseTop = System
055: .getProperty("ch.ethz.inf.project.home");
056: if (proseTop == null)
057: throw new RuntimeException(
058: "expecting the property 'ch.ethz.inf.project.home' to be set;"
059: + "Probable cause: this method is not called from a prose-enabled jvm (jprose)");
060: File executable = new File(proseTop
061: + System.getProperty("file.separator", "/")
062: + "programs"
063: + System.getProperty("file.separator", "/") + "clprose"
064: + dotExe);
065:
066: if (!executable.exists()) {
067: System.err.println("executable:" + executable);
068: executable = new File(proseTop
069: + System.getProperty("file.separator", "/") + "bin"
070: + System.getProperty("file.separator", "/")
071: + "clprose" + dotExe);
072: }
073: return executable.toString();
074: }
075:
076: private static Vector doGetCommonArgs(String address, String txid,
077: boolean isReal) {
078: Vector arguments = new Vector();
079: if (txid != null) {
080: arguments.add("--txid");
081: arguments.add(txid);
082: }
083: arguments.add("--address");
084: arguments.add(address);
085: if (!isReal)
086: arguments.add("--test");
087:
088: return arguments;
089: }
090:
091: public static String[] insertScriptCommandline(String classpath,
092: String aspectClassName, Object insertId, String address,
093: String txid, boolean isReal) throws java.io.IOException {
094: Vector arguments = new Vector();
095: arguments.add(doGetScriptPath());
096: arguments.add("-classpath");
097: arguments.add(classpath);
098: arguments.add("--insert");
099: arguments.add(aspectClassName);
100: if (insertId != null) {
101: arguments.add("--insertId");
102: arguments.add(insertId.toString());
103: }
104: arguments.addAll(doGetCommonArgs(address, txid, isReal));
105: return (String[]) arguments.toArray(new String[] {});
106: }
107:
108: public static void main(String[] args) {
109: int exitCode = 1; // assume there will be an error
110:
111: WebServer server = WebServer.allowRMIPeersToRetrieveByteCode();
112:
113: String insertId = System.getProperty("insertId", null);
114: String remoteProse = System
115: .getProperty("prose.address", "NONE");
116: String aspectToInsert = System.getProperty("insert", "NONE");
117: String aspectToWithdraw = System
118: .getProperty("withdraw", "NONE");
119: String transactionId = System.getProperty("txId", null);
120: String finishTxAction = System.getProperty("finishtx", "NONE");
121: String list = System.getProperty("list", "NONE");
122:
123: try {
124: String host = null;
125: int port = -1;
126: try {
127: host = remoteProse.substring(0, remoteProse
128: .indexOf(':'));
129: String portName = remoteProse.substring(remoteProse
130: .indexOf(':') + 1, remoteProse.length());
131: port = Integer.parseInt(portName);
132: } catch (java.lang.StringIndexOutOfBoundsException e) {
133: throw new java.net.MalformedURLException(
134: "Missing ':' separator");
135: } catch (java.lang.NumberFormatException e) {
136: throw new java.net.MalformedURLException(
137: "Illegal port number");
138: }
139:
140: String instance = System.getProperty("prose.instance");
141:
142: RemoteAspectManager bothRams[] = RemoteProseComponent
143: .doGetRemoteAspectManagers(host, port);
144: RemoteAspectManager ram = null;
145:
146: if ("activeInstance".equals(instance))
147: ram = bothRams[0];
148: else if ("testInstance".equals(instance))
149: ram = bothRams[1];
150: else
151: throw new Error("instance must be specified by script!");
152:
153: if ("NONE".equals(remoteProse)) {
154: System.err.println("no host:port specified");
155: usage();
156: System.exit(1);
157: }
158:
159: if ("commit".equals(finishTxAction)) {
160: if (transactionId == null)
161: throw new IllegalArgumentException(
162: "transaction id must be specified");
163:
164: ram.commit(transactionId);
165: }
166:
167: if ("abort".equals(finishTxAction)) {
168: if (transactionId == null)
169: throw new IllegalArgumentException(
170: "transaction id must be specified");
171:
172: ram.abort(transactionId);
173: }
174:
175: if (!("NONE".equals(aspectToInsert))) {
176: // we have to insert an aspect
177: // assumption : no constructor
178: Class cls = Class.forName(aspectToInsert);
179: Aspect aspect = (Aspect) cls.newInstance();
180: if (insertId != null)
181: aspect.associateTo(insertId);
182:
183: // obtain remote prose reference
184: if (transactionId == null)
185: ram.insert(aspect);
186: else
187: ram.insert(aspect, transactionId);
188: }
189:
190: if (!("NONE".equals(aspectToWithdraw))) {
191: Object[] array = ram.allAspects().toArray();
192: int withdrawIdx = Integer.parseInt(aspectToWithdraw);
193: if (transactionId == null)
194: ram.withdraw((AspectSurrogate) array[withdrawIdx]);
195: else
196: ram.withdraw((AspectSurrogate) array[withdrawIdx],
197: transactionId);
198: }
199:
200: if (!("NONE".equals(list))) {
201: Object[] array = ram.allAspects().toArray();
202: for (int i = 0; i < array.length; i++)
203: System.err
204: .println("Aspect[" + i + "]: " + array[i]);
205: }
206: server.stop();
207: exitCode = 0;
208: } catch (java.lang.ClassNotFoundException cannotFoundClass) {
209: System.err.println("prose: *Error* The class '"
210: + aspectToInsert
211: + "' could not be found in the classpath");
212: } catch (java.lang.IllegalAccessException cannotCallConstructor) {
213: System.err
214: .println("prose: *Error* The class "
215: + aspectToInsert
216: + "' cannot be instantiated. Please check"
217: + " that the constructor (paramterless) has the correct access rights");
218: } catch (java.net.UnknownHostException wrongHostName) {
219: System.err
220: .println("prose: *Error* The prose service you specified does not exist at "
221: + remoteProse);
222: } catch (java.rmi.ConnectException serverNotThere) {
223: System.err
224: .println("prose: *Error* The prose service you specified does not exist at "
225: + remoteProse);
226: } catch (java.lang.InstantiationException cannotCreateAspect) {
227: System.err
228: .println("prose: *Error* The aspect class you specified ("
229: + aspectToInsert
230: + ") "
231: + " cannot be instantiated");
232: } catch (java.rmi.RemoteException cannotConnect) {
233: System.err
234: .println("prose: *Error* The following exception occured while trying to connect to"
235: + " the service "
236: + cannotConnect + ":\n");
237: } catch (java.net.MalformedURLException shitFromUser) {
238: System.err
239: .println("prose: *Error* The address you specified does not have the format <host>:<port>");
240: } catch (java.lang.IllegalArgumentException missingArgs) {
241: System.err.println("prose: *Error* "
242: + missingArgs.getMessage());
243: } catch (java.io.IOException cannotTalkToRemoteProse) {
244: System.err.println("prose: *Error* "
245: + cannotTalkToRemoteProse.getMessage());
246: } finally {
247: server.stop();
248: }
249: System.exit(exitCode);
250: }
251: }
252:
253: //======================================================================
254: //
255: // $Log: CommandlineProseClient.java,v $
256: // Revision 1.2 2003/07/10 14:59:19 apopovic
257: // Removed stupind println
258: //
259: // Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
260: // Imported from ETH Zurich
261: //
262: // Revision 1.4 2003/07/02 13:19:06 anicoara
263: // Bug fixes: 'CatchCut' used not to be serialiable. Fixed; CommandlineProse used to falsely report serialization errors; fixed
264: //
265: // Revision 1.3 2003/06/07 16:12:58 popovici
266: // Extra changes for wincompat
267: //
268: // Revision 1.2 2003/06/06 12:37:37 popovici
269: // Naming.lookup replaced with a serversocket serving serialized objects; Changed:
270: // - CommandlineProseClient
271: // - RemoteProseComponent, which not opens a socket and has a utility method 'doGetAspe..'
272: // - other classes, due to different types of errors being thrown
273: //
274: // Revision 1.1 2003/05/25 13:25:18 popovici
275: // Refactoring
276: // inf.iks.tools is now prose.tools
277: // Stupid 'MyTableModel' renamed to 'WorksheetClientMode'
278: // - other renamings from bad names to reasonable names
279: //
280: // Revision 1.7 2003/05/25 11:48:51 popovici
281: // Major transformation of the prose tools:
282: // - GUI now stable and supports aspect insertion from another classpath than its own
283: // - CommandlineProseClient supports transactions test managers, independent classpath and has a better errhanling
284: //
285: // Revision 1.6 2003/05/06 15:51:19 popovici
286: // Mozilla-ification
287: //
288: // Revision 1.5 2003/05/05 14:03:12 popovici
289: // renaming from runes to prose
290: //
291: // Revision 1.4 2003/04/17 15:14:56 popovici
292: // Extension->Aspect renaming
293: //
294: // Revision 1.3 2003/03/04 18:35:57 popovici
295: // Organization of imprts
296: //
297: // Revision 1.2 2003/02/17 13:52:28 popovici
298: // Link to the web server removed temporarily
299: //
300: // Revision 1.1 2003/02/17 09:23:54 popovici
301: // RemoteAspectManager interface, implementation, and client added;
302: // Benchmark changed to work with the remote aspect manager;
303: // Benchmark changed to include aspect insertion;
304: //
|