View Javadoc

1   /**
2    * Copyright (c) 2012-2013, JCabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.ssl.maven.plugin;
31  
32  import com.jcabi.log.Logger;
33  import java.io.File;
34  import org.apache.commons.codec.digest.DigestUtils;
35  import org.apache.maven.plugin.AbstractMojo;
36  import org.apache.maven.plugin.MojoFailureException;
37  import org.apache.maven.project.MavenProject;
38  import org.jfrog.maven.annomojo.annotations.MojoGoal;
39  import org.jfrog.maven.annomojo.annotations.MojoParameter;
40  import org.jfrog.maven.annomojo.annotations.MojoPhase;
41  import org.slf4j.impl.StaticLoggerBinder;
42  
43  /**
44   * Generate SSL keystore and configure in JVM.
45   *
46   * @author Yegor Bugayenko (yegor@tpc2.com)
47   * @version $Id$
48   * @since 0.5
49   */
50  @MojoGoal("keygen")
51  @MojoPhase("initialize")
52  public final class KeygenMojo extends AbstractMojo {
53  
54      /**
55       * Maven project.
56       */
57      @MojoParameter(
58          expression = "${project}",
59          required = true,
60          readonly = true,
61          description = "Maven project"
62      )
63      private transient MavenProject project;
64  
65      /**
66       * Shall we skip execution?
67       */
68      @MojoParameter(
69          defaultValue = "false",
70          required = false,
71          description = "Skips execution"
72      )
73      private transient boolean skip;
74  
75      /**
76       * Name of keystore.jks file.
77       */
78      @MojoParameter(
79          defaultValue = "${project.build.directory}/keystore.jks",
80          required = false,
81          description = "Name of keystore.jks file"
82      )
83      private transient File keystore;
84  
85      /**
86       * Name of cacerts.jks file.
87       */
88      @MojoParameter(
89          defaultValue = "${project.build.directory}/cacerts.jks",
90          required = false,
91          description = "Name of cacerts.jks file"
92      )
93      private transient File cacerts;
94  
95      /**
96       * Set skip option.
97       * @param skp Shall we skip execution?
98       */
99      public void setSkip(final boolean skp) {
100         this.skip = skp;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public void execute() throws MojoFailureException {
108         StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
109         if (this.skip) {
110             Logger.info(this, "execution skipped because of 'skip' option");
111             return;
112         }
113         final Keystore store = new Keystore(
114             DigestUtils.md5Hex(this.getClass().getName())
115         );
116         if (!store.isActive()) {
117             try {
118                 store.activate(this.keystore);
119                 new Cacerts(this.cacerts).imprt();
120             } catch (java.io.IOException ex) {
121                 throw new IllegalStateException(ex);
122             }
123         }
124         store.populate(this.project.getProperties());
125         Logger.info(this, "Keystore is active: %s", store);
126     }
127 
128 }