Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] JNI methods throwing Exceptions

2002-10-16 Alain Magloire

        Some of the native functions were throwing exceptions
        particularly on the windows platform and it was not
        clearly advertise.  Eclipse uses a tool to externalize strings,
        to prevent this, strings need a comment "//$NON-NLS-1$".

        This also incorporated some fixes by Alex Chapiro, in
        Spawner.Reaper Thread an exception can be thrown for
        example if the application does not exist, the reaper
        thread will catch the IOException an notify spawner of
        the failure by setting pid = -1;

        * utils/../utils/pty/PTYInputStream.java (close0):
        Advertise that we can throw an IOException.
        * utils/../utils/pty/PTYOutputStream.java (close): Put
        the "$NON-NLS-1$" magic.
        (write0): Advertise we can throw IOException.
        (close0): Advertise we can throw IOException.
        * utils/../utils/spawner/ProcessFactory.java: Reformat.
        * utils/../utils/spawner/Spawner.java (Reaper):
        The run method when calling exec0 did not catch the exception.
        And the waitFor() should not be done on a pid == -1;
        * utils/../utils/spawner/SpawnerInputStream.java: Reformat.
        * utils/../utils/spawner/SpawnerOutputStream.java: Reformat.


Index: org/eclipse/cdt/utils/pty/PTYInputStream.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java,v
retrieving revision 1.1
diff -u -r1.1 PTYInputStream.java
--- org/eclipse/cdt/utils/pty/PTYInputStream.java	6 Sep 2002 14:08:29 -0000	1.1
+++ org/eclipse/cdt/utils/pty/PTYInputStream.java	17 Oct 2002 13:45:12 -0000
@@ -68,7 +68,7 @@
 	}
 
 	private native int read0(int fd, byte[] buf, int len) throws IOException;
-	native int close0(int fd);
+	private native int close0(int fd) throws IOException;
 
 	static {
 		System.loadLibrary("pty");
Index: org/eclipse/cdt/utils/pty/PTYOutputStream.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java,v
retrieving revision 1.1
diff -u -r1.1 PTYOutputStream.java
--- org/eclipse/cdt/utils/pty/PTYOutputStream.java	6 Sep 2002 14:08:29 -0000	1.1
+++ org/eclipse/cdt/utils/pty/PTYOutputStream.java	17 Oct 2002 13:45:12 -0000
@@ -59,15 +59,15 @@
 			return;
 		int status = close0(fd);
 		if (status == -1)
-			throw new IOException("close error");
+			throw new IOException("close error"); //$NON-NLS-1$
 		fd = -1;
 	}
 
-	private native int write0(int fd, byte[] b, int len);
-	private native int close0(int fd);
+	private native int write0(int fd, byte[] b, int len) throws IOException;
+	private native int close0(int fd) throws IOException;
 
 	static {
-		System.loadLibrary("pty");
+		System.loadLibrary("pty"); //$NON-NLS-1$
 	}
 
 }
Index: org/eclipse/cdt/utils/spawner/ProcessFactory.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/spawner/ProcessFactory.java,v
retrieving revision 1.1
diff -u -r1.1 ProcessFactory.java
--- org/eclipse/cdt/utils/spawner/ProcessFactory.java	26 Jun 2002 20:38:33 -0000	1.1
+++ org/eclipse/cdt/utils/spawner/ProcessFactory.java	17 Oct 2002 13:45:12 -0000
@@ -8,71 +8,66 @@
 import java.io.File;
 import java.io.IOException;
 
