View Javadoc
1   package org.apache.maven.scm.provider.vss.commands.checkout;
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.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmVersion;
25  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
26  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
29  import org.apache.maven.scm.provider.vss.commands.VssConstants;
30  import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
31  import org.codehaus.plexus.util.cli.CommandLineUtils;
32  import org.codehaus.plexus.util.cli.Commandline;
33  
34  /**
35   * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
36   */
37  public class VssCheckOutCommand
38      extends AbstractCheckOutCommand
39  {
40  
41      /** {@inheritDoc} */
42      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repository, ScmFileSet fileSet,
43                                                         ScmVersion version, boolean recursive, boolean shallow )
44          throws ScmException
45      {
46          if ( getLogger().isDebugEnabled() )
47          {
48              getLogger().debug( "executing checkout command..." );
49          }
50  
51          VssScmProviderRepository repo = (VssScmProviderRepository) repository;
52  
53          Commandline cl = buildCmdLine( repo, fileSet, version );
54  
55          VssCheckOutConsumer consumer = new VssCheckOutConsumer( repo, getLogger() );
56  
57          //      TODO handle deleted files from VSS
58          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
59  
60          int exitCode;
61  
62          if ( getLogger().isDebugEnabled() )
63          {
64              getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
65          }
66  
67          exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
68  
69          if ( exitCode != 0 )
70          {
71              String error = stderr.getOutput();
72  
73              if ( getLogger().isDebugEnabled() )
74              {
75                  getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
76              }
77              if ( error.indexOf( "A writable copy of" ) < 0 )
78              {
79                  return new CheckOutScmResult( cl.toString(), "The vss command failed.", error, false );
80              }
81              // print out the writable copy for manual handling
82              if ( getLogger().isWarnEnabled() )
83              {
84                  getLogger().warn( error );
85              }
86          }
87  
88          return new CheckOutScmResult( cl.toString(), consumer.getUpdatedFiles() );
89      }
90  
91      public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
92          throws ScmException
93      {
94  
95          Commandline command = new Commandline();
96  
97          command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
98  
99          try
100         {
101             command.addSystemEnvironment();
102         }
103         catch ( Exception e )
104         {
105             throw new ScmException( "Can't add system environment.", e );
106         }
107 
108         command.addEnvironment( "SSDIR", repo.getVssdir() );
109 
110         String ssDir = VssCommandLineUtils.getSsDir();
111 
112         command.setExecutable( ssDir + VssConstants.SS_EXE );
113 
114         command.createArg().setValue( VssConstants.COMMAND_GET );
115 
116         command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
117 
118         //User identification to get access to vss repository
119         if ( repo.getUserPassword() != null )
120         {
121             command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
122         }
123 
124         //Display the history of an entire project list
125         command.createArg().setValue( VssConstants.FLAG_RECURSION );
126 
127         //Ignore: Do not ask for input under any circumstances.
128         command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
129 
130         //Ignore: Do not touch local writable files.
131         command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE );
132 
133         if ( version != null )
134         {
135             command.createArg().setValue( VssConstants.FLAG_VERSION_LABEL + '"' + version + '"' );
136         }
137 
138         return command;
139     }
140 }