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.plugins.annotations.Parameter;
20 import org.apache.maven.shared.utils.cli.Commandline;
21 import org.codehaus.plexus.util.StringUtils;
22
23 import java.io.File;
24 import java.net.MalformedURLException;
25
26 /**
27 * Abstract mojo to execute a {@link KeyToolRequestWithKeyStoreParameters} request.
28 *
29 * @param <R> generic type of request used by the mojo
30 * @author tchemit <chemit@codelutin.com>
31 * @since 1.2
32 */
33 public abstract class AbstractKeyToolRequestWithKeyStoreParametersMojo<R extends KeyToolRequestWithKeyStoreParameters>
34 extends AbstractKeyToolRequestMojo<R>
35 {
36
37 /**
38 * Keystore location.
39 * <p/>
40 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
41 */
42 @Parameter
43 private String keystore;
44
45 /**
46 * Keystore type.
47 * <p/>
48 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
49 */
50 @Parameter
51 private String storetype;
52
53 /**
54 * Keystore password.
55 * <p/>
56 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
57 */
58 @Parameter( alias = "storepass" )
59 private String storepass;
60
61 /**
62 * Provider name.
63 * <p/>
64 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
65 *
66 * @since 1.2
67 */
68 @Parameter
69 private String providername;
70
71 /**
72 * Provider class name.
73 * <p/>
74 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
75 *
76 * @since 1.2
77 */
78 @Parameter
79 private String providerclass;
80
81 /**
82 * Provider argument.
83 * <p/>
84 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
85 *
86 * @since 1.2
87 */
88 @Parameter
89 private String providerarg;
90
91 /**
92 * Provider classpath.
93 * <p/>
94 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
95 *
96 * @since 1.2
97 */
98 @Parameter
99 private String providerpath;
100
101 /**
102 * Constructor of abstract mojo.
103 *
104 * @param requestType type of keytool request used by the mojo
105 */
106 public AbstractKeyToolRequestWithKeyStoreParametersMojo( Class<R> requestType )
107 {
108 super( requestType );
109 }
110
111 /**
112 * {@inheritDoc}
113 */
114 @Override
115 protected R createKeytoolRequest()
116 {
117 R request = super.createKeytoolRequest();
118
119 if ( StringUtils.isNotEmpty( keystore ) )
120 {
121
122 File file = getFile( keystore );
123
124 // make sure the parent directory of the keystore exists
125
126 boolean mkdirs = file.getParentFile().mkdirs();
127 getLog().debug( "mdkirs: " + mkdirs + " " + file.getParentFile() );
128
129 // force to not use this parameter
130 request.setKeystore( file.getAbsolutePath() );
131 }
132
133 request.setProviderarg( providerarg );
134 request.setProviderclass( providerclass );
135 request.setProvidername( providername );
136 request.setProviderpath( providerpath );
137 request.setStorepass( storepass );
138 request.setStoretype( storetype );
139 return request;
140 }
141
142 /**
143 * {@inheritDoc}
144 */
145 @Override
146 protected String getCommandlineInfo( final Commandline commandLine )
147 {
148 String commandLineInfo = super.getCommandlineInfo( commandLine );
149
150 commandLineInfo = StringUtils.replace( commandLineInfo, this.storepass, "'*****'" );
151
152 return commandLineInfo;
153 }
154
155 /**
156 * Create the parent directory of the given file location.
157 *
158 * @param file file location to check
159 */
160 protected final void createParentDirIfNecessary( final String file )
161 {
162 if ( file != null )
163 {
164 final File fileDir = new File( file ).getParentFile();
165
166 if ( fileDir != null )
167 {
168 // not a relative path
169 boolean mkdirs = fileDir.mkdirs();
170 getLog().debug( "mdkirs: " + mkdirs + " " + fileDir );
171 }
172 }
173 }
174
175 protected File getFile( String path )
176
177 {
178 try
179 {
180 return new File( new File( path ).toURL().getFile() );
181 }
182 catch ( MalformedURLException e )
183 {
184 throw new IllegalStateException( "Could not obtain directory " + path );
185 }
186 }
187
188 protected File getKeystoreFile()
189 {
190 return getFile(keystore);
191 }
192 }