raster@14181
|
1 |
/*
|
raster@14181
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
raster@14181
|
3 |
*
|
raster@14181
|
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
|
raster@14181
|
5 |
*
|
raster@14181
|
6 |
* The contents of this file are subject to the terms of either the GNU
|
raster@14181
|
7 |
* General Public License Version 2 only ("GPL") or the Common
|
raster@14181
|
8 |
* Development and Distribution License("CDDL") (collectively, the
|
raster@14181
|
9 |
* "License"). You may not use this file except in compliance with the
|
raster@14181
|
10 |
* License. You can obtain a copy of the License at
|
raster@14181
|
11 |
* http://www.netbeans.org/cddl-gplv2.html
|
raster@14181
|
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
raster@14181
|
13 |
* specific language governing permissions and limitations under the
|
raster@14181
|
14 |
* License. When distributing the software, include this License Header
|
raster@14181
|
15 |
* Notice in each file and include the License file at
|
raster@14181
|
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
raster@14181
|
17 |
* particular file as subject to the "Classpath" exception as provided
|
raster@14181
|
18 |
* by Sun in the GPL Version 2 section of the License file that
|
raster@14181
|
19 |
* accompanied this code. If applicable, add the following below the
|
raster@14181
|
20 |
* License Header, with the fields enclosed by brackets [] replaced by
|
raster@14181
|
21 |
* your own identifying information:
|
raster@14181
|
22 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
raster@14181
|
23 |
*
|
raster@14181
|
24 |
* If you wish your version of this file to be governed by only the CDDL
|
raster@14181
|
25 |
* or only the GPL Version 2, indicate your decision by adding
|
raster@14181
|
26 |
* "[Contributor] elects to include this software in this distribution
|
raster@14181
|
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
raster@14181
|
28 |
* single choice of license, a recipient has the option to distribute
|
raster@14181
|
29 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
raster@14181
|
30 |
* to extend the choice of license to its licensees as provided above.
|
raster@14181
|
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
raster@14181
|
32 |
* Version 2 license, then the option applies only if the new code is
|
raster@14181
|
33 |
* made subject to such option by the copyright holder.
|
raster@14181
|
34 |
*
|
raster@14181
|
35 |
* Contributor(s):
|
raster@14181
|
36 |
*
|
raster@14181
|
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc.
|
raster@14181
|
38 |
*/
|
raster@14181
|
39 |
package org.netbeans.api.ada.platform;
|
raster@14181
|
40 |
|
raster@14181
|
41 |
import java.io.File;
|
raster@14181
|
42 |
import java.io.IOException;
|
raster@14488
|
43 |
import java.io.InputStream;
|
raster@14488
|
44 |
import java.io.OutputStream;
|
raster@14181
|
45 |
import java.io.Reader;
|
raster@14181
|
46 |
import java.io.StringReader;
|
raster@14181
|
47 |
import java.io.Writer;
|
raster@14181
|
48 |
import java.util.concurrent.Future;
|
phejl@14545
|
49 |
import org.netbeans.api.extexecution.ExecutionDescriptor;
|
phejl@14545
|
50 |
import org.netbeans.api.extexecution.ExecutionService;
|
phejl@14545
|
51 |
import org.netbeans.api.extexecution.ExternalProcessBuilder;
|
phejl@14545
|
52 |
import org.netbeans.api.extexecution.input.InputProcessor;
|
raster@14181
|
53 |
import org.openide.util.Exceptions;
|
raster@14181
|
54 |
|
raster@14181
|
55 |
/**
|
raster@14181
|
56 |
*
|
raster@14181
|
57 |
* @author Andrea Lucarelli
|
raster@14181
|
58 |
*/
|
raster@14181
|
59 |
public class AdaExecution {
|
raster@14181
|
60 |
// execution commands
|
raster@14181
|
61 |
|
raster@14181
|
62 |
private String command;
|
raster@14181
|
63 |
private String workingDirectory;
|
raster@14181
|
64 |
private String commandArgs;
|
raster@14181
|
65 |
private String displayName;
|
raster@14698
|
66 |
private boolean redirect;
|
raster@16367
|
67 |
|
raster@14181
|
68 |
private ExecutionDescriptor descriptor = new ExecutionDescriptor().frontWindow(true).controllable(true).inputVisible(true).showProgress(true).showSuspended(true);
|
raster@14181
|
69 |
|
raster@14181
|
70 |
/**
|
raster@14181
|
71 |
* Execute the process described by this object
|
raster@14181
|
72 |
* @return a Future object that provides the status of the running process
|
raster@14181
|
73 |
*/
|
raster@14181
|
74 |
public synchronized Future<Integer> run() {
|
raster@14181
|
75 |
try {
|
raster@14181
|
76 |
ExecutionService service = ExecutionService.newService(buildProcess(), descriptor, displayName);
|
raster@14181
|
77 |
return service.run();
|
raster@14181
|
78 |
} catch (Exception ex) {
|
raster@14181
|
79 |
Exceptions.printStackTrace(ex);
|
raster@14181
|
80 |
return null;
|
raster@14181
|
81 |
}
|
raster@14181
|
82 |
|
raster@14181
|
83 |
}
|
raster@14181
|
84 |
|
raster@14488
|
85 |
// TODO: To modify in Custom Execution Service. See org.netbeans.modules.extexecution.api.ExecutionServiceTest.
|
raster@14181
|
86 |
private ExternalProcessBuilder buildProcess() throws IOException {
|
raster@14181
|
87 |
ExternalProcessBuilder processBuilder = new ExternalProcessBuilder(command);
|
raster@14181
|
88 |
processBuilder = processBuilder.workingDirectory(new File(workingDirectory));
|
raster@14698
|
89 |
processBuilder = processBuilder.redirectErrorStream(redirect);
|
raster@14181
|
90 |
if (commandArgs != null) {
|
raster@14461
|
91 |
String args[] = org.openide.util.Utilities.parseParameters(commandArgs);
|
raster@14461
|
92 |
for (int index = 0; index < args.length; index++) {
|
raster@14461
|
93 |
processBuilder = processBuilder.addArgument(args[index]);
|
raster@14461
|
94 |
}
|
raster@14181
|
95 |
}
|
raster@14181
|
96 |
return processBuilder;
|
raster@14181
|
97 |
}
|
raster@14181
|
98 |
|
raster@14698
|
99 |
// TODO: To use when Custom Execution Service will be created.
|
raster@14488
|
100 |
private static class CheckProcess extends Process {
|
raster@14488
|
101 |
|
raster@14488
|
102 |
private final int returnValue;
|
raster@14488
|
103 |
private boolean finished;
|
raster@14488
|
104 |
private boolean started;
|
raster@14488
|
105 |
|
raster@14488
|
106 |
public CheckProcess(int returnValue) {
|
raster@14488
|
107 |
this.returnValue = returnValue;
|
raster@14488
|
108 |
}
|
raster@14488
|
109 |
|
raster@14488
|
110 |
public void start() {
|
raster@14488
|
111 |
synchronized (this) {
|
raster@14488
|
112 |
started = true;
|
raster@14488
|
113 |
notifyAll();
|
raster@14488
|
114 |
}
|
raster@14488
|
115 |
}
|
raster@14488
|
116 |
|
raster@14488
|
117 |
public boolean isStarted() {
|
raster@14488
|
118 |
synchronized (this) {
|
raster@14488
|
119 |
return started;
|
raster@14488
|
120 |
}
|
raster@14488
|
121 |
}
|
raster@14488
|
122 |
|
raster@14488
|
123 |
public boolean isFinished() {
|
raster@14488
|
124 |
synchronized (this) {
|
raster@14488
|
125 |
return finished;
|
raster@14488
|
126 |
}
|
raster@14488
|
127 |
}
|
raster@14488
|
128 |
|
raster@14488
|
129 |
@Override
|
raster@14488
|
130 |
public void destroy() {
|
raster@14488
|
131 |
synchronized (this) {
|
raster@14488
|
132 |
if (finished) {
|
raster@14488
|
133 |
return;
|
raster@14488
|
134 |
}
|
raster@14488
|
135 |
|
raster@14488
|
136 |
finished = true;
|
raster@14488
|
137 |
notifyAll();
|
raster@14488
|
138 |
}
|
raster@14488
|
139 |
}
|
raster@14488
|
140 |
|
raster@14488
|
141 |
@Override
|
raster@14488
|
142 |
public int exitValue() {
|
raster@14488
|
143 |
synchronized (this) {
|
raster@14488
|
144 |
if (!finished) {
|
raster@14488
|
145 |
throw new IllegalStateException("Not finished yet");
|
raster@14488
|
146 |
}
|
raster@14488
|
147 |
}
|
raster@14488
|
148 |
return returnValue;
|
raster@14488
|
149 |
}
|
raster@14488
|
150 |
|
raster@14488
|
151 |
@Override
|
raster@14488
|
152 |
public InputStream getErrorStream() {
|
raster@14488
|
153 |
return new InputStream() {
|
raster@14488
|
154 |
|
raster@14488
|
155 |
@Override
|
raster@14488
|
156 |
public int read() throws IOException {
|
raster@14488
|
157 |
return -1;
|
raster@14488
|
158 |
}
|
raster@14488
|
159 |
};
|
raster@14488
|
160 |
}
|
raster@14488
|
161 |
|
raster@14488
|
162 |
@Override
|
raster@14488
|
163 |
public InputStream getInputStream() {
|
raster@14488
|
164 |
return new InputStream() {
|
raster@14488
|
165 |
|
raster@14488
|
166 |
@Override
|
raster@14488
|
167 |
public int read() throws IOException {
|
raster@14488
|
168 |
throw new UnsupportedOperationException("Not supported yet.");
|
raster@14488
|
169 |
}
|
raster@14488
|
170 |
};
|
raster@14488
|
171 |
}
|
raster@14488
|
172 |
|
raster@14488
|
173 |
@Override
|
raster@14488
|
174 |
public OutputStream getOutputStream() {
|
raster@14488
|
175 |
return new OutputStream() {
|
raster@14488
|
176 |
|
raster@14488
|
177 |
@Override
|
raster@14488
|
178 |
public void write(int b) throws IOException {
|
raster@14488
|
179 |
// throw it away
|
raster@14488
|
180 |
}
|
raster@14488
|
181 |
};
|
raster@14488
|
182 |
}
|
raster@14488
|
183 |
|
raster@14488
|
184 |
@Override
|
raster@14488
|
185 |
public int waitFor() throws InterruptedException {
|
raster@14488
|
186 |
synchronized (this) {
|
raster@14488
|
187 |
while (!finished) {
|
raster@14488
|
188 |
wait();
|
raster@14488
|
189 |
}
|
raster@14488
|
190 |
}
|
raster@14488
|
191 |
return returnValue;
|
raster@14488
|
192 |
}
|
raster@14488
|
193 |
|
raster@14488
|
194 |
public void waitStarted() throws InterruptedException {
|
raster@14488
|
195 |
synchronized (this) {
|
raster@14488
|
196 |
while (!started) {
|
raster@14488
|
197 |
wait();
|
raster@14488
|
198 |
}
|
raster@14488
|
199 |
}
|
raster@14488
|
200 |
}
|
raster@14488
|
201 |
}
|
raster@14488
|
202 |
|
raster@14181
|
203 |
public synchronized String getCommand() {
|
raster@14181
|
204 |
return command;
|
raster@14181
|
205 |
}
|
raster@14181
|
206 |
|
raster@14181
|
207 |
public synchronized void setCommand(String command) {
|
raster@14181
|
208 |
this.command = command;
|
raster@14181
|
209 |
}
|
raster@14181
|
210 |
|
raster@14181
|
211 |
public synchronized String getCommandArgs() {
|
raster@14181
|
212 |
return commandArgs;
|
raster@14181
|
213 |
}
|
raster@14181
|
214 |
|
raster@14181
|
215 |
public synchronized void setCommandArgs(String commandArgs) {
|
raster@14181
|
216 |
this.commandArgs = commandArgs;
|
raster@14181
|
217 |
}
|
raster@14181
|
218 |
|
raster@14181
|
219 |
public synchronized String getWorkingDirectory() {
|
raster@14181
|
220 |
return workingDirectory;
|
raster@14181
|
221 |
}
|
raster@14181
|
222 |
|
raster@14181
|
223 |
public synchronized void setWorkingDirectory(String workingDirectory) {
|
raster@14181
|
224 |
this.workingDirectory = workingDirectory;
|
raster@14181
|
225 |
}
|
raster@14181
|
226 |
|
raster@14181
|
227 |
public synchronized String getDisplayName() {
|
raster@14181
|
228 |
return displayName;
|
raster@14181
|
229 |
}
|
raster@14181
|
230 |
|
raster@14181
|
231 |
public synchronized void setDisplayName(String displayName) {
|
raster@14181
|
232 |
this.displayName = displayName;
|
raster@14181
|
233 |
}
|
raster@14181
|
234 |
|
raster@14181
|
235 |
public synchronized void setShowControls(boolean showControls) {
|
raster@14181
|
236 |
descriptor = descriptor.controllable(showControls);
|
raster@14181
|
237 |
}
|
raster@14181
|
238 |
|
raster@14181
|
239 |
public synchronized void setShowInput(boolean showInput) {
|
raster@14181
|
240 |
descriptor = descriptor.inputVisible(showInput);
|
raster@14181
|
241 |
}
|
raster@14181
|
242 |
|
raster@14181
|
243 |
public synchronized void setShowProgress(boolean showProgress) {
|
raster@14181
|
244 |
descriptor = descriptor.showProgress(showProgress);
|
raster@14181
|
245 |
}
|
raster@14181
|
246 |
|
raster@14698
|
247 |
public synchronized void setRedirectError(boolean redirect){
|
raster@14698
|
248 |
this.redirect = redirect;
|
raster@14698
|
249 |
}
|
raster@14698
|
250 |
|
raster@14181
|
251 |
/**
|
raster@14181
|
252 |
* Can the process be suppended
|
raster@14181
|
253 |
* @param showSuspended boolean to set the status
|
raster@14181
|
254 |
*/
|
raster@14181
|
255 |
public synchronized void setShowSuspended(boolean showSuspended) {
|
raster@14181
|
256 |
descriptor = descriptor.showSuspended(showSuspended);
|
raster@14181
|
257 |
}
|
raster@14181
|
258 |
|
raster@14181
|
259 |
/**
|
raster@14181
|
260 |
* Show the window of the running process
|
raster@14181
|
261 |
* @param showWindow display the windown or not?
|
raster@14181
|
262 |
*/
|
raster@14181
|
263 |
public synchronized void setShowWindow(boolean showWindow) {
|
raster@14181
|
264 |
descriptor = descriptor.frontWindow(showWindow);
|
raster@14181
|
265 |
}
|
raster@14181
|
266 |
private final AdaOutputProcessor outProcessor = new AdaOutputProcessor();
|
raster@14181
|
267 |
|
raster@14181
|
268 |
/**
|
raster@14181
|
269 |
* Attach a Processor to collect the output of the running process
|
raster@14181
|
270 |
*/
|
raster@14181
|
271 |
public void attachOutputProcessor() {
|
raster@14181
|
272 |
descriptor = descriptor.outProcessorFactory(new ExecutionDescriptor.InputProcessorFactory() {
|
raster@14181
|
273 |
|
raster@14698
|
274 |
public InputProcessor newInputProcessor() {
|
raster@14698
|
275 |
return outProcessor;
|
raster@14698
|
276 |
}
|
raster@14698
|
277 |
|
phejl@14545
|
278 |
public InputProcessor newInputProcessor(InputProcessor defaultProcessor) {
|
raster@14698
|
279 |
return outProcessor;
|
raster@14181
|
280 |
}
|
raster@14181
|
281 |
});
|
raster@14181
|
282 |
}
|
raster@14181
|
283 |
|
raster@14181
|
284 |
/**
|
raster@14181
|
285 |
* Retive the output form the running process
|
raster@14181
|
286 |
* @return a string reader for the process
|
raster@14181
|
287 |
*/
|
raster@14181
|
288 |
public Reader getOutput() {
|
raster@14181
|
289 |
return new StringReader(outProcessor.getData());
|
raster@14181
|
290 |
}
|
raster@14181
|
291 |
|
raster@14181
|
292 |
/**
|
raster@14181
|
293 |
* Attach input processor to the running process
|
raster@14181
|
294 |
*/
|
raster@14181
|
295 |
public void attachInputProcessor() {
|
raster@14181
|
296 |
//descriptor = descriptor.
|
raster@14181
|
297 |
}
|
raster@14181
|
298 |
|
raster@14181
|
299 |
/**
|
raster@14181
|
300 |
* Writes data to the running process
|
raster@14181
|
301 |
* @return StringWirter
|
raster@14181
|
302 |
*/
|
raster@14181
|
303 |
public Writer getInput() {
|
raster@14181
|
304 |
return null;
|
raster@14181
|
305 |
}
|
raster@14488
|
306 |
|
raster@14488
|
307 |
public void setPostExecution(Runnable postExecution) {
|
raster@14488
|
308 |
descriptor.postExecution(postExecution);
|
raster@14488
|
309 |
}
|
raster@14181
|
310 |
}
|