You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by pa...@apache.org on 2002/08/21 22:53:19 UTC
cvs commit: jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher ChildMain.java LaunchTask.java NonBlockingInputStream.java ParentListener.java
patrickl 2002/08/21 13:53:19
Modified: daemon/src/java/org/apache/commons/launcher ChildMain.java
LaunchTask.java NonBlockingInputStream.java
ParentListener.java
Log:
Eliminate need for heartbeat files to monitor parent JVM
Revision Changes Path
1.9 +2 -9 jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/ChildMain.java
Index: ChildMain.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/ChildMain.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ChildMain.java 21 Aug 2002 03:09:04 -0000 1.8
+++ ChildMain.java 21 Aug 2002 20:53:19 -0000 1.9
@@ -109,12 +109,6 @@
"org.apache.commons.launcher.executableName";
/**
- * The heartbeatFile system property name.
- */
- public final static String HEARTBEAT_FILE_PROP_NAME =
- "org.apache.commons.launcher.heartbeatFile";
-
- /**
* The miminizedWindowTitle system property name.
*/
public final static String MINIMIZED_WINDOW_TITLE_PROP_NAME =
@@ -209,11 +203,10 @@
System.setIn(new NonBlockingInputStream(System.in));
waitForChild = true;
- String heartbeatFile = System.getProperty(ChildMain.HEARTBEAT_FILE_PROP_NAME);
- ParentListener heartbeat = new ParentListener(heartbeatFile);
+ ParentListener heartbeat = new ParentListener();
// Make the thread a daemon thread so that it does not
// prevent this process from exiting when all of the
- // appliation's threads finish.
+ // application's threads finish.
heartbeat.setDaemon(true);
heartbeat.start();
}
1.24 +0 -23 jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/LaunchTask.java
Index: LaunchTask.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/LaunchTask.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- LaunchTask.java 31 Jul 2002 02:04:48 -0000 1.23
+++ LaunchTask.java 21 Aug 2002 20:53:19 -0000 1.24
@@ -58,7 +58,6 @@
package org.apache.commons.launcher;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
@@ -578,24 +577,6 @@
}
}
- // Create the heartbeatFile. This file is needed by the
- // ParentListener class on Windows since the entire child JVM
- // process will block on Windows machines using some versions of
- // Unix shells such as MKS, etc.
- File heartbeatFile = null;
- FileOutputStream heartbeatOutputStream = null;
- if (filteredWaitForChild) {
- File tmpDir = null;
- String tmpDirName = (String)sysProps.get("java.io.tmpdir");
- if (tmpDirName != null)
- tmpDir = new File(tmpDirName);
- heartbeatFile = File.createTempFile(ChildMain.HEARTBEAT_FILE_PROP_NAME + ".", "", tmpDir);
- // Open the heartbeat file for writing so that it the child JVM
- // will not be able to delete it while this process is running
- heartbeatOutputStream = new FileOutputStream(heartbeatFile);
- sysProps.put(ChildMain.HEARTBEAT_FILE_PROP_NAME, heartbeatFile.getCanonicalPath());
- }
-
// Assemble child command
String[] cmd = new String[5 + jvmArgs.size() + sysProps.size() + appArgs.size()];
int nextCmdArg = 0;
@@ -697,10 +678,6 @@
// Let threads flush any unflushed output
stdout.join();
stderr.join();
- if (heartbeatOutputStream != null)
- heartbeatOutputStream.close();
- if (heartbeatFile != null)
- heartbeatFile.delete();
int exitValue = proc.exitValue();
if (filteredFailOnError && exitValue != 0)
throw new BuildException(Launcher.getLocalizedString("child.failed", this.getClass().getName()) + " " + exitValue);
1.2 +10 -23 jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/NonBlockingInputStream.java
Index: NonBlockingInputStream.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/NonBlockingInputStream.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NonBlockingInputStream.java 21 Aug 2002 03:09:04 -0000 1.1
+++ NonBlockingInputStream.java 21 Aug 2002 20:53:19 -0000 1.2
@@ -98,11 +98,8 @@
*/
public synchronized int read() throws IOException {
- int available = waitForAvailable();
- if (available > 0)
- return super.read();
- else
- return available;
+ waitForAvailable();
+ return super.read();
}
@@ -119,10 +116,7 @@
public synchronized int read(byte[] b) throws IOException {
int available = waitForAvailable();
- if (available > 0)
- return super.read(b, 0, available);
- else
- return available;
+ return super.read(b, 0, available);
}
@@ -144,13 +138,9 @@
{
int available = waitForAvailable();
- if (available > 0) {
- if (available < len)
- len = available;
- return super.read(b, off, len);
- } else {
- return available;
- }
+ if (available < len)
+ len = available;
+ return super.read(b, off, len);
}
@@ -167,13 +157,9 @@
public synchronized long skip(long n) throws IOException {
int available = waitForAvailable();
- if (available > 0) {
- if (available < n)
- n = available;
- return super.skip(n);
- } else {
- return available;
- }
+ if (available < n)
+ n = available;
+ return super.skip(n);
}
@@ -192,6 +178,7 @@
int available = 0;
while ((available = available()) == 0) {
+ Thread.currentThread().yield();
try {
Thread.currentThread().sleep(100);
} catch (Exception e) {}
1.4 +18 -98 jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/ParentListener.java
Index: ParentListener.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/ParentListener.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ParentListener.java 31 Jul 2002 01:20:09 -0000 1.3
+++ ParentListener.java 21 Aug 2002 20:53:19 -0000 1.4
@@ -57,9 +57,7 @@
package org.apache.commons.launcher;
-import java.io.File;
-import java.io.InputStream;
-import java.io.IOException;
+import java.io.PrintStream;
/**
* A class for detecting if the parent JVM that launched this process has
@@ -69,110 +67,32 @@
*/
public class ParentListener extends Thread {
- //------------------------------------------------------------------ Fields
-
- /**
- * Cached heartbeat file.
- */
- private File heartbeatFile = null;
-
- //------------------------------------------------------------ Constructors
-
- /**
- * Validates and caches a lock file created by the parent JVM.
- *
- * @param path the lock file that the parent JVM has an open
- * FileOutputStream
- * @throws IOException if the heartbeat cannot be converted into a valid
- * File object
- */
- public ParentListener(String path) throws IOException {
-
- if (path == null)
- throw new IOException();
-
- // Make sure we have a valid path
- heartbeatFile = new File(path);
- heartbeatFile.getCanonicalPath();
-
- }
//----------------------------------------------------------------- Methods
/**
- * Periodically check that the parent JVM has not terminated. On all
- * platforms other than Windows, this method will check that System.in has
- * not been closed. On Windows NT, 2000, and XP the lock file specified in
- * the {@link #ParentListener(String)} constructor is monitored as reading
- * System.in will block the entire process on Windows machines that use
- * some versions of Unix shells such as MKS, etc. No monitoring is done
- * on Window 95, 98, and ME.
+ * Periodically check that the parent JVM has not terminated by checking
+ * if {@link System#err} is open. If {@link System#err} is in an error
+ * state, the parent JVM is assumed to be terminated and this method will
+ * invoke {@link Syste#exit(int)}. This method <b>must</b> be executed
+ * <b>before</b> {@link System#setErr(int)} is ever invoked.
+ * <p>
+ * TODO: {@link System#err} never seems to close while System.in is
+ * being read on Windows platforms. Need to find a workaround for this
+ * case.
*/
public void run() {
- String osname = System.getProperty("os.name").toLowerCase();
-
- // We need to use file locking on Windows since reading System.in
- // will block the entire process on some Windows machines.
- if (osname.indexOf("windows") >= 0) {
-
- // Do nothing if this is a Windows 9x platform since our file
- // locking mechanism does not work on the early versions of
- // Windows
- if (osname.indexOf("nt") == -1 && osname.indexOf("2000") == -1 && osname.indexOf("xp") == -1)
- return;
-
- // If we can delete the heartbeatFile on Windows, it means that
- // the parent JVM has closed its FileOutputStream on the file.
- // Note that the parent JVM's stream should only be closed when
- // it exits.
- for ( ; ; ) {
- if (heartbeatFile.delete())
- break;
- // Wait awhile before we try again
- yield();
- try {
- sleep(5000);
- } catch (Exception e) {}
- }
-
- } else {
-
- // Cache System.in in case the application redirects
- InputStream is = System.in;
- int bytesAvailable = 0;
- int bytesRead = 0;
- byte[] buf = new byte[1024];
+ // Save System.err in case the application invokes System.setErr()
+ // later
+ PrintStream err = System.err;
+ while (!err.checkError()) {
+ // Wait a while before the next loop
+ yield();
try {
- while (true) {
- synchronized (is) {
- // Mark the stream position so that other threads can
- // reread the strea
- is.mark(buf.length);
- // Read one more byte than has already been read to
- // force the stream to wait for input
- bytesAvailable = is.available();
- if (bytesAvailable < buf.length) {
- bytesRead = is.read(buf, 0, bytesAvailable + 1);
- // Reset so that we "unread" the bytes that we read
- is.reset();
- if (bytesRead == -1)
- break;
- } else {
- // Make the buffer larger
- if (buf.length < Integer.MAX_VALUE / 2)
- buf = new byte[buf.length * 2];
- }
- }
- yield();
- }
- } catch (IOException ioe) {}
-
+ sleep(3000);
+ } catch (Exception e) {}
}
-
- // Clean up before exiting
- if (heartbeatFile != null)
- heartbeatFile.delete();
// Exit this process since the parent JVM has exited
System.exit(0);
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>