001    package org.apache.maven.scm.provider.vss.commands.checkout;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmVersion;
025    import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
026    import org.apache.maven.scm.command.checkout.CheckOutScmResult;
027    import org.apache.maven.scm.provider.ScmProviderRepository;
028    import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
029    import org.apache.maven.scm.provider.vss.commands.VssConstants;
030    import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
031    import org.codehaus.plexus.util.cli.CommandLineUtils;
032    import org.codehaus.plexus.util.cli.Commandline;
033    
034    /**
035     * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
036     * @version $Id: VssCheckOutCommand.java 02.06.2006 00:05:51
037     */
038    public class VssCheckOutCommand
039        extends AbstractCheckOutCommand
040    {
041    
042        /** {@inheritDoc} */
043        protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repository, ScmFileSet fileSet,
044                                                            ScmVersion version, boolean recursive )
045            throws ScmException
046        {
047            if ( getLogger().isDebugEnabled() )
048            {
049                getLogger().debug( "executing checkout command..." );
050            }
051    
052            VssScmProviderRepository repo = (VssScmProviderRepository) repository;
053    
054            Commandline cl = buildCmdLine( repo, fileSet, version );
055    
056            VssCheckOutConsumer consumer = new VssCheckOutConsumer( repo, getLogger() );
057    
058            //      TODO handle deleted files from VSS
059            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
060    
061            int exitCode;
062    
063            if ( getLogger().isDebugEnabled() )
064            {
065                getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
066            }
067    
068            exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
069    
070            if ( exitCode != 0 )
071            {
072                String error = stderr.getOutput();
073    
074                if ( getLogger().isDebugEnabled() )
075                {
076                    getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
077                }
078                if ( error.indexOf( "A writable copy of" ) < 0 )
079                {
080                    return new CheckOutScmResult( cl.toString(), "The vss command failed.", error, false );
081                }
082                // print out the writable copy for manual handling
083                if ( getLogger().isWarnEnabled() )
084                {
085                    getLogger().warn( error );
086                }
087            }
088    
089            return new CheckOutScmResult( cl.toString(), consumer.getUpdatedFiles() );
090        }
091    
092        public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
093            throws ScmException
094        {
095    
096            Commandline command = new Commandline();
097    
098            command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
099    
100            try
101            {
102                command.addSystemEnvironment();
103            }
104            catch ( Exception e )
105            {
106                throw new ScmException( "Can't add system environment.", e );
107            }
108    
109            command.addEnvironment( "SSDIR", repo.getVssdir() );
110    
111            String ssDir = VssCommandLineUtils.getSsDir();
112    
113            command.setExecutable( ssDir + VssConstants.SS_EXE );
114    
115            command.createArg().setValue( VssConstants.COMMAND_GET );
116    
117            command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
118    
119            //User identification to get access to vss repository
120            if ( repo.getUserPassword() != null )
121            {
122                command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
123            }
124    
125            //Display the history of an entire project list
126            command.createArg().setValue( VssConstants.FLAG_RECURSION );
127    
128            //Ignore: Do not ask for input under any circumstances.
129            command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
130    
131            //Ignore: Do not touch local writable files.
132            command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE );
133    
134            if ( version != null )
135            {
136                command.createArg().setValue( VssConstants.FLAG_VERSION_LABEL + '"' + version + '"' );
137            }
138    
139            return command;
140        }
141    }