001package org.apache.maven.scm.provider.integrity.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.ScmException; 023import org.apache.maven.scm.ScmFileSet; 024import org.apache.maven.scm.ScmResult; 025import org.apache.maven.scm.command.blame.AbstractBlameCommand; 026import org.apache.maven.scm.command.blame.BlameScmResult; 027import org.apache.maven.scm.provider.ScmProviderRepository; 028import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository; 029import org.codehaus.plexus.util.cli.CommandLineException; 030import org.codehaus.plexus.util.cli.CommandLineUtils; 031import org.codehaus.plexus.util.cli.Commandline; 032 033/** 034 * MKS Integrity implementation for Maven's AbstractBlameCommand 035 * <br>This class will execute a 'si annotate' command for the specified filename 036 * 037 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a> 038 * @since 1.6 039 */ 040public class IntegrityBlameCommand 041 extends AbstractBlameCommand 042{ 043 /** 044 * {@inheritDoc} 045 */ 046 @Override 047 public BlameScmResult executeBlameCommand( ScmProviderRepository repository, ScmFileSet workingDirectory, 048 String filename ) 049 throws ScmException 050 { 051 getLogger().info( "Attempting to display blame results for file: " + filename ); 052 if ( null == filename || filename.length() == 0 ) 053 { 054 throw new ScmException( "A single filename is required to execute the blame command!" ); 055 } 056 BlameScmResult result; 057 IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository; 058 // Since the si annotate command is not completely API ready, we will use the CLI for this command 059 // Ensure shell 'si' client is connected. 060 doShellConnect( iRepo, workingDirectory ); 061 result = doShellAnnotate( iRepo, workingDirectory, filename ); 062 063 return result; 064 } 065 066 /** 067 * Execute 'si connect' command in current shell. 068 * 069 * @param iRepo the Integrity repository instance. 070 * @param workingDirectory the SCM working directory. 071 * @throws ScmException if connect command failed. 072 */ 073 private void doShellConnect( IntegrityScmProviderRepository iRepo, ScmFileSet workingDirectory ) 074 throws ScmException 075 { 076 Commandline shell = new Commandline(); 077 shell.setWorkingDirectory( workingDirectory.getBasedir() ); 078 shell.setExecutable( "si" ); 079 shell.createArg().setValue( "connect" ); 080 shell.createArg().setValue( "--hostname=" + iRepo.getHost() ); 081 shell.createArg().setValue( "--port=" + iRepo.getPort() ); 082 shell.createArg().setValue( "--user=" + iRepo.getUser() ); 083 shell.createArg().setValue( "--batch" ); 084 shell.createArg().setValue( "--password=" + iRepo.getPassword() ); 085 CommandLineUtils.StringStreamConsumer shellConsumer = new CommandLineUtils.StringStreamConsumer(); 086 087 try 088 { 089 getLogger().debug( "Executing: " + CommandLineUtils.toString( shell.getCommandline() ) ); 090 int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer, shellConsumer ); 091 if ( exitCode != 0 ) 092 { 093 throw new ScmException( "Can't login to integrity. Message : " + shellConsumer.toString() ); 094 } 095 } 096 catch ( CommandLineException cle ) 097 { 098 getLogger().error( "Command Line Connect Exception: " + cle.getMessage() ); 099 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}