| @@ -1,124 +1,147 | |||||
| 1 | package org.implab.gradle.common.exec; |
|
1 | package org.implab.gradle.common.exec; | |
| 2 |
|
2 | |||
| 3 | import java.util.ArrayList; |
|
3 | import java.util.ArrayList; | |
| 4 | import java.util.HashMap; |
|
4 | import java.util.HashMap; | |
| 5 | import java.util.List; |
|
5 | import java.util.List; | |
| 6 | import java.util.Map; |
|
6 | import java.util.Map; | |
| 7 | import java.util.Objects; |
|
7 | import java.util.Objects; | |
| 8 | import java.util.Optional; |
|
8 | import java.util.Optional; | |
| 9 | import java.util.concurrent.CompletableFuture; |
|
9 | import java.util.concurrent.CompletableFuture; | |
| 10 | import java.util.stream.Stream; |
|
10 | import java.util.stream.Stream; | |
|
|
11 | import java.util.Collection; | |||
| 11 |
|
12 | |||
| 12 | import org.eclipse.jdt.annotation.NonNullByDefault; |
|
13 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
| 13 |
|
14 | |||
| 14 | import java.io.File; |
|
15 | import java.io.File; | |
| 15 | import java.io.IOException; |
|
16 | import java.io.IOException; | |
| 16 |
|
17 | |||
| 17 | /** Command line builder */ |
|
18 | /** Command line builder */ | |
| 18 | @NonNullByDefault |
|
19 | @NonNullByDefault | |
| 19 | public abstract class ExecBuilder { |
|
20 | public abstract class ExecBuilder { | |
| 20 |
|
21 | |||
| 21 | private final List<String> commandLine = new ArrayList<>(); |
|
22 | private final List<String> commandLine = new ArrayList<>(); | |
| 22 |
|
23 | |||
| 23 | private final Map<String, String> environment = new HashMap<>(); |
|
24 | private final Map<String, String> environment = new HashMap<>(); | |
| 24 |
|
25 | |||
| 25 | private File directory; |
|
26 | private File directory; | |
| 26 |
|
27 | |||
| 27 | private RedirectFrom inputRedirect; |
|
28 | private RedirectFrom inputRedirect; | |
| 28 |
|
29 | |||
| 29 | private RedirectTo outputRedirect; |
|
30 | private RedirectTo outputRedirect; | |
| 30 |
|
31 | |||
| 31 | private RedirectTo errorRedirect; |
|
32 | private RedirectTo errorRedirect; | |
| 32 |
|
33 | |||
| 33 | /** Sets command line, clears previous one if any */ |
|
34 | /** Sets command line, clears previous one if any */ | |
| 34 |
public |
|
35 | public ExecBuilder command(String cmd, String... args) { | |
| 35 | commandLine.clear(); |
|
36 | commandLine.clear(); | |
| 36 | commandLine.add(cmd); |
|
37 | commandLine.add(cmd); | |
| 37 | Stream.of(args).forEach(commandLine::add); |
|
38 | Stream.of(args).forEach(commandLine::add); | |
|
|
39 | return this; | |||
|
|
40 | } | |||
|
|
41 | ||||
|
|
42 | public ExecBuilder command(Collection<String> cmd) { | |||
|
|
43 | this.commandLine.clear(); | |||
|
|
44 | this.commandLine.addAll(cmd); | |||
|
|
45 | return this; | |||
| 38 | } |
|
46 | } | |
| 39 |
|
47 | |||
| 40 | /** Adds an argument to the command line */ |
|
48 | /** Adds an argument to the command line */ | |
| 41 |
public |
|
49 | public ExecBuilder argument(String arg) { | |
| 42 | Objects.requireNonNull(arg, "arg parameter can't be null"); |
|
50 | Objects.requireNonNull(arg, "arg parameter can't be null"); | |
| 43 | commandLine.add(arg); |
|
51 | commandLine.add(arg); | |
|
|
52 | return this; | |||
| 44 | } |
|
53 | } | |
| 45 |
|
54 | |||
| 46 | /** Sets the working directory */ |
|
55 | /** Sets the working directory */ | |
| 47 |
public |
|
56 | public ExecBuilder directory(File directory) { | |
| 48 | Objects.requireNonNull(directory, "directory parameter can't be null"); |
|
57 | Objects.requireNonNull(directory, "directory parameter can't be null"); | |
| 49 | this.directory = directory; |
|
58 | this.directory = directory; | |
|
|
59 | return this; | |||
| 50 | } |
|
60 | } | |
| 51 |
|
61 | |||
| 52 | /** |
|
62 | /** | |
| 53 | * Sets the environment value. The value cannot be null. |
|
63 | * Sets the environment value. The value cannot be null. | |
| 54 | * |
|
64 | * | |
| 55 | * @param envVar The name of the environment variable |
|
65 | * @param envVar The name of the environment variable | |
| 56 | * @param value The value to set, |
|
66 | * @param value The value to set, | |
| 57 | */ |
|
67 | */ | |
| 58 |
public |
|
68 | public ExecBuilder setEnvironment(String envVar, String value) { | |
| 59 | Objects.requireNonNull(value, "Value can't be null"); |
|
69 | Objects.requireNonNull(value, "Value can't be null"); | |
| 60 | Objects.requireNonNull(envVar, "envVar parameter can't be null"); |
|
70 | Objects.requireNonNull(envVar, "envVar parameter can't be null"); | |
| 61 | environment.put(envVar, value); |
|
71 | environment.put(envVar, value); | |
|
|
72 | return this; | |||
| 62 | } |
|
73 | } | |
| 63 |
|
74 | |||
| 64 |
public |
|
75 | public ExecBuilder setEnvironment(Map<String, String> env) { | |
|
|
76 | Objects.requireNonNull(env, "env parameter can't be null"); | |||
|
|
77 | ||||
|
|
78 | environment.clear(); | |||
|
|
79 | environment.putAll(env); | |||
|
|
80 | return this; | |||
|
|
81 | } | |||
|
|
82 | ||||
|
|
83 | public ExecBuilder unsetEnvironment(String envVar) { | |||
| 65 | environment.remove(envVar); |
|
84 | environment.remove(envVar); | |
|
|
85 | return this; | |||
| 66 | } |
|
86 | } | |
| 67 |
|
87 | |||
| 68 | /** |
|
88 | /** | |
| 69 | * Sets redirection for the stdin, {@link RedirectFrom} will be applied |
|
89 | * Sets redirection for the stdin, {@link RedirectFrom} will be applied | |
| 70 | * every time the process is started. |
|
90 | * every time the process is started. | |
| 71 | * |
|
91 | * | |
| 72 | * <p> |
|
92 | * <p> | |
| 73 | * If redirection |
|
93 | * If redirection | |
| 74 | */ |
|
94 | */ | |
| 75 |
public |
|
95 | public ExecBuilder stdin(RedirectFrom from) { | |
| 76 | Objects.requireNonNull(from, "from parameter can't be null"); |
|
96 | Objects.requireNonNull(from, "from parameter can't be null"); | |
| 77 | inputRedirect = from; |
|
97 | inputRedirect = from; | |
|
|
98 | return this; | |||
| 78 | } |
|
99 | } | |
| 79 |
|
100 | |||
| 80 | /** |
|
101 | /** | |
| 81 | * Sets redirection for the stdout, {@link RedirectTo} will be applied |
|
102 | * Sets redirection for the stdout, {@link RedirectTo} will be applied | |
| 82 | * every time the process is started. |
|
103 | * every time the process is started. | |
| 83 | */ |
|
104 | */ | |
| 84 |
public |
|
105 | public ExecBuilder stdout(RedirectTo out) { | |
| 85 | Objects.requireNonNull(out, "out parameter can't be null"); |
|
106 | Objects.requireNonNull(out, "out parameter can't be null"); | |
| 86 | outputRedirect = out; |
|
107 | outputRedirect = out; | |
|
|
108 | return this; | |||
| 87 | } |
|
109 | } | |
| 88 |
|
110 | |||
| 89 | /** |
|
111 | /** | |
| 90 | * Sets redirection for the stderr, {@link RedirectTo} will be applied |
|
112 | * Sets redirection for the stderr, {@link RedirectTo} will be applied | |
| 91 | * every time the process is started. |
|
113 | * every time the process is started. | |
| 92 | */ |
|
114 | */ | |
| 93 |
public |
|
115 | public ExecBuilder stderr(RedirectTo err) { | |
| 94 | Objects.requireNonNull(err, "err parameter can't be null"); |
|
116 | Objects.requireNonNull(err, "err parameter can't be null"); | |
| 95 | errorRedirect = err; |
|
117 | errorRedirect = err; | |
|
|
118 | return this; | |||
| 96 | } |
|
119 | } | |
| 97 |
|
120 | |||
| 98 | /** Implement this function to */ |
|
121 | /** Implement this function to */ | |
| 99 | protected abstract CompletableFuture<Integer> startInternal( |
|
122 | protected abstract CompletableFuture<Integer> startInternal( | |
| 100 | Map<String, String> environment, |
|
123 | Map<String, String> environment, | |
| 101 | File directory, |
|
124 | File directory, | |
| 102 | List<String> commandLine, |
|
125 | List<String> commandLine, | |
| 103 | Optional<RedirectFrom> stdinRedirect, |
|
126 | Optional<RedirectFrom> stdinRedirect, | |
| 104 | Optional<RedirectTo> stdoutRedirect, |
|
127 | Optional<RedirectTo> stdoutRedirect, | |
| 105 | Optional<RedirectTo> stderrRedirect) throws IOException; |
|
128 | Optional<RedirectTo> stderrRedirect) throws IOException; | |
| 106 |
|
129 | |||
| 107 | /** |
|
130 | /** | |
| 108 | * Creates and starts new process and returns {@link CompletableFuture}. The |
|
131 | * Creates and starts new process and returns {@link CompletableFuture}. The | |
| 109 | * process may be a remote process, or a shell process or etc. |
|
132 | * process may be a remote process, or a shell process or etc. | |
| 110 | * |
|
133 | * | |
| 111 | * @return |
|
134 | * @return | |
| 112 | * @throws IOException |
|
135 | * @throws IOException | |
| 113 | */ |
|
136 | */ | |
| 114 | public CompletableFuture<Integer> start() throws IOException { |
|
137 | public CompletableFuture<Integer> start() throws IOException { | |
| 115 | return startInternal( |
|
138 | return startInternal( | |
| 116 | environment, |
|
139 | environment, | |
| 117 | directory, |
|
140 | directory, | |
| 118 | commandLine, |
|
141 | commandLine, | |
| 119 | Optional.ofNullable(inputRedirect), |
|
142 | Optional.ofNullable(inputRedirect), | |
| 120 | Optional.ofNullable(outputRedirect), |
|
143 | Optional.ofNullable(outputRedirect), | |
| 121 | Optional.ofNullable(errorRedirect)); |
|
144 | Optional.ofNullable(errorRedirect)); | |
| 122 | } |
|
145 | } | |
| 123 |
|
146 | |||
| 124 | } |
|
147 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
