001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ExecUtils.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import java.io.IOException;
011: import java.util.Map;
012:
013: public abstract class ExecUtils {
014: public static Process exec(String command) throws IOException,
015: InterruptedException {
016: return exec(command, null);
017: }
018:
019: public static Process exec(String command, String[] envp)
020: throws IOException, InterruptedException {
021: Process process = null;
022: if (null == envp) {
023: process = Runtime.getRuntime().exec(command);
024: } else {
025: process = Runtime.getRuntime().exec(command, envp);
026: }
027:
028: try {
029: process.waitFor();
030: } catch (InterruptedException e) {
031: throw e;
032: }
033:
034: return process;
035: }
036:
037: public static Process exec(String commands[], String[] envp)
038: throws IOException, InterruptedException {
039: Process process = null;
040: if (null == envp) {
041: process = Runtime.getRuntime().exec(commands);
042: } else {
043: process = Runtime.getRuntime().exec(commands, envp);
044: }
045:
046: try {
047: process.waitFor();
048: } catch (InterruptedException e) {
049: throw e;
050: }
051:
052: return process;
053: }
054:
055: public static Process exec(String command, long interval,
056: long timeout) throws IOException, InterruptedException {
057: return exec(command, null, interval, timeout);
058: }
059:
060: public static Process exec(String[] commands, long interval,
061: long timeout) throws IOException, InterruptedException {
062: return exec(commands, null, interval, timeout);
063: }
064:
065: public static synchronized Process exec(String command,
066: String[] envp, long interval, long timeout)
067: throws IOException, InterruptedException {
068: Process process = null;
069: if (null == envp) {
070: process = Runtime.getRuntime().exec(command);
071: } else {
072: process = Runtime.getRuntime().exec(command, envp);
073: }
074:
075: return processTimeout(process, interval, timeout);
076: }
077:
078: public static synchronized Process exec(String[] commands,
079: String[] envp, long interval, long timeout)
080: throws IOException, InterruptedException {
081: Process process = null;
082: if (null == envp) {
083: process = Runtime.getRuntime().exec(commands);
084: } else {
085: process = Runtime.getRuntime().exec(commands, envp);
086: }
087:
088: return processTimeout(process, interval, timeout);
089: }
090:
091: private static synchronized Process processTimeout(Process process,
092: long interval, long timeout) throws InterruptedException {
093: long time_waiting = 0;
094: boolean process_finished = false;
095:
096: while (time_waiting < timeout && !process_finished) {
097: process_finished = true;
098: try {
099: Thread.sleep(interval);
100: } catch (InterruptedException e) {
101: e.fillInStackTrace();
102: throw e;
103: }
104:
105: try {
106: process.exitValue();
107: } catch (IllegalThreadStateException e) {
108: // process hasn't finished yet
109: process_finished = false;
110: }
111: time_waiting += interval;
112: }
113:
114: if (process_finished) {
115: return process;
116: } else {
117: process.destroy();
118: return null;
119: }
120: }
121:
122: public static void outputSystemProperties() {
123: Map.Entry property = null;
124: for (Object element : System.getProperties().entrySet()) {
125: property = (Map.Entry) element;
126: System.out.println(property.getKey() + "="
127: + property.getValue());
128: }
129: }
130: }
|