KickJava   Java API By Example, From Geeks To Geeks.

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


1 //
2
// This file is part of the midas 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 midas.
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: RemoteProseComponent.java,v 1.2 2003/08/26 19:59:18 anicoara Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26
package ch.ethz.prose.tools;
27
28 // used packages
29
import java.rmi.NoSuchObjectException JavaDoc;
30 import java.rmi.Remote JavaDoc;
31 import java.rmi.RemoteException JavaDoc;
32 import java.rmi.server.UnicastRemoteObject JavaDoc;
33 import java.net.ServerSocket JavaDoc;
34 import java.net.Socket JavaDoc;
35 import java.io.ObjectOutputStream JavaDoc;
36 import java.io.ObjectInputStream JavaDoc;
37
38 import ch.ethz.prose.ProseSystem;
39 import ch.ethz.prose.SystemStartupException;
40 import ch.ethz.prose.SystemTeardownException;
41
42 /**
43  * Class RemoteExtensionSystem starts up a remote service (a
44  * <code>RemoteExtensionManager</code>) for the insertion of
45  * extensions.
46  *
47  * <p>
48  *
49  * The current implementation uses a <code>RemoteExtensionManagerImpl</code>.
50  *
51  * <p>
52  *
53  * Note that for starting the remote extension system you will have to add
54  * a property like:
55  * <code>ch.ethz.prose.ESSystem.X=ch.ethz.prose.tools.RemoteExtensionSystem</code>
56  * to the System's properties.
57  *
58  * @property prose.port the port to start prose on
59  *
60  * @version $Revision: 1.2 $
61  * @author Andrei Popovici
62  *
63  */

