KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > ProseSystem


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: ProseSystem.java,v 1.2 2003/08/26 19:50:30 anicoara Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26

27 package ch.ethz.prose;
28
29 // used packages
30
import java.lang.reflect.Constructor JavaDoc;
31 import java.security.AccessControlException JavaDoc;
32 import java.security.AccessController JavaDoc;
33 import java.security.PrivilegedActionException JavaDoc;
34 import java.security.PrivilegedExceptionAction JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 import ch.ethz.jvmai.JVMAspectInterface;
38 import ch.ethz.inf.util.Logger;
39
40 /**
41  * Class ProseSystem represents the user view of the system.
42  * It allows to start and stop prose, and to get access
43  * to an code <code>AspectManager</code>Users
44  * program aspects and they will insert them into the system
45  * using the <code>insertExtension</code> and <code>withdrawExtension</code>
46  * methods of the extension manager.
47  * <p>
48  * This class is also used for bootstrapping the system. Before using
49  * the extension system, users of this class (eventually applications)
50  * will have to call
51  * <blockquote><pre>
52  * ProseSystem.startup();
53  * Another way to start this system happens over the <code>RootComponent<code/>
54  * module.
55  * </pre></blockquote>
56  * this will setup the system, according to special system properties too,
57  * like
58  * <ul>
59  * <li> <code>ch.ethz.prose.EXManager</code>
60  * </ul>
61  *
62  * @property ch.ethz.prose.JVMAIProvider <b>Values:class names</b> <br>
63  * <b>Usage:</b> controlls the creation of the JVMAI
64  *
65  * @property ch.ethz.prose.JVMAIPackagePrefixes <b>Values: package prefixes</b> <br>
66  * <b>Usage:</b> controlls the package prefixes eliminated or used by jvmai
67  *
68  * @property ch.ethz.prose.JVMAIOpenWorldAssumption <b>Values:true or false</b> <br>
69  * <b>Usage:</b> controlls whether prose uses an open world (excludes package prefixes) or
70  * a closed world (includes package prefixes)
71  *
72  *
73  *
74  *
75  *
76  * @version $Revision: 1.2 $
77  * @author Andrei Popovici
78  */

79 public class ProseSystem implements Component {
80
81   static private AspectManager currentExtM = null;
82   static private AspectManager testExtM = null;
83   static private JVMAspectInterface aspectInterface = null;
84   static boolean systemUp = false;
85   static ProsePermission permission = new ProsePermission("startupExtensionSystem");
86
87   // caching Variables as input to function <JoinPointManager>.connectToJVMAI
88
static ch.ethz.jvmai.Provider provider = null;
89   static String JavaDoc[] packagePrefixes = null;
90   static boolean openWorldAssumption;
91
92
93   /** Start up application-specific functionality.
94    * This method should be called before using the extension system.
95
96    * <p><em>Note:this method is idempotent. Succesive, successful calls to
97    * <code>startup</code> have the same effect as a single call.
98    * Use <code>teardown</code> in between to actualy re-do the startup. NOT YET IMPLEMENTED.</em><p>
99    *
100    * Note: in case that the ProsePermission "startupExtensionSystem" is granted to the
101    * caller, this method is executed as privileged only restricted
102    * by the permissions not granted to RUNES classes
103    *
104    * @exception SystemStartupException one of the specified properties did
105    * not comply to the startup/teardown naming convention or one of the
106    * startup methods of additional systems failed.
107    *
108    */

109   public synchronized static void startup() throws SystemStartupException
110     {
111       if (systemUp)
112     return;
113
114       // ATENTION!
115
// Because the JVM 1.4.1 blocks, we run "RemoteProseComponent" before "ProseSystem".
116
if (!System.getProperty("prose.port","UNKNOWN").equals("UNKNOWN"))
117         ch.ethz.prose.tools.RemoteProseComponent.startup();
118       try { Thread.currentThread().sleep(500); } catch (InterruptedException JavaDoc e) {}
119
120       try {
121     // security check
122
//AccessController.checkPermission(permission); // FIXME : uncomment this line.
123

124     // if no exception occurred, we can perform the startup under priv. rights
125
AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
126         public Object JavaDoc run() throws SystemStartupException {
127           doStartup();
128           return null;
129         }
130       });
131
132       } catch (AccessControlException JavaDoc e) {
133     // we start the system in the standard way
134
doStartup();
135
136       } catch (PrivilegedActionException JavaDoc e) {
137     e.printStackTrace();
138     throw new SystemStartupException(e.toString());
139       }
140
141     /* ATENTION!
142      * Because the JVM 1.4.1 blocks, we run "RemoteProseComponent" before "ProseSystem".
143       if (!System.getProperty("prose.port","UNKNOWN").equals("UNKNOWN"))
144     ch.ethz.prose.tools.RemoteProseComponent.startup();
145      */

