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   * @version $Id: IntegrityBlameCommand.java 1.3 2011/08/22 13:06:15EDT Cletus D'Souza (dsouza) Exp  $
39   * @since 1.6
40   */
41  public class IntegrityBlameCommand
42      extends AbstractBlameCommand
43  {
44      /**
45       * {@inheritDoc}
46       */
47      @Override
48      public BlameScmResult executeBlameCommand( ScmProviderRepository repository, ScmFileSet workingDirectory,
49                                                 String filename )
50          throws ScmException
51      {
52          getLogger().info( "Attempting to display blame results for file: " + filename );
53          if ( null == filename || filename.length() == 0 )
54          {
55              throw new ScmException( "A single filename is required to execute the blame command!" );
56          }
57          BlameScmResult result;
58          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
59          // Since the si annotate command is not completely API ready, we will use the CLI for this command
60          // Ensure shell 'si' client is connected.
61          doShellConnect( iRepo, workingDirectory );
62          result = doShellAnnotate( iRepo, workingDirectory, filename );
63  
64          return result;
65      }
66  
67      /**
68       * Execute 'si connect' command in current shell.
69       *
70       * @param iRepo            the Integrity repository instance.
71       * @param workingDirectory the SCM working directory.
72       * @throws ScmException if connect command failed.
73       */
74      private void doShellConnect( IntegrityScmProviderRepository iRepo, ScmFileSet workingDirectory )
75          throws ScmException
76      {
77          Commandline shell = new Commandline();
78          shell.setWorkingDirectory( workingDirectory.getBasedir() );
79          shell.setExecutable( "si" );
80          shell.createArg().setValue( "connect" );
81          shell.createArg().setValue( "--hostname=" + iRepo.getHost() );
82          shell.createArg().setValue( "--port=" + iRepo.getPort() );
83          shell.createArg().setValue( "--user=" + iRepo.getUser() );
84          shell.createArg().setValue( "--batch" );
85          shell.createArg().setValue( "--password=" + iRepo.getPassword() );
86          CommandLineUtils.StringStreamConsumer shellConsumer = new CommandLineUtils.StringStreamConsumer();
87  
88          try
89          {
90              getLogger().debug( "Executing: " + CommandLineUtils.toString( shell.getCommandline() ) );
91              int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer, shellConsumer );
92              if ( exitCode != 0 )
93              {
94                  throw new ScmException( "Can't login to integrity. Message : " + shellConsumer.toString() );
95              }
96          }
97          catch ( CommandLineException cle )
98          {
99              getLogger().error( "Command Line Connect Exception: " + cle.getMessage() );
100             throw new ScmException( "Can't login to integrity. Message : " + cle.getMessage() );
101         }
102 
103     }
104 
105     /**
106      * Execute 'si annotate' command in current shell and process output as {@link BlameScmResult} instance.
107      *
108      * @param iRepo            the Integrity repository instance.
109      * @param workingDirectory the SCM working directory.
110      * @param filename         the file name.
111      * @return the {@link BlameScmResult} instance.
112      */
113     private BlameScmResult doShellAnnotate( IntegrityScmProviderRepository iRepo, ScmFileSet workingDirectory,
114                                             String filename )
115     {
116         BlameScmResult result;
117         Commandline shell = new Commandline();
118         shell.setWorkingDirectory( workingDirectory.getBasedir() );
119         shell.setExecutable( "si" );
120         shell.createArg().setValue( "annotate" );
121         shell.createArg().setValue( "--hostname=" + iRepo.getHost() );
122         shell.createArg().setValue( "--port=" + iRepo.getPort() );
123         shell.createArg().setValue( "--user=" + iRepo.getUser() );
124         shell.createArg().setValue( "--fields=date,revision,author" );
125         shell.createArg().setValue( '"' + filename + '"' );
126         IntegrityBlameConsumer shellConsumer = new IntegrityBlameConsumer( getLogger() );
127 
128         try
129         {
130             getLogger().debug( "Executing: " + CommandLineUtils.toString( shell.getCommandline() ) );
131             int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
132                                                                 new CommandLineUtils.StringStreamConsumer() );
133             boolean success = ( exitCode == 0 ? true : false );
134             ScmResult scmResult =
135                 new ScmResult( shell.getCommandline().toString(), "", "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( shell.getCommandline().toString(), cle.getMessage(), "", false );
142         }
143 
144         return result;
145     }
146 
147 }