Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.api.extexecution; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.util.ArrayList; |
47 |
import java.util.HashMap; |
48 |
import java.util.List; |
49 |
import java.util.Map; |
50 |
import java.util.concurrent.Callable; |
51 |
import org.netbeans.api.annotations.common.NonNull; |
52 |
import org.netbeans.api.annotations.common.NullAllowed; |
53 |
import org.netbeans.spi.extexecution.ProcessBuilderFactory; |
54 |
import org.netbeans.spi.extexecution.ProcessBuilderImplementation; |
55 |
import org.openide.util.NbBundle; |
56 |
import org.openide.util.Parameters; |
57 |
|
58 |
/** |
59 |
* Abstraction of process builders. You can freely configure the parameters |
60 |
* and then create a process by calling the {@link #call()} method. You can |
61 |
* also (re)configure the builder and spawn a different process. |
62 |
* <p> |
63 |
* Note the API does not prescribe the actual meaning of {@link Process}. |
64 |
* It may be local process, remote process or some other implementation. |
65 |
* <p> |
66 |
* You can use the default implementation returned by {@link #getLocal()} |
67 |
* for creating the local machine OS processes. |
68 |
* <p> |
69 |
* <i>Thread safety</i> of this class depends on thread safety of |
70 |
* the {@link ProcessBuilderImplementation} the class is using. If it is thread |
71 |
* safe (if possible the implementation should be even stateless) this class |
72 |
* is thread safe as well. |
73 |
* |
74 |
* @author Petr Hejl |
75 |
* @since 1.28 |
76 |
*/ |
77 |
public final class ProcessBuilder implements Callable<Process> { |
78 |
|
79 |
private final ProcessBuilderImplementation implementation; |
80 |
|
81 |
private final String description; |
82 |
|
83 |
/**<i>GuardedBy("this")</i>*/ |
84 |
private String executable; |
85 |
|
86 |
/**<i>GuardedBy("this")</i>*/ |
87 |
private String workingDirectory; |
88 |
|
89 |
/**<i>GuardedBy("this")</i>*/ |
90 |
private List<String> arguments = new ArrayList<String>(); |
91 |
|
92 |
/**<i>GuardedBy("this")</i>*/ |
93 |
private List<String> paths = new ArrayList<String>(); |
94 |
|
95 |
/**<i>GuardedBy("this")</i>*/ |
96 |
private Map<String, String> envVariables = new HashMap<String, String>(); |
97 |
|
98 |
/**<i>GuardedBy("this")</i>*/ |
99 |
private boolean redirectErrorStream; |
100 |
|
101 |
static { |
102 |
ProcessBuilderFactory.Accessor.DEFAULT = new ProcessBuilderFactory.Accessor() { |
103 |
|
104 |
@Override |
105 |
public ProcessBuilder createProcessBuilder(ProcessBuilderImplementation impl, String description) { |
106 |
return new ProcessBuilder(impl, description); |
107 |
} |
108 |
}; |
109 |
} |
110 |
|
111 |
private ProcessBuilder(ProcessBuilderImplementation implementation, String description) { |
112 |
this.implementation = implementation; |
113 |
this.description = description; |
114 |
} |
115 |
|
116 |
/** |
117 |
* Returns the {@link ProcessBuilder} creating the OS process on local |
118 |
* machine. Returned implementation is <code>thread safe</code>. |
119 |
* |
120 |
* @return the {@link ProcessBuilder} creating the OS process on local |
121 |
* machine |
122 |
*/ |
123 |
public static ProcessBuilder getLocal() { |
124 |
return new ProcessBuilder(new LocalProcessFactory(), |
125 |
NbBundle.getMessage(ProcessBuilder.class, "LocalProcessFactory")); |
126 |
} |
127 |
|
128 |
/** |
129 |
* Returns the human readable description of this builder. |
130 |
* |
131 |
* @return the human readable description of this builder |
132 |
*/ |
133 |
@NonNull |
134 |
public String getDescription() { |
135 |
return description; |
136 |
} |
137 |
|
138 |
/** |
139 |
* Sets the executable to run. There is no default value. The {@link #call()} |
140 |
* methods throws {@link IllegalStateException} when there is no executable |
141 |
* configured. |
142 |
* |
143 |
* @param executable the executable to run |
144 |
*/ |
145 |
public void setExecutable(@NonNull String executable) { |
146 |
Parameters.notNull("executable", executable); |
147 |
|
148 |
synchronized (this) { |
149 |
this.executable = executable; |
150 |
} |
151 |
} |
152 |
|
153 |
/** |
154 |
* Sets the working directory for the process created by subsequent call |
155 |
* of {@link #call()}. The default value is implementation specific. |
156 |
* |
157 |
* @param workingDirectory the working directory of the process |
158 |
*/ |
159 |
public void setWorkingDirectory(@NullAllowed String workingDirectory) { |
160 |
synchronized (this) { |
161 |
this.workingDirectory = workingDirectory; |
162 |
} |
163 |
} |
164 |
|
165 |
/** |
166 |
* Sets the arguments passed to the process created by subsequent call |
167 |
* of {@link #call()}. By default there are no arguments. |
168 |
* |
169 |
* @param arguments the arguments passed to the process |
170 |
*/ |
171 |
public void setArguments(@NonNull List<String> arguments) { |
172 |
Parameters.notNull("arguments", arguments); |
173 |
|
174 |
synchronized (this) { |
175 |
this.arguments.clear(); |
176 |
this.arguments.addAll(arguments); |
177 |
} |
178 |
} |
179 |
|
180 |
/** |
181 |
* Sets the environment variables for the process created by subsequent call |
182 |
* of {@link #call()}. By default there are no environment variables with |
183 |
* exception of <code>PATH</code> possibly configured by {@link #setPaths(java.util.List)}. |
184 |
* |
185 |
* @param envVariables the environment variables for the process |
186 |
*/ |
187 |
public void setEnvironmentVariables(@NonNull Map<String, String> envVariables) { |
188 |
Parameters.notNull("envVariables", envVariables); |
189 |
|
190 |
synchronized (this) { |
191 |
this.envVariables.clear(); |
192 |
this.envVariables.putAll(envVariables); |
193 |
} |
194 |
} |
195 |
|
196 |
/** |
197 |
* Sets the additional paths to be included in <code>PATH</code> environment |
198 |
* variable for the process. |
199 |
* |
200 |
* @param paths the additional paths to be included in <code>PATH</code> |
201 |
* environment variable |
202 |
*/ |
203 |
public void setPaths(@NonNull List<String> paths) { |
204 |
Parameters.notNull("paths", paths); |
205 |
|
206 |
synchronized (this) { |
207 |
this.paths.clear(); |
208 |
this.paths.addAll(paths); |
209 |
} |
210 |
} |
211 |
|
212 |
/** |
213 |
* Configures the error stream redirection. If <code>true</code> the error |
214 |
* stream of process created by subsequent call of {@link #call()} method |
215 |
* will be redirected to standard output stream. |
216 |
* |
217 |
* @param redirectErrorStream the error stream redirection |
218 |
*/ |
219 |
public void setRedirectErrorStream(boolean redirectErrorStream) { |
220 |
synchronized (this) { |
221 |
this.redirectErrorStream = redirectErrorStream; |
222 |
} |
223 |
} |
224 |
|
225 |
/** |
226 |
* Creates the new {@link Process} based on the properties configured |
227 |
* in this builder. |
228 |
* <p> |
229 |
* Actual behavior depends on the builder implementation, but it should |
230 |
* respect all the properties configured on this builder. |
231 |
* |
232 |
* @see ProcessBuilderImplementation |
233 |
* @return the new {@link Process} based on the properties configured |
234 |
* in this builder |
235 |
* @throws IOException if the process could not be created |
236 |
* @throws IllegalStateException if there is no executable configured |
237 |
* by {@link #setExecutable(java.lang.String)} |
238 |
*/ |
239 |
@NonNull |
240 |
@Override |
241 |
public Process call() throws IOException { |
242 |
String currentExecutable = null; |
243 |
String currentWorkingDirectory = null; |
244 |
List<String> currentArguments = new ArrayList<String>(); |
245 |
List<String> currentPaths = new ArrayList<String>(); |
246 |
Map<String, String> currentEnvVariables = new HashMap<String, String>(); |
247 |
boolean currentRedirectErrorStream = false; |
248 |
|
249 |
synchronized (this) { |
250 |
currentExecutable = executable; |
251 |
currentWorkingDirectory = workingDirectory; |
252 |
currentArguments.addAll(arguments); |
253 |
currentPaths.addAll(paths); |
254 |
currentEnvVariables.putAll(envVariables); |
255 |
currentRedirectErrorStream = redirectErrorStream; |
256 |
} |
257 |
|
258 |
if (currentExecutable == null) { |
259 |
throw new IllegalStateException("The executable has not been configured"); |
260 |
} |
261 |
|
262 |
return implementation.createProcess(currentExecutable, currentWorkingDirectory, currentArguments, |
263 |
currentPaths, currentEnvVariables, currentRedirectErrorStream); |
264 |
} |
265 |
|
266 |
private static class LocalProcessFactory implements ProcessBuilderImplementation { |
267 |
|
268 |
@Override |
269 |
public Process createProcess(String executable, String workingDirectory, List<String> arguments, |
270 |
List<String> paths, Map<String, String> environment, boolean redirectErrorStream) throws IOException { |
271 |
|
272 |
ExternalProcessBuilder builder = new ExternalProcessBuilder(executable); |
273 |
if (workingDirectory != null) { |
274 |
builder = builder.workingDirectory(new File(workingDirectory)); |
275 |
} |
276 |
for (String argument : arguments) { |
277 |
builder = builder.addArgument(argument); |
278 |
} |
279 |
for (String path : paths) { |
280 |
builder = builder.prependPath(new File(path)); |
281 |
} |
282 |
for (Map.Entry<String, String> entry : environment.entrySet()) { |
283 |
builder = builder.addEnvironmentVariable(entry.getKey(), entry.getValue()); |
284 |
} |
285 |
builder = builder.redirectErrorStream(redirectErrorStream); |
286 |
|
287 |
return builder.call(); |
288 |
} |
289 |
} |
290 |
} |