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