classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] [generics] Patch: FYI: ProcessBuilder


From: Tom Tromey
Subject: [cp-patches] [generics] Patch: FYI: ProcessBuilder
Date: 04 May 2005 16:01:21 -0600

I'm checking this in on the generics branch.

This implements ProcessBuilder.  The C code hasn't been tested yet,
fyi.

Tom

2005-05-04  Tom Tromey  <address@hidden>

        * native/jni/java-lang/java_lang_VMProcess.c
        (Java_java_lang_VMProcess_nativeSpawn): Added 'redirect'
        argument.  Use defines instead of contents.
        * vm/reference/java/lang/VMProcess.java (redirect): New field.
        (spawn): Updated.
        (setProcessInfo): Updated.
        (VMProcess): Added 'redirect' argument.
        (nativeSpawn): Likewise.
        (exec): New overload.
        * java/lang/ProcessBuilder.java: New file.

Index: native/jni/java-lang/java_lang_VMProcess.c
===================================================================
RCS file: 
/cvsroot/classpath/classpath/native/jni/java-lang/java_lang_VMProcess.c,v
retrieving revision 1.2.2.3
diff -u -r1.2.2.3 java_lang_VMProcess.c
--- native/jni/java-lang/java_lang_VMProcess.c  18 Apr 2005 01:37:42 -0000      
1.2.2.3
+++ native/jni/java-lang/java_lang_VMProcess.c  4 May 2005 21:49:54 -0000
@@ -1,5 +1,5 @@
 /* java_lang_VMProcess.c -- native code for java.lang.VMProcess
-   Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2002, 2004, 2005 Free Software Foundation, 
Inc.
 
 This file is part of GNU Classpath.
 
@@ -135,7 +135,8 @@
 JNIEXPORT void JNICALL
 Java_java_lang_VMProcess_nativeSpawn (JNIEnv * env, jobject this,
                                      jobjectArray cmdArray,
-                                     jobjectArray envArray, jobject dirFile)
+                                     jobjectArray envArray, jobject dirFile,
+                                     jboolean redirect)
 {
   int fds[3][2] = { {-1, -1}, {-1, -1}, {-1, -1} };
   jobject streams[3] = { NULL, NULL, NULL };
@@ -151,6 +152,7 @@
   jmethodID method;
   jclass clazz;
   int i;
+  int pipe_count = redirect ? 2 : 3;
 
   /* Check for null */
   if (cmdArray == NULL)
@@ -218,7 +220,7 @@
     }
 
   /* Create inter-process pipes */
-  for (i = 0; i < 3; i++)
+  for (i = 0; i < pipe_count; i++)
     {
       if (pipe (fds[i]) == -1)
        {
@@ -232,7 +234,8 @@
   /* Set close-on-exec flag for parent's ends of pipes */
   (void) fcntl (fds[0][1], F_SETFD, 1);
   (void) fcntl (fds[1][0], F_SETFD, 1);
-  (void) fcntl (fds[2][0], F_SETFD, 1);
+  if (pipe_count == 3)
+    (void) fcntl (fds[2][0], F_SETFD, 1);
 
   /* Fork into parent and child processes */
   if ((pid = fork ()) == (pid_t) - 1)
@@ -267,7 +270,16 @@
            }
          close (fds[1][1]);
        }