64 public
65 class RemoteProseComponent
66 {
67
68   protected static String JavaDoc ACTIVE_INSTANCE = "activeInstance";
69   protected static String JavaDoc TEST_INSTANCE = "testInstance";
70   private static boolean systemUp = false;
71   private static RemoteAspectManager activeInstance = null;
72   private static RemoteAspectManager testInstance = null;
73   private static Remote JavaDoc activeInstanceRef = null;
74   private static Remote JavaDoc testInstanceRef = null;
75   private static ServerSocket JavaDoc listener = null;
76
77
78
79   /**
80    * Create and export an instance of <code>RemoteExtnesionManager</code>
81    * <p>
82    * <em>Note: this method is idempotent. Succesive calls have the same
83    * effect as just one.</em>
84    *
85    * @exception SystemStartupException could not successfully start the
86    * remote extension manager.
87    */

88   public synchronized static void startup() throws SystemStartupException
89     {
90        if (systemUp)
91          return;
92        systemUp = true;
93
94        System.setSecurityManager(new java.rmi.RMISecurityManager JavaDoc());
95
96        // ATENTION!
97
// Comment because the JVM 1.4.1. blocks. To avoid this deadlock, now first starts "RemoteProseComponent" and then "ProseSystem".
98
// make sure that prose runs
99
// ProseSystem.startup();
100

101        int portNumber = -1;
102        try
103      {
104        portNumber = Integer.parseInt(System.getProperty("prose.port","UNDEFINED"));
105      }
106        catch (NumberFormatException JavaDoc noPortWasSpecified)
107      {
108        throw new SystemStartupException("To use prose Remotely please specify the 'prose.port' property");
109      }
110
111        try
112      {
113        // create, and export the objects.
114
activeInstance = new RemoteAspectManagerImpl(true);
115        activeInstanceRef = UnicastRemoteObject.exportObject(activeInstance);
116
117        testInstance = new RemoteAspectManagerImpl(false);
118        testInstanceRef = UnicastRemoteObject.exportObject(testInstance);
119
120        listener = new ServerSocket JavaDoc(portNumber);
121
122        Thread JavaDoc worker = new Thread JavaDoc()
123          {
124            public void run()
125          {
126            while (systemUp)
127              {
128                try
129              {
130                Socket JavaDoc s = listener.accept();
131                ObjectOutputStream JavaDoc objOut = new ObjectOutputStream JavaDoc(s.getOutputStream());
132                objOut.writeObject(activeInstanceRef);
133                objOut.writeObject(testInstanceRef);
134              }
135                catch (Exception JavaDoc e)
136              { e.printStackTrace(); }
137              }
138          }
139          };
140
141        worker.start();
142
143      }
144        catch (RemoteException JavaDoc e)
145      {
146        e.printStackTrace();
147        throw new SystemStartupException("Cannot export RemoteAspectManager");
148
149      }
150        catch (java.io.IOException JavaDoc e)
151      {
152        e.printStackTrace();
153        throw new SystemStartupException("Cannot start a listener socket on the given port");
154      }
155
156
157
158     }
159
160   /**
161    * Unexport (without forcing)
162    * the current instance of <code>RemoteExtensionManager</code> server.
163    * <p>
164    * <em>Note: this method is idempotent. Succesive calls have the same
165    * effect as just one.</em>
166    */

167   public synchronized static void teardown() throws SystemTeardownException
168     {
169        if (!systemUp)
170          return;
171
172        try
173      {
174        UnicastRemoteObject.unexportObject(activeInstance,true);
175        UnicastRemoteObject.unexportObject(testInstance,true);
176      }
177        catch (NoSuchObjectException JavaDoc noChance)
178      {
179        // object HAS to be there
180
throw new Error JavaDoc("FIXME FOR PROSE DEVELOPER: why is the object not there?");
181      }
182
183        systemUp = false;
184     }
185
186
187   protected static RemoteAspectManager[] doGetRemoteAspectManagers(String JavaDoc host, int connectTo)
188     throws java.net.UnknownHostException JavaDoc,java.io.IOException JavaDoc
189     {
190       RemoteAspectManager[] result = new RemoteAspectManager[2];
191       Socket JavaDoc s = new Socket JavaDoc(host,connectTo);
192       ObjectInputStream JavaDoc objIn = new ObjectInputStream JavaDoc(s.getInputStream());
193       try
194     {
195       result[0]= (RemoteAspectManager)objIn.readObject();
196       result[1]= (RemoteAspectManager)objIn.readObject();
197     }
198       catch (java.lang.ClassNotFoundException JavaDoc e)
199     {
200       throw new Error JavaDoc(e.toString());
201     }
202       return result;
203     }
204
205
206 }
207
208
209 //======================================================================
210
//
211
// $Log: RemoteProseComponent.java,v $
212
// Revision 1.2 2003/08/26 19:59:18 anicoara
213
// Bug fix: JVM1.4.1 blocks. To avoid the deadlock first starts RemoteProseComponent and then ProseSystem (Comment the ProseSystem_startup line)
214
//
215
// Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
216
// Imported from ETH Zurich
217
//
218
// Revision 1.4 2003/06/10 12:58:29 popovici
219
// Syntax bug fixed
220
//
221
// Revision 1.3 2003/06/06 12:37:37 popovici
222
// Naming.lookup replaced with a serversocket serving serialized objects; Changed:
223
// - CommandlineProseClient
224
// - RemoteProseComponent, which not opens a socket and has a utility method 'doGetAspe..'
225
// - other classes, due to different types of errors being thrown
226
//
227
// Revision 1.2 2003/05/26 17:49:45 popovici
228
// changes in the portnames; idempotent methods better implemented. ProseSystem now depends on the remote prose
229
//
230
// Revision 1.1 2003/05/25 13:25:21 popovici
231
// Refactoring
232
// inf.iks.tools is now prose.tools
233
// Stupid 'MyTableModel' renamed to 'WorksheetClientMode'
234
// - other renamings from bad names to reasonable names
235
//
236
// Revision 1.5 2003/05/25 11:48:52 popovici
237
// Major transformation of the prose tools:
238
// - GUI now stable and supports aspect insertion from another classpath than its own
239
// - CommandlineProseClient supports transactions test managers, independent classpath and has a better errhanling
240
//
241
// Revision 1.4 2003/05/05 14:03:13 popovici
242
// renaming from runes to prose
243
//
244
// Revision 1.3 2003/04/17 15:14:54 popovici
245
// Extension->Aspect renaming
246
//
247
// Revision 1.2 2003/03/04 18:35:56 popovici
248
// Organization of imprts
249
//
250
// Revision 1.1 2003/02/17 09:23:55 popovici
251
// RemoteAspectManager interface, implementation, and client added;
252
// Benchmark changed to work with the remote aspect manager;
253
// Benchmark changed to include aspect insertion;
254
//
255
// Revision 1.2 2002/07/25 08:55:40 popovici
256
// Mozilla Licensing
257
//
258
// Revision 1.1.1.1 2001/11/30 14:50:28 popovici
259
// Sources from runes
260
//
261
// Revision 1.1.2.1 2001/03/26 15:05:47 mrmuller
262
// adapted to new package structure
263
//
264
// Revision 1.1.2.4 2001/02/20 10:14:10 popovici
265
// Logger.messages added.
266
//
267
// Revision 1.1.2.3 2001/02/15 15:21:35 popovici
268
// - Thead for periodically recontacting the rmiregistry added
269
// - Startup enhanced with sequence for rebinding
270
// - Teardonw enahcned with sequence for unbinding
271
//
272
// Revision 1.1.2.2 2001/02/15 11:40:56 popovici
273
// Documentation Improvements
274
//
275
// Revision 1.1.2.1 2001/02/14 13:44:42 popovici
276
// Initial Revision
277
//
278
Popular Tags