View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.fileinfo;
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.CommandParameter;
23  import org.apache.maven.scm.CommandParameters;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.command.fileinfo.AbstractFileInfoCommand;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.integrity.APISession;
30  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
31  import org.codehaus.plexus.util.cli.CommandLineException;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.io.File;
36  
37  /**
38   * MKS Integrity implementation for Maven's AbstractFileInfoCommand
39   * <br>This command will run a 'si memberinfo' command.  Even though this
40   * command is supported via the API, we're using the CLI to execute as
41   * its not clear what exactly this command is supposed to be returning
42   * to the Maven SCM framework.  Hence the CLI output is returned to the
43   * console verbatim
44   *
45   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
46   * @version $Id: IntegrityFileInfoCommand.java 1.3 2011/08/22 13:06:28EDT Cletus D'Souza (dsouza) Exp  $
47   * @since 1.6
48   */
49  public class IntegrityFileInfoCommand
50      extends AbstractFileInfoCommand
51  {
52      /**
53       * Even though this command is supported via the MKS JAVA API, since at this time we really don't
54       * know what the SCM plugin is looking to get in return for this command, we're simply going to
55       * run this command via the CLI and return the output verbatim
56       */
57      @Override
58      public ScmResult executeFileInfoCommand( ScmProviderRepository repository, File workingDirectory, String filename )
59          throws ScmException
60      {
61          getLogger().info( "Attempting to display scm file information for file: " + filename );
62          if ( null == filename || filename.length() == 0 )
63          {
64              throw new ScmException( "A single filename is required to execute the fileinfo command!" );
65          }
66          ScmResult result;
67          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
68          APISession api = iRepo.getAPISession();
69          Commandline shell = new Commandline();
70          shell.setWorkingDirectory( workingDirectory );
71          shell.setExecutable( "si" );
72          shell.createArg().setValue( "memberinfo" );
73          shell.createArg().setValue( "--hostname=" + api.getHostName() );
74          shell.createArg().setValue( "--port=" + api.getPort() );
75          shell.createArg().setValue( "--user=" + api.getUserName() );
76          shell.createArg().setValue( '"' + filename + '"' );
77          IntegrityFileInfoConsumer shellConsumer = new IntegrityFileInfoConsumer( getLogger() );
78  
79          try
80          {
81              getLogger().debug( "Executing: " + shell.getCommandline() );
82              int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
83                                                                  new CommandLineUtils.StringStreamConsumer() );
84              boolean success = ( exitCode == 128 ? false : true );
85              result = new ScmResult( shell.getCommandline().toString(), "", "Exit Code: " + exitCode, success );
86  
87          }
88          catch ( CommandLineException cle )
89          {
90              getLogger().error( "Command Line Exception: " + cle.getMessage() );
91              result = new ScmResult( shell.getCommandline().toString(), cle.getMessage(), "", false );
92          }
93  
94          return result;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
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 }