KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > jarprocessor > CommandStep


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.jarprocessor;
12
13 import java.io.File JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 /**
18  * @author aniefer@ca.ibm.com
19  *
20  */

21 public abstract class CommandStep implements IProcessStep {
22     protected String JavaDoc command = null;
23     protected String JavaDoc extension = null;
24     private Properties JavaDoc options = null;
25     protected boolean verbose = false;
26     
27     public CommandStep(Properties JavaDoc options, String JavaDoc command, String JavaDoc extension, boolean verbose) {
28         this.command = command;
29         this.extension = extension;
30         this.options = options;
31         this.verbose = verbose;
32     }
33
34     protected static int execute(String JavaDoc[] cmd) {
35         return execute(cmd, false);
36     }
37     
38     protected static int execute(String JavaDoc[] cmd, boolean verbose) {
39         Runtime JavaDoc runtime = Runtime.getRuntime();
40         Process JavaDoc proc = null;
41         try {
42             proc = runtime.exec(cmd);
43             StreamProcessor errorStreamProcessor = new StreamProcessor(proc.getErrorStream(), StreamProcessor.STDERR, verbose); //$NON-NLS-1$
44
StreamProcessor outputStreamProcessor = new StreamProcessor(proc.getInputStream(), StreamProcessor.STDOUT, verbose); //$NON-NLS-1$
45
errorStreamProcessor.start();
46             outputStreamProcessor.start();
47         } catch (Exception JavaDoc e) {
48             if(verbose) {
49                 System.out.println("Error executing command " + Utils.concat(cmd)); //$NON-NLS-1$
50
e.printStackTrace();
51             }
52             return -1;
53         }
54         try {
55             int result = proc.waitFor();
56             return result;
57         } catch (InterruptedException JavaDoc e) {
58             if(verbose)
59                 e.printStackTrace();
60         }
61         return -1;
62     }
63     
64     public Properties JavaDoc getOptions() {
65         if(options == null)
66             options = new Properties JavaDoc();
67         return options;
68     }
69     
70     public void adjustInf(File JavaDoc input, Properties JavaDoc inf, List JavaDoc containers) {
71         //nothing
72
}
73 }
74
Popular Tags