home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 July / PCO_07_97.ISO / NET_COM / cc32e40.exe / nav40.z / java40.jar / java / lang / UNIXProcess.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-07  |  3.4 KB  |  149 lines

  1. package java.lang;
  2.  
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileDescriptor;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.PipedOutputStream;
  11. import java.util.Hashtable;
  12.  
  13. class UNIXProcess extends Process implements Runnable {
  14.    static Hashtable subprocs = null;
  15.    private boolean isalive = false;
  16.    private int exit_code;
  17.    private FileDescriptor stdin_fd;
  18.    private FileDescriptor stdout_fd;
  19.    private FileDescriptor stderr_fd;
  20.    private FileDescriptor sync_fd;
  21.    int pid;
  22.    private OutputStream stdin_stream;
  23.    private InputStream raw_stdout;
  24.    private InputStream raw_stderr;
  25.    private ProcessInputStream piped_stdout_in;
  26.    private ProcessInputStream piped_stderr_in;
  27.    private PipedOutputStream piped_stdout_out;
  28.    private PipedOutputStream piped_stderr_out;
  29.    private int numReaders;
  30.  
  31.    private UNIXProcess() {
  32.    }
  33.  
  34.    public native void run();
  35.  
  36.    private native int forkAndExec(String[] var1, String[] var2) throws IOException;
  37.  
  38.    private native void notifyReaders();
  39.  
  40.    private static void deadChild(int var0, int var1) {
  41.       UNIXProcess var2 = (UNIXProcess)subprocs.get(new Integer(var0));
  42.       if (var2 != null) {
  43.          synchronized(var2){}
  44.  
  45.          try {
  46.             var2.isalive = false;
  47.             subprocs.remove(new Integer(var0));
  48.             var2.exit_code = var1;
  49.             var2.notifyReaders();
  50.             var2.notifyAll();
  51.          } catch (Throwable var5) {
  52.             throw var5;
  53.          }
  54.  
  55.       }
  56.    }
  57.  
  58.    synchronized int getNumReaders() throws InterruptedException {
  59.       return this.numReaders;
  60.    }
  61.  
  62.    synchronized void decrNumReaders() {
  63.       if (--this.numReaders <= 0) {
  64.          try {
  65.             this.stdin_stream.close();
  66.          } catch (IOException var3) {
  67.          }
  68.  
  69.          try {
  70.             this.raw_stdout.close();
  71.          } catch (IOException var2) {
  72.          }
  73.  
  74.          try {
  75.             this.raw_stderr.close();
  76.          } catch (IOException var1) {
  77.          }
  78.       }
  79.  
  80.       this.notifyAll();
  81.    }
  82.  
  83.    UNIXProcess(String[] var1, String[] var2) throws IOException {
  84.       this.stdin_fd = new FileDescriptor();
  85.       this.stdout_fd = new FileDescriptor();
  86.       this.stderr_fd = new FileDescriptor();
  87.       this.sync_fd = new FileDescriptor();
  88.       this.pid = this.forkAndExec(var1, var2);
  89.       this.isalive = true;
  90.       SecurityManager.enablePrivilege("SuperUser");
  91.       this.stdin_stream = new BufferedOutputStream(new FileOutputStream(this.stdin_fd));
  92.       this.raw_stdout = new FileInputStream(this.stdout_fd);
  93.       this.raw_stderr = new FileInputStream(this.stderr_fd);
  94.       this.piped_stdout_out = new PipedOutputStream();
  95.       this.piped_stderr_out = new PipedOutputStream();
  96.       this.piped_stdout_in = new ProcessInputStream(this, this.piped_stdout_out, this.raw_stdout);
  97.       this.piped_stderr_in = new ProcessInputStream(this, this.piped_stderr_out, this.raw_stderr);
  98.       Thread var3 = new Thread(this.piped_stdout_in, "stdout reader pid=" + this.pid);
  99.       Thread var4 = new Thread(this.piped_stderr_in, "stderr reader pid=" + this.pid);
  100.       this.numReaders = 2;
  101.       var3.start();
  102.       var4.start();
  103.       subprocs.put(new Integer(this.pid), this);
  104.       FileOutputStream var5 = new FileOutputStream(this.sync_fd);
  105.       var5.write(65);
  106.       var5.close();
  107.       SecurityManager.revertPrivilege();
  108.    }
  109.  
  110.    public OutputStream getOutputStream() {
  111.       return this.stdin_stream;
  112.    }
  113.  
  114.    public InputStream getInputStream() {
  115.       return this.piped_stdout_in;
  116.    }
  117.  
  118.    public InputStream getErrorStream() {
  119.       return this.piped_stderr_in;
  120.    }
  121.  
  122.    public synchronized int waitFor() throws InterruptedException {
  123.       while(this.isalive) {
  124.          this.wait();
  125.       }
  126.  
  127.       return this.exit_code;
  128.    }
  129.  
  130.    public synchronized int exitValue() {
  131.       if (this.isalive) {
  132.          throw new IllegalThreadStateException("process hasn't exited");
  133.       } else {
  134.          return this.exit_code;
  135.       }
  136.    }
  137.  
  138.    public native void destroy();
  139.  
  140.    static {
  141.       SecurityManager.enablePrivilege("SuperUser");
  142.       subprocs = new Hashtable();
  143.       Thread var0 = new Thread(new UNIXProcess(), "process reaper");
  144.       var0.setDaemon(true);
  145.       var0.start();
  146.       SecurityManager.revertPrivilege();
  147.    }
  148. }
  149.