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.execution.MavenSession;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.plugins.annotations.Component;
22  import org.apache.maven.plugins.annotations.Parameter;
23  import org.apache.maven.shared.utils.cli.Commandline;
24  import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
25  import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
26  import org.apache.maven.toolchain.Toolchain;
27  import org.apache.maven.toolchain.ToolchainManager;
28  
29  import java.io.File;
30  
31  /**
32   * Abstract keytool mojo implementing the {@link KeyToolRequest}.
33   *
34   * @param <R> generic type of request used by the mojo
35   * @author tchemit <chemit@codelutin.com>
36   * @since 1.2
37   */
38  public abstract class AbstractKeyToolRequestMojo<R extends KeyToolRequest>
39      extends AbstractKeyToolMojo
40  {
41  
42      /**
43       * List of additional arguments to append to the keytool command line.
44       * <p/>
45       * <strong>Note: This parameter is left for compatibility reason but
46       * should be used at a last resort whenparameters are not found in
47       * dedicated mojo due to possible side-effects on parameters
48       * (see https://jira.codehaus.org/browse/MKEYTOOL-17)</strong>
49       *
50       * @since 1.1
51       */
52      @Parameter
53      private String[] arguments;
54  
55      /**
56       * Where to execute the keytool command.
57       */
58      @Parameter( defaultValue = "${basedir}", required = true, alias = "workingdir" )
59      private File workingDirectory;
60  
61      /**
62       * Keytool component.
63       *
64       * @since 1.2
65       */
66      @Component( role = KeyTool.class )
67      private KeyTool keyTool;
68  
69      /**
70       * To obtain a toolchain if possible.
71       *
72       * @since 1.4
73       */
74      @Component
75      private ToolchainManager toolchainManager;
76  
77      /**
78       * The current build session instance. This is used for
79       * toolchain manager API calls.
80       *
81       * @since 1.4
82       */
83      @Component
84      private MavenSession session;
85  
86      /**
87       * Type of keytool request used by the mojo.
88       */
89      private final Class<R> requestType;
90  
91      /**
92       * Constructor of abstract mojo.
93       *
94       * @param requestType type of keytool request used by the mojo
95       */
96      protected AbstractKeyToolRequestMojo( Class<R> requestType )
97      {
98          this.requestType = requestType;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     public void execute()
105         throws MojoExecutionException
106     {
107 
108         if ( isSkip() )
109         {
110             getLog().info( getMessage( "disabled" ) );
111         }
112         else
113         {
114 
115             // add toolchain support if found
116             Toolchain toolchain = getToolchain();
117 
118             if ( toolchain != null )
119             {
120                 getLog().info( "Toolchain in keytool-maven-plugin: " + toolchain );
121                 keyTool.setToolchain(toolchain);
122             }
123 
124             //creates the keytool request
125             R request = createKeytoolRequest();
126 
127             try
128             {
129                 JavaToolResult result = keyTool.execute( request );
130 
131                 consumeResult( result );
132             }
133             catch ( JavaToolException e )
134             {
135                 throw new MojoExecutionException( getMessage( "commandLineException", e.getMessage() ), e );
136             }
137         }
138     }
139 
140     /**
141      * To prepare the incoming request, says fill it with mojo parameters.
142      *
143      * @return the created keytool request
144      * @see KeyToolRequest
145      */
146     protected R createKeytoolRequest()
147     {
148         R request;
149         try
150         {
151             request = requestType.getConstructor().newInstance();
152         }
153         catch ( Exception e )
154         {
155             // just throw a runtime exception : this should never happen!
156             throw new RuntimeException( "Failed to create keytool request ", e );
157         }
158 
159         // add default parameters
160         request.setVerbose( isVerbose() );
161         request.setWorkingDirectory( this.workingDirectory );
162         request.setArguments( this.arguments );
163         return request;
164     }
165 
166     /**
167      * Gets a string representation of a {@code Commandline}.
168      * <p>This method creates the string representation by calling {@code commandLine.toString()} by default.</p>
169      *
170      * @param commandLine The {@code Commandline} to get a string representation of (can not be null).
171      * @return The string representation of {@code commandLine}.
172      */
173     protected String getCommandlineInfo( final Commandline commandLine )
174     {
175         if ( commandLine == null )
176         {
177             throw new NullPointerException( "commandLine" );
178         }
179         return commandLine.toString();
180     }
181 
182     /**
183      * To consume the keytool comand execution.
184      *
185      * @param result result of the command line action
186      * @throws MojoExecutionException if the result is not 0 (means something bad occurs)
187      */
188     protected final void consumeResult( JavaToolResult result )
189         throws MojoExecutionException
190     {
191         Commandline commandLine = result.getCommandline();
192 
193         int resultCode = result.getExitCode();
194 
195         if ( resultCode != 0 )
196         {
197             throw new MojoExecutionException( getMessage( "failure", getCommandlineInfo( commandLine ), resultCode ) );
198         }
199     }
200 
201     /**
202      * FIXME tchemit-20123-11-13, need to find out how to do this...
203      * TODO remove the part with ToolchainManager lookup once we depend on
204      * 2.0.9 (have it as prerequisite). Define as regular component field then.
205      *
206      * @return Toolchain instance
207      */
208     private Toolchain getToolchain()
209     {
210         Toolchain tc = null;
211         if ( toolchainManager != null )
212         {
213             tc = toolchainManager.getToolchainFromBuildContext( "jdk", session );
214         }
215 
216         return tc;
217     }
218 }