-
 public class ProcessFactory {
 
-    static private ProcessFactory instance;
-    private boolean hasSpawner;
-    private Runtime runtime;
-    
-    private ProcessFactory() {
-        hasSpawner = false;
-        runtime = Runtime.getRuntime();
-        try 
-        {
-            System.loadLibrary( "spawner" );
-            hasSpawner = true;
-        }
-        catch( SecurityException e )
-        {
-        }
-        catch( UnsatisfiedLinkError e )
-        {
-        }
-    }
-
-    public static ProcessFactory getFactory() {
-        if( instance == null )
-            instance = new ProcessFactory();
-        return instance;
-    }
-
-    public Process exec( String cmd ) throws IOException {
-        if( hasSpawner )
-            return new Spawner( cmd );
-        return runtime.exec( cmd );
-    }
-
-    public Process exec( String[] cmdarray ) throws IOException {
-        if( hasSpawner )
-            return new Spawner( cmdarray );
-        return runtime.exec( cmdarray );
-    }
-
-    public Process exec( String[] cmdarray, String[] envp ) throws IOException {
-        if( hasSpawner )
-            return new Spawner( cmdarray, envp );
-        return runtime.exec( cmdarray, envp );
-    }
-
-    public Process exec( String cmd, String[] envp ) throws IOException {
-        if( hasSpawner )
-            return new Spawner( cmd, envp );
-        return runtime.exec( cmd, envp );
-    }
-
-    public Process exec( String cmd, String[] envp, File dir ) throws IOException {
-        if( hasSpawner )
-            return new Spawner( cmd, envp, dir );
-        return runtime.exec( cmd, envp, dir );
-    }
-
-    public Process exec( String cmdarray[], String[] envp, File dir ) 
-        throws IOException 
-    {
-        if( hasSpawner )
-            return new Spawner( cmdarray, envp, dir );
-        return runtime.exec( cmdarray, envp, dir );
-    }
+	static private ProcessFactory instance;
+	private boolean hasSpawner;
+	private Runtime runtime;
+
+	private ProcessFactory() {
+		hasSpawner = false;
+		runtime = Runtime.getRuntime();
+		try {
+			System.loadLibrary("spawner"); //$NON-NLS-1$
+			hasSpawner = true;
+		} catch (SecurityException e) {
+			//e.printStackTrace();
+		} catch (UnsatisfiedLinkError e) {
+			//e.printStackTrace();
+		}
+	}
+
+	public static ProcessFactory getFactory() {
+		if (instance == null)
+			instance = new ProcessFactory();
+		return instance;
+	}
+
+	public Process exec(String cmd) throws IOException {
+		if (hasSpawner)
+			return new Spawner(cmd);
+		return runtime.exec(cmd);
+	}
+
+	public Process exec(String[] cmdarray) throws IOException {
+		if (hasSpawner)
+			return new Spawner(cmdarray);
+		return runtime.exec(cmdarray);
+	}
+
+	public Process exec(String[] cmdarray, String[] envp) throws IOException {
+		if (hasSpawner)
+			return new Spawner(cmdarray, envp);
+		return runtime.exec(cmdarray, envp);
+	}
+
+	public Process exec(String cmd, String[] envp) throws IOException {
+		if (hasSpawner)
+			return new Spawner(cmd, envp);
+		return runtime.exec(cmd, envp);
+	}
+
+	public Process exec(String cmd, String[] envp, File dir)
+		throws IOException {
+		if (hasSpawner)
+			return new Spawner(cmd, envp, dir);
+		return runtime.exec(cmd, envp, dir);
+	}
+
+	public Process exec(String cmdarray[], String[] envp, File dir)
+		throws IOException {
+		if (hasSpawner)
+			return new Spawner(cmdarray, envp, dir);
+		return runtime.exec(cmdarray, envp, dir);
+	}
 }
-
Index: org/eclipse/cdt/utils/spawner/Spawner.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/spawner/Spawner.java,v
retrieving revision 1.4
diff -u -r1.4 Spawner.java
--- org/eclipse/cdt/utils/spawner/Spawner.java	16 Oct 2002 00:13:30 -0000	1.4
+++ org/eclipse/cdt/utils/spawner/Spawner.java	17 Oct 2002 13:45:12 -0000
@@ -33,16 +33,16 @@
 		for (int n = 0; tokenizer.hasMoreTokens(); n++)
 			cmdarray[n] = tokenizer.nextToken();
 		if (bNoRedirect)
