1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| package edu.rice.cs.util; |
38 |
| |
39 |
| import edu.rice.cs.drjava.config.PropertyMaps; |
40 |
| |
41 |
| import java.io.File; |
42 |
| import java.io.IOException; |
43 |
| import java.util.ArrayList; |
44 |
| import java.util.List; |
45 |
| import java.util.Map; |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| public class GeneralProcessCreator extends ProcessCreator { |
54 |
| protected List<List<List<String>>> _seqs; |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
0
| public GeneralProcessCreator(String cmdline, String workdir, PropertyMaps pm) {
|
62 |
0
| _cmdline = cmdline;
|
63 |
0
| _workdir = workdir;
|
64 |
0
| _props = pm;
|
65 |
| } |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
0
| public GeneralProcessCreator(List<List<List<String>>> seqs, String workdir, PropertyMaps pm) {
|
74 |
0
| _seqs = seqs;
|
75 |
0
| _workdir = workdir;
|
76 |
0
| _props = pm;
|
77 |
| } |
78 |
| |
79 |
| |
80 |
0
| protected static String getProcessCmdLine(List<String> cmds) {
|
81 |
0
| StringBuilder sb = new StringBuilder();
|
82 |
0
| for (int i = 0; i < cmds.size(); ++i) {
|
83 |
0
| sb.append(" ");
|
84 |
0
| sb.append(StringOps.unescapeFileName(cmds.get(i)));
|
85 |
| } |
86 |
0
| String s = sb.toString();
|
87 |
0
| if (s.length() > 0) {
|
88 |
0
| s = s.substring(1);
|
89 |
| } |
90 |
0
| return s;
|
91 |
| } |
92 |
| |
93 |
| |
94 |
0
| protected static String getProcessChainCmdLine(List<List<String>> pipe) {
|
95 |
0
| StringBuilder sb = new StringBuilder();
|
96 |
0
| final String sep = " " + ProcessChain.PIPE_SEPARATOR + " ";
|
97 |
0
| for (int i = 0; i < pipe.size(); ++i) {
|
98 |
0
| sb.append(sep);
|
99 |
0
| sb.append(getProcessCmdLine(pipe.get(i)));
|
100 |
| } |
101 |
0
| String s = sb.toString();
|
102 |
0
| if (s.length() > 0) {
|
103 |
0
| s = s.substring(sep.length());
|
104 |
| } |
105 |
0
| return s;
|
106 |
| } |
107 |
| |
108 |
| |
109 |
0
| protected static String getProcessSequenceCmdLine(List<List<List<String>>> seqs) {
|
110 |
0
| StringBuilder sb = new StringBuilder();
|
111 |
0
| final String sep = " " + ProcessChain.PROCESS_SEPARATOR + " ";
|
112 |
0
| for (int i = 0; i < seqs.size(); ++i) {
|
113 |
0
| sb.append(sep);
|
114 |
0
| sb.append(getProcessChainCmdLine(seqs.get(i)));
|
115 |
| } |
116 |
0
| String s = sb.toString();
|
117 |
0
| if (s.length() > 0) {
|
118 |
0
| s = s.substring(sep.length());
|
119 |
| } |
120 |
0
| return s;
|
121 |
| } |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
0
| public String cmdline() {
|
127 |
0
| if (_cmdline == null) {
|
128 |
0
| if (_cachedCmdLine == null) {
|
129 |
0
| if (_seqs.size() == 1) {
|
130 |
| |
131 |
0
| List<List<String>> pipe = _seqs.get(0);
|
132 |
0
| if (pipe.size() == 1) {
|
133 |
| |
134 |
0
| List<String> cmds = pipe.get(0);
|
135 |
0
| _cachedCmdLine = getProcessCmdLine(cmds);
|
136 |
| } |
137 |
| else { |
138 |
| |
139 |
0
| _cachedCmdLine = getProcessChainCmdLine(pipe);
|
140 |
| } |
141 |
| } |
142 |
| else { |
143 |
| |
144 |
0
| _cachedCmdLine = getProcessSequenceCmdLine(_seqs);
|
145 |
| } |
146 |
| } |
147 |
0
| return _cachedCmdLine;
|
148 |
| } |
149 |
| else { |
150 |
0
| return _cmdline;
|
151 |
| } |
152 |
| } |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
0
| public Map<String,String> environment() {
|
158 |
0
| return _env;
|
159 |
| } |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
0
| public String workDir() {
|
165 |
0
| return _workdir;
|
166 |
| } |
167 |
| |
168 |
| public static final edu.rice.cs.util.Log LOG = new edu.rice.cs.util.Log("process.txt",false); |
169 |
| |
170 |
| |
171 |
0
| public Process start() throws IOException {
|
172 |
| |
173 |
0
| _evaluatedWorkDir = StringOps.replaceVariables(_workdir, _props, PropertyMaps.GET_CURRENT);
|
174 |
0
| _evaluatedWorkDir = StringOps.unescapeFileName(_evaluatedWorkDir);
|
175 |
0
| File dir = null;
|
176 |
0
| if (!_evaluatedWorkDir.trim().equals("")) { dir = new File(_evaluatedWorkDir); }
|
177 |
| |
178 |
| |
179 |
0
| String[] env = null;
|
180 |
0
| if ((_env != null) && (_env.size() > 0)) {
|
181 |
0
| env = new String[_env.size()];
|
182 |
0
| int i = 0;
|
183 |
0
| for(String key: _env.keySet()) {
|
184 |
0
| String value = _env.get(key);
|
185 |
0
| env[i] = key + "=" + value;
|
186 |
| } |
187 |
| } |
188 |
| |
189 |
| |
190 |
0
| if (_cmdline != null) {
|
191 |
0
| _evaluatedCmdLine = StringOps.replaceVariables(_cmdline, _props, PropertyMaps.GET_CURRENT);
|
192 |
0
| _seqs = StringOps.commandLineToLists(_evaluatedCmdLine);
|
193 |
| } |
194 |
0
| LOG.log("\t" + edu.rice.cs.plt.iter.IterUtil.toString(_seqs));
|
195 |
0
| if (_seqs.size()<1) { throw new IOException("No process to start."); }
|
196 |
0
| if (_seqs.size() == 1) {
|
197 |
| |
198 |
0
| List<List<String>> pipe = _seqs.get(0);
|
199 |
0
| if (pipe.size()<1) { throw new IOException("No process to start."); }
|
200 |
0
| if (pipe.size() == 1) {
|
201 |
| |
202 |
0
| List<String> cmds = pipe.get(0);
|
203 |
0
| if (cmds.size()<1) { throw new IOException("No process to start."); }
|
204 |
0
| String[] cmdarray = new String[cmds.size()];
|
205 |
0
| for (int i = 0; i < cmds.size(); ++i) {
|
206 |
0
| cmdarray[i] = StringOps.unescapeFileName(cmds.get(i));
|
207 |
| } |
208 |
| |
209 |
0
| return Runtime.getRuntime().exec(cmdarray,env,dir);
|
210 |
| } |
211 |
| |
212 |
0
| ProcessCreator[] creators = new ProcessCreator[pipe.size()];
|
213 |
0
| for (int i = 0; i < pipe.size(); ++i) {
|
214 |
0
| List<String> cmds = pipe.get(i);
|
215 |
0
| if (cmds.size()<1) { throw new IOException("No process to start."); }
|
216 |
0
| String[] cmdarray = new String[cmds.size()];
|
217 |
0
| for (int j=0; j<cmds.size(); ++j) {
|
218 |
0
| cmdarray[j] = StringOps.unescapeFileName(cmds.get(j));
|
219 |
| } |
220 |
0
| creators[i] = new ProcessCreator(cmdarray, _workdir);
|
221 |
| } |
222 |
0
| return new ProcessChain(creators);
|
223 |
| } |
224 |
| |
225 |
0
| ProcessCreator[] creators = new ProcessCreator[_seqs.size()];
|
226 |
0
| for (int i = 0; i < _seqs.size(); ++i) {
|
227 |
0
| List<List<List<String>>> l = new ArrayList<List<List<String>>>();
|
228 |
0
| l.add(_seqs.get(i));
|
229 |
0
| creators[i] = new GeneralProcessCreator(l, _workdir, _props);
|
230 |
| } |
231 |
0
| return new ProcessSequence(creators);
|
232 |
| } |
233 |
| } |