forked from javaee-samples/javaee7-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerOperations.java
More file actions
357 lines (265 loc) · 12.4 KB
/
ServerOperations.java
File metadata and controls
357 lines (265 loc) · 12.4 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/** Portions Copyright Payara Services Limited **/
package org.javaee7;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Various high level Java EE 7 samples specific operations to execute against
* the various servers used for running the samples
*
* @author arjan
*
*/
public class ServerOperations {
private static final Logger logger = Logger.getLogger(ServerOperations.class.getName());
/**
* Add the default test user and credentials to the identity store of
* supported containers
*/
public static void addUsersToContainerIdentityStore() {
// TODO: abstract adding container managed users to utility class
// TODO: consider PR for sending CLI commands to Arquillian
String javaEEServer = System.getProperty("javaEEServer");
if ("glassfish-remote".equals(javaEEServer) || "payara-remote".equals(javaEEServer)) {
System.out.println("Adding user for glassfish-remote");
List<String> cmd = new ArrayList<>();
cmd.add("create-file-user");
cmd.add("--groups");
cmd.add("g1");
cmd.add("--passwordfile");
cmd.add(Paths.get("").toAbsolutePath() + "/src/test/resources/password.txt");
cmd.add("u1");
CliCommands.payaraGlassFish(cmd);
} else if ("piranha-embedded".equals(javaEEServer)) {
System.out.println("Adding user for piranha-embedded");
System.setProperty("io.piranha.identitystore.callers", "<u><caller callername=\"u1\" password=\"p1\" groups=\"g1\"/></u>");
}
else {
if (javaEEServer == null) {
System.out.println("javaEEServer not specified");
} else {
System.out.println(javaEEServer + " not supported");
}
}
// TODO: support other servers than Payara and GlassFish
// WildFly ./bin/add-user.sh -a -u u1 -p p1 -g g1
}
public static void addCertificateToContainerTrustStore(Certificate clientCertificate) {
String javaEEServer = System.getProperty("javaEEServer");
if ("glassfish-remote".equals(javaEEServer) || "payara-remote".equals(javaEEServer)) {
String gfHome = System.getProperty("glassfishRemote_gfHome");
if (gfHome == null) {
logger.info("glassfishRemote_gfHome not specified");
return;
}
Path gfHomePath = Paths.get(gfHome);
if (!gfHomePath.toFile().exists()) {
logger.severe("glassfishRemote_gfHome at " + gfHome + " does not exists");
return;
}
if (!gfHomePath.toFile().isDirectory()) {
logger.severe("glassfishRemote_gfHome at " + gfHome + " is not a directory");
return;
}
String domain = System.getProperty("payara_domain", "domain1");
if (domain != null) {
domain = getPayaraDomainFromServer();
logger.info("Using domain \"" + domain + "\" obtained from server. If this is not correct use -Dpayara_domain to override.");
}
Path cacertsPath = gfHomePath.resolve("glassfish/domains/" + domain + "/config/cacerts.jks");
if (!cacertsPath.toFile().exists()) {
logger.severe("The container trust store at " + cacertsPath.toAbsolutePath() + " does not exists");
logger.severe("Is the domain \"" + domain + "\" correct?");
return;
}
logger.info("*** Adding certificate to container trust store: " + cacertsPath.toAbsolutePath());
KeyStore keyStore = null;
try (InputStream in = new FileInputStream(cacertsPath.toAbsolutePath().toFile())) {
keyStore = KeyStore.getInstance("JKS");
keyStore.load(in, "changeit".toCharArray());
keyStore.setCertificateEntry("arquillianClientTestCert", clientCertificate);
keyStore.store(new FileOutputStream(cacertsPath.toAbsolutePath().toFile()), "changeit".toCharArray());
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
throw new IllegalStateException(e);
}
restartContainer(domain);
} else {
if (javaEEServer == null) {
System.out.println("javaEEServer not specified");
} else {
System.out.println(javaEEServer + " not supported");
}
}
}
public static URL toContainerHttps(URL url) {
if ("https".equals(url.getProtocol())) {
return url;
}
String javaEEServer = System.getProperty("javaEEServer");
// String protocol, String host, int port, String file
if ("glassfish-remote".equals(javaEEServer) || "payara-remote".equals(javaEEServer)) {
try {
URL httpsUrl = new URL(
"https",
url.getHost(),
8181,
url.getFile()
);
System.out.println("Changing base URL from " + url + " into " + httpsUrl);
return httpsUrl;
} catch (MalformedURLException e) {
System.out.println("Failure creating HTTPS URL");
e.printStackTrace();
logger.log(Level.SEVERE, "Failure creating HTTPS URL", e);
}
} else {
if (javaEEServer == null) {
System.out.println("javaEEServer not specified");
} else {
System.out.println(javaEEServer + " not supported");
}
}
return null;
}
private static String getPayaraDomainFromServer() {
System.out.println("Getting Payara domain from server");
List<String> output = new ArrayList<>();
List<String> cmd = new ArrayList<>();
cmd.add("list-domains");
CliCommands.payaraGlassFish(cmd, output);
String domain = null;
for (String line : output) {
if (line.contains(" not running")) {
continue;
}
if (line.contains(" running")) {
domain = line.substring(0, line.lastIndexOf(" running"));
break;
}
}
if (domain == null) {
throw new IllegalStateException("Running domain could not be obtained for target Payara. Please specify explicitly using -Dpayara_domain");
}
return domain;
}
public static void addContainerSystemProperty(String key, String value) {
String javaEEServer = System.getProperty("javaEEServer");
if ("glassfish-remote".equals(javaEEServer) || "payara-remote".equals(javaEEServer)) {
System.out.println("Adding system property");
List<String> cmd = new ArrayList<>();
cmd.add("create-jvm-options");
cmd.add("-D" + key + "=\"" + value + "\"");
CliCommands.payaraGlassFish(cmd);
} else {
if (javaEEServer == null) {
System.out.println("javaEEServer not specified");
} else {
System.out.println(javaEEServer + " not supported");
}
}
}
public static void restartContainer() {
restartContainer(null);
}
public static void restartContainer(String domain) {
// Arquillian connectors can stop/start already, but not on demand by code
String javaEEServer = System.getProperty("javaEEServer");
if ("glassfish-remote".equals(javaEEServer) || "payara-remote".equals(javaEEServer)) {
System.out.println("Restarting domain");
List<String> cmd = new ArrayList<>();
cmd.add("restart-domain");
String restartDomain = domain;
if (restartDomain == null) {
restartDomain = System.getProperty("payara_domain");
}
if (restartDomain == null) {
restartDomain = getPayaraDomainFromServer();
}
cmd.add(restartDomain);
CliCommands.payaraGlassFish(cmd);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
if (javaEEServer == null) {
System.out.println("javaEEServer not specified");
} else {
System.out.println(javaEEServer + " not supported");
}
}
}
public static void restartContainerDebug() {
// Arquillian connectors can stop/start already, but not on demand by code
String javaEEServer = System.getProperty("javaEEServer");
if ("glassfish-remote".equals(javaEEServer) || "payara-remote".equals(javaEEServer)) {
System.out.println("Stopping domain");
List<String> cmd = new ArrayList<>();
cmd.add("stop-domain");
CliCommands.payaraGlassFish(cmd);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Starting domain");
cmd = new ArrayList<>();
cmd.add("start-domain");
CliCommands.payaraGlassFish(cmd);
System.out.println("Command returned");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("After sleep");
}
}
public static void setupContainerJDBCIDigestIdentityStore() {
String javaEEServer = System.getProperty("javaEEServer");
if ("glassfish-remote".equals(javaEEServer) || "payara-remote".equals(javaEEServer)) {
System.out.println("Setting up container JDBC identity store for " + javaEEServer);
List<String> cmd = new ArrayList<>();
cmd.add("create-auth-realm");
cmd.add("--classname");
cmd.add("com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm");
cmd.add("--property");
cmd.add(
"jaas-context=jdbcDigestRealm:" +
"encoding=HASHED:" +
"password-column=password:" +
"datasource-jndi=java\\:comp/DefaultDataSource:" +
"group-table=grouptable:"+
"charset=UTF-8:" +
"user-table=usertable:" +
"group-name-column=groupname:" +
"digest-algorithm=None:" +
"user-name-column=username");
cmd.add("eesamplesdigestrealm");
CliCommands.payaraGlassFish(cmd);
} else {
if (javaEEServer == null) {
System.out.println("javaEEServer not specified");
} else {
System.out.println(javaEEServer + " not supported");
}
}
// TODO: support other servers than Payara and GlassFish
// WildFly ./bin/add-user.sh -a -u u1 -p p1 -g g1
}
}