-			exec_detached(cmdarray, new String[0], ".");
+			exec_detached(cmdarray, new String[0], "."); //$NON-NLS-1$
 		else
-			exec(cmdarray, new String[0], ".");
+			exec(cmdarray, new String[0], "."); //$NON-NLS-1$
 	}
 	/**
 	 * Executes the specified command and arguments in a separate process with the
 	 * specified environment and working directory.
 	 **/
 	protected Spawner(String[] cmdarray, String[] envp, File dir) throws IOException {
-		String dirpath = ".";
+		String dirpath = "."; //$NON-NLS-1$
 		if (dir != null)
 			dirpath = dir.getAbsolutePath();
 		exec(cmdarray, envp, dirpath);
@@ -87,7 +87,7 @@
 		String[] cmdarray = new String[tokenizer.countTokens()];
 		for (int n = 0; tokenizer.hasMoreTokens(); n++)
 			cmdarray[n] = tokenizer.nextToken();
-		String dirpath = ".";
+		String dirpath = "."; //$NON-NLS-1$
 		if (dir != null)
 			dirpath = dir.getAbsolutePath();
 		exec(cmdarray, envp, dirpath);
@@ -135,7 +135,7 @@
 	 **/
 	public synchronized int exitValue() {
 		if (!isDone) {
-			throw new IllegalThreadStateException("Process not Terminated");
+			throw new IllegalThreadStateException("Process not Terminated"); //$NON-NLS-1$
 		}
 		return status;
 	}
@@ -196,7 +196,7 @@
 		if (envp == null)
 			envp = new String[0];
 
-		Thread reaper = new Reaper(cmdarray, envp, dirpath);
+		Reaper reaper = new Reaper(cmdarray, envp, dirpath);
 		reaper.setDaemon(true);
 		reaper.start();
 
@@ -212,7 +212,7 @@
 
 		// Check for errors.
 		if (pid == -1) {
-			throw new IOException("Exec error");
+			throw new IOException("Exec error:" + reaper.getErrorMessage()); //$NON-NLS-1$
 		}
 		in = new SpawnerInputStream(channels[1]);
 		err = new SpawnerInputStream(channels[2]);
@@ -229,17 +229,17 @@
 			envp = new String[0];
 		pid = exec1(cmdarray, envp, dirpath);
 		if (pid == -1) {
-			throw new IOException("Exec error");
+			throw new IOException("Exec error"); //$NON-NLS-1$
 		}
 	}
 
-	native int exec0( String[] cmdarray, String[] envp, String dir, int[] chan);
-	native int exec1( String[] cmdarray, String[] envp, String dir);
+	native int exec0( String[] cmdarray, String[] envp, String dir, int[] chan) throws IOException;
+	native int exec1( String[] cmdarray, String[] envp, String dir) throws IOException;
 	native int raise(int pid, int sig);
 	native int waitFor(int pid);
 
 	static {
-		System.loadLibrary("spawner");
+		System.loadLibrary("spawner"); //$NON-NLS-1$
 	}
 
 	// Spawn a thread to handle the forking and waiting
@@ -250,26 +250,41 @@
 		String[] cmdarray;
 		String[] envp;
 		String dirpath;
+		String errMesg;
+
 		public Reaper(String[] array, String[] env, String dir) {
-			super("Spawner Reaper");
+			super("Spawner Reaper"); //$NON-NLS-1$
 			cmdarray = array;
 			envp = env;
 			dirpath = dir;
+			errMesg = new String();
 		}
 
 		public void run() {
-			pid = exec0(cmdarray, envp, dirpath, channels);
+			try {
+				pid = exec0(cmdarray, envp, dirpath, channels);
+			} catch (IOException e) {
+				pid = -1;
+				errMesg = e.getMessage();
+			}
+
 			// Tell spawner that the process started.
 			synchronized (Spawner.this) {
 				Spawner.this.notifyAll();
 			}
 
-			// Sync with spawner and notify when done.
-			status = waitFor(pid);
-			synchronized (Spawner.this) {
-				isDone = true;
-				Spawner.this.notifyAll();
+			if (pid != -1) {
+				// Sync with spawner and notify when done.
+				status = waitFor(pid);
+				synchronized (Spawner.this) {
+					isDone = true;
+					Spawner.this.notifyAll();
+				}
 			}
+		}
+
+		public String getErrorMessage() {
+			return errMesg;
 		}
 	}
 }
