KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > ProcessImpl


1 /*
2  * @(#)ProcessImpl.java 1.30 04/03/25
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang;
9
10 import java.io.*;
11
12 /* This class is for the exclusive use of ProcessBuilder.start() to
13  * create new processes.
14  *
15  * @author Martin Buchholz
16  * @version 1.30, 04/03/25
17  * @since 1.5
18  */

19
20 final class ProcessImpl extends Process JavaDoc {
21
22     // System-dependent portion of ProcessBuilder.start()
23
static Process JavaDoc start(String JavaDoc cmdarray[],
24              java.util.Map JavaDoc<String JavaDoc,String JavaDoc> environment,
25              String JavaDoc dir,
26              boolean redirectErrorStream)
27     throws IOException
28     {
29     String JavaDoc envblock = ProcessEnvironment.toEnvironmentBlock(environment);
30     return new ProcessImpl JavaDoc(cmdarray, envblock, dir, redirectErrorStream);
31     }
32
33     private long handle = 0;
34     private FileDescriptor stdin_fd;
35     private FileDescriptor stdout_fd;
36     private FileDescriptor stderr_fd;
37     private OutputStream stdin_stream;
38     private InputStream stdout_stream;
39     private InputStream stderr_stream;
40
41     private ProcessImpl(String JavaDoc cmd[],
42             String JavaDoc envblock,
43             String JavaDoc path,
44             boolean redirectErrorStream)
45     throws IOException
46     {
47     // Win32 CreateProcess requires cmd[0] to be normalized
48
cmd[0] = new File(cmd[0]).getPath();
49
50     StringBuilder JavaDoc cmdbuf = new StringBuilder JavaDoc(80);
51     for (int i = 0; i < cmd.length; i++) {
52             if (i > 0) {
53                 cmdbuf.append(' ');
54             }
55         String JavaDoc s = cmd[i];
56         if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0) {
57             if (s.charAt(0) != '"') {
58             cmdbuf.append('"');
59             cmdbuf.append(s);
60             if (s.endsWith("\\")) {
61             cmdbuf.append("\\");
62             }
63             cmdbuf.append('"');
64                 } else if (s.endsWith("\"")) {
65             /* The argument has already been quoted. */
66             cmdbuf.append(s);
67         } else {
68             /* Unmatched quote for the argument. */
69             throw new IllegalArgumentException JavaDoc();
70         }
71         } else {
72             cmdbuf.append(s);
73         }
74     }
75     String JavaDoc cmdstr = cmdbuf.toString();
76
77     stdin_fd = new FileDescriptor();
78     stdout_fd = new FileDescriptor();
79     stderr_fd = new FileDescriptor();
80
81     handle = create(cmdstr, envblock, path, redirectErrorStream,
82             stdin_fd, stdout_fd, stderr_fd);
83
84     java.security.AccessController.doPrivileged(
85                     new java.security.PrivilegedAction JavaDoc() {
86         public Object JavaDoc run() {
87         stdin_stream =
88             new BufferedOutputStream(new FileOutputStream(stdin_fd));
89         stdout_stream =
90             new BufferedInputStream(new FileInputStream(stdout_fd));
91         stderr_stream =
92             new FileInputStream(stderr_fd);
93         return null;
94         }
95     });
96     }
97
98     public OutputStream getOutputStream() {
99     return stdin_stream;
100     }
101
102     public InputStream getInputStream() {
103     return stdout_stream;
104     }
105
106     public InputStream getErrorStream() {
107     return stderr_stream;
108     }
109
110     public void finalize() {
111     close();
112     }
113
114     public native int exitValue();
115     public native int waitFor();
116     public native void destroy();
117
118     private native long create(String JavaDoc cmdstr,
119                    String JavaDoc envblock,
120                    String JavaDoc dir,
121                    boolean redirectErrorStream,
122                    FileDescriptor in_fd,
123                    FileDescriptor out_fd,
124                    FileDescriptor err_fd);
125     private native void close();
126 }
127
Popular Tags