001package org.apache.maven.scm.provider.clearcase.command.update;
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.changelog.ChangeLogCommand;
026import org.apache.maven.scm.command.update.AbstractUpdateCommand;
027import org.apache.maven.scm.command.update.UpdateScmResult;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
030import org.apache.maven.scm.provider.clearcase.command.changelog.ClearCaseChangeLogCommand;
031import org.codehaus.plexus.util.cli.CommandLineException;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.File;
036
037/**
038 * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
039 * @author Olivier Lamy
040 *
041 */
042public class ClearCaseUpdateCommand
043    extends AbstractUpdateCommand
044    implements ClearCaseCommand
045{
046    /** {@inheritDoc} */
047    protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
048                                                    ScmVersion version )
049        throws ScmException
050    {
051        if ( getLogger().isDebugEnabled() )
052        {
053            getLogger().debug( "executing update command..." );
054        }
055        Commandline cl = createCommandLine( fileSet );
056
057        ClearCaseUpdateConsumer consumer = new ClearCaseUpdateConsumer( getLogger() );
058
059        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
060
061        int exitCode;
062
063        try
064        {
065            if ( getLogger().isDebugEnabled() )
066            {
067                getLogger().debug(
068                                   "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
069                                       + cl.toString() );
070            }
071            exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
072        }
073        catch ( CommandLineException ex )
074        {
075            throw new ScmException( "Error while executing clearcase command.", ex );
076        }
077
078        if ( exitCode != 0 )
079        {
080            return new UpdateScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
081        }
082
083        return new UpdateScmResult( cl.toString(), consumer.getUpdatedFiles() );
084    }
085
086    /** {@inheritDoc} */
087    protected ChangeLogCommand getChangeLogCommand()
088    {
089        ClearCaseChangeLogCommand changeLogCmd = new ClearCaseChangeLogCommand();
090
091        changeLogCmd.setLogger( getLogger() );
092
093        return changeLogCmd;
094    }
095
096    // ----------------------------------------------------------------------
097    //
098    // ----------------------------------------------------------------------
099
100    public static Commandline createCommandLine( ScmFileSet scmFileSet )
101    {
102        Commandline command = new Commandline();
103
104        File workingDirectory = scmFileSet.getBasedir();
105
106        command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
107
108        command.setExecutable( "cleartool" );
109
110        command.createArg().setValue( "update" );
111
112        command.createArg().setValue( "-f" );
113
114        return command;
115    }
116}