Index: org/eclipse/cdt/utils/spawner/SpawnerInputStream.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/spawner/SpawnerInputStream.java,v
retrieving revision 1.1
diff -u -r1.1 SpawnerInputStream.java
--- org/eclipse/cdt/utils/spawner/SpawnerInputStream.java	26 Jun 2002 20:38:33 -0000	1.1
+++ org/eclipse/cdt/utils/spawner/SpawnerInputStream.java	17 Oct 2002 13:45:12 -0000
@@ -9,69 +9,72 @@
 import java.io.IOException;
 
 class SpawnerInputStream extends InputStream {
-    private int fd;
+	private int fd;
 
-    /**
-     * Fome a Unix valid file descriptor set a Reader.
-     * @param desc file descriptor.
-     */
-    public SpawnerInputStream (int fd) {
-	this.fd = fd;
-    }
-
-    /**
-     * Implementation of read for the InputStream.
-     *
-     * @exception IOException on error.
-     */
-    public int read () throws IOException {
-    	byte b[] = new byte[1];
-    	if(1 != read(b, 0, 1))
-    		return -1;
-    	return (int)b[0];
-    }
+	/**
+	 * Fome a Unix valid file descriptor set a Reader.
+	 * @param desc file descriptor.
+	 */
+	public SpawnerInputStream(int fd) {
+		this.fd = fd;
+	}
+
+	/**
+	 * Implementation of read for the InputStream.
+	 *
+	 * @exception IOException on error.
+	 */
+	public int read() throws IOException {
+		byte b[] = new byte[1];
+		if (1 != read(b, 0, 1))
+			return -1;
+		return (int) b[0];
+	}
 
 	/**
 	 * @see InputStream#read(byte[], int, int)
 	 */
 	public int read(byte[] buf, int off, int len) throws IOException {
 		if (buf == null) {
-		    throw new NullPointerException();
-		} else if ((off < 0) || (off > buf.length) || (len < 0) ||
-			   ((off + len) > buf.length) || ((off + len) < 0)) {
-		    throw new IndexOutOfBoundsException();
+			throw new NullPointerException();
+		} else if (
+			(off < 0)
+				|| (off > buf.length)
+				|| (len < 0)
+				|| ((off + len) > buf.length)
+				|| ((off + len) < 0)) {
+			throw new IndexOutOfBoundsException();
 		} else if (len == 0) {
-		    return 0;
+			return 0;
 		}
 		byte[] tmpBuf = new byte[len];
 
-		len = read0( fd, tmpBuf, len );
-		if( len <= 0 )
+		len = read0(fd, tmpBuf, len);
+		if (len <= 0)
 			return -1;
-			
+
 		System.arraycopy(tmpBuf, 0, buf, off, len);
 		return len;
 	}
 
+	/**
+	 * Close the Reader
+	 * @exception IOException on error.
+	 */
+	public void close() throws IOException {
+		if (fd == -1)
+			return;
+		int status = close0(fd);
+		if (status == -1)
+			throw new IOException("close error");
+		fd = -1;
+	}
+
+	private native int read0(int fd, byte[] buf, int len) throws IOException;
+	private native int close0(int fd) throws IOException;
 
-    /**
-     * Close the Reader
-     * @exception IOException on error.
-     */
-    public void close () throws IOException {
-    	if (fd == -1)
-    		return;
-	int status = close0 (fd);
-	if (status == -1)
-	    throw new IOException ("close error");
-	fd = -1;
-    }
-
-    private native int read0 (int fd, byte[] buf, int len) throws IOException;
-    native int close0 (int fd);
-
-    static {
-	System.loadLibrary ("spawner");
-    }
+	static {
+		System.loadLibrary("spawner");
+	}
 
 }
