KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > update > CommandLine


1 package com.openedit.archive.update;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 public class CommandLine implements Executor {
11     private static final Log log = LogFactory.getLog(CommandLine.class);
12
13     public boolean runExec(List JavaDoc com) {
14         if ( com ==null)
15             return false;
16         String JavaDoc[] cmd = (String JavaDoc[]) com.toArray(new String JavaDoc[com.size()]);
17         return exec(cmd);
18     }
19     
20     public boolean exec(List JavaDoc commandLine) {
21         if ( commandLine ==null)
22             return false;
23         String JavaDoc[] cmd = new String JavaDoc[commandLine.size()];
24         int i =0;
25         for (Iterator JavaDoc iter = commandLine.iterator(); iter.hasNext();) {
26             String JavaDoc element = (String JavaDoc) iter.next();
27             cmd[i++] = element;
28         }
29         return exec(cmd);
30     }
31
32     public boolean exec(String JavaDoc[] cmd) {
33         if ( cmd ==null)
34             return false;
35         int exitVal = 0;
36         try {
37             Runtime JavaDoc runtime = Runtime.getRuntime();
38             reportInfo("Running: ", cmd);
39             Process JavaDoc proc = runtime.exec(cmd);
40             exitVal = proc.waitFor();
41             if (exitVal != 0) {
42                 reportError(cmd);
43             }
44         } catch (IOException JavaDoc x) {
45             reportError(cmd);
46             return false;
47         } catch (InterruptedException JavaDoc e) {
48             reportError(cmd);
49             return false;
50         }
51         return exitVal == 0;
52     }
53
54     protected void reportError(String JavaDoc[] cmd) {
55         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
56         for (int i = 0; i < cmd.length; i++) {
57             out.append(cmd[i]);
58             out.append(" ");
59         }
60         log.error("Error on: " + out);
61     }
62
63     protected void reportInfo(String JavaDoc label, String JavaDoc[] cmd) {
64         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
65         for (int i = 0; i < cmd.length; i++) {
66             out.append(cmd[i]);
67             out.append(" ");
68         }
69         log.info(label + out);
70     }
71
72 }
Popular Tags