View Javadoc
1   package org.apache.maven.scm.provider.tfs;
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 java.net.URI;
23  
24  import org.apache.maven.scm.CommandParameters;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.command.add.AddScmResult;
28  import org.apache.maven.scm.command.blame.BlameScmResult;
29  import org.apache.maven.scm.command.branch.BranchScmResult;
30  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
31  import org.apache.maven.scm.command.checkin.CheckInScmResult;
32  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
33  import org.apache.maven.scm.command.diff.DiffScmResult;
34  import org.apache.maven.scm.command.edit.EditScmResult;
35  import org.apache.maven.scm.command.export.ExportScmResult;
36  import org.apache.maven.scm.command.list.ListScmResult;
37  import org.apache.maven.scm.command.status.StatusScmResult;
38  import org.apache.maven.scm.command.tag.TagScmResult;
39  import org.apache.maven.scm.command.unedit.UnEditScmResult;
40  import org.apache.maven.scm.command.update.UpdateScmResult;
41  import org.apache.maven.scm.provider.AbstractScmProvider;
42  import org.apache.maven.scm.provider.ScmProviderRepository;
43  import org.apache.maven.scm.provider.tfs.command.TfsAddCommand;
44  import org.apache.maven.scm.provider.tfs.command.TfsBranchCommand;
45  import org.apache.maven.scm.provider.tfs.command.TfsChangeLogCommand;
46  import org.apache.maven.scm.provider.tfs.command.TfsCheckInCommand;
47  import org.apache.maven.scm.provider.tfs.command.TfsCheckOutCommand;
48  import org.apache.maven.scm.provider.tfs.command.TfsEditCommand;
49  import org.apache.maven.scm.provider.tfs.command.TfsListCommand;
50  import org.apache.maven.scm.provider.tfs.command.TfsStatusCommand;
51  import org.apache.maven.scm.provider.tfs.command.TfsTagCommand;
52  import org.apache.maven.scm.provider.tfs.command.TfsUnEditCommand;
53  import org.apache.maven.scm.provider.tfs.command.TfsUpdateCommand;
54  import org.apache.maven.scm.provider.tfs.command.blame.TfsBlameCommand;
55  import org.apache.maven.scm.repository.ScmRepositoryException;
56  
57  /**
58   * @plexus.component role="org.apache.maven.scm.provider.ScmProvider" role-hint="tfs"
59   */
60  public class TfsScmProvider
61      extends AbstractScmProvider
62  {
63  
64      public static final String TFS_URL_FORMAT = "[[domain\\]username[;password]@]http[s]://server_name[:port]"
65          + "[:isCheckinPoliciesEnabled]:workspace:$/TeamProject/Path/To/Project";
66  
67      // ----------------------------------------------------------------------
68      // ScmProvider Implementation
69      // ----------------------------------------------------------------------
70  
71      public String getScmType()
72      {
73          return "tfs";
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      public boolean requiresEditMode()
80      {
81          return true;
82      }
83  
84      public ScmProviderRepository makeProviderScmRepository( String scmUrl, char delimiter )
85          throws ScmRepositoryException
86      {
87          // Look for the TFS URL after any '@' delmiter used to pass
88          // usernames/password etc
89          // We deliberately look for the last '@' character as username could
90          // contain an '@' also.
91          int lastAtPos = scmUrl.lastIndexOf( '@' );
92          getLogger().info( "scmUrl - " + scmUrl );
93  
94          String tfsUrl = ( lastAtPos < 0 ) ? scmUrl : scmUrl.substring( lastAtPos + 1 );
95          String usernamePassword = ( lastAtPos < 0 ) ? null : scmUrl.substring( 0, lastAtPos );
96  
97          // Look for TFS path after the end of the TFS URL
98          int tfsPathPos = tfsUrl.lastIndexOf( delimiter + "$/" );
99          String serverPath = "$/";
100         if ( tfsPathPos > 0 )
101         {
102             serverPath = tfsUrl.substring( tfsPathPos + 1 );
103             tfsUrl = tfsUrl.substring( 0, tfsPathPos );
104         }
105 
106         // Look for workspace ater the end of the TFS URL
107         int workspacePos = tfsUrl.lastIndexOf( delimiter );
108         String workspace = tfsUrl.substring( workspacePos + 1 );
109         tfsUrl = tfsUrl.substring( 0, workspacePos );
110         getLogger().info( "workspace: " + workspace );
111 
112         // Look for workspace ater the end of the TFS URL
113         int checkinPoliciesPos = tfsUrl.lastIndexOf( delimiter );
114         String checkinPolicies = tfsUrl.substring( checkinPoliciesPos + 1 );
115         tfsUrl = tfsUrl.substring( 0, checkinPoliciesPos );
116         getLogger().info( "checkinPolicies: " + checkinPolicies );
117 
118         try
119         {
120             // Use URI's validation to determine if valid URI.
121             URI tfsUri = URI.create( tfsUrl );
122             String scheme = tfsUri.getScheme();
123             getLogger().info( "Scheme - " + scheme );
124             if ( scheme == null || !( scheme.equalsIgnoreCase( "http" ) || scheme.equalsIgnoreCase( "https" ) ) )
125             {
126                 throw new ScmRepositoryException( "TFS Url \"" + tfsUrl + "\" is not a valid URL. "
127                     + "The TFS Url syntax is " + TFS_URL_FORMAT );
128             }
129         }
130         catch ( IllegalArgumentException e )
131         {
132             throw new ScmRepositoryException( "TFS Url \"" + tfsUrl + "\" is not a valid URL. The TFS Url syntax is "
133                 + TFS_URL_FORMAT );
134         }
135 
136         String username = null;
137         String password = null;
138 
139         if ( usernamePassword != null )
140         {
141             // Deliberately not using .split here in case password contains a
142             // ';'
143             int delimPos = usernamePassword.indexOf( ';' );
144             username = ( delimPos < 0 ) ? usernamePassword : usernamePassword.substring( 0, delimPos );
145             password = ( delimPos < 0 ) ? null : usernamePassword.substring( delimPos + 1 );
146         }
147 
148         boolean useCheckinPolicies = Boolean.parseBoolean( checkinPolicies );
149 
150         return new TfsScmProviderRepository( tfsUrl, username, password, serverPath, workspace, useCheckinPolicies );
151     }
152 
153     protected ChangeLogScmResult changelog( ScmProviderRepository repository, ScmFileSet fileSet,
154                                             CommandParameters parameters )
155         throws ScmException
156     {
157         TfsChangeLogCommand command = new TfsChangeLogCommand();
158         command.setLogger( getLogger() );
159         return (ChangeLogScmResult) command.execute( repository, fileSet, parameters );
160     }
161 
162     protected CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet,
163                                           CommandParameters parameters )
164         throws ScmException
165     {
166         TfsCheckOutCommand command = new TfsCheckOutCommand();
167         command.setLogger( getLogger() );
168         return (CheckOutScmResult) command.execute( repository, fileSet, parameters );
169     }
170 
171     protected EditScmResult edit( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
172         throws ScmException
173     {
174         TfsEditCommand command = new TfsEditCommand();
175         command.setLogger( getLogger() );
176         return (EditScmResult) command.execute( repository, fileSet, parameters );
177     }
178 
179     protected UnEditScmResult unedit( ScmProviderRepository repository, ScmFileSet fileSet,
180                                       CommandParameters parameters )
181         throws ScmException
182     {
183         TfsUnEditCommand command = new TfsUnEditCommand();
184         command.setLogger( getLogger() );
185         return (UnEditScmResult) command.execute( repository, fileSet, parameters );
186     }
187 
188     protected StatusScmResult status( ScmProviderRepository repository, ScmFileSet fileSet,
189                                       CommandParameters parameters )
190         throws ScmException
191     {
192         TfsStatusCommand command = new TfsStatusCommand();
193         command.setLogger( getLogger() );
194         return (StatusScmResult) command.execute( repository, fileSet, parameters );
195     }
196 
197     protected UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet,
198                                       CommandParameters parameters )
199         throws ScmException
200     {
201         TfsUpdateCommand command = new TfsUpdateCommand();
202         command.setLogger( getLogger() );
203         return (UpdateScmResult) command.execute( repository, fileSet, parameters );
204     }
205 
206     protected CheckInScmResult checkin( ScmProviderRepository repository, ScmFileSet fileSet,
207                                         CommandParameters parameters )
208         throws ScmException
209     {
210         TfsCheckInCommand command = new TfsCheckInCommand();
211         command.setLogger( getLogger() );
212         return (CheckInScmResult) command.execute( repository, fileSet, parameters );
213     }
214 
215     public AddScmResult add( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
216         throws ScmException
217     {
218         TfsAddCommand command = new TfsAddCommand();
219         command.setLogger( getLogger() );
220         return (AddScmResult) command.execute( repository, fileSet, parameters );
221     }
222 
223     protected TagScmResult tag( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
224         throws ScmException
225     {
226         TfsTagCommand command = new TfsTagCommand();
227         command.setLogger( getLogger() );
228         return (TagScmResult) command.execute( repository, fileSet, parameters );
229     }
230 
231     protected BranchScmResult branch( ScmProviderRepository repository, ScmFileSet fileSet,
232                                       CommandParameters parameters )
233         throws ScmException
234     {
235         TfsBranchCommand command = new TfsBranchCommand();
236         command.setLogger( getLogger() );
237         return (BranchScmResult) command.execute( repository, fileSet, parameters );
238     }
239 
240     protected ListScmResult list( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
241         throws ScmException
242     {
243         TfsListCommand command = new TfsListCommand();
244         command.setLogger( getLogger() );
245         return (ListScmResult) command.execute( repository, fileSet, parameters );
246     }
247 
248     protected BlameScmResult blame( ScmProviderRepository repository, ScmFileSet fileSet,
249                                     CommandParameters parameters )
250         throws ScmException
251     {
252         TfsBlameCommand command = new TfsBlameCommand();
253         command.setLogger( getLogger() );
254         return (BlameScmResult) command.execute( repository, fileSet, parameters );
255     }
256 
257     protected DiffScmResult diff( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
258         throws ScmException
259     {
260         // Because tf launches only external diffs
261         return super.diff( repository, fileSet, parameters );
262     }
263 
264     protected ExportScmResult export( ScmProviderRepository repository, ScmFileSet fileSet,
265                                       CommandParameters parameters )
266         throws ScmException
267     {
268         // Use checkout instead
269         return super.export( repository, fileSet, parameters );
270     }
271 
272 }