View Javadoc
1   package org.codehaus.mojo.keytool;
2   
3   /*
4    * Copyright 2005-2013 The Codehaus
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License" );
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.apache.maven.plugins.annotations.Mojo;
21  import org.apache.maven.plugins.annotations.Parameter;
22  import org.apache.maven.shared.utils.cli.Commandline;
23  import org.codehaus.mojo.keytool.requests.KeyToolImportKeystoreRequest;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  import java.io.File;
27  
28  /**
29   * To import all entries of a keystore to another keystore.
30   * <p/>
31   * Implemented as a wrapper around the SDK {@code keytool -importkeystore} command.
32   * <p/>
33   * <strong>Note</strong> This operation was not implemented by the keytool before jdk 1.6.
34   * <p/>
35   * See <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/keytool.html">keystore documentation</a>.
36   *
37   * @author tchemit <chemit@codelutin.com>
38   * @since 1.2
39   */
40  @Mojo(name = "importKeystore", requiresProject = true)
41  public class ImportKeystoreMojo
42      extends AbstractKeyToolRequestMojo<KeyToolImportKeystoreRequest>
43  {
44  
45      /**
46       * Source keystore name.
47       * <p/>
48       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
49       *
50       * @since 1.2
51       */
52      @Parameter
53      private String srckeystore;
54  
55      /**
56       * Destination keystore name.
57       * <p/>
58       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
59       *
60       * @since 1.2
61       */
62      @Parameter
63      private String destkeystore;
64  
65      /**
66       * Source keystore type.
67       * <p/>
68       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
69       *
70       * @since 1.2
71       */
72      @Parameter
73      private String srcstoretype;
74  
75      /**
76       * Destination keystore type.
77       * <p/>
78       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
79       *
80       * @since 1.2
81       */
82      @Parameter
83      private String deststoretype;
84  
85      /**
86       * Source keystore password.
87       * <p/>
88       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
89       *
90       * @since 1.2
91       */
92      @Parameter
93      private String srcstorepass;
94  
95      /**
96       * Destination keystore password.
97       * <p/>
98       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
99       *
100      * @since 1.2
101      */
102     @Parameter
103     private String deststorepass;
104 
105     /**
106      * Source keystore password protected.
107      * <p/>
108      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
109      *
110      * @since 1.2
111      */
112     @Parameter
113     private boolean srcprotected;
114 
115     /**
116      * Source keystore provider name.
117      * <p/>
118      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
119      *
120      * @since 1.2
121      */
122     @Parameter
123     private String srcprovidername;
124 
125     /**
126      * Destination keystore provider name.
127      * <p/>
128      * See <a hresf="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
129      *
130      * @since 1.2
131      */
132     @Parameter
133     private String destprovidername;
134 
135     /**
136      * Source alias.
137      * <p/>
138      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
139      *
140      * @since 1.2
141      */
142     @Parameter
143     private String srcalias;
144 
145     /**
146      * Destination alias.
147      * <p/>
148      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
149      *
150      * @since 1.2
151      */
152     @Parameter
153     private String destalias;
154 
155     /**
156      * Source key password.
157      * <p/>
158      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
159      *
160      * @since 1.2
161      */
162     @Parameter
163     private String srckeypass;
164 
165     /**
166      * Destination key password.
167      * <p/>
168      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
169      *
170      * @since 1.2
171      */
172     @Parameter
173     private String destkeypass;
174 
175     /**
176      * Do not prompt.
177      * <p/>
178      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
179      *
180      * @since 1.2
181      */
182     @Parameter
183     private boolean noprompt;
184 
185     /**
186      * Provider class name.
187      * <p/>
188      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
189      *
190      * @since 1.2
191      */
192     @Parameter
193     private String providerclass;
194 
195     /**
196      * Provider argument.
197      * <p/>
198      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
199      *
200      * @since 1.2
201      */
202     @Parameter
203     private String providerarg;
204 
205     /**
206      * Provider classpath.
207      * <p/>
208      * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
209      *
210      * @since 1.2
211      */
212     @Parameter
213     private String providerpath;
214 
215     /**
216      * If value is {@code true}, then will do nothing if keystore already exists.
217      *
218      * @since 1.3
219      */
220     @Parameter
221     private boolean skipIfExist;
222 
223     /**
224      * Default contructor.
225      */
226     public ImportKeystoreMojo()
227     {
228         super( KeyToolImportKeystoreRequest.class );
229     }
230 
231     @Override
232     public void execute()
233         throws MojoExecutionException
234     {
235 
236         if ( skipIfExist )
237         {
238 
239             // check if keystore already exist
240             File destinationKeystoreFile = new File( destkeystore );
241             boolean keystoreFileExists = destinationKeystoreFile.exists();
242 
243             if ( keystoreFileExists )
244             {
245                 getLog().info( "Skip execution, keystore already exists at " + destinationKeystoreFile );
246                 setSkip( true );
247             }
248 
249         }
250         super.execute();
251     }
252 
253 
254     /**
255      * {@inheritDoc}
256      */
257     @Override
258     protected KeyToolImportKeystoreRequest createKeytoolRequest()
259     {
260         KeyToolImportKeystoreRequest request = super.createKeytoolRequest();
261 
262         request.setSrckeystore( this.srckeystore );
263         request.setDestkeystore( this.destkeystore );
264         request.setSrcstoretype( this.srcstoretype );
265         request.setDeststoretype( this.deststoretype );
266         request.setSrcstorepass( this.srcstorepass );
267         request.setDeststorepass( this.deststorepass );
268         request.setSrcprotected( this.srcprotected );
269         request.setSrcprovidername( this.srcprovidername );
270         request.setDestprovidername( this.destprovidername );
271         request.setSrcalias( this.srcalias );
272         request.setDestalias( this.destalias );
273         request.setSrckeypass( this.srckeypass );
274         request.setDestkeypass( this.destkeypass );
275         request.setNoprompt( this.noprompt );
276         request.setProviderclass( this.providerclass );
277         request.setProviderarg( this.providerarg );
278         request.setProviderpath( this.providerpath );
279         return request;
280     }
281 
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     protected String getCommandlineInfo( Commandline commandLine )
287     {
288         String commandLineInfo = super.getCommandlineInfo( commandLine );
289 
290         commandLineInfo = StringUtils.replace( commandLineInfo, this.srckeypass, "'*****'" );
291         commandLineInfo = StringUtils.replace( commandLineInfo, this.destkeypass, "'*****'" );
292         commandLineInfo = StringUtils.replace( commandLineInfo, this.srcstorepass, "'*****'" );
293         commandLineInfo = StringUtils.replace( commandLineInfo, this.deststorepass, "'*****'" );
294 
295         return commandLineInfo;
296     }
297 }