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