Index: org/eclipse/cdt/utils/spawner/SpawnerOutputStream.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/spawner/SpawnerOutputStream.java,v
retrieving revision 1.1
diff -u -r1.1 SpawnerOutputStream.java
--- org/eclipse/cdt/utils/spawner/SpawnerOutputStream.java	26 Jun 2002 20:38:33 -0000	1.1
+++ org/eclipse/cdt/utils/spawner/SpawnerOutputStream.java	17 Oct 2002 13:45:12 -0000
@@ -8,64 +8,66 @@
 import java.io.OutputStream;
 import java.io.IOException;
 
-public class SpawnerOutputStream extends OutputStream
-{
-    private int fd;
+public class SpawnerOutputStream extends OutputStream {
+	private int fd;
 
-    /**
-     * Fome a Unix valid file descriptor set a Reader.
-     * @param desc file descriptor.
-     */
-    public SpawnerOutputStream (int fd) {
-	this.fd = fd;
-    }
+	/**
+	 * Fome a Unix valid file descriptor set a Reader.
+	 * @param desc file descriptor.
+	 */
+	public SpawnerOutputStream(int fd) {
+		this.fd = fd;
+	}
 
 	/**
 	 * @see OutputStream#write(byte[], int, int)
 	 */
 	public void write(byte[] b, int off, int len) throws IOException {
 		if (b == null) {
-		    throw new NullPointerException();
-		} else if ((off < 0) || (off > b.length) || (len < 0) ||
-			   ((off + len) > b.length) || ((off + len) < 0)) {
-		    throw new IndexOutOfBoundsException();
+			throw new NullPointerException();
+		} else if (
+			(off < 0)
+				|| (off > b.length)
+				|| (len < 0)
+				|| ((off + len) > b.length)
+				|| ((off + len) < 0)) {
+			throw new IndexOutOfBoundsException();
 		} else if (len == 0) {
-		    return;
-		}	
+			return;
+		}
 		byte[] tmpBuf = new byte[len];
 		System.arraycopy(b, off, tmpBuf, off, len);
 		write0(fd, tmpBuf, len);
 	}
-    /**
-     * Implementation of read for the InputStream.
-     *
-     * @exception IOException on error.
-     */
-    public void write (int b) throws IOException {
-		byte[] buf = new byte[1];    	
-		buf[0] = (byte)b;
+	/**
+	 * Implementation of read for the InputStream.
+	 *
+	 * @exception IOException on error.
+	 */
+	public void write(int b) throws IOException {
+		byte[] buf = new byte[1];
+		buf[0] = (byte) b;
 		write(buf, 0, 1);
-    }
+	}
 
-    /**
-     * Close the Reader
-     * @exception IOException on error.
-     */
-    public void close () throws IOException {
-    if (fd == -1)
-    	return;
-	int status = close0 (fd);
-	if (status == -1)
-	    throw new IOException ("close error");
-	fd = -1;
-    }
+	/**
+	 * Close the Reader
+	 * @exception IOException on error.
+	 */
+	public void close() throws IOException {
+		if (fd == -1)
+			return;
+		int status = close0(fd);
+		if (status == -1)
+			throw new IOException("close error"); //$NON-NLS-1$
+		fd = -1;
+	}
 
-    private native int write0 (int fd, byte[] b, int len);
-    private native int close0 (int fd);
+	private native int write0(int fd, byte[] b, int len) throws IOException;
+	private native int close0(int fd);
 
-    static
-    {
-	System.loadLibrary ("spawner");
-    }
+	static {
+		System.loadLibrary("spawner"); //$NON-NLS-1$
+	}
 
 }



Back to the top