1 package org.apache.maven.scm.provider.starteam;
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.CommandParameters;
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.command.add.AddScmResult;
26 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
27 import org.apache.maven.scm.command.checkin.CheckInScmResult;
28 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
29 import org.apache.maven.scm.command.diff.DiffScmResult;
30 import org.apache.maven.scm.command.edit.EditScmResult;
31 import org.apache.maven.scm.command.remove.RemoveScmResult;
32 import org.apache.maven.scm.command.status.StatusScmResult;
33 import org.apache.maven.scm.command.tag.TagScmResult;
34 import org.apache.maven.scm.command.unedit.UnEditScmResult;
35 import org.apache.maven.scm.command.update.UpdateScmResult;
36 import org.apache.maven.scm.provider.AbstractScmProvider;
37 import org.apache.maven.scm.provider.ScmProviderRepository;
38 import org.apache.maven.scm.provider.starteam.command.add.StarteamAddCommand;
39 import org.apache.maven.scm.provider.starteam.command.changelog.StarteamChangeLogCommand;
40 import org.apache.maven.scm.provider.starteam.command.checkin.StarteamCheckInCommand;
41 import org.apache.maven.scm.provider.starteam.command.checkout.StarteamCheckOutCommand;
42 import org.apache.maven.scm.provider.starteam.command.diff.StarteamDiffCommand;
43 import org.apache.maven.scm.provider.starteam.command.edit.StarteamEditCommand;
44 import org.apache.maven.scm.provider.starteam.command.remove.StarteamRemoveCommand;
45 import org.apache.maven.scm.provider.starteam.command.status.StarteamStatusCommand;
46 import org.apache.maven.scm.provider.starteam.command.tag.StarteamTagCommand;
47 import org.apache.maven.scm.provider.starteam.command.unedit.StarteamUnEditCommand;
48 import org.apache.maven.scm.provider.starteam.command.update.StarteamUpdateCommand;
49 import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
50 import org.apache.maven.scm.repository.ScmRepositoryException;
51 import org.codehaus.plexus.util.StringUtils;
52
53 import java.io.File;
54 import java.io.IOException;
55
56
57
58
59
60
61 public class StarteamScmProvider
62 extends AbstractScmProvider
63 {
64 public static final String STARTEAM_URL_FORMAT =
65 "[username[:password]@]hostname:port:/projectName/[viewName/][folderHiearchy/]";
66
67
68
69
70
71 public ScmProviderRepository makeProviderScmRepository( String scmSpecificUrl, char delimiter )
72 throws ScmRepositoryException
73 {
74 String user = null;
75
76 String password = null;
77
78 int index = scmSpecificUrl.indexOf( '@' );
79
80 String rest = scmSpecificUrl;
81
82 if ( index != -1 )
83 {
84 String userAndPassword = scmSpecificUrl.substring( 0, index );
85
86 rest = scmSpecificUrl.substring( index + 1 );
87
88 index = userAndPassword.indexOf( ":" );
89
90 if ( index != -1 )
91 {
92 user = userAndPassword.substring( 0, index );
93
94 password = userAndPassword.substring( index + 1 );
95 }
96 else
97 {
98 user = userAndPassword;
99 }
100 }
101
102 String[] tokens = StringUtils.split( rest, Character.toString( delimiter ) );
103
104 String host;
105
106 int port;
107
108 String path;
109
110 if ( tokens.length == 3 )
111 {
112 host = tokens[0];
113
114 port = new Integer( tokens[1] ).intValue();
115
116 path = tokens[2];
117 }
118 else if ( tokens.length == 2 )
119 {
120 getLogger().warn( "Your scm URL use a deprecated format. The new format is :" + STARTEAM_URL_FORMAT );
121
122 host = tokens[0];
123
124 if ( tokens[1].indexOf( '/' ) == -1 )
125 {
126 throw new ScmRepositoryException(
127 "Invalid SCM URL: The url has to be on the form: " + STARTEAM_URL_FORMAT );
128 }
129
130 int at = tokens[1].indexOf( '/' );
131
132 port = new Integer( tokens[1].substring( 0, at ) ).intValue();
133
134 path = tokens[1].substring( at );
135 }
136 else
137 {
138 throw new ScmRepositoryException(
139 "Invalid SCM URL: The url has to be on the form: " + STARTEAM_URL_FORMAT );
140 }
141
142 try
143 {
144 return new StarteamScmProviderRepository( user, password, host, port, path );
145 }
146 catch ( Exception e )
147 {
148 throw new ScmRepositoryException(
149 "Invalid SCM URL: The url has to be on the form: " + STARTEAM_URL_FORMAT );
150 }
151 }
152
153 public String getScmType()
154 {
155 return "starteam";
156 }
157
158
159
160
161 public AddScmResult add( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
162 throws ScmException
163 {
164 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
165
166 StarteamAddCommand command = new StarteamAddCommand();
167
168 command.setLogger( getLogger() );
169
170 return (AddScmResult) command.execute( repository, fileSet, parameters );
171 }
172
173
174
175
176 public ChangeLogScmResult changelog( ScmProviderRepository repository, ScmFileSet fileSet,
177 CommandParameters parameters )
178 throws ScmException
179 {
180 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
181
182 StarteamChangeLogCommand command = new StarteamChangeLogCommand();
183
184 command.setLogger( getLogger() );
185
186 return (ChangeLogScmResult) command.execute( repository, fileSet, parameters );
187 }
188
189
190
191
192 public CheckInScmResult checkin( ScmProviderRepository repository, ScmFileSet fileSet,
193 CommandParameters parameters )
194 throws ScmException
195 {
196 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
197
198 StarteamCheckInCommand command = new StarteamCheckInCommand();
199
200 command.setLogger( getLogger() );
201
202 return (CheckInScmResult) command.execute( repository, fileSet, parameters );
203 }
204
205
206
207
208 public CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet,
209 CommandParameters parameters )
210 throws ScmException
211 {
212 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
213
214 StarteamCheckOutCommand command = new StarteamCheckOutCommand();
215
216 command.setLogger( getLogger() );
217
218 return (CheckOutScmResult) command.execute( repository, fileSet, parameters );
219 }
220
221
222
223
224 public DiffScmResult diff( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
225 throws ScmException
226 {
227 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
228
229 StarteamDiffCommand command = new StarteamDiffCommand();
230
231 command.setLogger( getLogger() );
232
233 return (DiffScmResult) command.execute( repository, fileSet, parameters );
234 }
235
236
237
238
239 public StatusScmResult status( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
240 throws ScmException
241 {
242 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
243
244 StarteamStatusCommand command = new StarteamStatusCommand();
245
246 command.setLogger( getLogger() );
247
248 return (StatusScmResult) command.execute( repository, fileSet, parameters );
249 }
250
251
252
253
254 public TagScmResult tag( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
255 throws ScmException
256 {
257 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
258
259 StarteamTagCommand command = new StarteamTagCommand();
260
261 command.setLogger( getLogger() );
262
263 return (TagScmResult) command.execute( repository, fileSet, parameters );
264 }
265
266
267
268
269 public UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
270 throws ScmException
271 {
272 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
273
274 StarteamUpdateCommand command = new StarteamUpdateCommand();
275
276 command.setLogger( getLogger() );
277
278 return (UpdateScmResult) command.execute( repository, fileSet, parameters );
279 }
280
281 protected EditScmResult edit( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
282 throws ScmException
283 {
284 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
285
286 StarteamEditCommand command = new StarteamEditCommand();
287
288 command.setLogger( getLogger() );
289
290 return (EditScmResult) command.execute( repository, fileSet, parameters );
291 }
292
293 protected UnEditScmResult unedit( ScmProviderRepository repository, ScmFileSet fileSet,
294 CommandParameters parameters )
295 throws ScmException
296 {
297 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
298
299 StarteamUnEditCommand command = new StarteamUnEditCommand();
300
301 command.setLogger( getLogger() );
302
303 return (UnEditScmResult) command.execute( repository, fileSet, parameters );
304 }
305
306
307
308
309 public RemoveScmResult remove( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
310 throws ScmException
311 {
312 fileSet = fixUpScmFileSetAbsoluteFilePath( fileSet );
313
314 StarteamRemoveCommand command = new StarteamRemoveCommand();
315
316 command.setLogger( getLogger() );
317
318 return (RemoveScmResult) command.execute( repository, fileSet, parameters );
319 }
320
321
322
323
324
325
326
327
328
329 private static ScmFileSet fixUpScmFileSetAbsoluteFilePath( ScmFileSet currentFileSet )
330 throws ScmException
331 {
332 ScmFileSet newFileSet = null;
333 try
334 {
335 File basedir = getAbsoluteFilePath( currentFileSet.getBasedir() );
336
337 File[] files = currentFileSet.getFiles();
338
339 for ( int i = 0; i < files.length; ++i )
340 {
341 if ( files[i].isAbsolute() )
342 {
343 files[i] = new File( getRelativePath( basedir, files[i] ) );
344 }
345 }
346
347 newFileSet = new ScmFileSet( basedir, files );
348 }
349 catch ( IOException e )
350 {
351 throw new ScmException( "Invalid file set.", e );
352 }
353
354 return newFileSet;
355 }
356
357 public static String getRelativePath( File basedir, File f )
358 throws ScmException, IOException
359 {
360 File fileOrDir = getAbsoluteFilePath( f );
361
362 if ( !fileOrDir.getCanonicalPath().startsWith( basedir.getCanonicalPath() ) )
363 {
364 throw new ScmException( fileOrDir.getPath() + " was not contained in " + basedir.getPath() );
365 }
366
367 return fileOrDir.getPath().substring( basedir.getPath().length() + 1, fileOrDir.getPath().length() );
368 }
369
370 private static File getAbsoluteFilePath( File fileOrDir )
371 throws IOException
372 {
373 String javaPathString = fileOrDir.getCanonicalPath().replace( '\\', '/' );
374
375 if ( javaPathString.endsWith( "/" ) )
376 {
377 javaPathString = javaPathString.substring( 0, javaPathString.length() - 1 );
378 }
379
380 return new File( javaPathString );
381 }
382 }