1 | |
package org.apache.maven.scm.provider.bazaar; |
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.scm.ScmException; |
23 | |
import org.apache.maven.scm.ScmFileSet; |
24 | |
import org.apache.maven.scm.ScmFileStatus; |
25 | |
import org.apache.maven.scm.ScmResult; |
26 | |
import org.apache.maven.scm.log.DefaultLog; |
27 | |
import org.apache.maven.scm.log.ScmLogger; |
28 | |
import org.apache.maven.scm.provider.bazaar.command.BazaarConstants; |
29 | |
import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer; |
30 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
31 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
32 | |
import org.codehaus.plexus.util.cli.Commandline; |
33 | |
|
34 | |
import java.io.File; |
35 | |
import java.util.ArrayList; |
36 | |
import java.util.HashMap; |
37 | |
import java.util.List; |
38 | |
import java.util.Map; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public final class BazaarUtils |
47 | |
{ |
48 | |
|
49 | |
private BazaarUtils() |
50 | 0 | { |
51 | 0 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | private static final Map<String,List<Integer>> EXITCODEMAP = new HashMap<String,List<Integer>>(); |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | private static final List<Integer> DEFAULTEEXITCODES = new ArrayList<Integer>(); |
62 | |
|
63 | |
|
64 | |
static |
65 | |
{ |
66 | 0 | DEFAULTEEXITCODES.add( Integer.valueOf( 0 ) ); |
67 | |
|
68 | |
|
69 | 0 | List<Integer> diffExitCodes = new ArrayList<Integer>(); |
70 | 0 | diffExitCodes.add( Integer.valueOf( 0 ) ); |
71 | 0 | diffExitCodes.add( Integer.valueOf( 1 ) ); |
72 | 0 | diffExitCodes.add( Integer.valueOf( 2 ) ); |
73 | 0 | EXITCODEMAP.put( BazaarConstants.DIFF_CMD, diffExitCodes ); |
74 | 0 | } |
75 | |
|
76 | |
public static ScmResult execute( BazaarConsumer consumer, ScmLogger logger, File workingDir, String[] cmdAndArgs ) |
77 | |
throws ScmException |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | |
|
82 | 0 | Commandline cmd = buildCmd( workingDir, cmdAndArgs ); |
83 | 0 | if ( logger.isInfoEnabled() ) |
84 | |
{ |
85 | 0 | logger.info( "EXECUTING: " + cmd ); |
86 | |
} |
87 | |
|
88 | |
|
89 | 0 | int exitCode = executeCmd( consumer, cmd ); |
90 | |
|
91 | |
|
92 | 0 | List<Integer> exitCodes = DEFAULTEEXITCODES; |
93 | 0 | if ( EXITCODEMAP.containsKey( cmdAndArgs[0] ) ) |
94 | |
{ |
95 | 0 | exitCodes = EXITCODEMAP.get( cmdAndArgs[0] ); |
96 | |
} |
97 | 0 | boolean success = exitCodes.contains( Integer.valueOf( exitCode ) ); |
98 | |
|
99 | |
|
100 | 0 | String providerMsg = "Execution of bazaar command succeded"; |
101 | 0 | if ( !success ) |
102 | |
{ |
103 | 0 | BazaarConfig config = new BazaarConfig( workingDir ); |
104 | 0 | providerMsg = "\nEXECUTION FAILED" + "\n Execution of cmd : " + cmdAndArgs[0] |
105 | |
+ " failed with exit code: " + exitCode + "." + "\n Working directory was: " + "\n " |
106 | |
+ workingDir.getAbsolutePath() + config.toString( workingDir ) + "\n"; |
107 | 0 | if ( logger.isErrorEnabled() ) |
108 | |
{ |
109 | 0 | logger.error( providerMsg ); |
110 | |
} |
111 | |
} |
112 | |
|
113 | 0 | return new ScmResult( cmd.toString(), providerMsg, consumer.getStdErr(), success ); |
114 | |
} |
115 | 0 | catch ( ScmException se ) |
116 | |
{ |
117 | 0 | String msg = |
118 | |
"EXECUTION FAILED\n Execution failed before invoking the Bazaar command. Last exception:" |
119 | |
+ "\n " + se.getMessage(); |
120 | |
|
121 | |
|
122 | 0 | if ( se.getCause() != null ) |
123 | |
{ |
124 | 0 | msg += "\n Nested exception:" + "\n " + se.getCause().getMessage(); |
125 | |
} |
126 | |
|
127 | |
|
128 | 0 | if ( logger.isErrorEnabled() ) |
129 | |
{ |
130 | 0 | logger.error( msg ); |
131 | |
} |
132 | 0 | throw se; |
133 | |
} |
134 | |
} |
135 | |
|
136 | |
static Commandline buildCmd( File workingDir, String[] cmdAndArgs ) |
137 | |
throws ScmException |
138 | |
{ |
139 | 0 | Commandline cmd = new Commandline(); |
140 | 0 | cmd.setExecutable( BazaarConstants.EXEC ); |
141 | 0 | cmd.setWorkingDirectory( workingDir.getAbsolutePath() ); |
142 | 0 | cmd.addArguments( cmdAndArgs ); |
143 | |
|
144 | 0 | if ( !workingDir.exists() ) |
145 | |
{ |
146 | 0 | boolean success = workingDir.mkdirs(); |
147 | 0 | if ( !success ) |
148 | |
{ |
149 | 0 | String msg = "Working directory did not exist" + " and it couldn't be created: " + workingDir; |
150 | 0 | throw new ScmException( msg ); |
151 | |
} |
152 | |
} |
153 | 0 | return cmd; |
154 | |
} |
155 | |
|
156 | |
static int executeCmd( BazaarConsumer consumer, Commandline cmd ) |
157 | |
throws ScmException |
158 | |
{ |
159 | |
final int exitCode; |
160 | |
try |
161 | |
{ |
162 | 0 | exitCode = CommandLineUtils.executeCommandLine( cmd, consumer, consumer ); |
163 | |
} |
164 | 0 | catch ( CommandLineException ex ) |
165 | |
{ |
166 | 0 | throw new ScmException( "Command could not be executed: " + cmd, ex ); |
167 | 0 | } |
168 | 0 | return exitCode; |
169 | |
} |
170 | |
|
171 | |
public static ScmResult execute( File workingDir, String[] cmdAndArgs ) |
172 | |
throws ScmException |
173 | |
{ |
174 | 0 | ScmLogger logger = new DefaultLog(); |
175 | 0 | return execute( new BazaarConsumer( logger ), logger, workingDir, cmdAndArgs ); |
176 | |
} |
177 | |
|
178 | |
public static String[] expandCommandLine( String[] cmdAndArgs, ScmFileSet additionalFiles ) |
179 | |
{ |
180 | 0 | List<File> files = additionalFiles.getFileList(); |
181 | 0 | String[] cmd = new String[files.size() + cmdAndArgs.length]; |
182 | |
|
183 | |
|
184 | 0 | System.arraycopy( cmdAndArgs, 0, cmd, 0, cmdAndArgs.length ); |
185 | |
|
186 | |
|
187 | 0 | for ( int i = 0; i < files.size(); i++ ) |
188 | |
{ |
189 | 0 | String file = files.get( i ).getPath().replace( '\\', File.separatorChar ); |
190 | 0 | cmd[i + cmdAndArgs.length] = file; |
191 | |
} |
192 | |
|
193 | 0 | return cmd; |
194 | |
} |
195 | |
|
196 | |
public static int getCurrentRevisionNumber( ScmLogger logger, File workingDir ) |
197 | |
throws ScmException |
198 | |
{ |
199 | |
|
200 | 0 | String[] revCmd = new String[]{BazaarConstants.REVNO_CMD}; |
201 | 0 | BazaarRevNoConsumer consumer = new BazaarRevNoConsumer( logger ); |
202 | 0 | BazaarUtils.execute( consumer, logger, workingDir, revCmd ); |
203 | |
|
204 | 0 | return consumer.getCurrentRevisionNumber(); |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
private static class BazaarRevNoConsumer |
213 | |
extends BazaarConsumer |
214 | |
{ |
215 | |
|
216 | |
private int revNo; |
217 | |
|
218 | |
BazaarRevNoConsumer( ScmLogger logger ) |
219 | |
{ |
220 | 0 | super( logger ); |
221 | 0 | } |
222 | |
|
223 | |
public void doConsume( ScmFileStatus status, String line ) |
224 | |
{ |
225 | |
try |
226 | |
{ |
227 | 0 | revNo = Integer.valueOf( line ).intValue(); |
228 | |
} |
229 | 0 | catch ( NumberFormatException e ) |
230 | |
{ |
231 | |
|
232 | 0 | } |
233 | 0 | } |
234 | |
|
235 | |
int getCurrentRevisionNumber() |
236 | |
{ |
237 | 0 | return revNo; |
238 | |
} |
239 | |
} |
240 | |
} |