146
147       systemUp=true;
148     }
149
150   /**
151    * the real startup method
152    */

153   private synchronized static void doStartup() throws SystemStartupException
154     {
155
156       try
157     {
158
159       // get the jvmai-provider
160
String JavaDoc providerClassName = System.getProperty("ch.ethz.prose.JVMAIProvider");
161       if(providerClassName==null)
162         providerClassName = "ch.ethz.inf.iks.jvmai.jvmdi.DebuggerProvider";
163
164       Class JavaDoc providerClass = Class.forName(providerClassName);
165       provider = (ch.ethz.jvmai.Provider)providerClass.newInstance();
166
167       packagePrefixes = new String JavaDoc[0];
168       openWorldAssumption = true;
169
170       String JavaDoc packageList = System.getProperty("ch.ethz.prose.JVMAIPackagePrefixes");
171       String JavaDoc openWorldParam = System.getProperty("ch.ethz.prose.JVMAIOpenWorldAssumption");
172
173       if(packageList==null && openWorldParam==null)
174         {
175           packagePrefixes = new String JavaDoc[] {"ch.ethz.prose."};
176           openWorldAssumption = true;
177         }
178       else
179         {
180           if(packageList==null)
181         {
182           packagePrefixes = new String JavaDoc[0];
183         }
184           else
185         {
186           StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(packageList,",");
187           packagePrefixes = new String JavaDoc[tokenizer.countTokens()];
188           for(int p=0; p<packagePrefixes.length; p++) {
189             packagePrefixes[p] = tokenizer.nextToken();
190           }
191         }
192           if(openWorldParam==null)
193         {
194           openWorldAssumption = true;
195         }
196           else
197         {
198           openWorldAssumption = openWorldParam.toUpperCase().equals("TRUE");
199         }
200
201         }
202       // initialize the aspect interface and the info interface
203
aspectInterface = provider.getAspectInterface();
204
205       // start the aspect interface!
206
aspectInterface.startup(packagePrefixes,openWorldAssumption);
207
208       aspectInterface.suspendNotification(Thread.currentThread());
209
210       // set the default extension manager
211
String JavaDoc extMName = System.getProperty("ch.ethz.prose.EXManager");
212       if (extMName != null)
213         {
214           Class JavaDoc[] paramTypes = new Class JavaDoc[] {boolean.class, JVMAspectInterface.class};
215           Object JavaDoc[] params = new Object JavaDoc[] {Boolean.TRUE, aspectInterface};
216           Class JavaDoc extMClass = Class.forName(extMName);
217           Constructor JavaDoc extMConstructor = extMClass.getConstructor(paramTypes);
218           currentExtM = (AspectManager)extMConstructor.newInstance(params);
219           params[0] = Boolean.FALSE;
220           testExtM = (AspectManager)extMConstructor.newInstance(params);
221         }
222       else
223         {
224           currentExtM = new LocalAspectManager(true, aspectInterface);
225           testExtM = new LocalAspectManager(false, aspectInterface);
226         }
227       currentExtM.startup();
228       testExtM.startup();
229
230       systemUp = true;
231     }
232       catch(Throwable JavaDoc e)
233     {
234       e.printStackTrace();
235       if (currentExtM != null)
236         {
237           currentExtM.teardown();
238           currentExtM = null;
239         }
240
241       if (testExtM != null)
242         {
243           currentExtM.teardown();
244           testExtM = null;
245         }
246       if (aspectInterface != null)
247         aspectInterface.teardown();
248       aspectInterface = null;
249
250       if (e instanceof SystemStartupException)
251         throw (SystemStartupException)e;
252
253       throw new SystemStartupException(e.toString());
254     }
255       finally
256     {
257       // start doing aspects
258
if (aspectInterface != null)
259         aspectInterface.resumeNotification(Thread.currentThread());
260     }
261       Logger.message("ProseComponent.startup: succeeded! ");
262     }
263
264
265   /** Tear down prose funtionality. Withdraw all existing aspects and remove the
266    * current AspectManager and JoinPointManager.
267    *
268    * <p><em>Note:this method is idempotent. Succesive, successful calls to
269    * <code>teardown</code> have the same effect as a single call.
270    * </em>.
271    *
272    * @exception SystemTeardownException if the teardown fails
273    */

