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