-
Notifications
You must be signed in to change notification settings - Fork 138
/
StartDomainCommand.java
326 lines (275 loc) · 12.4 KB
/
StartDomainCommand.java
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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.admin.servermgmt.cli;
import com.sun.enterprise.admin.cli.Environment;
import com.sun.enterprise.admin.launcher.GFLauncher;
import com.sun.enterprise.admin.launcher.GFLauncherException;
import com.sun.enterprise.admin.launcher.GFLauncherFactory;
import com.sun.enterprise.admin.launcher.GFLauncherInfo;
import com.sun.enterprise.admin.util.CommandModelData.ParamModelData;
import com.sun.enterprise.universal.i18n.LocalStringsImpl;
import com.sun.enterprise.universal.process.ProcessStreamDrainer;
import com.sun.enterprise.universal.xml.MiniXmlParserException;
import jakarta.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.glassfish.api.Param;
import org.glassfish.api.admin.CommandException;
import org.glassfish.api.admin.CommandValidationException;
import org.glassfish.api.admin.RuntimeType;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.security.common.FileRealmHelper;
import org.jvnet.hk2.annotations.Service;
import static com.sun.enterprise.admin.cli.CLIConstants.RESTART_DEBUG_OFF;
import static com.sun.enterprise.admin.cli.CLIConstants.RESTART_DEBUG_ON;
import static com.sun.enterprise.admin.cli.CLIConstants.RESTART_NORMAL;
import static com.sun.enterprise.admin.cli.CLIConstants.WALL_CLOCK_START_PROP;
import static java.util.logging.Level.FINER;
import static org.glassfish.api.admin.RuntimeType.DAS;
/**
* The start-domain command.
*
* @author bnevins
* @author Bill Shannon
*/
@Service(name = "start-domain")
@PerLookup
public class StartDomainCommand extends LocalDomainCommand implements StartServerCommand {
private static final LocalStringsImpl strings = new LocalStringsImpl(StartDomainCommand.class);
@Param(optional = true, shortName = "v", defaultValue = "false")
private boolean verbose;
@Param(optional = true, defaultValue = "false")
private boolean upgrade;
@Param(optional = true, shortName = "w", defaultValue = "false")
private boolean watchdog;
/**
* Starts the server in debug mode with suspend set in domain.xml
*/
@Param(optional = true, shortName = "d", defaultValue = "false")
private boolean debug;
/**
* Starts the server in debug mode with suspend on
*/
@Param(optional = true, shortName = "s", defaultValue = "false")
private boolean suspend;
@Param(name = "domain_name", primary = true, optional = true)
private String domainName0;
@Param(name = "dry-run", shortName = "n", optional = true, defaultValue = "false")
private boolean dry_run;
@Param(name = "drop-interrupted-commands", optional = true, defaultValue = "false")
private boolean drop_interrupted_commands;
@Inject
ServerEnvironment serverEnvironment;
private GFLauncherInfo launchParameters;
private GFLauncher glassFishLauncher;
private StartServerHelper startServerHelper;
// the name of the master password option
private final String newpwName = Environment.getPrefix() + "NEWPASSWORD";
@Override
public List<String> getLauncherArgs() {
return glassFishLauncher.getCommandLine();
}
@Override
public RuntimeType getType() {
return DAS;
}
@Override
protected void validate() throws CommandException, CommandValidationException {
setDomainName(domainName0);
super.validate();
}
@Override
protected int executeCommand() throws CommandException {
try {
// createLauncher needs to go before the startServerHelper is created!!
createLauncher();
String masterPassword = getMasterPassword();
startServerHelper = new StartServerHelper(programOpts.isTerse(), getServerDirs(), glassFishLauncher, masterPassword);
if (!startServerHelper.prepareForLaunch()) {
return ERROR;
}
if (!upgrade && glassFishLauncher.needsManualUpgrade()) {
logger.info(strings.get("manualUpgradeNeeded"));
return ERROR;
}
doAutoUpgrade(masterPassword);
if (dry_run) {
logger.fine(Strings.get("dry_run_msg"));
List<String> cmd = glassFishLauncher.getCommandLine();
StringBuilder sb = new StringBuilder();
for (String s : cmd) {
sb.append(s);
sb.append('\n');
}
logger.info(sb.toString());
return SUCCESS;
}
doAdminPasswordCheck();
// Launch returns very quickly if verbose is not set
// if verbose is set then it returns after the domain dies
glassFishLauncher.launch();
if (verbose || upgrade || watchdog) { // we can potentially loop forever here...
while (true) {
int returnValue = glassFishLauncher.getExitValue();
switch (returnValue) {
case RESTART_NORMAL:
logger.info(strings.get("restart"));
break;
case RESTART_DEBUG_ON:
logger.info(strings.get("restartChangeDebug", "on"));
launchParameters.setDebug(true);
break;
case RESTART_DEBUG_OFF:
logger.info(strings.get("restartChangeDebug", "off"));
launchParameters.setDebug(false);
break;
default:
return returnValue;
}
if (env.debug()) {
System.setProperty(WALL_CLOCK_START_PROP, "" + System.currentTimeMillis());
}
glassFishLauncher.relaunch();
}
} else {
startServerHelper.waitForServerStart();
startServerHelper.report();
return SUCCESS;
}
} catch (GFLauncherException gfle) {
throw new CommandException(gfle.getMessage());
} catch (MiniXmlParserException me) {
throw new CommandException(me);
}
}
/**
* Create a glassFishLauncher for the domain specified by arguments to this command. The glassFishLauncher is for a
* server of the specified type. Sets the glassFishLauncher and launchParameters fields. It has to be public because it
* is part of an interface
*/
@Override
public void createLauncher() throws GFLauncherException, MiniXmlParserException {
glassFishLauncher = GFLauncherFactory.getInstance(getType());
launchParameters = glassFishLauncher.getInfo();
launchParameters.setDomainName(getDomainName());
launchParameters.setDomainParentDir(getDomainsDir().getPath());
launchParameters.setVerbose(verbose || upgrade);
launchParameters.setSuspend(suspend);
launchParameters.setDebug(debug);
launchParameters.setUpgrade(upgrade);
launchParameters.setWatchdog(watchdog);
launchParameters.setDropInterruptedCommands(drop_interrupted_commands);
launchParameters.setRespawnInfo(programOpts.getClassName(), programOpts.getClassPath(), respawnArgs());
glassFishLauncher.setup();
}
/**
* @return the asadmin command line arguments necessary to start this domain admin server.
*/
private String[] respawnArgs() {
List<String> args = new ArrayList<>(15);
args.addAll(Arrays.asList(programOpts.getProgramArguments()));
// now the start-domain specific arguments
args.add(getName()); // the command name
args.add("--verbose=" + String.valueOf(verbose));
args.add("--watchdog=" + String.valueOf(watchdog));
args.add("--debug=" + String.valueOf(debug));
args.add("--domaindir");
args.add(getDomainsDir().toString());
if (ok(getDomainName())) {
args.add(getDomainName()); // the operand
}
if (logger.isLoggable(FINER)) {
logger.log(FINER, "Respawn args: {0}", args.toString());
}
String[] a = new String[args.size()];
args.toArray(a);
return a;
}
/**
* If this domain needs to be upgraded and --upgrade wasn't specified, first start the domain to do the upgrade and then
* start the domain again for real.
*/
private void doAutoUpgrade(String mpv) throws GFLauncherException, MiniXmlParserException, CommandException {
if (upgrade || !glassFishLauncher.needsAutoUpgrade()) {
return;
}
logger.info(strings.get("upgradeNeeded"));
launchParameters.setUpgrade(true);
glassFishLauncher.setup();
glassFishLauncher.launch();
Process glassFishProcess = glassFishLauncher.getProcess();
int exitCode = -1;
try {
exitCode = glassFishProcess.waitFor();
} catch (InterruptedException ex) {
// should never happen
}
if (exitCode != SUCCESS) {
ProcessStreamDrainer psd = glassFishLauncher.getProcessStreamDrainer();
String output = psd.getOutErrString();
if (ok(output)) {
throw new CommandException(strings.get("upgradeFailedOutput", launchParameters.getDomainName(), exitCode, output));
} else {
throw new CommandException(strings.get("upgradeFailed", launchParameters.getDomainName(), exitCode));
}
}
logger.info(strings.get("upgradeSuccessful"));
// need a new glassFishLauncher to start the domain for real
createLauncher();
// continue with normal start...
}
/**
* Check to make sure that at least one admin user is able to login. If none is found, then prompt for an admin
* password.
*
* NOTE: this depends on glassFishLauncher.setup having already been called.
*/
private void doAdminPasswordCheck() throws CommandException {
String adminRealmKeyFile = glassFishLauncher.getAdminRealmKeyFile();
if (adminRealmKeyFile != null) {
try {
FileRealmHelper fileRealmHelper = new FileRealmHelper(adminRealmKeyFile);
if (!fileRealmHelper.hasAuthenticatableUser()) {
// Prompt for the password for the first user and set it
Set<String> adminUsers = fileRealmHelper.getUserNames();
if (adminUsers == null || adminUsers.isEmpty()) {
throw new CommandException("no admin users");
}
String firstAdminUser = adminUsers.iterator().next();
ParamModelData npwo = new ParamModelData(newpwName, String.class, false, null);
npwo.prompt = strings.get("new.adminpw", firstAdminUser);
npwo.promptAgain = strings.get("new.adminpw.again", firstAdminUser);
npwo.param._password = true;
logger.info(strings.get("new.adminpw.prompt"));
char[] newPasswordArray = super.getPassword(npwo, null, true);
String newPassword = newPasswordArray != null ? new String(newPasswordArray) : null;
if (newPassword == null) {
throw new CommandException(strings.get("no.console"));
}
fileRealmHelper.updateUser(firstAdminUser, firstAdminUser, newPassword.toCharArray(), null);
fileRealmHelper.persist();
}
} catch (IOException ioe) {
throw new CommandException(ioe);
}
}
}
}