001package org.apache.maven.scm.provider.integrity.command.fileinfo;
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.CommandParameter;
023import org.apache.maven.scm.CommandParameters;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmResult;
027import org.apache.maven.scm.command.fileinfo.AbstractFileInfoCommand;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.integrity.APISession;
030import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
031import org.codehaus.plexus.util.cli.CommandLineException;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.File;
036
037/**
038 * MKS Integrity implementation for Maven's AbstractFileInfoCommand
039 * <br>This command will run a 'si memberinfo' command.  Even though this
040 * command is supported via the API, we're using the CLI to execute as
041 * its not clear what exactly this command is supposed to be returning
042 * to the Maven SCM framework.  Hence the CLI output is returned to the
043 * console verbatim
044 *
045 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
046 * @version $Id: IntegrityFileInfoCommand.java 1.3 2011/08/22 13:06:28EDT Cletus D'Souza (dsouza) Exp  $
047 * @since 1.6
048 */
049public class IntegrityFileInfoCommand
050    extends AbstractFileInfoCommand
051{
052    /**
053     * Even though this command is supported via the MKS JAVA API, since at this time we really don't
054     * know what the SCM plugin is looking to get in return for this command, we're simply going to
055     * run this command via the CLI and return the output verbatim
056     */
057    @Override
058    public ScmResult executeFileInfoCommand( ScmProviderRepository repository, File workingDirectory, String filename )
059        throws ScmException
060    {
061        getLogger().info( "Attempting to display scm file information for file: " + filename );
062        if ( null == filename || filename.length() == 0 )
063        {
064            throw new ScmException( "A single filename is required to execute the fileinfo command!" );
065        }
066        ScmResult result;
067        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
068        APISession api = iRepo.getAPISession();
069        Commandline shell = new Commandline();
070        shell.setWorkingDirectory( workingDirectory );
071        shell.setExecutable( "si" );
072        shell.createArg().setValue( "memberinfo" );
073        shell.createArg().setValue( "--hostname=" + api.getHostName() );
074        shell.createArg().setValue( "--port=" + api.getPort() );
075        shell.createArg().setValue( "--user=" + api.getUserName() );
076        shell.createArg().setValue( '"' + filename + '"' );
077        IntegrityFileInfoConsumer shellConsumer = new IntegrityFileInfoConsumer( getLogger() );
078
079        try
080        {
081            getLogger().debug( "Executing: " + shell.getCommandline() );
082            int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
083                                                                new CommandLineUtils.StringStreamConsumer() );
084            boolean success = ( exitCode == 128 ? false : true );
085            result = new ScmResult( shell.getCommandline().toString(), "", "Exit Code: " + exitCode, success );
086
087        }
088        catch ( CommandLineException cle )
089        {
090            getLogger().error( "Command Line Exception: " + cle.getMessage() );
091            result = new ScmResult( shell.getCommandline().toString(), cle.getMessage(), "", false );
092        }
093
094        return result;
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
102                                        CommandParameters parameters )
103        throws ScmException
104    {
105        return executeFileInfoCommand( repository, fileSet.getBasedir(),
106                                       parameters.getString( CommandParameter.FILE ) );
107    }
108
109}