View Javadoc
1   package org.apache.maven.scm.provider.vss.commands.edit;
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.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmResult;
31  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
32  import org.apache.maven.scm.command.edit.AbstractEditCommand;
33  import org.apache.maven.scm.command.edit.EditScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
36  import org.apache.maven.scm.provider.vss.commands.VssConstants;
37  import org.apache.maven.scm.provider.vss.commands.changelog.VssHistoryCommand;
38  import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
39  import org.codehaus.plexus.util.cli.CommandLineUtils;
40  import org.codehaus.plexus.util.cli.Commandline;
41  
42  /**
43   * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
44   * @version $Id: VssCheckOutCommand.java 02.06.2006 00:05:51
45   */
46  public class VssEditCommand
47      extends AbstractEditCommand
48  {
49  
50      /** {@inheritDoc} */
51      protected ScmResult executeEditCommand( ScmProviderRepository repository, ScmFileSet fileSet )
52          throws ScmException
53      {
54          if ( getLogger().isDebugEnabled() )
55          {
56              getLogger().debug( "executing checkout command..." );
57          }
58  
59          VssScmProviderRepository repo = (VssScmProviderRepository) repository;
60  
61          List<Commandline> commandLines = buildCmdLine( repo, fileSet );
62  
63          VssEditConsumer consumer = new VssEditConsumer( repo, getLogger() );
64  
65          //      TODO handle deleted files from VSS
66          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
67  
68          int exitCode;
69  
70          StringBuilder sb = new StringBuilder();
71          List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
72  
73          for ( Commandline cl : commandLines )
74          {
75  
76              if ( getLogger().isDebugEnabled() )
77              {
78                  getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
79              }
80  
81              exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
82  
83              if ( exitCode != 0 )
84              {
85                  String error = stderr.getOutput();
86  
87                  if ( getLogger().isDebugEnabled() )
88                  {
89                      getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
90                  }
91                  if ( error.indexOf( "A writable copy of" ) < 0 )
92                  {
93                      return new EditScmResult( cl.toString(), "The vss command failed.", error, false );
94                  }
95                  // print out the writable copy for manual handling
96                  if ( getLogger().isWarnEnabled() )
97                  {
98                      getLogger().warn( error );
99                  }
100                 break;
101             }
102 
103             sb.append( cl.toString() + '\n' );
104             updatedFiles.addAll( consumer.getUpdatedFiles() );
105 
106         }
107         return new EditScmResult( sb.toString(), updatedFiles );
108 
109     }
110 
111     public List<Commandline> buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet )
112         throws ScmException
113     {
114         List<File> files = fileSet.getFileList();
115         List<Commandline> commands = new ArrayList<Commandline>();
116 
117         if ( files.size() > 0 )
118         {
119 
120             String base;
121             try
122             {
123                 base = fileSet.getBasedir().getCanonicalPath();
124             }
125             catch ( IOException e )
126             {
127                 throw new ScmException( "Invalid canonical path", e );
128             }
129 
130             for ( File file : files )
131             {
132 
133                 Commandline command = new Commandline();
134 
135                 try
136                 {
137                     command.addSystemEnvironment();
138                 }
139                 catch ( Exception e )
140                 {
141                     throw new ScmException( "Can't add system environment.", e );
142                 }
143 
144                 command.addEnvironment( "SSDIR", repo.getVssdir() );
145 
146                 String ssDir = VssCommandLineUtils.getSsDir();
147 
148                 command.setExecutable( ssDir + VssConstants.SS_EXE );
149 
150                 command.createArg().setValue( VssConstants.COMMAND_CHECKOUT );
151 
152                 String absolute;
153                 try
154                 {
155                     absolute = file.getCanonicalPath();
156                     String relative;
157                     int index = absolute.indexOf( base );
158                     if ( index >= 0 )
159                     {
160                         relative = absolute.substring( index + base.length() );
161                     }
162                     else
163                     {
164                         relative = file.getPath();
165                     }
166 
167                     relative = relative.replace( '\\', '/' );
168 
169                     if ( !relative.startsWith( "/" ) )
170                     {
171                         relative = '/' + relative;
172                     }
173 
174                     String relativeFolder = relative.substring( 0, relative.lastIndexOf( '/' ) );
175 
176                     command.setWorkingDirectory( new File( fileSet.getBasedir().getAbsolutePath() + File.separatorChar
177                         + relativeFolder ).getCanonicalPath() );
178 
179                     command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() + relative );
180                 }
181                 catch ( IOException e )
182                 {
183                     throw new ScmException( "Invalid canonical path", e );
184                 }
185 
186                 //User identification to get access to vss repository
187                 if ( repo.getUserPassword() != null )
188                 {
189                     command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
190                 }
191 
192                 //Ignore: Do not ask for input under any circumstances.
193                 command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
194 
195                 commands.add( command );
196 
197             }
198 
199         }
200         else
201         {
202             Commandline command = new Commandline();
203 
204             command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
205 
206             try
207             {
208                 command.addSystemEnvironment();
209             }
210             catch ( Exception e )
211             {
212                 throw new ScmException( "Can't add system environment.", e );
213             }
214 
215             command.addEnvironment( "SSDIR", repo.getVssdir() );
216 
217             String ssDir = VssCommandLineUtils.getSsDir();
218 
219             command.setExecutable( ssDir + VssConstants.SS_EXE );
220 
221             command.createArg().setValue( VssConstants.COMMAND_CHECKOUT );
222 
223             command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
224             //Display the history of an entire project list
225             command.createArg().setValue( VssConstants.FLAG_RECURSION );
226 
227             //User identification to get access to vss repository
228             if ( repo.getUserPassword() != null )
229             {
230                 command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
231             }
232 
233             //Ignore: Do not ask for input under any circumstances.
234             command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
235 
236             commands.add( command );
237 
238         }
239 
240         return commands;
241     }
242 
243     /**
244      * @return
245      */
246     protected ChangeLogCommand getChangeLogCommand()
247     {
248         VssHistoryCommand command = new VssHistoryCommand();
249 
250         command.setLogger( getLogger() );
251 
252         return command;
253     }
254 
255 }