Coverage Report - org.apache.maven.scm.provider.bazaar.BazaarUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
BazaarUtils
0 %
0/61
0 %
0/18
2,6
BazaarUtils$BazaarRevNoConsumer
0 %
0/7
N/A
2,6
 
 1  
 package org.apache.maven.scm.provider.bazaar;
 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.ScmFileStatus;
 25  
 import org.apache.maven.scm.ScmResult;
 26  
 import org.apache.maven.scm.log.DefaultLog;
 27  
 import org.apache.maven.scm.log.ScmLogger;
 28  
 import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
 29  
 import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
 30  
 import org.codehaus.plexus.util.cli.CommandLineException;
 31  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 32  
 import org.codehaus.plexus.util.cli.Commandline;
 33  
 
 34  
 import java.io.File;
 35  
 import java.util.ArrayList;
 36  
 import java.util.HashMap;
 37  
 import java.util.List;
 38  
 import java.util.Map;
 39  
 
 40  
 /**
 41  
  * Common code for executing bazaar commands.
 42  
  *
 43  
  * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
 44  
  * @version $Id: BazaarUtils.java 1306858 2012-03-29 13:41:29Z olamy $
 45  
  */
 46  
 public final class BazaarUtils
 47  
 {
 48  
 
 49  
     private BazaarUtils()
 50  0
     {
 51  0
     }
 52  
 
 53  
     /**
 54  
      * Map between command and its valid exit codes
 55  
      */
 56  0
     private static final Map<String,List<Integer>> EXITCODEMAP = new HashMap<String,List<Integer>>();
 57  
 
 58  
     /**
 59  
      * Default exit codes for entries not in exitCodeMap
 60  
      */
 61  0
     private static final List<Integer> DEFAULTEEXITCODES = new ArrayList<Integer>();
 62  
 
 63  
     /** Setup exit codes*/
 64  
     static
 65  
     {
 66  0
         DEFAULTEEXITCODES.add( Integer.valueOf( 0 ) );
 67  
 
 68  
         //Diff is different
 69  0
         List<Integer> diffExitCodes = new ArrayList<Integer>();
 70  0
         diffExitCodes.add( Integer.valueOf( 0 ) ); //No difference
 71  0
         diffExitCodes.add( Integer.valueOf( 1 ) ); //Conflicts in merge-like or changes in diff-like
 72  0
         diffExitCodes.add( Integer.valueOf( 2 ) ); //Unrepresentable diff changes
 73  0
         EXITCODEMAP.put( BazaarConstants.DIFF_CMD, diffExitCodes );
 74  0
     }
 75  
 
 76  
     public static ScmResult execute( BazaarConsumer consumer, ScmLogger logger, File workingDir, String[] cmdAndArgs )
 77  
         throws ScmException
 78  
     {
 79  
         try
 80  
         {
 81  
             //Build commandline
 82  0
             Commandline cmd = buildCmd( workingDir, cmdAndArgs );
 83  0
             if ( logger.isInfoEnabled() )
 84  
             {
 85  0
                 logger.info( "EXECUTING: " + cmd );
 86  
             }
 87  
 
 88  
             //Execute command
 89  0
             int exitCode = executeCmd( consumer, cmd );
 90  
 
 91  
             //Return result
 92  0
             List<Integer> exitCodes = DEFAULTEEXITCODES;
 93  0
             if ( EXITCODEMAP.containsKey( cmdAndArgs[0] ) )
 94  
             {
 95  0
                 exitCodes = EXITCODEMAP.get( cmdAndArgs[0] );
 96  
             }
 97  0
             boolean success = exitCodes.contains( Integer.valueOf( exitCode ) );
 98  
 
 99  
             //On failure (and not due to exceptions) - run diagnostics
 100  0
             String providerMsg = "Execution of bazaar command succeded";
 101  0
             if ( !success )
 102  
             {
 103  0
                 BazaarConfig config = new BazaarConfig( workingDir );
 104  0
                 providerMsg = "\nEXECUTION FAILED" + "\n  Execution of cmd : " + cmdAndArgs[0]
 105  
                     + " failed with exit code: " + exitCode + "." + "\n  Working directory was: " + "\n    "
 106  
                     + workingDir.getAbsolutePath() + config.toString( workingDir ) + "\n";
 107  0
                 if ( logger.isErrorEnabled() )
 108  
                 {
 109  0
                     logger.error( providerMsg );
 110  
                 }
 111  
             }
 112  
 
 113  0
             return new ScmResult( cmd.toString(), providerMsg, consumer.getStdErr(), success );
 114  
         }
 115  0
         catch ( ScmException se )
 116  
         {
 117  0
             String msg =
 118  
                 "EXECUTION FAILED\n  Execution failed before invoking the Bazaar command. Last exception:"
 119  
                     + "\n    " + se.getMessage();
 120  
 
 121  
             //Add nested cause if any
 122  0
             if ( se.getCause() != null )
 123  
             {
 124  0
                 msg += "\n  Nested exception:" + "\n    " + se.getCause().getMessage();
 125  
             }
 126  
 
 127  
             //log and return
 128  0
             if ( logger.isErrorEnabled() )
 129  
             {
 130  0
                 logger.error( msg );
 131  
             }
 132  0
             throw se;
 133  
         }
 134  
     }
 135  
 
 136  
     static Commandline buildCmd( File workingDir, String[] cmdAndArgs )
 137  
         throws ScmException
 138  
     {
 139  0
         Commandline cmd = new Commandline();
 140  0
         cmd.setExecutable( BazaarConstants.EXEC );
 141  0
         cmd.setWorkingDirectory( workingDir.getAbsolutePath() );
 142  0
         cmd.addArguments( cmdAndArgs );
 143  
 
 144  0
         if ( !workingDir.exists() )
 145  
         {
 146  0
             boolean success = workingDir.mkdirs();
 147  0
             if ( !success )
 148  
             {
 149  0
                 String msg = "Working directory did not exist" + " and it couldn't be created: " + workingDir;
 150  0
                 throw new ScmException( msg );
 151  
             }
 152  
         }
 153  0
         return cmd;
 154  
     }
 155  
 
 156  
     static int executeCmd( BazaarConsumer consumer, Commandline cmd )
 157  
         throws ScmException
 158  
     {
 159  
         final int exitCode;
 160  
         try
 161  
         {
 162  0
             exitCode = CommandLineUtils.executeCommandLine( cmd, consumer, consumer );
 163  
         }
 164  0
         catch ( CommandLineException ex )
 165  
         {
 166  0
             throw new ScmException( "Command could not be executed: " + cmd, ex );
 167  0
         }
 168  0
         return exitCode;
 169  
     }
 170  
 
 171  
     public static ScmResult execute( File workingDir, String[] cmdAndArgs )
 172  
         throws ScmException
 173  
     {
 174  0
         ScmLogger logger = new DefaultLog();
 175  0
         return execute( new BazaarConsumer( logger ), logger, workingDir, cmdAndArgs );
 176  
     }
 177  
 
 178  
     public static String[] expandCommandLine( String[] cmdAndArgs, ScmFileSet additionalFiles )
 179  
     {
 180  0
         List<File> files = additionalFiles.getFileList();
 181  0
         String[] cmd = new String[files.size() + cmdAndArgs.length];
 182  
 
 183  
         // Copy command into array
 184  0
         System.arraycopy( cmdAndArgs, 0, cmd, 0, cmdAndArgs.length );
 185  
 
 186  
         // Add files as additional parameter into the array
 187  0
         for ( int i = 0; i < files.size(); i++ )
 188  
         {
 189  0
             String file = files.get( i ).getPath().replace( '\\', File.separatorChar );
 190  0
             cmd[i + cmdAndArgs.length] = file;
 191  
         }
 192  
 
 193  0
         return cmd;
 194  
     }
 195  
 
 196  
     public static int getCurrentRevisionNumber( ScmLogger logger, File workingDir )
 197  
         throws ScmException
 198  
     {
 199  
 
 200  0
         String[] revCmd = new String[]{BazaarConstants.REVNO_CMD};
 201  0
         BazaarRevNoConsumer consumer = new BazaarRevNoConsumer( logger );
 202  0
         BazaarUtils.execute( consumer, logger, workingDir, revCmd );
 203  
 
 204  0
         return consumer.getCurrentRevisionNumber();
 205  
     }
 206  
 
 207  
     /**
 208  
      * Get current (working) revision.
 209  
      * <p/>
 210  
      * Resolve revision to the last integer found in the command output.
 211  
      */
 212  
     private static class BazaarRevNoConsumer
 213  
         extends BazaarConsumer
 214  
     {
 215  
 
 216  
         private int revNo;
 217  
 
 218  
         BazaarRevNoConsumer( ScmLogger logger )
 219  
         {
 220  0
             super( logger );
 221  0
         }
 222  
 
 223  
         public void doConsume( ScmFileStatus status, String line )
 224  
         {
 225  
             try
 226  
             {
 227  0
                 revNo = Integer.valueOf( line ).intValue();
 228  
             }
 229  0
             catch ( NumberFormatException e )
 230  
             {
 231  
                 // ignore
 232  0
             }
 233  0
         }
 234  
 
 235  
         int getCurrentRevisionNumber()
 236  
         {
 237  0
             return revNo;
 238  
         }
 239  
     }
 240  
 }