Coverage Report - org.apache.maven.shared.jarsigner.JarSignerCommandLineBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
JarSignerCommandLineBuilder
79 %
62/78
55 %
20/36
4,667
 
 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.Logger;
 23  
 import org.codehaus.plexus.logging.console.ConsoleLogger;
 24  
 import org.codehaus.plexus.util.StringUtils;
 25  
 import org.codehaus.plexus.util.cli.Commandline;
 26  
 
 27  
 import java.io.File;
 28  
 import java.io.IOException;
 29  
 
 30  
 /**
 31  
  * To build the command line for a given {@link JarSignerRequest}.
 32  
  *
 33  
  * @author tchemit <chemit@codelutin.com>
 34  
  * @version $Id: JarSignerCommandLineBuilder.java 1195511 2011-10-31 15:13:49Z olamy $
 35  
  * @since 1.0
 36  
  */
 37  3
 public class JarSignerCommandLineBuilder
 38  
 {
 39  1
     private static final Logger DEFAULT_LOGGER = new ConsoleLogger( 0, JarSignerCommandLineBuilder.class.getName() );
 40  
 
 41  3
     private Logger logger = DEFAULT_LOGGER;
 42  
 
 43  
     private String jarSignerFile;
 44  
 
 45  
     public Commandline build( JarSignerRequest request )
 46  
         throws CommandLineConfigurationException
 47  
     {
 48  
         try
 49  
         {
 50  3
             checkRequiredState();
 51  
         }
 52  0
         catch ( IOException e )
 53  
         {
 54  0
             throw new CommandLineConfigurationException( e.getMessage(), e );
 55  3
         }
 56  
 
 57  3
         Commandline cli = new Commandline();
 58  
 
 59  3
         cli.setExecutable( jarSignerFile );
 60  
 
 61  3
         cli.setWorkingDirectory( request.getWorkingDirectory() );
 62  
 
 63  3
         if ( request.isVerbose() )
 64  
         {
 65  3
             cli.createArg().setValue( "-verbose" );
 66  
         }
 67  
 
 68  3
         String maxMemory = request.getMaxMemory();
 69  3
         if ( StringUtils.isNotEmpty( maxMemory ) )
 70  
         {
 71  0
             cli.createArg().setValue( "-J-Xmx" + maxMemory );
 72  
         }
 73  
 
 74  3
         String[] arguments = request.getArguments();
 75  3
         if ( arguments != null )
 76  
         {
 77  0
             cli.addArguments( arguments );
 78  
         }
 79  
 
 80  3
         if ( request instanceof JarSignerSignRequest )
 81  
         {
 82  2
             build( (JarSignerSignRequest) request, cli );
 83  
         }
 84  
 
 85  3
         if ( request instanceof JarSignerVerifyRequest )
 86  
         {
 87  1
             build( (JarSignerVerifyRequest) request, cli );
 88  
         }
 89  
 
 90  3
         return cli;
 91  
     }
 92  
 
 93  
     public void setLogger( Logger logger )
 94  
     {
 95  3
         this.logger = logger;
 96  3
     }
 97  
 
 98  
     public void setJarSignerFile( String jarSignerFile )
 99  
     {
 100  3
         this.jarSignerFile = jarSignerFile;
 101  3
     }
 102  
 
 103  
     protected void checkRequiredState()
 104  
         throws IOException
 105  
     {
 106  3
         if ( logger == null )
 107  
         {
 108  0
             throw new IllegalStateException( "A logger instance is required." );
 109  
         }
 110  
 
 111  3
         if ( jarSignerFile == null )
 112  
         {
 113  0
             throw new IllegalStateException( "A jarSigner file is required." );
 114  
         }
 115  3
     }
 116  
 
 117  
     protected void build( JarSignerSignRequest request, Commandline cli )
 118  
     {
 119  2
         String keystore = request.getKeystore();
 120  2
         if ( !StringUtils.isEmpty( keystore ) )
 121  
         {
 122  2
             cli.createArg().setValue( "-keystore" );
 123  2
             cli.createArg().setValue( keystore );
 124  
         }
 125  
 
 126  2
         String storepass = request.getStorepass();
 127  2
         if ( !StringUtils.isEmpty( storepass ) )
 128  
         {
 129  2
             cli.createArg().setValue( "-storepass" );
 130  2
             cli.createArg().setValue( storepass );
 131  
         }
 132  
 
 133  2
         String keypass = request.getKeypass();
 134  2
         if ( !StringUtils.isEmpty( keypass ) )
 135  
         {
 136  2
             cli.createArg().setValue( "-keypass" );
 137  2
             cli.createArg().setValue( keypass );
 138  
         }
 139  
 
 140  2
         String storetype = request.getStoretype();
 141  2
         if ( !StringUtils.isEmpty( storetype ) )
 142  
         {
 143  0
             cli.createArg().setValue( "-storetype" );
 144  0
             cli.createArg().setValue( storetype );
 145  
         }
 146  
 
 147  2
         String providerName = request.getProviderName();
 148  2
         if ( !StringUtils.isEmpty( providerName ) )
 149  
         {
 150  0
             cli.createArg().setValue( "-providerName" );
 151  0
             cli.createArg().setValue( providerName );
 152  
         }
 153  
 
 154  2
         String providerClass = request.getProviderClass();
 155  2
         if ( !StringUtils.isEmpty( providerClass ) )
 156  
         {
 157  0
             cli.createArg().setValue( "-providerClass" );
 158  0
             cli.createArg().setValue( providerClass );
 159  
         }
 160  
 
 161  2
         String providerArg = request.getProviderArg();
 162  2
         if ( !StringUtils.isEmpty( providerArg ) )
 163  
         {
 164  0
             cli.createArg().setValue( "-providerArg" );
 165  0
             cli.createArg().setValue( providerArg );
 166  
         }
 167  
 
 168  2
         String sigfile = request.getSigfile();
 169  2
         if ( !StringUtils.isEmpty( sigfile ) )
 170  
         {
 171  0
             cli.createArg().setValue( "-sigfile" );
 172  0
             cli.createArg().setValue( sigfile );
 173  
         }
 174  
 
 175  2
         File signedjar = request.getSignedjar();
 176  2
         if ( signedjar != null )
 177  
         {
 178  2
             cli.createArg().setValue( "-signedjar" );
 179  2
             cli.createArg().setValue( signedjar.getAbsolutePath() );
 180  
         }
 181  2
         cli.createArg().setFile( request.getArchive() );
 182  
 
 183  2
         String alias = request.getAlias();
 184  2
         if ( !StringUtils.isEmpty( alias ) )
 185  
         {
 186  2
             cli.createArg().setValue( alias );
 187  
         }
 188  
 
 189  
 
 190  2
     }
 191  
 
 192  
     protected Commandline build( JarSignerVerifyRequest request, Commandline cli )
 193  
         throws CommandLineConfigurationException
 194  
     {
 195  1
         cli.createArg( true ).setValue( "-verify" );
 196  
 
 197  1
         if ( request.isCerts() )
 198  
         {
 199  1
             cli.createArg().setValue( "-certs" );
 200  
         }
 201  
 
 202  1
         cli.createArg().setFile( request.getArchive() );
 203  1
         return cli;
 204  
     }
 205  
 }