001package org.apache.maven.scm.provider.starteam.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.starteam.command.StarteamCommand;
029import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
030import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
031import org.codehaus.plexus.util.StringUtils;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.util.ArrayList;
036import java.util.List;
037
038/**
039 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
040 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
041 * @author Olivier Lamy
042 *
043 */
044public class StarteamCheckOutCommand
045    extends AbstractCheckOutCommand
046    implements StarteamCommand
047{
048    // ----------------------------------------------------------------------
049    // AbstractCheckOutCommand Implementation
050    // ----------------------------------------------------------------------
051
052    /** {@inheritDoc} */
053    protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
054                                                        ScmVersion version, boolean recursive )
055        throws ScmException
056    {
057        if ( fileSet.getFileList().size() != 0 )
058        {
059            throw new ScmException( "This provider doesn't support checking out subsets of a directory" );
060        }
061
062        if ( getLogger().isInfoEnabled() )
063        {
064            getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
065        }
066
067        StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
068
069        StarteamCheckOutConsumer consumer = new StarteamCheckOutConsumer( getLogger(), fileSet.getBasedir() );
070
071        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
072
073        Commandline cl = createCommandLine( repository, fileSet, version );
074
075        int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
076
077        if ( exitCode != 0 )
078        {
079            return new CheckOutScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
080        }
081
082        return new CheckOutScmResult( cl.toString(), consumer.getCheckedOutFiles() );
083    }
084
085    // ----------------------------------------------------------------------
086    //
087    // ----------------------------------------------------------------------
088
089    public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet baseDir,
090                                                 ScmVersion version )
091    {
092        List<String> args = new ArrayList<String>();
093
094        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
095        {
096            args.add( "-vl" );
097            args.add( version.getName() );
098        }
099
100        StarteamCommandLineUtils.addEOLOption( args );
101
102        return StarteamCommandLineUtils.createStarteamCommandLine( "co", args, baseDir, repo );
103    }
104}