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