1
2
3
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
18
19
20
21 @Immutable
22 @EqualsAndHashCode(of = "password")
23 final class Keystore {
24
25
26
27
28 public static final String KEY = "javax.net.ssl.keyStore";
29
30
31
32
33 public static final String KEY_PWD = "javax.net.ssl.keyStorePassword";
34
35
36
37
38 private final transient String password;
39
40
41
42
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
70
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
80
81
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
95
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 }