KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > ExecProcUtil


1 /*
2  
3  Derby - Class org.apache.derbyTesting.functionTests.util.ExecProcUtil
4  
5  Licensed to the Apache Software Foundation (ASF) under one or more
6  contributor license agreements. See the NOTICE file distributed with
7  this work for additional information regarding copyright ownership.
8  The ASF licenses this file to You under the Apache License, Version 2.0
9  (the "License"); you may not use this file except in compliance with
10  the License. You may obtain a copy of the License at
11  
12  http://www.apache.org/licenses/LICENSE-2.0
13  
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19  
20  */

21 package org.apache.derbyTesting.functionTests.util;
22
23 import org.apache.derbyTesting.functionTests.harness.ProcessStreamResult;
24 import org.apache.derbyTesting.functionTests.harness.TimedProcess;
25 import java.util.Vector JavaDoc;
26 import java.io.BufferedOutputStream JavaDoc;
27 /**
28  * Utility class to hold helper methods to exec new processes
29  */

30 public class ExecProcUtil {
31     
32     /**
33      * For each new exec process done, set
34      * timeout for ProcessStreamResult after which the thread that
35      * handles the streams for the process exits. Timeout is in minutes.
36      * Note: timeout handling will only come into effect when
37      * ProcessStreamResult#Wait() is called
38      */

39     private static String JavaDoc timeoutMinutes = "2";
40     
41     /**
42      * timeout in seconds for the processes spawned.
43      */

44     private static int timeoutSecondsForProcess = 180;
45     
46     /**
47      * Execute the given command and dump the results to standard out
48      *
49      * @param args command and arguments
50      * @param vCmd java command line arguments.
51      * @param bos buffered stream (System.out) to dump results to.
52      * @exception Exception
53      */

54     public static void execCmdDumpResults(String JavaDoc[] args, Vector JavaDoc vCmd,
55             BufferedOutputStream JavaDoc bos) throws Exception JavaDoc {
56         // We need the process inputstream and errorstream
57
ProcessStreamResult prout = null;
58         ProcessStreamResult prerr = null;
59
60         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
61
62         for (int i = 0; i < args.length; i++) {
63             sb.append(args[i] + " ");
64         }
65         System.out.println(sb.toString());
66         int totalSize = vCmd.size() + args.length;
67         String JavaDoc serverCmd[] = new String JavaDoc[totalSize];
68
69         int i = 0;
70         for (i = 0; i < vCmd.size(); i++)
71             serverCmd[i] = (String JavaDoc) vCmd.elementAt(i);
72
73         for (int j = 0; i < totalSize; i++)
74             serverCmd[i] = args[j++];
75
76         System.out.flush();
77         bos.flush();
78
79         // Start a process to run the command
80
Process JavaDoc pr = Runtime.getRuntime().exec(serverCmd);
81
82         // TimedProcess, kill process if process doesnt finish in a certain
83
// amount of time
84
TimedProcess tp = new TimedProcess(pr);
85         prout = new ProcessStreamResult(pr.getInputStream(), bos,
86                 timeoutMinutes);
87         prerr = new ProcessStreamResult(pr.getErrorStream(), bos,
88                 timeoutMinutes);
89
90         // wait until all the results have been processed
91
boolean outTimedOut = prout.Wait();
92         boolean errTimedOut = prerr.Wait();
93         
94         // wait for this process to terminate, upto a wait period
95
// of 'timeoutSecondsForProcess'
96
// if process has already been terminated, this call will
97
// return immediately.
98
tp.waitFor(timeoutSecondsForProcess);
99         pr = null;
100         
101         if (outTimedOut || errTimedOut)
102             System.out.println(" Reading from process streams timed out.. ");
103
104         System.out.flush();
105     }
106     
107 }
108
Popular Tags