001    package org.apache.maven.scm.provider.clearcase.command.checkin;
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    
022    import java.io.File;
023    import java.util.List;
024    
025    import org.apache.maven.scm.ScmException;
026    import org.apache.maven.scm.ScmFileSet;
027    import org.apache.maven.scm.ScmVersion;
028    import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
029    import org.apache.maven.scm.command.checkin.CheckInScmResult;
030    import org.apache.maven.scm.provider.ScmProviderRepository;
031    import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
032    import org.codehaus.plexus.util.cli.CommandLineException;
033    import org.codehaus.plexus.util.cli.CommandLineUtils;
034    import org.codehaus.plexus.util.cli.Commandline;
035    
036    /**
037     * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
038     * @author Olivier Lamy
039     * @version $Id: ClearCaseCheckInCommand.java 1054455 2011-01-02 18:40:29Z olamy $
040     */
041    public class ClearCaseCheckInCommand
042        extends AbstractCheckInCommand
043        implements ClearCaseCommand
044    {
045        // ----------------------------------------------------------------------
046        // AbstractCheckOutCommand Implementation
047        // ----------------------------------------------------------------------
048    
049        /** {@inheritDoc} */
050        protected CheckInScmResult executeCheckInCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet,
051                                                          String message, ScmVersion version )
052            throws ScmException
053        {
054            if ( getLogger().isDebugEnabled() )
055            {
056                getLogger().debug( "executing checkin command..." );
057            }
058            Commandline cl = createCommandLine( fileSet, message );
059    
060            ClearCaseCheckInConsumer consumer = new ClearCaseCheckInConsumer( getLogger() );
061    
062            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
063    
064            int exitCode;
065    
066            try
067            {
068                if ( getLogger().isDebugEnabled() )
069                {
070                    getLogger().debug(
071                                       "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
072                                           + cl.toString() );
073                }
074                exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
075            }
076            catch ( CommandLineException ex )
077            {
078                throw new ScmException( "Error while executing clearcase command.", ex );
079            }
080    
081            if ( exitCode != 0 )
082            {
083                return new CheckInScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
084            }
085    
086            return new CheckInScmResult( cl.toString(), consumer.getCheckedInFiles() );
087        }
088    
089        // ----------------------------------------------------------------------
090        //
091        // ----------------------------------------------------------------------
092    
093        public static Commandline createCommandLine( ScmFileSet scmFileSet, String message )
094            throws ScmException
095        {
096            Commandline command = new Commandline();
097    
098            File workingDirectory = scmFileSet.getBasedir();
099    
100            command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
101    
102            command.setExecutable( "cleartool" );
103    
104            command.createArg().setValue( "ci" );
105    
106            if ( message != null )
107            {
108                command.createArg().setValue( "-c" );
109                command.createArg().setLine( "\"" + message + "\"" );
110            }
111            else
112            {
113                command.createArg().setValue( "-nc" );
114            }
115    
116            List<File> files = scmFileSet.getFileList();
117            if ( files.isEmpty() )
118            {
119                throw new ScmException( "There are no files in the fileset to check in!" );
120            }
121            for ( File file : files )
122            {
123                command.createArg().setValue( file.getAbsolutePath() );
124            }
125    
126            return command;
127        }
128    }