274   public static void teardown() throws SystemTeardownException
275     {
276       if (!systemUp)
277     return;
278       if (getAspectManager() != null)
279     {
280       currentExtM.teardown();
281       currentExtM = null;
282     }
283
284       if (getTestAspectManager() != null)
285     {
286       testExtM.teardown();
287       testExtM = null;
288     }
289
290       if (aspectInterface != null)
291     aspectInterface.teardown();
292
293       aspectInterface = null;
294
295       systemUp = false;
296     }
297
298
299   /**
300    * Returns <code>true</code> if the local AspectManager
301    * is set up and running.
302    */

303   public static boolean isActive()
304     {
305
306       return systemUp;
307     }
308
309
310   /**
311    * Return the aspect manager currently in use.
312    */

313   public static AspectManager getAspectManager()
314     {
315       try { startup(); } catch (Exception JavaDoc e) {}
316       return currentExtM;
317     }
318
319   /**
320    * Return the currently used extension manager for testing aspects. This
321    * extension manager is not attached to the JVMAI System.
322    */

323   public static AspectManager getTestAspectManager()
324     {
325       try { startup(); } catch (Exception JavaDoc e) {}
326       return testExtM;
327     }
328 }
329
330
331
332
333 //======================================================================
334
//
335
// $Log: ProseSystem.java,v $
336
// Revision 1.2 2003/08/26 19:50:30 anicoara
337
// Bug fix: JVM1.4.1 blocks (RemoteProseComponent should run before ProseSystem)
338
//
339
// Revision 1.1.1.1 2003/07/02 15:30:51 apopovic
340
// Imported from ETH Zurich
341
//
342
// Revision 1.4 2003/06/10 12:59:47 popovici
343
// startup and teardown idempotence used to produce a startup during the teardown;
344
// flag is now set *after* the teardown has successfully completed
345
//
346
// Revision 1.3 2003/05/26 17:49:44 popovici
347
// changes in the portnames; idempotent methods better implemented. ProseSystem now depends on the remote prose
348
//
349
// Revision 1.2 2003/05/26 13:28:50 popovici
350
// Documentation Improvements
351
//
352
// Revision 1.1 2003/05/05 13:58:32 popovici
353
// renaming from runes to prose
354
//
355
// Revision 1.1 2003/04/17 15:15:05 popovici
356
// Extension->Aspect renaming
357
//
358
// Revision 1.16 2003/04/17 12:49:40 popovici
359
// Refactoring of the crosscut package
360
// ExceptionCut renamed to ThrowCut
361
// McutSignature is now SignaturePattern
362
//
363
// Revision 1.15 2003/04/17 08:47:13 popovici
364
// Important functionality additions
365
// - Cflow specializers
366
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
367
// - Transactional capabilities
368
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
369
// between static and dynamic specializers.
370
// - Functionality pulled up in abstract classes
371
// - Uniformization of advice methods patterns and names
372
//
373
// Revision 1.14 2003/03/05 16:24:19 popovici
374
// Added the jvmai properties for prose startup
375
//
376
// Revision 1.13 2003/03/04 18:36:36 popovici
377
// Organization of imprts
378
//
379
// Revision 1.12 2003/03/04 11:27:14 popovici
380
// Important refactorization step (march):
381
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
382
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
383
// structures
384
//
385
// Revision 1.11 2003/01/27 13:01:19 pschoch
386
// Now implements Interface Component
387
//
388
// Revision 1.10 2002/11/27 17:11:01 popovici
389
// the DebuggerProvider is now the default provider of the extension
390
// system.
391
//
392
// Revision 1.9 2002/11/26 17:14:31 pschoch
393
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
394
// ProseSystem now owns and starts the Aspect interface.
395
// ProseSystem now containes a 'test' AspectManager
396
// AspectManager now owns the JoinPointManager.
397
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
398
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
399
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
400
// Documentation updated accordingly.
401
//
402
// Revision 1.8 2002/10/31 16:47:13 pschoch
403
// Method setJoinPointManager fixed
404
//
405
// Revision 1.7 2002/10/25 07:42:32 popovici
406
// Undo Chnages Phillippe
407
//
408
// Revision 1.5 2002/03/28 13:48:34 popovici
409
// Mozilla-ified
410
//
411
// Revision 1.4 2002/02/18 14:40:36 popovici
412
// Bug fix in ProseSystem; notification is now suspended during ProseSystem.startup
413
//
414
// Revision 1.3 2002/02/13 16:08:01 smarkwal
415
// does not rely anymore on Provider.getProvider()
416
//
417
// Revision 1.2 2002/02/05 10:17:32 smarkwal
418
// JVMDI-specific code replaced by JVMAI. Implementation-classes and reflection-package removed.
419
//
420
// Revision 1.1.1.1 2001/11/29 18:13:15 popovici
421
// Sources from runes
422
//
423
// Revision 1.1.2.15 2001/06/15 08:06:50 popovici
424
// Property tags added for the ExtnsionSystem class
425
//
426
// Revision 1.1.2.14 2001/04/27 07:31:57 popovici
427
// Documentation fix (minor).
428
//
429
// Revision 1.1.2.13 2001/04/13 09:12:08 mrmuller
430
// system now flaged as running as soon as the local extension manager is working
431
//
432
// Revision 1.1.2.12 2001/03/26 15:04:06 mrmuller
433
// improved exception handling
434
//
435
// Revision 1.1.2.11 2001/03/22 17:10:28 mrmuller
436
// replaced PrivilegedAction with PrivilegedExceptionAction
437
//
438
// Revision 1.1.2.10 2001/03/16 13:14:18 mrmuller
439
// added isActive() method. startup now runs privileged if caller was granted ProsePermission "startupExtensionSystem"
440
//
441
// Revision 1.1.2.9 2001/02/28 13:46:37 popovici
442
// More explicit exception handling in the case of target extensions for
443
// startup and teardown
444
//
445
// Revision 1.1.2.8 2001/02/23 16:29:53 mrmuller
446
// Added logging output
447
//
448
// Revision 1.1.2.7 2001/02/22 16:18:49 popovici
449
// method 'setup' removed.
450
// - method 'withdrawAll' made more robust for the case no extension manager is set
451
// - methods 'teardown' and 'startup' enhanced with the idempotent functionality
452
//
453
// Revision 1.1.2.6 2001/02/20 09:29:02 popovici
454
// Withdrawal of extensions and removal of managers in teardown.
455
//
456
// Revision 1.1.2.5 2001/02/07 14:09:39 popovici
457
// - Idempotence Functionality added but commented out, since junit tests do not support it yetExtensionSystem.java
458
// - Documentation for startup/teardown updated
459
//
460
// Revision 1.1.2.4 2001/02/07 11:51:07 popovici
461
// - AspectManager functionality moved to 'LocalAspectManager'
462
// - methods 'startup' and 'teardown' and the startup/teardown framework
463
// added.
464
// - method 'applicationSetup' removed.
465
//
466
// Revision 1.1.2.3 2001/01/23 09:28:52 popovici
467
// extensions() changed to comply to new ExtensionManger Interface
468
//
469
// Revision 1.1.2.2 2000/10/27 08:41:35 groos
470
// - misc doc typo corrections
471
// - suggestion of how to format code
472
// (no code change, just reformatting)
473
//
474
// Revision 1.1.2.1 2000/10/24 17:48:01 popovici
475
// Initial Revision
476
//
477
Popular Tags