-      if (fds[2][1] != 2)
+      if (pipe_count == 3)
+       {
+         /* Duplicate stdout to stderr.  */
+         if (dup2 (1, 2) == -1)
+           {
+             fprintf (stderr, "dup2: %s", strerror (errno));
+             exit (127);
+           }
+       }
+      else if (fds[2][1] != 2)
        {
          if (dup2 (fds[2][1], 2) == -1)
            {
@@ -308,11 +320,12 @@
   method = (*env)->GetMethodID (env, clazz, "<init>", "(II)V");
   if ((*env)->ExceptionOccurred (env))
     goto done;
-  for (i = 0; i < 3; i++)
+  for (i = 0; i < pipe_count; i++)
     {
-      /* Mode is WRITE (2) for in and READ (1) for out and err. */
       const int fd = fds[i][i == 0];
-      const int mode = (i == 0) ? 2 : 1;
+      const int mode = ((i == 0)
+                       ? gnu_java_nio_channels_FileChannelImpl_WRITE
+                       : gnu_java_nio_channels_FileChannelImpl_READ);
       jclass sclazz;
       jmethodID smethod;
 
@@ -320,7 +333,7 @@
       if ((*env)->ExceptionOccurred (env))
        goto done;
 
-      if (mode == 2)
+      if (mode == gnu_java_nio_channels_FileChannelImpl_WRITE)
        sclazz = (*env)->FindClass (env, "java/io/FileOutputStream");
       else
        sclazz = (*env)->FindClass (env, "java/io/FileInputStream");
@@ -360,7 +373,7 @@
    */
 
   /* Close child's ends of pipes */
-  for (i = 0; i < 3; i++)
+  for (i = 0; i < pipe_count; i++)
     {
       const int fd = fds[i][i != 0];
 
@@ -374,7 +387,7 @@
    * was created for a file descriptor, we don't close it because it
    * will get closed when the Stream object is finalized.
    */
-  for (i = 0; i < 3; i++)
+  for (i = 0; i < pipe_count; i++)
     {
       const int fd = fds[i][i == 0];
 
Index: vm/reference/java/lang/VMProcess.java
===================================================================
RCS file: /cvsroot/classpath/classpath/vm/reference/java/lang/VMProcess.java,v
retrieving revision 1.2.2.3
diff -u -r1.2.2.3 VMProcess.java
--- vm/reference/java/lang/VMProcess.java       16 Feb 2005 01:11:42 -0000      
1.2.2.3
+++ vm/reference/java/lang/VMProcess.java       4 May 2005 21:49:54 -0000
@@ -43,6 +43,8 @@
 import java.io.OutputStream;
 import java.util.HashMap;
 import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Represents one external process. Each instance of this class is in
@@ -92,6 +94,7 @@
   InputStream stdout;                         // process output stream
   InputStream stderr;                         // process error stream
   int exitValue;                              // process exit value
+  boolean redirect;                           // redirect stderr -> stdout
 
   //
   // Dedicated thread that does all the fork()'ing and wait()'ing
@@ -196,7 +199,8 @@
        {
          try
            {
-             process.nativeSpawn(process.cmd, process.env, process.dir);
+             process.nativeSpawn(process.cmd, process.env, process.dir,
+                                 process.redirect);
              process.state = RUNNING;
              activeMap.put(new Long(process.pid), process);
            }
@@ -215,7 +219,8 @@
   }
 
   // Constructor
-  private VMProcess(String[] cmd, String[] env, File dir) throws IOException
+  private VMProcess(String[] cmd, String[] env, File dir, boolean redirect)
+    throws IOException
   {
     
     // Initialize this process
@@ -223,6 +228,7 @@
     this.cmd = cmd;
     this.env = env;
     this.dir = dir;
+    this.redirect = redirect;
   
     // Add process to the new process work list and wakeup processThread
     synchronized (workList)
@@ -275,11 +281,20 @@
 
   // Invoked by native code (from nativeSpawn()) to record process info.
   private void setProcessInfo(OutputStream stdin,
-                 InputStream stdout, InputStream stderr, long pid)
+                             InputStream stdout, InputStream stderr, long pid)
   {
     this.stdin = stdin;
     this.stdout = stdout;
-    this.stderr = stderr;
+    if (stderr == null)
+      this.stderr = new InputStream()
+       {
+         public int read() throws IOException
+         {
+           return -1;
+         }
+       };
+    else
+      this.stderr = stderr;
     this.pid = pid;
   }
 
@@ -288,7 +303,20 @@
    */
   static Process exec(String[] cmd, String[] env, File dir) throws IOException
   {
-    return new VMProcess(cmd, env, dir);
+    return new VMProcess(cmd, env, dir, false);
+  }
+
+  static Process exec(List<String> cmd, Map<String, String> env,
+                     File dir, boolean redirect) throws IOException
+  {
+    String[] acmd = cmd.toArray(new String[cmd.size()]);
+    String[] aenv = new String[env.size()];
+
+    int i = 0;
+    for (Map.Entry<String, String> entry : env.entrySet())
+      aenv[i++] = entry.getKey() + "=" + entry.getValue();
+
+    return new VMProcess(acmd, aenv, dir, redirect);
   }
 
   public OutputStream getOutputStream()
@@ -347,7 +375,8 @@
    *
    * @throws IOException if the O/S process could not be created.
    */
-  native void nativeSpawn(String[] cmd, String[] env, File dir)
+  native void nativeSpawn(String[] cmd, String[] env, File dir,
+                         boolean redirect)
     throws IOException;
 
   /**
Index: java/lang/ProcessBuilder.java
===================================================================
RCS file: java/lang/ProcessBuilder.java
diff -N java/lang/ProcessBuilder.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ java/lang/ProcessBuilder.java       1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,116 @@
+/* ProcessBuilder.java - Represent spawned system process
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package java.lang;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+public class ProcessBuilder
+{
+  private File directory = new File(System.getProperty("user.dir"));
+  private List<String> command;
+  // FIXME: make a copy.
+  private Map<String, String> environment = System.getenv();
+  private boolean redirect = false;
+
+  public ProcessBuilder(List<String> command)
+  {
+    this.command = command;
+  }
+
+  public ProcessBuilder(String... command)
+  {
+    this.command = Arrays.asList(command);
+  }
+
+  public List<String> command()
+  {
+    return command;
+  }
+
+  public ProcessBuilder command(List<String> command)
+  {
+    this.command = command;
+    return this;
+  }
+
+  public ProcessBuilder command(String... command)
+  {
+    this.command = Arrays.asList(command);
+    return this;
+  }
+
+  public File directory()
+  {
+    return directory;
+  }
+
+  public ProcessBuilder directory(File directory)
+  {
+    this.directory = directory;
+    return this;
+  }
+
+  public Map<String, String> environment()
+  {
+    return environment;
+  }
+
+  public boolean redirectErrorStream()
+  {
+    return redirect;
+  }
+
+  public ProcessBuilder redirectErrorStream(boolean redirect)
+  {
+    this.redirect = redirect;
+    return this;
+  }
+
+  public Process start() throws IOException
+  {
+    SecurityManager sm = SecurityManager.current; // Be thread-safe!
+    if (sm != null)
+      sm.checkExec(command.get(0));
+    return VMProcess.exec(command, environment, directory, redirect);
+  }
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]