001    package org.apache.maven.scm.provider.clearcase.command.edit;
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.ScmResult;
028    import org.apache.maven.scm.command.edit.AbstractEditCommand;
029    import org.apache.maven.scm.command.edit.EditScmResult;
030    import org.apache.maven.scm.log.ScmLogger;
031    import org.apache.maven.scm.provider.ScmProviderRepository;
032    import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
033    import org.codehaus.plexus.util.cli.CommandLineException;
034    import org.codehaus.plexus.util.cli.CommandLineUtils;
035    import org.codehaus.plexus.util.cli.Commandline;
036    
037    /**
038     * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
039     *
040     */
041    public class ClearCaseEditCommand
042        extends AbstractEditCommand
043        implements ClearCaseCommand
044    {
045        /** {@inheritDoc} */
046        protected ScmResult executeEditCommand( ScmProviderRepository repository, ScmFileSet fileSet )
047            throws ScmException
048        {
049            if ( getLogger().isDebugEnabled() )
050            {
051                getLogger().debug( "executing edit command..." );
052            }
053            Commandline cl = createCommandLine( getLogger(), fileSet );
054    
055            ClearCaseEditConsumer consumer = new ClearCaseEditConsumer( getLogger() );
056    
057            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
058    
059            int exitCode;
060    
061            try
062            {
063                if ( getLogger().isDebugEnabled() )
064                {
065                    getLogger().debug(
066                                       "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
067                                           + cl.toString() );
068                }
069                exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
070            }
071            catch ( CommandLineException ex )
072            {
073                throw new ScmException( "Error while executing clearcase command.", ex );
074            }
075    
076            if ( exitCode != 0 )
077            {
078                return new EditScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
079            }
080    
081            return new EditScmResult( cl.toString(), consumer.getEditFiles() );
082        }
083    
084        // ----------------------------------------------------------------------
085        //
086        // ----------------------------------------------------------------------
087    
088        public static Commandline createCommandLine( ScmLogger logger, ScmFileSet scmFileSet )
089        {
090            Commandline command = new Commandline();
091    
092            File workingDirectory = scmFileSet.getBasedir();
093    
094            command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
095    
096            command.setExecutable( "cleartool" );
097    
098            command.createArg().setValue( "co" );
099    
100            command.createArg().setValue( "-nc" );
101    
102            List<File> files = scmFileSet.getFileList();
103            for ( File file : files )
104            {
105                if ( logger.isInfoEnabled() )
106                {
107                    logger.info( "edit file: " + file.getAbsolutePath() );
108                }
109                command.createArg().setValue( file.getAbsolutePath() );
110            }
111    
112            return command;
113        }
114    
115        public static Commandline createCheckoutCurrentDirCommandLine( ScmFileSet scmFileSet )
116        {
117            Commandline command = new Commandline();
118    
119            File workingDirectory = scmFileSet.getBasedir();
120    
121            command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
122    
123            command.setExecutable( "cleartool" );
124    
125            command.createArg().setValue( "co" );
126    
127            command.createArg().setValue( "-nc" );
128    
129            command.createArg().setValue( "." );
130    
131            return command;
132        }
133    
134        public static Commandline createCheckinCurrentDirCommandLine( ScmFileSet scmFileSet )
135        {
136            Commandline command = new Commandline();
137    
138            File workingDirectory = scmFileSet.getBasedir();
139    
140            command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
141    
142            command.setExecutable( "cleartool" );
143    
144            command.createArg().setValue( "ci" );
145    
146            command.createArg().setValue( "-nc" );
147    
148            command.createArg().setValue( "." );
149    
150            return command;
151        }
152    
153    }