-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DefaultExecActionFactory.java
408 lines (335 loc) · 14.7 KB
/
DefaultExecActionFactory.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.process.internal;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.gradle.api.Action;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.DefaultFileCollectionFactory;
import org.gradle.api.internal.file.FileCollectionFactory;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.file.IdentityFileResolver;
import org.gradle.api.model.ObjectFactory;
import org.gradle.initialization.BuildCancellationToken;
import org.gradle.initialization.DefaultBuildCancellationToken;
import org.gradle.internal.concurrent.CompositeStoppable;
import org.gradle.internal.concurrent.DefaultExecutorFactory;
import org.gradle.internal.concurrent.ExecutorFactory;
import org.gradle.internal.concurrent.Stoppable;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.process.CommandLineArgumentProvider;
import org.gradle.process.ExecResult;
import org.gradle.process.ExecSpec;
import org.gradle.process.JavaDebugOptions;
import org.gradle.process.JavaExecSpec;
import org.gradle.process.JavaForkOptions;
import org.gradle.process.ProcessForkOptions;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
public class DefaultExecActionFactory implements ExecFactory {
protected final FileResolver fileResolver;
protected final Executor executor;
protected final FileCollectionFactory fileCollectionFactory;
protected final BuildCancellationToken buildCancellationToken;
private DefaultExecActionFactory(FileResolver fileResolver, FileCollectionFactory fileCollectionFactory, Executor executor, BuildCancellationToken buildCancellationToken) {
this.fileResolver = fileResolver;
this.fileCollectionFactory = fileCollectionFactory;
this.buildCancellationToken = buildCancellationToken;
this.executor = executor;
}
// Do not use this. It's here because some of the services this type needs are not easily accessed in certain cases and will be removed ay some point. Use one of the other methods instead
public static DefaultExecActionFactory root() {
IdentityFileResolver resolver = new IdentityFileResolver();
return of(resolver, new DefaultFileCollectionFactory(resolver, null), new DefaultExecutorFactory(), new DefaultBuildCancellationToken());
}
public static DefaultExecActionFactory of(FileResolver fileResolver, FileCollectionFactory fileCollectionFactory, ExecutorFactory executorFactory) {
return of(fileResolver, fileCollectionFactory, executorFactory, new DefaultBuildCancellationToken());
}
public static DefaultExecActionFactory of(FileResolver fileResolver, FileCollectionFactory fileCollectionFactory, ExecutorFactory executorFactory, BuildCancellationToken buildCancellationToken) {
return new RootExecFactory(fileResolver, fileCollectionFactory, executorFactory, buildCancellationToken);
}
@Override
public ExecFactory forContext(FileResolver fileResolver, FileCollectionFactory fileCollectionFactory, Instantiator instantiator, ObjectFactory objectFactory) {
return new DecoratingExecActionFactory(fileResolver, fileCollectionFactory, instantiator, executor, buildCancellationToken, objectFactory);
}
@Override
public ExecFactory forContext(FileResolver fileResolver, FileCollectionFactory fileCollectionFactory, Instantiator instantiator, BuildCancellationToken buildCancellationToken, ObjectFactory objectFactory) {
return new DecoratingExecActionFactory(fileResolver, fileCollectionFactory, instantiator, executor, buildCancellationToken, objectFactory);
}
@Override
public ExecAction newDecoratedExecAction() {
throw new UnsupportedOperationException();
}
@Override
public ExecAction newExecAction() {
return new DefaultExecAction(fileResolver, executor, buildCancellationToken);
}
@Override
public JavaForkOptionsInternal newDecoratedJavaForkOptions() {
throw new UnsupportedOperationException();
}
@Override
public JavaForkOptionsInternal newJavaForkOptions() {
return new DefaultJavaForkOptions(fileResolver, fileCollectionFactory, new DefaultJavaDebugOptions());
}
@Override
public JavaForkOptionsInternal immutableCopy(JavaForkOptionsInternal options) {
// NOTE: We do not want/need a decorated version of JavaForkOptions or JavaDebugOptions because
// these immutable instances are held across builds and will retain classloaders/services in the decorated object
JavaForkOptionsInternal copy = new DefaultJavaForkOptions(fileResolver, new DefaultFileCollectionFactory(fileResolver, null), new DefaultJavaDebugOptions());
options.copyTo(copy);
return new ImmutableJavaForkOptions(copy);
}
@Override
public JavaExecAction newDecoratedJavaExecAction() {
throw new UnsupportedOperationException();
}
@Override
public JavaExecAction newJavaExecAction() {
return new DefaultJavaExecAction(fileResolver, fileCollectionFactory, executor, buildCancellationToken, newJavaForkOptions());
}
@Override
public ExecHandleBuilder newExec() {
return new DefaultExecHandleBuilder(fileResolver, executor, buildCancellationToken);
}
@Override
public JavaExecHandleBuilder newJavaExec() {
return new JavaExecHandleBuilder(fileResolver, fileCollectionFactory, executor, buildCancellationToken, newJavaForkOptions());
}
@Override
public ExecResult javaexec(Action<? super JavaExecSpec> action) {
JavaExecAction execAction = newDecoratedJavaExecAction();
action.execute(execAction);
return execAction.execute();
}
@Override
public ExecResult exec(Action<? super ExecSpec> action) {
ExecAction execAction = newDecoratedExecAction();
action.execute(execAction);
return execAction.execute();
}
private static class RootExecFactory extends DefaultExecActionFactory implements Stoppable {
public RootExecFactory(FileResolver fileResolver, FileCollectionFactory fileCollectionFactory, ExecutorFactory executorFactory, BuildCancellationToken buildCancellationToken) {
super(fileResolver, fileCollectionFactory, executorFactory.create("Exec process"), buildCancellationToken);
}
@Override
public void stop() {
CompositeStoppable.stoppable(executor).stop();
}
}
private static class DecoratingExecActionFactory extends DefaultExecActionFactory {
private final Instantiator instantiator;
private final ObjectFactory objectFactory;
DecoratingExecActionFactory(FileResolver fileResolver, FileCollectionFactory fileCollectionFactory, Instantiator instantiator, Executor executor, BuildCancellationToken buildCancellationToken, ObjectFactory objectFactory) {
super(fileResolver, fileCollectionFactory, executor, buildCancellationToken);
this.instantiator = instantiator;
this.objectFactory = objectFactory;
}
@Override
public ExecAction newDecoratedExecAction() {
return instantiator.newInstance(DefaultExecAction.class, fileResolver, executor, buildCancellationToken);
}
@Override
public JavaExecAction newDecoratedJavaExecAction() {
return instantiator.newInstance(DefaultJavaExecAction.class, fileResolver, fileCollectionFactory, executor, buildCancellationToken, newDecoratedJavaForkOptions());
}
@Override
public JavaForkOptionsInternal newDecoratedJavaForkOptions() {
JavaDebugOptions javaDebugOptions = objectFactory.newInstance(DefaultJavaDebugOptions.class, objectFactory);
return instantiator.newInstance(DefaultJavaForkOptions.class, fileResolver, fileCollectionFactory, javaDebugOptions);
}
}
private static class ImmutableJavaForkOptions implements JavaForkOptionsInternal {
private final JavaForkOptionsInternal delegate;
public ImmutableJavaForkOptions(JavaForkOptionsInternal delegate) {
this.delegate = delegate;
}
@Override
public String getExecutable() {
return delegate.getExecutable();
}
@Override
public void setExecutable(String executable) {
throw new UnsupportedOperationException();
}
@Override
public Map<String, Object> getSystemProperties() {
return ImmutableMap.copyOf(delegate.getSystemProperties());
}
@Override
public void setExecutable(Object executable) {
throw new UnsupportedOperationException();
}
@Override
public void setSystemProperties(Map<String, ?> properties) {
throw new UnsupportedOperationException();
}
@Override
public ProcessForkOptions executable(Object executable) {
throw new UnsupportedOperationException();
}
@Override
public JavaForkOptions systemProperties(Map<String, ?> properties) {
throw new UnsupportedOperationException();
}
@Override
public File getWorkingDir() {
return delegate.getWorkingDir();
}
@Override
public void setWorkingDir(File dir) {
throw new UnsupportedOperationException();
}
@Override
public JavaForkOptions systemProperty(String name, Object value) {
throw new UnsupportedOperationException();
}
@Override
public void setWorkingDir(Object dir) {
throw new UnsupportedOperationException();
}
@Override
public String getDefaultCharacterEncoding() {
return delegate.getDefaultCharacterEncoding();
}
@Override
public ProcessForkOptions workingDir(Object dir) {
throw new UnsupportedOperationException();
}
@Override
public Map<String, Object> getEnvironment() {
return ImmutableMap.copyOf(delegate.getEnvironment());
}
@Override
public void setEnvironment(Map<String, ?> environmentVariables) {
throw new UnsupportedOperationException();
}
@Override
public void setDefaultCharacterEncoding(String defaultCharacterEncoding) {
throw new UnsupportedOperationException();
}
@Override
public ProcessForkOptions environment(Map<String, ?> environmentVariables) {
throw new UnsupportedOperationException();
}
@Override
public String getMinHeapSize() {
return delegate.getMinHeapSize();
}
@Override
public void setMinHeapSize(String heapSize) {
throw new UnsupportedOperationException();
}
@Override
public ProcessForkOptions environment(String name, Object value) {
throw new UnsupportedOperationException();
}
@Override
public ProcessForkOptions copyTo(ProcessForkOptions options) {
throw new UnsupportedOperationException();
}
@Override
public String getMaxHeapSize() {
return delegate.getMaxHeapSize();
}
@Override
public void setMaxHeapSize(String heapSize) {
throw new UnsupportedOperationException();
}
@Override
public List<String> getJvmArgs() {
return ImmutableList.copyOf(delegate.getJvmArgs());
}
@Override
public void setJvmArgs(List<String> arguments) {
throw new UnsupportedOperationException();
}
@Override
public void setJvmArgs(Iterable<?> arguments) {
throw new UnsupportedOperationException();
}
@Override
public JavaForkOptions jvmArgs(Iterable<?> arguments) {
throw new UnsupportedOperationException();
}
@Override
public JavaForkOptions jvmArgs(Object... arguments) {
throw new UnsupportedOperationException();
}
@Override
public List<CommandLineArgumentProvider> getJvmArgumentProviders() {
throw new UnsupportedOperationException();
}
@Override
public FileCollection getBootstrapClasspath() {
return delegate.getBootstrapClasspath();
}
@Override
public void setBootstrapClasspath(FileCollection classpath) {
throw new UnsupportedOperationException();
}
@Override
public JavaForkOptions bootstrapClasspath(Object... classpath) {
throw new UnsupportedOperationException();
}
@Override
public boolean getEnableAssertions() {
return delegate.getEnableAssertions();
}
@Override
public void setEnableAssertions(boolean enabled) {
throw new UnsupportedOperationException();
}
@Override
public boolean getDebug() {
return delegate.getDebug();
}
@Override
public void setDebug(boolean enabled) {
throw new UnsupportedOperationException();
}
@Override
public JavaDebugOptions getDebugOptions() {
return delegate.getDebugOptions();
}
@Override
public void debugOptions(Action<JavaDebugOptions> action) {
throw new UnsupportedOperationException();
}
@Override
public List<String> getAllJvmArgs() {
return ImmutableList.copyOf(delegate.getAllJvmArgs());
}
@Override
public void setAllJvmArgs(List<String> arguments) {
throw new UnsupportedOperationException();
}
@Override
public void setAllJvmArgs(Iterable<?> arguments) {
throw new UnsupportedOperationException();
}
@Override
public JavaForkOptions copyTo(JavaForkOptions options) {
return delegate.copyTo(options);
}
@Override
public boolean isCompatibleWith(JavaForkOptions options) {
return delegate.isCompatibleWith(options);
}
}
}