1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.ssl.maven.plugin;
31
32 import com.jcabi.aspects.Immutable;
33 import com.jcabi.aspects.Loggable;
34 import com.jcabi.log.Logger;
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.Properties;
38 import javax.validation.constraints.NotNull;
39 import lombok.EqualsAndHashCode;
40
41
42
43
44
45
46
47
48 @Immutable
49 @EqualsAndHashCode(of = "password")
50 final class Keystore {
51
52
53
54
55 public static final String KEY = "javax.net.ssl.keyStore";
56
57
58
59
60 public static final String KEY_PWD = "javax.net.ssl.keyStorePassword";
61
62
63
64
65 private final transient String password;
66
67
68
69
70
71 public Keystore(@NotNull final String pwd) {
72 this.password = pwd;
73 }
74
75
76
77
78 @Override
79 public String toString() {
80 final String[] names = new String[] {
81 Keystore.KEY,
82 Keystore.KEY_PWD,
83 };
84 final StringBuilder text = new StringBuilder();
85 text.append('[');
86 for (final String name : names) {
87 if (text.length() > 1) {
88 text.append(", ");
89 }
90 text.append(name).append('=');
91 final String value = System.getProperty(name);
92 if (name == null) {
93 text.append("NULL");
94 } else {
95 text.append(value);
96 }
97 }
98 text.append(']');
99 return text.toString();
100 }
101
102
103
104
105
106 @Loggable(Loggable.DEBUG)
107 public boolean isActive() {
108 final String pwd = System.getProperty(Keystore.KEY_PWD);
109 return pwd != null && pwd.equals(this.password);
110 }
111
112
113
114
115
116
117 @Loggable(Loggable.DEBUG)
118 public void activate(final File file) throws IOException {
119 file.getParentFile().mkdirs();
120 file.delete();
121 new Keytool(file, this.password).genkey();
122 System.setProperty(Keystore.KEY, file.getAbsolutePath());
123 System.setProperty(Keystore.KEY_PWD, this.password);
124 new Keytool(file, this.password).list();
125 }
126
127
128
129
130
131 @Loggable(Loggable.DEBUG)
132 public void populate(final Properties props) {
133 final String[] names = new String[] {
134 Keystore.KEY,
135 Keystore.KEY_PWD,
136 };
137 for (final String name : names) {
138 final String value = System.getProperty(name);
139 if (value == null) {
140 continue;
141 }
142 props.put(name, value);
143 Logger.info(
144 this,
145 "Maven property ${%s} set to '%s'",
146 name,
147 System.getProperty(name)
148 );
149 }
150 }
151
152 }