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 * @version $Id: IntegrityBlameCommand.java 1.3 2011/08/22 13:06:15EDT Cletus D'Souza (dsouza) Exp  $
039 * @since 1.6
040 */
041public class IntegrityBlameCommand
042    extends AbstractBlameCommand
043{
044    /**
045     * {@inheritDoc}
046     */
047    @Override
048    public BlameScmResult executeBlameCommand( ScmProviderRepository repository, ScmFileSet workingDirectory,
049                                               String filename )
050        throws ScmException
051    {
052        getLogger().info( "Attempting to display blame results for file: " + filename );
053        if ( null == filename || filename.length() == 0 )
054        {
055            throw new ScmException( "A single filename is required to execute the blame command!" );
056        }
057        BlameScmResult result;
058        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
059        // Since the si annotate command is not completely API ready, we will use the CLI for this command
060        // Ensure shell 'si' client is connected.
061        doShellConnect( iRepo, workingDirectory );
062        result = doShellAnnotate( iRepo, workingDirectory, filename );
063
064        return result;
065    }
066
067    /**
068     * Execute 'si connect' command in current shell.
069     *
070     * @param iRepo            the Integrity repository instance.
071     * @param workingDirectory the SCM working directory.
072     * @throws ScmException if connect command failed.
073     */
074    private void doShellConnect( IntegrityScmProviderRepository iRepo, ScmFileSet workingDirectory )
075        throws ScmException
076    {
077        Commandline shell = new Commandline();
078        shell.setWorkingDirectory( workingDirectory.getBasedir() );
079        shell.setExecutable( "si" );
080        shell.createArg().setValue( "connect" );
081        shell.createArg().setValue( "--hostname=" + iRepo.getHost() );
082        shell.createArg().setValue( "--port=" + iRepo.getPort() );
083        shell.createArg().setValue( "--user=" + iRepo.getUser() );
084        shell.createArg().setValue( "--batch" );
085        shell.createArg().setValue( "--password=" + iRepo.getPassword() );
086        CommandLineUtils.StringStreamConsumer shellConsumer = new CommandLineUtils.StringStreamConsumer();
087
088        try
089        {
090            getLogger().debug( "Executing: " + CommandLineUtils.toString( shell.getCommandline() ) );
091            int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer, shellConsumer );
092            if ( exitCode != 0 )
093            {
094                throw new ScmException( "Can't login to integrity. Message : " + shellConsumer.toString() );
095            }
096        }
097        catch ( CommandLineException cle )
098        {
099            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}