1 package org.apache.maven.plugin.cxx;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.maven.plugin.MojoExecutionException;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import java.util.List;
28 import java.util.Map;
29 import java.util.ArrayList;
30 import java.util.Properties;
31 import java.util.Iterator;
32 import java.io.OutputStream;
33
34 import org.codehaus.plexus.util.cli.CommandLineUtils;
35 import org.apache.maven.plugin.cxx.utils.ExecutorService;
36
37 import org.apache.commons.exec.CommandLine;
38 import org.apache.commons.exec.DefaultExecutor;
39 import org.apache.commons.exec.ExecuteException;
40 import org.apache.commons.exec.Executor;
41
42
43
44
45
46
47 public abstract class AbstractLaunchMojo extends AbstractCxxMojo
48 {
49 protected abstract List<String> getArgsList();
50
51 protected abstract String getCommandArgs();
52
53 private List<String> getCommandLineArgs() throws MojoExecutionException
54 {
55 ArrayList<String> commandArguments = new ArrayList<String>();
56
57 if ( getCommandArgs() != null )
58 {
59 String[] args = parseCommandlineArgs( getCommandArgs() );
60 for ( String argument : args )
61 {
62 commandArguments.add( argument );
63 }
64 }
65 else if ( getArgsList() != null )
66 {
67 List<String> args = getArgsList();
68 for ( String argument : args )
69 {
70 if ( argument == null )
71 {
72 throw new MojoExecutionException( "Misconfigured argument, value is null. "
73 + "Set the argument to an empty value if this is the required behaviour." );
74 }
75 else
76 {
77 commandArguments.add( argument );
78 }
79 }
80 }
81 return commandArguments;
82 }
83
84
85
86
87
88
89 protected String[] parseCommandlineArgs( String commandLineArgs ) throws MojoExecutionException
90 {
91 if ( commandLineArgs == null )
92 {
93 return null;
94 }
95 else
96 {
97 try
98 {
99 return CommandLineUtils.translateCommandline( commandLineArgs );
100 }
101 catch ( Exception e )
102 {
103 throw new MojoExecutionException( e.getMessage() );
104 }
105 }
106 }
107
108 protected abstract String getExecutable();
109
110 protected abstract Map getMoreEnvironmentVariables();
111
112 protected Properties getEnvs()
113 {
114 Properties enviro = new Properties();
115 try
116 {
117 enviro = ExecutorService.getSystemEnvVars();
118 }
119 catch ( IOException e )
120 {
121 getLog().error( "Could not assign default system enviroment variables.", e );
122 }
123
124 if ( getMoreEnvironmentVariables() != null )
125 {
126 Iterator iter = getMoreEnvironmentVariables().keySet().iterator();
127 while ( iter.hasNext() )
128 {
129 String key = (String) iter.next();
130 String value = (String) getMoreEnvironmentVariables().get( key );
131 enviro.put( key, value );
132 }
133 }
134 return enviro;
135 }
136
137
138 protected abstract File getWorkingDir();
139
140 private void ensureExistWorkingDirectory() throws MojoExecutionException
141 {
142 if ( !getWorkingDir().exists() )
143 {
144 getLog().info( "Making working directory '" + getWorkingDir().getAbsolutePath() + "'." );
145 if ( !getWorkingDir().mkdirs() )
146 {
147 throw new MojoExecutionException( "Could not make working directory: '"
148 + getWorkingDir().getAbsolutePath() + "'" );
149 }
150 }
151 }
152
153
154
155
156
157
158
159
160 protected abstract List<String> getSuccesCode();
161
162 protected boolean isResultCodeAFailure( int result )
163 {
164 if ( getSuccesCode() == null || getSuccesCode().size() == 0 )
165 {
166 return result != 0;
167 }
168 for ( Iterator it = getSuccesCode().iterator(); it.hasNext(); )
169 {
170 int code = Integer.parseInt( (String) it.next() );
171 if ( code == result )
172 {
173 return false;
174 }
175 }
176 return true;
177 }
178
179 protected abstract boolean isSkip();
180
181 protected OutputStream getOutputStreamOut()
182 {
183 return System.out;
184 }
185
186 protected OutputStream getOutputStreamErr()
187 {
188 return System.err;
189 }
190
191 protected InputStream getInputStream()
192 {
193 return System.in;
194 }
195
196 protected void preExecute( Executor exec, CommandLine commandLine, Properties enviro ) throws MojoExecutionException
197 {
198 }
199
200 protected void postExecute( int resultCode ) throws MojoExecutionException
201 {
202 if ( isResultCodeAFailure( resultCode ) )
203 {
204 throw new MojoExecutionException( "Result of command line execution is: '" + resultCode + "'." );
205 }
206 }
207
208 @Override
209 public void execute() throws MojoExecutionException
210 {
211 if ( isSkip() )
212 {
213 getLog().info( "skipping execute as per configuraion" );
214 return;
215 }
216
217 if ( basedir == null )
218 {
219 throw new IllegalStateException( "basedir is null. Should not be possible." );
220 }
221
222 List<String> commandArguments = getCommandLineArgs();
223
224 Properties enviro = getEnvs();
225
226 ensureExistWorkingDirectory();
227
228 CommandLine commandLine = ExecutorService.getExecutablePath( getExecutable(), enviro, getWorkingDir() );
229
230 Executor exec = new DefaultExecutor();
231
232 commandLine.addArguments( (String[]) commandArguments.toArray( new String[commandArguments.size()] ), false );
233
234 exec.setWorkingDirectory( getWorkingDir() );
235
236 try
237 {
238 getLog().info( "Executing command line: " + commandLine );
239
240 preExecute( exec, commandLine, enviro );
241
242 int resultCode = ExecutorService.executeCommandLine( exec, commandLine, enviro, getOutputStreamOut(),
243 getOutputStreamErr(), getInputStream() );
244
245 postExecute( resultCode );
246 }
247 catch ( ExecuteException e )
248 {
249 throw new MojoExecutionException( "Command execution failed.", e );
250 }
251 catch ( IOException e )
252 {
253 throw new MojoExecutionException( "Command execution failed.", e );
254 }
255 }
256 }