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.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmVersion;
025import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
026import org.apache.maven.scm.command.checkout.CheckOutScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.cvslib.command.CvsCommand;
029import org.apache.maven.scm.provider.cvslib.command.CvsCommandUtils;
030import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
031import org.codehaus.plexus.util.FileUtils;
032import org.codehaus.plexus.util.StringUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.IOException;
036
037/**
038 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse </a>
039 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
040 *
041 */
042public abstract class AbstractCvsCheckOutCommand
043    extends AbstractCheckOutCommand
044    implements CvsCommand
045{
046    /** {@inheritDoc} */
047    protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
048                                                        ScmVersion version, boolean recursive )
049        throws ScmException
050    {
051        if ( fileSet.getBasedir().exists() )
052        {
053            try
054            {
055                FileUtils.deleteDirectory( fileSet.getBasedir() );
056            }
057            catch ( IOException e )
058            {
059                if ( getLogger().isWarnEnabled() )
060                {
061                    getLogger().warn( "Can't delete " + fileSet.getBasedir().getAbsolutePath(), e );
062                }
063            }
064        }
065
066        CvsScmProviderRepository repository = (CvsScmProviderRepository) repo;
067
068        Commandline cl = CvsCommandUtils.getBaseCommand( "checkout", repository, fileSet );
069
070        cl.setWorkingDirectory( fileSet.getBasedir().getParentFile().getAbsolutePath() );
071
072        if ( version != null && !StringUtils.isEmpty( version.getName() ) )
073        {
074            cl.createArg().setValue( "-r" );
075            cl.createArg().setValue( version.getName() );
076        }
077
078        cl.createArg().setValue( "-d" );
079
080        cl.createArg().setValue( fileSet.getBasedir().getName() );
081
082        cl.createArg().setValue( repository.getModule() );
083
084        if ( getLogger().isInfoEnabled() )
085        {
086            getLogger().info( "Executing: " + cl );
087            getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
088        }
089
090        return executeCvsCommand( cl );
091    }
092
093    protected abstract CheckOutScmResult executeCvsCommand( Commandline cl )
094        throws ScmException;
095}