001package org.apache.maven.scm.provider.clearcase.command.status;
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.command.status.AbstractStatusCommand;
025import org.apache.maven.scm.command.status.StatusScmResult;
026import org.apache.maven.scm.provider.ScmProviderRepository;
027import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
028import org.codehaus.plexus.util.cli.CommandLineException;
029import org.codehaus.plexus.util.cli.CommandLineUtils;
030import org.codehaus.plexus.util.cli.Commandline;
031
032import java.io.File;
033
034/**
035 * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
036 *
037 */
038public class ClearCaseStatusCommand
039    extends AbstractStatusCommand
040    implements ClearCaseCommand
041{
042    /** {@inheritDoc} */
043    protected StatusScmResult executeStatusCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet )
044        throws ScmException
045    {
046        if ( getLogger().isDebugEnabled() )
047        {
048            getLogger().debug( "executing status command..." );
049        }
050        Commandline cl = createCommandLine( scmFileSet );
051
052        ClearCaseStatusConsumer consumer = new ClearCaseStatusConsumer( getLogger(), scmFileSet.getBasedir() );
053
054        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
055
056        int exitCode;
057
058        try
059        {
060            if ( getLogger().isDebugEnabled() )
061            {
062                getLogger().debug(
063                                   "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
064                                       + cl.toString() );
065            }
066            exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
067        }
068        catch ( CommandLineException ex )
069        {
070            throw new ScmException( "Error while executing clearcase command.", ex );
071        }
072
073        if ( exitCode != 0 )
074        {
075            return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
076        }
077
078        return new StatusScmResult( cl.toString(), consumer.getCheckedOutFiles() );
079    }
080
081    // ----------------------------------------------------------------------
082    //
083    // ----------------------------------------------------------------------
084
085    public static Commandline createCommandLine( ScmFileSet scmFileSet )
086    {
087        Commandline command = new Commandline();
088
089        File workingDirectory = scmFileSet.getBasedir();
090
091        command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
092
093        command.setExecutable( "cleartool" );
094
095        command.createArg().setValue( "lscheckout" );
096        command.createArg().setValue( "-cview" );
097        command.createArg().setValue( "-r" );
098        command.createArg().setValue( "-fmt" );
099        command.createArg().setValue( "%n\\n" );
100
101        return command;
102    }
103}