KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > CLIProcessExecutor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.commands;
25
26 import com.sun.enterprise.util.io.StreamFlusher;
27 import com.sun.enterprise.util.OS;
28 import java.io.File JavaDoc;
29
30 /**
31  * CLIProcessExecutor
32  * A simple process executor class that is used by CLI.
33  * @author jane.young@sun.com
34  * @version $Revision: 1.4.2.1 $
35  */

36 public class CLIProcessExecutor
37 {
38     Process JavaDoc process;
39
40     public CLIProcessExecutor() {
41         process = null;
42     }
43     
44
45     /**
46      * This method invokes the runtime exec
47      * @param cmd the command to execute
48      * @param wait if true, wait for process to end.
49      * @exception Exception
50      */

51     public void execute(String JavaDoc[] cmd, boolean wait) throws Exception JavaDoc
52     {
53         process=Runtime.getRuntime().exec(cmd);
54             //process = new ProcessBuilder(cmd).start();
55

56         // start stream flusher to push output to parent streams and null.
57
StreamFlusher sfErr=new StreamFlusher(process.getErrorStream(), System.err, null);
58         sfErr.start();
59
60         // set flusher on stdout also, if not could stop with too much output
61
StreamFlusher sfOut=new StreamFlusher(process.getInputStream(), System.out);
62         sfOut.start();
63         try {
64             // must sleep for a couple of seconds, so if there is a jvm startup error,
65
//the parent process
66
//is around to catch and report it when the process in executed in verbose mode.
67
Thread.currentThread().sleep(5000);
68             //wait is not required for command like start database
69
//where the process does not return since start database
70
//spawn it's own process.
71
if (wait) {
72                 process.waitFor();
73             }
74         }
75         catch (InterruptedException JavaDoc ie) {
76         }
77     }
78
79    /**
80       return the exit value of this process.
81       if process is null, then there is no process running
82       therefore the return value is 0.
83     */

84     public int exitValue() {
85         if (process == null) return -1;
86         return process.exitValue();
87     }
88
89 }
90
Popular Tags