View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.ssl.maven.plugin;
6   
7   import com.jcabi.aspects.Immutable;
8   import com.jcabi.aspects.Loggable;
9   import com.jcabi.log.Logger;
10  import java.io.File;
11  import java.io.IOException;
12  import java.util.Properties;
13  import javax.validation.constraints.NotNull;
14  import lombok.EqualsAndHashCode;
15  
16  /**
17   * Keystore abstraction.
18   *
19   * @since 0.5
20   */
21  @Immutable
22  @EqualsAndHashCode(of = "password")
23  final class Keystore {
24  
25      /**
26       * Constant {@code javax.net.ssl.keyStore}.
27       */
28      public static final String KEY = "javax.net.ssl.keyStore";
29  
30      /**
31       * Constant {@code javax.net.ssl.keyStorePassword}.
32       */
33      public static final String KEY_PWD = "javax.net.ssl.keyStorePassword";
34  
35      /**
36       * Unique password of it.
37       */
38      private final transient String password;
39  
40      /**
41       * Ctor.
42       * @param pwd The password
43       */
44      Keystore(@NotNull final String pwd) {
45          this.password = pwd;
46      }
47  
48      @Override
49      public String toString() {
50          final String[] names = {Keystore.KEY, Keystore.KEY_PWD};
51          final StringBuilder text = new StringBuilder();
52          text.append('[');
53          for (final String name : names) {
54              if (text.length() > 1) {
55                  text.append(", ");
56              }
57              text.append(name).append('=');
58              if (name == null) {
59                  text.append("NULL");
60              } else {
61                  text.append(System.getProperty(name));
62              }
63          }
64          text.append(']');
65          return text.toString();
66      }
67  
68      /**
69       * Is it active now in the JVM?
70       * @return TRUE if JVM is using our keystore
71       */
72      @Loggable(Loggable.DEBUG)
73      public boolean isActive() {
74          final String pwd = System.getProperty(Keystore.KEY_PWD);
75          return pwd != null && pwd.equals(this.password);
76      }
77  
78      /**
79       * Activate it, in the given file.
80       * @param file The file to use
81       * @throws IOException If fails
82       */
83      @Loggable(Loggable.DEBUG)
84      public void activate(final File file) throws IOException {
85          file.getParentFile().mkdirs();
86          file.delete();
87          new Keytool(file, this.password).genkey();
88          System.setProperty(Keystore.KEY, file.getAbsolutePath());
89          System.setProperty(Keystore.KEY_PWD, this.password);
90          new Keytool(file, this.password).list();
91      }
92  
93      /**
94       * Populate given properties with this keystore's path and password.
95       * @param props The properties
96       */
97      @Loggable(Loggable.DEBUG)
98      public void populate(final Properties props) {
99          final String[] names = {Keystore.KEY, Keystore.KEY_PWD};
100         for (final String name : names) {
101             if (System.getProperty(name) == null) {
102                 continue;
103             }
104             props.put(name, System.getProperty(name));
105             Logger.info(
106                 this,
107                 "Maven property ${%s} set to '%s'",
108                 name,
109                 System.getProperty(name)
110             );
111         }
112     }
113 
114 }