KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > newjvm > AbstractSlaveJVM


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32 END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.newjvm;
35
36 import edu.rice.cs.util.Log;
37 import edu.rice.cs.util.UnexpectedException;
38 //import edu.rice.cs.util.PreventExitSecurityManager;
39

40 import java.io.Serializable JavaDoc;
41 import java.rmi.*;
42 import java.rmi.server.UnicastRemoteObject JavaDoc;
43
44 //import edu.rice.cs.util.PreventExitSecurityManager;
45

46 /** A partial implementation of a {@link SlaveRemote} that provides the quit functionality and that also periodically
47  * checks if the master is still alive and automatically quits if not.
48  * @version $Id: AbstractSlaveJVM.java 3898 2006-06-27 18:52:14Z rcartwright $
49  */

50 public abstract class AbstractSlaveJVM implements SlaveRemote, Serializable JavaDoc {
51   public static final int CHECK_MAIN_VM_ALIVE_SECONDS = 1;
52   
53   protected static final Log _log = new Log("MasterSlave.txt", false);
54   
55 // /** remote reference to the Master JVM; after initialization it is immutable until quit is executed. */
56
// public volatile MasterRemote _master;
57

58   /** Name of the thread to quit the slave. */
59   protected volatile String JavaDoc _quitSlaveThreadName = "Quit SlaveJVM Thread";
60
61   /** Name of the thread to periodically poll the master. */
62   protected volatile String JavaDoc _pollMasterThreadName = "Poll MasterJVM Thread";
63   
64   private volatile Thread JavaDoc _checkMaster = null;
65
66   private final Object JavaDoc _slaveJVMLock = new Object JavaDoc();
67   
68   private volatile boolean _slaveExited = false;
69   
70   private void shutdown() {
71 // try {
72
// boolean exported = UnicastRemoteObject.unexportObject(this, true);
73
// if (! exported) _log.log("ERROR: " + this + " was not unexported before shutdown");
74
// }
75
// catch(NoSuchObjectException e) { throw new UnexpectedException(e); } // should never happen
76
_log.log(AbstractSlaveJVM.this + ".shutdown() calling System.exit(0)");
77     System.exit(0);
78   }
79   
80   /** Quits the slave JVM, calling {@link #beforeQuit} before it does. */
81   public final synchronized void quit() {
82 // _log.log(this + ".quit() called");
83
// _master = null;
84

85     beforeQuit();
86     
87     _slaveExited = false;
88 // Utilities.showDebug("quit() called");
89

90     // put exit into another thread to allow this RMI call to return normally.
91
Thread JavaDoc t = new Thread JavaDoc(_quitSlaveThreadName) {
92       public void run() {
93         try {
94           // wait for parent RMI calling thread to exit
95
synchronized(_slaveJVMLock) {
96             while (! _slaveExited) {
97 // _log.log("Waiting for " + AbstractSlaveJVM.this + ".quit() to exit");
98
_slaveJVMLock.wait();
99             }
100           }
101           shutdown();
102         }
103         catch (Throwable JavaDoc th) {
104           _log.log(this + ".quit() failed!");
105           quitFailed(th);
106         }
107       }
108     };
109
110     t.start();
111 // _log.log(this + ".quit() RMI call exited");
112
synchronized(_slaveJVMLock) {
113       _slaveExited = true;
114       _slaveJVMLock.notify(); // There does not appear to be any constraint forcing this thread to exit before shutdown
115
}
116   }
117
118   /** This method is called just before the JVM is quit. It can be overridden to provide cleanup code, etc. */
119   protected void beforeQuit() { }
120
121   /** This method is called if the interpreterJVM cannot be exited (likely because of a unexpected security manager.) */
122   protected void quitFailed(Throwable JavaDoc th) { }
123
124   /** Initializes the Slave JVM including starting background thread to periodically poll the master JVM and
125    * automatically quit if it's dead. Unsynchronized because
126    * (i) this method can only be called once (without throwing an error) and _master is immutable once assigned here
127    * until quit()
128    * (ii) this method does not depend on any mutable state in this (which constrains {@link #handleStart}); and
129    * (iii) this method (and perhaps {@link #handleStart}) perform remote calls on master.
130    * This method delegates starting actions other than polling master to {@link #handleStart}.
131    */

132   public final void start(final MasterRemote master) throws RemoteException {
133     
134     if (_checkMaster != null) throw new UnexpectedException(this + ".start(...) called a second time");
135
136     _checkMaster = new Thread JavaDoc(_pollMasterThreadName) {
137       public void run() { // Note: this method is NOT synchronized; it runs in a different thread.
138
// PreventExitSecurityManager.activate();
139
while (true) {
140           try { Thread.sleep(CHECK_MAIN_VM_ALIVE_SECONDS*1000); }
141           catch (InterruptedException JavaDoc ie) { }
142 // _log.log(this + " polling " + master + " to confirm Master JVM is still alive");
143
try { master.checkStillAlive(); }
144           catch (RemoteException re) { quit(); } // Master JVM service is defunct. Quit! */
145
}
146       }
147     };
148     
149    
150
151     _checkMaster.setDaemon(true);
152     _checkMaster.start();
153     _log.log(_checkMaster + " created and STARTed by " + this);
154
155     handleStart(master); // master is passed as parameter because in some refactorings, _master is eliminated
156
}
157
158   /** Called when the slave JVM has started running. Subclasses must implement this method. */
159   protected abstract void handleStart(MasterRemote master);
160   
161 // public void finalize() { _log.log(this + " has been FINALIZED"); }
162
}
163
Popular Tags