View Javadoc

1   package org.apache.maven.scm.provider.starteam;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
58   * @version $Id: StarteamScmProvider.java 525180 2007-04-03 15:45:31Z evenisse $
59   * @plexus.component role="org.apache.maven.scm.provider.ScmProvider" role-hint="starteam"
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      // ScmProvider Implementation
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#add(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#changelog(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#checkin(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#checkout(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#diff(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#status(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#tag(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#update(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * @see org.apache.maven.scm.provider.AbstractScmProvider#remove(org.apache.maven.scm.provider.ScmProviderRepository,org.apache.maven.scm.ScmFileSet,org.apache.maven.scm.CommandParameters)
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      * Starteam provider requires that all files in ScmFileSet must be relative to basedir
323      * This function ensures and converts all absolute paths to relative paths
324      *
325      * @param currentFileSet
326      * @return
327      * @throws ScmException
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 }