001package org.apache.maven.scm.provider.cvslib.command.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
022import org.apache.maven.scm.CommandParameter;
023import org.apache.maven.scm.CommandParameters;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmResult;
027import org.apache.maven.scm.ScmVersion;
028import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
029import org.apache.maven.scm.command.checkout.CheckOutScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.cvslib.command.CvsCommand;
032import org.apache.maven.scm.provider.cvslib.command.CvsCommandUtils;
033import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
034import org.codehaus.plexus.util.FileUtils;
035import org.codehaus.plexus.util.StringUtils;
036import org.codehaus.plexus.util.cli.Commandline;
037
038import java.io.IOException;
039
040/**
041 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse </a>
042 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
043 *
044 */
045public abstract class AbstractCvsCheckOutCommand
046    extends AbstractCheckOutCommand
047    implements CvsCommand
048{
049
050    /**
051     * The overriden {@link #executeCommand(ScmProviderRepository, ScmFileSet, CommandParameters)}
052     * in this class will not call this method!
053     * <p>
054     * {@inheritDoc}
055     */
056    protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
057                                                       ScmVersion version, boolean recursive, boolean shallow )
058        throws ScmException
059    {
060        throw new UnsupportedOperationException( "Should not get here" );
061    }
062
063    @Override
064    public ScmResult executeCommand( ScmProviderRepository repo, ScmFileSet fileSet,
065                                     CommandParameters parameters )
066        throws ScmException
067    {
068        ScmVersion version = parameters.getScmVersion( CommandParameter.SCM_VERSION, null );
069        boolean binary = parameters.getBoolean( CommandParameter.BINARY, false );
070
071        if ( fileSet.getBasedir().exists() )
072        {
073            try
074            {
075                FileUtils.deleteDirectory( fileSet.getBasedir() );
076            }
077            catch ( IOException e )
078            {
079                if ( getLogger().isWarnEnabled() )
080                {
081                    getLogger().warn( "Can't delete " + fileSet.getBasedir().getAbsolutePath(), e );
082                }
083            }
084        }
085
086        CvsScmProviderRepository repository = (CvsScmProviderRepository) repo;
087
088        Commandline cl = CvsCommandUtils.getBaseCommand( "checkout", repository, fileSet );
089
090        cl.setWorkingDirectory( fileSet.getBasedir().getParentFile().getAbsolutePath() );
091
092        if ( binary )
093        {
094            cl.createArg().setValue( "-kb" );
095        }
096
097        if ( version != null && !StringUtils.isEmpty( version.getName() ) )
098        {
099            cl.createArg().setValue( "-r" );
100            cl.createArg().setValue( version.getName() );
101        }
102
103        cl.createArg().setValue( "-d" );
104
105        cl.createArg().setValue( fileSet.getBasedir().getName() );
106
107        cl.createArg().setValue( repository.getModule() );
108
109        if ( getLogger().isInfoEnabled() )
110        {
111            getLogger().info( "Executing: " + cl );
112            getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
113        }
114
115        return executeCvsCommand( cl );
116    }
117
118    protected abstract CheckOutScmResult executeCvsCommand( Commandline cl )
119        throws ScmException;
120}