Coverage Report - org.apache.maven.shared.jarsigner.DefaultJarSigner
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultJarSigner
72 %
37/51
50 %
13/26
3,625
DefaultJarSigner$1
100 %
2/2
N/A
3,625
DefaultJarSigner$2
80 %
4/5
50 %
1/2
3,625
DefaultJarSigner$3
33 %
1/3
N/A
3,625
 
 1  
 package org.apache.maven.shared.jarsigner;
 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.codehaus.plexus.logging.AbstractLogEnabled;
 23  
 import org.codehaus.plexus.util.Os;
 24  
 import org.codehaus.plexus.util.StringUtils;
 25  
 import org.codehaus.plexus.util.cli.CommandLineException;
 26  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 27  
 import org.codehaus.plexus.util.cli.Commandline;
 28  
 import org.codehaus.plexus.util.cli.StreamConsumer;
 29  
 
 30  
 import java.io.File;
 31  
 import java.io.IOException;
 32  
 import java.io.InputStream;
 33  
 import java.util.Map;
 34  
 
 35  
 /**
 36  
  * Default implementation of component {@link JarSigner}.
 37  
  *
 38  
  * @author tchemit <chemit@codelutin.com>
 39  
  * @version $Id: DefaultJarSigner.java 1195937 2011-11-01 11:38:37Z olamy $
 40  
  * @plexus.component role="org.apache.maven.shared.jarsigner.JarSigner" role-hint="default"
 41  
  * @since 1.0
 42  
  */
 43  18
 public class DefaultJarSigner
 44  
     extends AbstractLogEnabled
 45  
     implements JarSigner
 46  
 {
 47  
 
 48  
     /**
 49  
      * The location of the jarSigner executable file.
 50  
      */
 51  
     protected String jarSignerFile;
 52  
 
 53  
     public JarSignerResult execute( JarSignerRequest request )
 54  
         throws JarSignerException
 55  
     {
 56  
 
 57  3
         if ( jarSignerFile == null )
 58  
         {
 59  
 
 60  
             // find the jar singer to use
 61  
             try
 62  
             {
 63  2
                 jarSignerFile = findJarSignerExecutable();
 64  
             }
 65  0
             catch ( IOException e )
 66  
             {
 67  0
                 throw new JarSignerException( "Error finding jar signer executable. Reason: " + e.getMessage(), e );
 68  2
             }
 69  
         }
 70  
 
 71  
         // creates the command line
 72  3
         Commandline cli = createCommandLine( request );
 73  
 
 74  
         // execute it
 75  3
         return executeCommandLine( cli, request );
 76  
     }
 77  
 
 78  
     protected Commandline createCommandLine( JarSignerRequest request )
 79  
         throws JarSignerException
 80  
     {
 81  3
         JarSignerCommandLineBuilder cliBuilder = new JarSignerCommandLineBuilder();
 82  3
         cliBuilder.setLogger( getLogger() );
 83  3
         cliBuilder.setJarSignerFile( jarSignerFile );
 84  
         try
 85  
         {
 86  3
             return cliBuilder.build( request );
 87  
         }
 88  0
         catch ( CommandLineConfigurationException e )
 89  
         {
 90  0
             throw new JarSignerException( "Error configuring command-line. Reason: " + e.getMessage(), e );
 91  
         }
 92  
     }
 93  
 
 94  
     protected JarSignerResult executeCommandLine( Commandline cli, JarSignerRequest request )
 95  
     {
 96  3
         if ( getLogger().isDebugEnabled() )
 97  
         {
 98  0
             getLogger().debug( "Executing: " + cli );
 99  
         }
 100  
 
 101  3
         final boolean verbose = request.isVerbose();
 102  
 
 103  3
         InputStream systemIn = new InputStream()
 104  3
         {
 105  
 
 106  
             public int read()
 107  
             {
 108  3
                 return -1;
 109  
             }
 110  
 
 111  
         };
 112  3
         StreamConsumer systemOut = request.getSystemOutStreamConsumer();
 113  
 
 114  3
         if ( systemOut == null )
 115  
         {
 116  3
             systemOut = new StreamConsumer()
 117  3
             {
 118  
 
 119  
                 public void consumeLine( final String line )
 120  
                 {
 121  16
                     if ( verbose )
 122  
                     {
 123  16
                         getLogger().info( line );
 124  
                     }
 125  
                     else
 126  
                     {
 127  0
                         getLogger().debug( line );
 128  
                     }
 129  16
                 }
 130  
 
 131  
             };
 132  
         }
 133  
 
 134  3
         StreamConsumer systemErr = request.getSystemErrorStreamConsumer();
 135  
 
 136  3
         if ( systemErr == null )
 137  
         {
 138  3
             systemErr = new StreamConsumer()
 139  3
             {
 140  
 
 141  
                 public void consumeLine( final String line )
 142  
                 {
 143  0
                     getLogger().warn( line );
 144  0
                 }
 145  
 
 146  
             };
 147  
         }
 148  
 
 149  3
         DefaultJarSignerResult result = new DefaultJarSignerResult();
 150  3
         result.setCommandline( cli );
 151  
 
 152  3
         if ( verbose )
 153  
         {
 154  3
             getLogger().info( cli.toString() );
 155  
         }
 156  
         else
 157  
         {
 158  0
             getLogger().debug( cli.toString() );
 159  
         }
 160  
 
 161  
         try
 162  
         {
 163  3
             int resultCode = CommandLineUtils.executeCommandLine( cli, systemIn, systemOut, systemErr );
 164  
 
 165  3
             result.setExitCode( resultCode );
 166  
         }
 167  0
         catch ( CommandLineException e )
 168  
         {
 169  0
             result.setExecutionException( e );
 170  3
         }
 171  
 
 172  3
         return result;
 173  
     }
 174  
 
 175  
     protected String findJarSignerExecutable()
 176  
         throws IOException
 177  
     {
 178  2
         String command = "jarsigner" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" );
 179  
 
 180  2
         String executable =
 181  
             findExecutable( command, System.getProperty( "java.home" ), new String[]{ "../bin", "bin", "../sh" } );
 182  
 
 183  2
         if ( executable == null )
 184  
         {
 185  
 
 186  0
             Map<String, String> env = System.getenv();
 187  
 
 188  0
             String[] variables = { "JDK_HOME", "JAVA_HOME" };
 189  
 
 190  0
             for ( int i = 0; i < variables.length && executable == null; i++ )
 191  
             {
 192  0
                 executable = findExecutable( command, env.get( variables[i] ), new String[]{ "bin", "sh" } );
 193  
             }
 194  
 
 195  
         }
 196  
 
 197  2
         if ( executable == null )
 198  
         {
 199  0
             executable = command;
 200  
         }
 201  
 
 202  2
         return executable;
 203  
     }
 204  
 
 205  
     /**
 206  
      * Finds the specified command in any of the given sub directories of the specified JDK/JRE home directory.
 207  
      *
 208  
      * @param command The command to find, must not be <code>null</code>.
 209  
      * @param homeDir The home directory to search in, may be <code>null</code>.
 210  
      * @param subDirs The sub directories of the home directory to search in, must not be <code>null</code>.
 211  
      * @return The (absolute) path to the command if found, <code>null</code> otherwise.
 212  
      */
 213  
     protected String findExecutable( String command, String homeDir, String[] subDirs )
 214  
     {
 215  2
         if ( StringUtils.isNotEmpty( homeDir ) )
 216  
         {
 217  4
             for ( int i = 0; i < subDirs.length; i++ )
 218  
             {
 219  4
                 File file = new File( new File( homeDir, subDirs[i] ), command );
 220  
 
 221  4
                 if ( file.isFile() )
 222  
                 {
 223  2
                     return file.getAbsolutePath();
 224  
                 }
 225  
             }
 226  
         }
 227  
 
 228  0
         return null;
 229  
     }
 230  
 }