View Javadoc

1   package org.apache.maven.scm.provider.cvslib.cvsjava.command.blame;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.command.blame.BlameScmResult;
23  import org.apache.maven.scm.provider.cvslib.command.blame.AbstractCvsBlameCommand;
24  import org.apache.maven.scm.provider.cvslib.command.blame.CvsBlameConsumer;
25  import org.apache.maven.scm.provider.cvslib.cvsjava.util.CvsConnection;
26  import org.apache.maven.scm.provider.cvslib.cvsjava.util.CvsLogListener;
27  import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
28  import org.codehaus.plexus.util.cli.Commandline;
29  
30  import java.io.BufferedReader;
31  import java.io.ByteArrayInputStream;
32  import java.io.InputStreamReader;
33  
34  /**
35   * @author Evgeny Mandrikov
36   * @since 1.4
37   */
38  public class CvsJavaBlameCommand
39      extends AbstractCvsBlameCommand
40  {
41      /**
42       * {@inheritDoc}
43       */
44      protected BlameScmResult executeCvsCommand( Commandline cl, CvsScmProviderRepository repository )
45      {
46          CvsLogListener logListener = new CvsLogListener();
47          CvsBlameConsumer consumer = new CvsBlameConsumer( getLogger() );
48          try
49          {
50              boolean isSuccess =
51                  CvsConnection.processCommand( cl.getArguments(), cl.getWorkingDirectory().getAbsolutePath(),
52                                                logListener, getLogger() );
53              if ( !isSuccess )
54              {
55                  return new BlameScmResult( cl.toString(), "The cvs command failed.", logListener.getStderr().toString(),
56                                             false );
57              }
58              BufferedReader stream = new BufferedReader(
59                  new InputStreamReader( new ByteArrayInputStream( logListener.getStdout().toString().getBytes() ) ) );
60              String line;
61              while ( ( line = stream.readLine() ) != null )
62              {
63                  consumer.consumeLine( line );
64              }
65          }
66          catch ( Exception e )
67          {
68              getLogger().error( e );
69              return new BlameScmResult( cl.toString(), "The cvs command failed.", logListener.getStdout().toString(),
70                                         false );
71          }
72  
73          return new BlameScmResult( cl.toString(), consumer.getLines() );
74      }
75  }