1 33 34 package edu.rice.cs.util.newjvm; 35 36 import java.io.*; 37 import java.rmi.server.UnicastRemoteObject ; 40 import java.rmi.RemoteException ; 41 42 import java.util.Arrays ; 43 44 import edu.rice.cs.util.Log; 45 import edu.rice.cs.util.StringOps; 46 import edu.rice.cs.util.UnexpectedException; 47 import edu.rice.cs.util.swing.ScrollableDialog; 48 import edu.rice.cs.util.swing.Utilities; 49 51 import static edu.rice.cs.drjava.config.OptionConstants.*; 52 53 66 public final class SlaveJVMRunner { 67 68 78 public static final boolean SHOW_DEBUG_DIALOGS = false; 79 80 protected static final Log _log = new Log("MasterSlave.txt", false); 81 82 private static final long RMI_TIMEOUT = 5000L; 83 84 private static Thread _main; 85 86 private static volatile boolean _notDone; 87 88 89 private SlaveJVMRunner() { } 90 91 private static AbstractSlaveJVM _getInstance(Class clazz) throws Exception { 92 try { return (AbstractSlaveJVM) clazz.getField("ONLY").get(null); } 93 catch (Throwable t) { 94 _log.log("SlaveRemote class does not have an ONLY field!"); 95 return (AbstractSlaveJVM) clazz.newInstance(); 96 } 97 } 98 99 105 public synchronized static void main(String [] args) { 106 try { 107 System.setProperty("java.rmi.server.hostname", "127.0.0.1"); 109 110 if (args.length != 3 && args.length != 2) System.exit(1); 111 112 _notDone = true; 113 114 _main = Thread.currentThread(); 115 116 117 final FileInputStream fstream = new FileInputStream(args[0]); 119 final ObjectInputStream ostream = new ObjectInputStream(new BufferedInputStream(fstream)); 120 121 _log.log("Slave JVM reading master remote stub from file " + args[0] + " with " + 122 fstream.getChannel().size() + " bytes"); 123 124 127 128 148 149 152 final MasterRemote masterRemote = (MasterRemote) ostream.readObject(); 153 _notDone = false; 154 _log.log("SlaveJVMRunner completed reading " + masterRemote); 155 fstream.close(); 156 ostream.close(); 157 158 AbstractSlaveJVM slave = null; 159 160 try { 161 Class slaveClass = Class.forName(args[1]); 162 slave = _getInstance(slaveClass); 164 165 SlaveRemote slaveRemote = (SlaveRemote) UnicastRemoteObject.exportObject(slave); 168 _log.log("SlaveJVMRunner exported stub " + slaveRemote); 169 170 slave.start(masterRemote); 173 _log.log("SlaveJVMRunner invoking the method registerSlave(" + slave + ") in the Master JVM"); 174 masterRemote.registerSlave(slave); 175 } 176 catch (Exception e) { 177 _log.log("SlaveJVMRunner could not instantiate and start slave class '" + slave + "'. Threw exception: " + e); 179 try { 180 masterRemote.errorStartingSlave(e); 182 } 183 catch (RemoteException re) { 184 String msg = "SlaveJVMRunner could not instantiate and register the slave.\n" + 186 " Also failed to display error through master JVM, because:\n" + 187 StringOps.getStackTrace(re) + "\n"; 188 _showErrorMessage(msg, e); 189 } 190 System.exit(3); 191 } 192 } 193 catch (Exception e) { _showErrorMessage("SlaveJVMRunner could not set up the Slave JVM.", e); 196 _log.log("SlaveJVMRunner could not set up the Slave JVM. Calling System.exit(2) in response to: " + e); 197 System.exit(2); 198 } 199 } 200 201 205 private static void _showErrorMessage(String cause, Throwable t) { 206 String msg = "An error occurred in SlaveJVMRunner while starting the slave JVM:\n " + 207 cause + "\n\nOriginal error:\n" + StringOps.getStackTrace(t); 208 209 _log.log("ERROR in SlaveJVMRunner: " + cause + "; threw " + t); 210 211 if (SHOW_DEBUG_DIALOGS) new ScrollableDialog(null, "Error", "Error details:", msg).show(); 212 else if (! Utilities.TEST_MODE) System.out.println(msg); 213 } 214 } 215 | Popular Tags |