1 package org.apache.maven.shared.jarsigner;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
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 String storepass = request.getStorepass();
76 if ( !StringUtils.isEmpty( storepass ) )
77 {
78 cli.createArg().setValue( "-storepass" );
79 Arg arg = cli.createArg();
80 arg.setValue( storepass );
81 arg.setMask( true );
82 }
83
84 String storetype = request.getStoretype();
85 if ( !StringUtils.isEmpty( storetype ) )
86 {
87 cli.createArg().setValue( "-storetype" );
88 cli.createArg().setValue( storetype );
89 }
90
91 String providerName = request.getProviderName();
92 if ( !StringUtils.isEmpty( providerName ) )
93 {
94 cli.createArg().setValue( "-providerName" );
95 cli.createArg().setValue( providerName );
96 }
97
98 String providerClass = request.getProviderClass();
99 if ( !StringUtils.isEmpty( providerClass ) )
100 {
101 cli.createArg().setValue( "-providerClass" );
102 cli.createArg().setValue( providerClass );
103 }
104
105 String providerArg = request.getProviderArg();
106 if ( !StringUtils.isEmpty( providerArg ) )
107 {
108 cli.createArg().setValue( "-providerArg" );
109 cli.createArg().setValue( providerArg );
110 }
111
112 if ( request.isProtectedAuthenticationPath() )
113 {
114 cli.createArg().setValue( "-protected" );
115 }
116
117 String maxMemory = request.getMaxMemory();
118 if ( StringUtils.isNotEmpty( maxMemory ) )
119 {
120 cli.createArg().setValue( "-J-Xmx" + maxMemory );
121 }
122
123 String[] arguments = request.getArguments();
124 if ( arguments != null )
125 {
126 cli.addArguments( arguments );
127 }
128
129 if ( request instanceof JarSignerSignRequest )
130 {
131 build( (JarSignerSignRequest) request, cli );
132 }
133
134 if ( request instanceof JarSignerVerifyRequest )
135 {
136 build( (JarSignerVerifyRequest) request, cli );
137 }
138
139 cli.createArg().setFile( request.getArchive() );
140
141 String alias = request.getAlias();
142 if ( !StringUtils.isEmpty( alias ) )
143 {
144 cli.createArg().setValue( alias );
145 }
146
147 return cli;
148 }
149
150 public void setLogger( Logger logger )
151 {
152 this.logger = logger;
153 }
154
155 public void setJarSignerFile( String jarSignerFile )
156 {
157 this.jarSignerFile = jarSignerFile;
158 }
159
160 protected void checkRequiredState()
161 throws IOException
162 {
163 if ( logger == null )
164 {
165 throw new IllegalStateException( "A logger instance is required." );
166 }
167
168 if ( jarSignerFile == null )
169 {
170 throw new IllegalStateException( "A jarSigner file is required." );
171 }
172 }
173
174 protected void build( JarSignerSignRequest request, Commandline cli )
175 {
176
177 String keypass = request.getKeypass();
178 if ( !StringUtils.isEmpty( keypass ) )
179 {
180 cli.createArg().setValue( "-keypass" );
181 Arg arg = cli.createArg();
182 arg.setValue( keypass );
183 arg.setMask( true );
184 }
185
186 String sigfile = request.getSigfile();
187 if ( !StringUtils.isEmpty( sigfile ) )
188 {
189 cli.createArg().setValue( "-sigfile" );
190 cli.createArg().setValue( sigfile );
191 }
192
193 String tsaLocation = request.getTsaLocation();
194 if ( StringUtils.isNotBlank( tsaLocation ) )
195 {
196 cli.createArg().setValue( "-tsa" );
197 cli.createArg().setValue( tsaLocation );
198 }
199
200 String tsaAlias = request.getTsaAlias();
201 if ( StringUtils.isNotBlank( tsaAlias ) )
202 {
203 cli.createArg().setValue( "-tsacert" );
204 cli.createArg().setValue( tsaAlias );
205 }
206
207 File signedjar = request.getSignedjar();
208 if ( signedjar != null )
209 {
210 cli.createArg().setValue( "-signedjar" );
211 cli.createArg().setValue( signedjar.getAbsolutePath() );
212 }
213
214 final File certchain = request.getCertchain();
215 if ( certchain != null )
216 {
217 cli.createArg().setValue( "-certchain" );
218 cli.createArg().setValue( certchain.getAbsolutePath() );
219 }
220 }
221
222 protected void build( JarSignerVerifyRequest request, Commandline cli )
223 throws CommandLineConfigurationException
224 {
225 cli.createArg( true ).setValue( "-verify" );
226
227 if ( request.isCerts() )
228 {
229 cli.createArg().setValue( "-certs" );
230 }
231 }
232 }