001package org.apache.maven.scm.provider.cvslib.cvsjava.command.blame;
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.command.blame.BlameScmResult;
023import org.apache.maven.scm.provider.cvslib.command.blame.AbstractCvsBlameCommand;
024import org.apache.maven.scm.provider.cvslib.command.blame.CvsBlameConsumer;
025import org.apache.maven.scm.provider.cvslib.cvsjava.util.CvsConnection;
026import org.apache.maven.scm.provider.cvslib.cvsjava.util.CvsLogListener;
027import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
028import org.codehaus.plexus.util.cli.Commandline;
029
030import java.io.BufferedReader;
031import java.io.ByteArrayInputStream;
032import java.io.InputStreamReader;
033
034/**
035 * @author Evgeny Mandrikov
036 * @since 1.4
037 */
038public class CvsJavaBlameCommand
039    extends AbstractCvsBlameCommand
040{
041    /**
042     * {@inheritDoc}
043     */
044    protected BlameScmResult executeCvsCommand( Commandline cl, CvsScmProviderRepository repository )
045    {
046        CvsLogListener logListener = new CvsLogListener();
047        CvsBlameConsumer consumer = new CvsBlameConsumer( getLogger() );
048        try
049        {
050            boolean isSuccess =
051                CvsConnection.processCommand( cl.getArguments(), cl.getWorkingDirectory().getAbsolutePath(),
052                                              logListener, getLogger() );
053            if ( !isSuccess )
054            {
055                return new BlameScmResult( cl.toString(), "The cvs command failed.", logListener.getStderr().toString(),
056                                           false );
057            }
058            BufferedReader stream = new BufferedReader(
059                new InputStreamReader( new ByteArrayInputStream( logListener.getStdout().toString().getBytes() ) ) );
060            String line;
061            while ( ( line = stream.readLine() ) != null )
062            {
063                consumer.consumeLine( line );
064            }
065        }
066        catch ( Exception e )
067        {
068            getLogger().error( e );
069            return new BlameScmResult( cl.toString(), "The cvs command failed.", logListener.getStdout().toString(),
070                                       false );
071        }
072
073        return new BlameScmResult( cl.toString(), consumer.getLines() );
074    }
075}