View Javadoc
1   package org.apache.maven.scm.provider.integrity.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.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmResult;
25  import org.apache.maven.scm.command.blame.AbstractBlameCommand;
26  import org.apache.maven.scm.command.blame.BlameScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
29  import org.codehaus.plexus.util.cli.CommandLineException;
30  import org.codehaus.plexus.util.cli.CommandLineUtils;
31  import org.codehaus.plexus.util.cli.Commandline;
32  
33  /**
34   * MKS Integrity implementation for Maven's AbstractBlameCommand
35   * <br>This class will execute a 'si annotate' command for the specified filename
36   *
37   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
38   * @since 1.6
39   */
40  public class IntegrityBlameCommand
41      extends AbstractBlameCommand
42  {
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      public BlameScmResult executeBlameCommand( ScmProviderRepository repository, ScmFileSet workingDirectory,
48                                                 String filename )
49          throws ScmException
50      {
51          getLogger().info( "Attempting to display blame results for file: " + filename );
52          if ( null == filename || filename.length() == 0 )
53          {
54              throw new ScmException( "A single filename is required to execute the blame command!" );
55          }
56          BlameScmResult result;
57          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
58          // Since the si annotate command is not completely API ready, we will use the CLI for this command
59          // Ensure shell 'si' client is connected.
60          doShellConnect( iRepo, workingDirectory );
61          result = doShellAnnotate( iRepo, workingDirectory, filename );
62  
63          return result;
64      }
65  
66      /**
67       * Execute 'si connect' command in current shell.
68       *
69       * @param iRepo            the Integrity repository instance.
70       * @param workingDirectory the SCM working directory.
71       * @throws ScmException if connect command failed.
72       */
73      private void doShellConnect( IntegrityScmProviderRepository iRepo, ScmFileSet workingDirectory )
74          throws ScmException
75      {
76          Commandline shell = new Commandline();
77          shell.setWorkingDirectory( workingDirectory.getBasedir() );
78          shell.setExecutable( "si" );
79          shell.createArg().setValue( "connect" );
80          shell.createArg().setValue( "--hostname=" + iRepo.getHost() );
81          shell.createArg().setValue( "--port=" + iRepo.getPort() );
82          shell.createArg().setValue( "--user=" + iRepo.getUser() );
83          shell.createArg().setValue( "--batch" );
84          shell.createArg().setValue( "--password=" + iRepo.getPassword() );
85          CommandLineUtils.StringStreamConsumer shellConsumer = new CommandLineUtils.StringStreamConsumer();
86  
87          try
88          {
89              getLogger().debug( "Executing: " + CommandLineUtils.toString( shell.getCommandline() ) );
90              int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer, shellConsumer );
91              if ( exitCode != 0 )
92              {
93                  throw new ScmException( "Can't login to integrity. Message : " + shellConsumer.toString() );
94              }
95          }
96          catch ( CommandLineException cle )
97          {
98              getLogger().error( "Command Line Connect Exception: " + cle.getMessage() );
99              throw new ScmException( "Can't login to integrity. Message : " + cle.getMessage() );
100         }
101 
102     }
103 
104     /**
105      * Execute 'si annotate' command in current shell and process output as {@link BlameScmResult} instance.
106      *
107      * @param iRepo            the Integrity repository instance.
108      * @param workingDirectory the SCM working directory.
109      * @param filename         the file name.
110      * @return the {@link BlameScmResult} instance.
111      */
112     private BlameScmResult doShellAnnotate( IntegrityScmProviderRepository iRepo, ScmFileSet workingDirectory,
113                                             String filename )
114     {
115         BlameScmResult result;
116         Commandline shell = new Commandline();
117         shell.setWorkingDirectory( workingDirectory.getBasedir() );
118         shell.setExecutable( "si" );
119         shell.createArg().setValue( "annotate" );
120         shell.createArg().setValue( "--hostname=" + iRepo.getHost() );
121         shell.createArg().setValue( "--port=" + iRepo.getPort() );
122         shell.createArg().setValue( "--user=" + iRepo.getUser() );
123         shell.createArg().setValue( "--fields=date,revision,author" );
124         shell.createArg().setValue( '"' + filename + '"' );
125         IntegrityBlameConsumer shellConsumer = new IntegrityBlameConsumer( getLogger() );
126         String commandLine = CommandLineUtils.toString( shell.getCommandline() );
127 
128         try
129         {
130             getLogger().debug( "Executing: " +  commandLine );
131             int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
132                                                                 new CommandLineUtils.StringStreamConsumer() );
133             boolean success = ( exitCode == 0 ? true : false );
134             ScmResult scmResult =
135                 new ScmResult( commandLine, "", "Exit Code: " + exitCode, success );
136             return new BlameScmResult( shellConsumer.getBlameList(), scmResult );
137         }
138         catch ( CommandLineException cle )
139         {
140             getLogger().error( "Command Line Exception: " + cle.getMessage() );
141             result = new BlameScmResult( commandLine, cle.getMessage(), "", false );
142         }
143 
144         return result;
145     }
146 
147 }