KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > tools > CommandlineProseClient


1 //
2
// This file is part of the prose package.
3
//
4
// The contents of this file are subject to the Mozilla Public License
5
// Version 1.1 (the "License"); you may not use this file except in
6
// compliance with the License. You may obtain a copy of the License at
7
// http://www.mozilla.org/MPL/
8
//
9
// Software distributed under the License is distributed on an "AS IS" basis,
10
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
// for the specific language governing rights and limitations under the
12
// License.
13
//
14
// The Original Code is prose.
15
//
16
// The Initial Developer of the Original Code is Andrei Popovici. Portions
17
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
18
// All Rights Reserved.
19
//
20
// Contributor(s):
21
// $Id: CommandlineProseClient.java,v 1.2 2003/07/10 14:59:19 apopovic Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26

27 package ch.ethz.prose.tools;
28
29 // used packages
30

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 JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 /**
38  * Class CommandlineProseClient XXX
39  *
40  * @version $Revision: 1.2 $
41  * @author Andrei Popovici
42  */

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 JavaDoc doGetScriptPath()
53     {
54     String JavaDoc dotExe="";
55     if ("x86".equals(System.getProperty("os.arch","NOWINDOWS")))
56         dotExe=".exe";
57       String JavaDoc proseTop = System.getProperty("ch.ethz.inf.project.home");
58       if (proseTop == null)
59     throw new RuntimeException JavaDoc("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 JavaDoc executable = new File JavaDoc (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 JavaDoc(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 JavaDoc doGetCommonArgs(String JavaDoc address, String JavaDoc txid, boolean isReal)
80     {
81       Vector JavaDoc arguments = new Vector JavaDoc();
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 JavaDoc[] insertScriptCommandline(String JavaDoc classpath,String JavaDoc aspectClassName, Object JavaDoc insertId, String JavaDoc address, String JavaDoc txid,boolean isReal)
96     throws java.io.IOException JavaDoc
97     {
98       Vector JavaDoc arguments = new Vector JavaDoc();
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 JavaDoc[])arguments.toArray(new String JavaDoc[]{});
111     }
112
113
114   public static void main(String JavaDoc[] args)
115     {
116       int exitCode=1; // assume there will be an error
117

118       WebServer server=WebServer.allowRMIPeersToRetrieveByteCode();
119
120       String JavaDoc insertId = System.getProperty("insertId",null);
121       String JavaDoc remoteProse = System.getProperty("prose.address","NONE");
122       String JavaDoc aspectToInsert = System.getProperty("insert","NONE");
123       String JavaDoc aspectToWithdraw=System.getProperty("withdraw","NONE");
124       String JavaDoc transactionId=System.getProperty("txId",null);
125       String JavaDoc finishTxAction=System.getProperty("finishtx","NONE");
126       String JavaDoc list=System.getProperty("list","NONE");
127
128
129
130       try
131     {
132       String JavaDoc host = null;
133       int port = -1;
134       try
135         {
136           host = remoteProse.substring(0,remoteProse.indexOf(':'));
137           String JavaDoc portName = remoteProse.substring(remoteProse.indexOf(':') +1, remoteProse.length());
138           port = Integer.parseInt(portName);
139         }
140       catch (java.lang.StringIndexOutOfBoundsException JavaDoc e)
141         {
142           throw new java.net.MalformedURLException JavaDoc("Missing ':' separator");
143         }
144       catch (java.lang.NumberFormatException JavaDoc e)
145         {
146           throw new java.net.MalformedURLException JavaDoc("Illegal port number");
147         }
148
149
150       String JavaDoc 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 JavaDoc("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 JavaDoc("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 JavaDoc("transaction id must be specified");
183
184           ram.abort(transactionId);
185         }
186
187       if ( !("NONE".equals(aspectToInsert)))
188         {
189           // we have to insert an aspect
190
// assumption : no constructor
191
Class JavaDoc cls = Class.forName(aspectToInsert);
192           Aspect aspect = (Aspect)cls.newInstance();
193           if (insertId != null)
194         aspect.associateTo(insertId);
195
196           // obtain remote prose reference
197
if (transactionId == null)
198         ram.insert(aspect);
199           else
200         ram.insert(aspect,transactionId);
201         }
202
203       if ( !("NONE".equals(aspectToWithdraw)) )
204         {
205           Object JavaDoc[] 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 JavaDoc[] 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 JavaDoc cannotFoundClass)
223     {
224       System.err.println("prose: *Error* The class '" + aspectToInsert + "' could not be found in the classpath");
225     }
226       catch (java.lang.IllegalAccessException JavaDoc 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 JavaDoc wrongHostName)
232     {
233       System.err.println("prose: *Error* The prose service you specified does not exist at " + remoteProse);
234     }
235       catch (java.rmi.ConnectException JavaDoc serverNotThere)
236     {
237       System.err.println("prose: *Error* The prose service you specified does not exist at " + remoteProse);
238     }
239       catch (java.lang.InstantiationException JavaDoc cannotCreateAspect)
240     {
241       System.err.println("prose: *Error* The aspect class you specified (" + aspectToInsert + ") " +
242                  " cannot be instantiated");
243     }
244       catch (java.rmi.RemoteException JavaDoc 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 JavaDoc 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 JavaDoc missingArgs)
254     {
255       System.err.println("prose: *Error* " + missingArgs.getMessage());
256     }
257       catch (java.io.IOException JavaDoc 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 //======================================================================
271
//
272
// $Log: CommandlineProseClient.java,v $
273
// Revision 1.2 2003/07/10 14:59:19 apopovic
274
// Removed stupind println
275
//
276
// Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
277
// Imported from ETH Zurich
278
//
279
// Revision 1.4 2003/07/02 13:19:06 anicoara
280
// Bug fixes: 'CatchCut' used not to be serialiable. Fixed; CommandlineProse used to falsely report serialization errors; fixed
281
//
282
// Revision 1.3 2003/06/07 16:12:58 popovici
283
// Extra changes for wincompat
284
//
285
// Revision 1.2 2003/06/06 12:37:37 popovici
286
// Naming.lookup replaced with a serversocket serving serialized objects; Changed:
287
// - CommandlineProseClient
288
// - RemoteProseComponent, which not opens a socket and has a utility method 'doGetAspe..'
289
// - other classes, due to different types of errors being thrown
290
//
291
// Revision 1.1 2003/05/25 13:25:18 popovici
292
// Refactoring
293
// inf.iks.tools is now prose.tools
294
// Stupid 'MyTableModel' renamed to 'WorksheetClientMode'
295
// - other renamings from bad names to reasonable names
296
//
297
// Revision 1.7 2003/05/25 11:48:51 popovici
298
// Major transformation of the prose tools:
299
// - GUI now stable and supports aspect insertion from another classpath than its own
300
// - CommandlineProseClient supports transactions test managers, independent classpath and has a better errhanling
301
//
302
// Revision 1.6 2003/05/06 15:51:19 popovici
303
// Mozilla-ification
304
//
305
// Revision 1.5 2003/05/05 14:03:12 popovici
306
// renaming from runes to prose
307
//
308
// Revision 1.4 2003/04/17 15:14:56 popovici
309
// Extension->Aspect renaming
310
//
311
// Revision 1.3 2003/03/04 18:35:57 popovici
312
// Organization of imprts
313
//
314
// Revision 1.2 2003/02/17 13:52:28 popovici
315
// Link to the web server removed temporarily
316
//
317
// Revision 1.1 2003/02/17 09:23:54 popovici
318
// RemoteAspectManager interface, implementation, and client added;
319
// Benchmark changed to work with the remote aspect manager;
320
// Benchmark changed to include aspect insertion;
321
//
322
Popular Tags