1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.utils.cli.javatool;
20
21 import java.io.File;
22 import java.io.InputStream;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.Map;
26
27 import org.apache.maven.shared.utils.Os;
28 import org.apache.maven.shared.utils.StringUtils;
29 import org.apache.maven.shared.utils.cli.CommandLineException;
30 import org.apache.maven.shared.utils.cli.CommandLineUtils;
31 import org.apache.maven.shared.utils.cli.Commandline;
32 import org.apache.maven.shared.utils.cli.StreamConsumer;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43 public abstract class AbstractJavaTool<Request extends JavaToolRequest> implements JavaTool<Request> {
44 private final Logger logger = LoggerFactory.getLogger(getClass());
45
46
47
48
49 private final String javaToolName;
50
51
52
53
54 private String javaToolFile;
55
56
57
58
59 private Object toolchain;
60
61
62
63
64 protected AbstractJavaTool(String javaToolName) {
65 this.javaToolName = javaToolName;
66 }
67
68
69
70
71
72
73
74
75
76 protected abstract Commandline createCommandLine(Request request, String javaToolFileLocation)
77 throws JavaToolException;
78
79 protected Logger getLogger() {
80 return logger;
81 }
82
83
84
85
86 public String getJavaToolName() {
87 return javaToolName;
88 }
89
90
91
92
93 public void setToolchain(Object toolchain) {
94 this.toolchain = toolchain;
95 }
96
97
98
99
100 public JavaToolResult execute(Request request) throws JavaToolException {
101
102 if (javaToolFile == null) {
103
104
105 try {
106 javaToolFile = findJavaToolExecutable();
107 } catch (Exception e) {
108 throw new JavaToolException(
109 "Error finding " + javaToolName + " executable. Reason: " + e.getMessage(), e);
110 }
111 }
112
113
114 Commandline cli = createCommandLine(request, javaToolFile);
115
116
117 JavaToolResult result = executeCommandLine(cli, request);
118
119
120 return result;
121 }
122
123
124
125
126 protected InputStream createSystemInputStream() {
127 InputStream systemIn = new InputStream() {
128
129
130
131
132 public int read() {
133 return -1;
134 }
135 };
136 return systemIn;
137 }
138
139
140
141
142
143
144 protected JavaToolResult executeCommandLine(Commandline cli, Request request) {
145 if (getLogger().isDebugEnabled()) {
146 getLogger().debug("Executing: " + cli);
147 }
148
149 JavaToolResult result = createResult();
150
151 result.setCommandline(cli);
152
153 InputStream systemIn = createSystemInputStream();
154
155 StreamConsumer systemOut = createSystemOutStreamConsumer(request);
156
157 StreamConsumer systemErr = createSystemErrorStreamConsumer(request);
158
159 try {
160 int resultCode = CommandLineUtils.executeCommandLine(cli, systemIn, systemOut, systemErr);
161
162 result.setExitCode(resultCode);
163 } catch (CommandLineException e) {
164 result.setExecutionException(e);
165 }
166
167 return result;
168 }
169
170
171
172
173
174 protected StreamConsumer createSystemErrorStreamConsumer(Request request) {
175 StreamConsumer systemErr = request.getSystemErrorStreamConsumer();
176
177 if (systemErr == null) {
178 systemErr = new StreamConsumer() {
179
180
181
182
183 @Override
184 public void consumeLine(final String line) {
185 getLogger().warn(line);
186 }
187 };
188 }
189 return systemErr;
190 }
191
192
193
194
195
196 protected StreamConsumer createSystemOutStreamConsumer(Request request) {
197 StreamConsumer systemOut = request.getSystemOutStreamConsumer();
198
199 if (systemOut == null) {
200
201 systemOut = new StreamConsumer() {
202
203
204
205
206 @Override
207 public void consumeLine(final String line) {
208 getLogger().info(line);
209 }
210 };
211 }
212 return systemOut;
213 }
214
215
216
217
218 protected JavaToolResult createResult() {
219 return new JavaToolResult();
220 }
221
222
223
224
225 protected String findJavaToolExecutable() {
226 String executable = null;
227
228 if (toolchain != null) {
229 executable = findToolchainExecutable();
230 }
231
232 String command = javaToolName + (Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : "");
233
234 if (executable == null) {
235 executable = findExecutable(command, System.getProperty("java.home"), "../bin", "bin", "../sh");
236 }
237
238 if (executable == null) {
239
240 Map<String, String> env = System.getenv();
241
242 String[] variables = {"JDK_HOME", "JAVA_HOME"};
243
244 for (String variable : variables) {
245 executable = findExecutable(command, env.get(variable), "bin", "sh");
246 if (executable != null) {
247 break;
248 }
249 }
250 }
251
252 if (executable == null) {
253 executable = command;
254 }
255
256 return executable;
257 }
258
259
260
261
262
263 private String findToolchainExecutable() {
264 try {
265 Method m = toolchain.getClass().getMethod("findTool", String.class);
266 return (String) m.invoke(toolchain, javaToolName);
267 } catch (NoSuchMethodException e) {
268
269 getLogger().warn("unexpected NoSuchMethodException", e);
270 } catch (SecurityException e) {
271
272 getLogger().warn("unexpected SecurityException", e);
273 } catch (IllegalAccessException e) {
274
275 getLogger().warn("unexpected IllegalAccessException", e);
276 } catch (IllegalArgumentException e) {
277
278 getLogger().warn("unexpected IllegalArgumentException", e);
279 } catch (InvocationTargetException e) {
280
281 getLogger().warn("unexpected InvocationTargetException", e);
282 }
283 return null;
284 }
285
286
287
288
289
290
291
292
293
294 private String findExecutable(String command, String homeDir, String... subDirs) {
295 String result = null;
296 if (StringUtils.isNotEmpty(homeDir)) {
297 for (String subDir : subDirs) {
298 File file = new File(new File(homeDir, subDir), command);
299
300 if (file.isFile()) {
301 result = file.getAbsolutePath();
302 break;
303 }
304 }
305 }
306
307 return result;
308 }
309 }