001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.newjvm;
038:
039: import edu.rice.cs.util.Log;
040: import edu.rice.cs.util.UnexpectedException;
041:
042: import java.io.Serializable;
043: import java.rmi.*;
044: import java.rmi.server.UnicastRemoteObject;
045:
046: /** A partial implementation of a {@link SlaveRemote} that provides the quit functionality and that also periodically
047: * checks if the master is still alive and automatically quits if not.
048: * @version $Id: AbstractSlaveJVM.java 4260 2007-10-10 20:28:34Z mgricken $
049: */
050: public abstract class AbstractSlaveJVM extends UnicastRemoteObject
051: implements SlaveRemote {
052: public static final int CHECK_MAIN_VM_ALIVE_SECONDS = 1;
053:
054: protected static final Log _log = new Log("MasterSlave.txt", false);
055:
056: // /** remote reference to the Master JVM; after initialization it is immutable until quit is executed. */
057: // public volatile MasterRemote _master;
058:
059: /** Name of the thread to quit the slave. */
060: protected volatile String _quitSlaveThreadName = "Quit SlaveJVM Thread";
061:
062: /** Name of the thread to periodically poll the master. */
063: protected volatile String _pollMasterThreadName = "Poll MasterJVM Thread";
064:
065: private volatile Thread _checkMaster = null;
066:
067: private final Object _slaveJVMLock = new Object();
068:
069: private volatile boolean _slaveExited = false;
070:
071: public AbstractSlaveJVM() throws RemoteException {
072: }
073:
074: private void shutdown() {
075: // try {
076: // boolean exported = UnicastRemoteObject.unexportObject(this, true);
077: // if (! exported) _log.log("ERROR: " + this + " was not unexported before shutdown");
078: // }
079: // catch(NoSuchObjectException e) { throw new UnexpectedException(e); } // should never happen
080: _log.log(AbstractSlaveJVM.this
081: + ".shutdown() calling System.exit(0)");
082: System.exit(0);
083: }
084:
085: /** Quits the slave JVM, calling {@link #beforeQuit} before it does. */
086: public final synchronized void quit() {
087: // _log.log(this + ".quit() called");
088: // _master = null;
089:
090: beforeQuit();
091:
092: _slaveExited = false;
093: // Utilities.showDebug("quit() called");
094:
095: // put exit into another thread to allow this RMI call to return normally.
096: Thread t = new Thread(_quitSlaveThreadName) {
097: public void run() {
098: try {
099: // wait for parent RMI calling thread to exit
100: synchronized (_slaveJVMLock) {
101: while (!_slaveExited) {
102: // _log.log("Waiting for " + AbstractSlaveJVM.this + ".quit() to exit");
103: _slaveJVMLock.wait();
104: }
105: }
106: shutdown();
107: } catch (Throwable t) {
108: _log.log(this + ".quit() failed!");
109: quitFailed(t);
110: }
111: }
112: };
113:
114: t.start();
115: // _log.log(this + ".quit() RMI call exited");
116: synchronized (_slaveJVMLock) {
117: _slaveExited = true;
118: _slaveJVMLock.notify(); // There does not appear to be any constraint forcing this thread to exit before shutdown
119: }
120: }
121:
122: /** This method is called just before the JVM is quit. It can be overridden to provide cleanup code, etc. */
123: protected void beforeQuit() {
124: }
125:
126: /** This method is called if the interpreterJVM cannot be exited (likely because of a unexpected security manager.) */
127: protected void quitFailed(Throwable th) {
128: }
129:
130: /** Initializes the Slave JVM including starting background thread to periodically poll the master JVM and
131: * automatically quit if it's dead. Unsynchronized because
132: * (i) this method can only be called once (without throwing an error) and _master is immutable once assigned here
133: * until quit()
134: * (ii) this method does not depend on any mutable state in this (which constrains {@link #handleStart}); and
135: * (iii) this method (and perhaps {@link #handleStart}) perform remote calls on master.
136: * This method delegates starting actions other than polling master to {@link #handleStart}.
137: */
138: public final void start(final MasterRemote master)
139: throws RemoteException {
140:
141: if (_checkMaster != null)
142: throw new UnexpectedException(this
143: + ".start(...) called a second time");
144:
145: _checkMaster = new Thread(_pollMasterThreadName) {
146: public void run() { // Note: this method is NOT synchronized; it runs in a different thread.
147: while (true) {
148: try {
149: Thread
150: .sleep(CHECK_MAIN_VM_ALIVE_SECONDS * 1000);
151: } catch (InterruptedException ie) {
152: }
153: // _log.log(this + " polling " + master + " to confirm Master JVM is still alive");
154: try {
155: master.checkStillAlive();
156: } catch (RemoteException re) {
157: quit();
158: } // Master JVM service is defunct. Quit! */
159: }
160: }
161: };
162:
163: _checkMaster.setDaemon(true);
164: _checkMaster.start();
165: _log.log(_checkMaster + " created and STARTed by " + this );
166:
167: handleStart(master); // master is passed as parameter because in some refactorings, _master is eliminated
168: }
169:
170: /** Called when the slave JVM has started running. Subclasses must implement this method. */
171: protected abstract void handleStart(MasterRemote master);
172:
173: // public void finalize() { _log.log(this + " has been FINALIZED"); }
174: }
|