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