001    package org.apache.maven.scm.provider.cvslib.cvsjava.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 org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.command.checkin.CheckInScmResult;
024    import org.apache.maven.scm.provider.cvslib.command.checkin.AbstractCvsCheckInCommand;
025    import org.apache.maven.scm.provider.cvslib.command.checkin.CvsCheckInConsumer;
026    import org.apache.maven.scm.provider.cvslib.cvsjava.util.CvsConnection;
027    import org.apache.maven.scm.provider.cvslib.cvsjava.util.CvsLogListener;
028    import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
029    import org.codehaus.plexus.util.FileUtils;
030    import org.codehaus.plexus.util.cli.Commandline;
031    
032    import java.io.BufferedReader;
033    import java.io.ByteArrayInputStream;
034    import java.io.File;
035    import java.io.IOException;
036    import java.io.InputStreamReader;
037    
038    /**
039     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
040     * @version $Id: CvsJavaCheckInCommand.java 685548 2008-08-13 13:35:49Z vsiveton $
041     */
042    public class CvsJavaCheckInCommand
043        extends AbstractCvsCheckInCommand
044    {
045        /** {@inheritDoc} */
046        protected CheckInScmResult executeCvsCommand( Commandline cl, CvsScmProviderRepository repository,
047                                                      File messageFile )
048            throws ScmException
049        {
050            CvsLogListener logListener = new CvsLogListener();
051    
052            CvsCheckInConsumer consumer = new CvsCheckInConsumer( repository.getPath(), getLogger() );
053    
054            try
055            {
056                boolean isSuccess = CvsConnection.processCommand( cl.getArguments(),
057                                                                  cl.getWorkingDirectory().getAbsolutePath(), logListener,
058                                                                  getLogger() );
059    
060                if ( !isSuccess )
061                {
062                    return new CheckInScmResult( cl.toString(), "The cvs command failed.",
063                                                 logListener.getStderr().toString(), false );
064                }
065                BufferedReader stream = new BufferedReader(
066                    new InputStreamReader( new ByteArrayInputStream( logListener.getStdout().toString().getBytes() ) ) );
067    
068                String line;
069    
070                while ( ( line = stream.readLine() ) != null )
071                {
072                    consumer.consumeLine( line );
073                }
074            }
075            catch ( Exception e )
076            {
077                e.printStackTrace();
078                return new CheckInScmResult( cl.toString(), "The cvs command failed.", logListener.getStdout().toString(),
079                                             false );
080            }
081            finally
082            {
083                try
084                {
085                    FileUtils.forceDelete( messageFile );
086                }
087                catch ( IOException ex )
088                {
089                    // ignore
090                }
091    
092    
093            }
094    
095            return new CheckInScmResult( cl.toString(), consumer.getCheckedInFiles() );
096        }
097    }