Skip to content

Commit 5120c6a

Browse files
committed
Cleanup around GFLauncher and StartServerHelper
Signed-off-by: David Matějček <[email protected]>
1 parent f3e5cf7 commit 5120c6a

File tree

21 files changed

+392
-509
lines changed

21 files changed

+392
-509
lines changed

nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/CLICommand.java

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public int execute(String... argv) throws CommandException {
285285
validate();
286286
if (programOpts.isEcho()) {
287287
logger.info(echoCommand());
288-
// In order to avoid echoing commands used intenally to the
288+
// In order to avoid echoing commands used internally to the
289289
// implementation of *this* command, we turn off echo after
290290
// having echoed this command.
291291
programOpts.setEcho(false);
@@ -297,14 +297,14 @@ public int execute(String... argv) throws CommandException {
297297
}
298298

299299
/**
300-
* Return the name of this command.
300+
* @return the name of this command.
301301
*/
302302
public String getName() {
303303
return name;
304304
}
305305

306306
/**
307-
* Return the command scope for this command. The command scope is
307+
* @return the command scope for this command. The command scope is
308308
* a name space in which commands are defined. Command clients can specify a scope
309309
* to use in looking up a command. Currently this is only used for remote
310310
* commands. By default, the context is null.
@@ -330,7 +330,7 @@ public ProgramOptions getProgramOptions() {
330330
}
331331

332332
/**
333-
* Return a BufferedReader for the man page for this command, or null if not found.
333+
* @return a BufferedReader for the man page for this command, or null if not found.
334334
*/
335335
public BufferedReader getManPage() {
336336
String commandName = getName();
@@ -347,17 +347,17 @@ public BufferedReader getManPage() {
347347
}
348348

349349
/**
350-
* Return a man page for this command that has the tokens substituted
350+
* @return a man page for this command that has the tokens substituted
351351
*/
352-
public BufferedReader expandManPage(Reader r) {
352+
public BufferedReader expandManPage(Reader reader) {
353353
manpageTokenValues[0] = programOpts.getCommandName();
354354
manpageTokenValues[1] = Environment.getPrefix();
355355
manpageTokenValues[2] = Version.getProductNameAbbreviation();
356356
TokenValueSet tvs = new TokenValueSet();
357357
for (int i = 0; i < manpageTokens.length; i++) {
358358
tvs.add(new TokenValue(manpageTokens[i], manpageTokenValues[i], "{", "}"));
359359
}
360-
return new BufferedReader(new LineTokenReplacer(tvs).getReader(r));
360+
return new BufferedReader(new LineTokenReplacer(tvs).getReader(reader));
361361
}
362362

363363
/**
@@ -368,15 +368,14 @@ public BufferedReader expandManPage(Reader r) {
368368
*/
369369
public String getUsage() {
370370
String usage;
371-
if (commandModel != null && ok(usage = commandModel.getUsageText())) {
372-
StringBuilder usageText = new StringBuilder();
373-
usageText.append(strings.get("Usage", strings.get("Usage.brief", programOpts.getCommandName())));
374-
usageText.append(" ");
375-
usageText.append(usage);
376-
return usageText.toString();
377-
} else {
371+
if (commandModel == null || !ok(usage = commandModel.getUsageText())) {
378372
return generateUsageText();
379373
}
374+
StringBuilder usageText = new StringBuilder();
375+
usageText.append(strings.get("Usage", strings.get("Usage.brief", programOpts.getCommandName())));
376+
usageText.append(" ");
377+
usageText.append(usage);
378+
return usageText.toString();
380379
}
381380

382381
private String generateUsageText() {
@@ -632,9 +631,8 @@ public static String quote(String value) {
632631
StringBuilder sb = new StringBuilder(len + 2);
633632
sb.append('"').append(value).append('"');
634633
return sb.toString();
635-
} else {
636-
return value;
637634
}
635+
return value;
638636
}
639637

640638
/**
@@ -798,30 +796,25 @@ protected void checkSanity() {
798796
* option. If so, get the man page using the getManPage method, copy the content to System.out, and return true.
799797
* Otherwise return false. Subclasses may override this method to perform a different check or to use a different method
800798
* to display the man page. If this method returns true, the validate and executeCommand methods won't be called.
799+
*
800+
* @return true to end the program as we just printed the man page.
801801
*/
802802
protected boolean checkHelp() throws CommandException {
803-
if (programOpts.isHelp()) {
804-
BufferedReader br = getManPage();
805-
if (br == null) {
806-
throw new CommandException(strings.get("ManpageMissing", name));
807-
}
808-
br = expandManPage(br);
803+
if (!programOpts.isHelp()) {
804+
return false;
805+
}
806+
final BufferedReader manPage = getManPage();
807+
if (manPage == null) {
808+
throw new CommandException(strings.get("ManpageMissing", name));
809+
}
810+
try (BufferedReader expandedManPage = expandManPage(manPage)) {
809811
String line;
810-
try {
811-
while ((line = br.readLine()) != null) {
812-
System.out.println(line);
813-
}
814-
} catch (IOException ioex) {
815-
throw new CommandException(strings.get("ManpageMissing", name), ioex);
816-
} finally {
817-
try {
818-
br.close();
819-
} catch (IOException ex) {
820-
}
812+
while ((line = expandedManPage.readLine()) != null) {
813+
System.out.println(line);
821814
}
822815
return true;
823-
} else {
824-
return false;
816+
} catch (IOException ioex) {
817+
throw new CommandException(strings.get("ManpageMissing", name), ioex);
825818
}
826819
}
827820

nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/HelpCommand.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2024, 2025 Contributors to the Eclipse Foundation.
33
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -54,16 +54,17 @@ public class HelpCommand extends CLICommand {
5454

5555
@Override
5656
protected int executeCommand() throws CommandException, CommandValidationException {
57-
try {
58-
new More(getPageLength(), getSource(), getDestination(), getUserInput(), getUserOutput(), getQuitChar(), getPrompt());
57+
try (Reader source = getSource()) {
58+
More more = new More(getUserInput(), getUserOutput(), getQuitChar(), getPrompt());
59+
more.view(getPageLength(), source, getDestination());
60+
return 0;
5961
} catch (IOException ioe) {
6062
throw new CommandException(ioe);
6163
}
62-
return 0;
6364
}
6465

6566
private String getCommandName() {
66-
return cmd != null ? cmd : DEFAULT_HELP_PAGE;
67+
return cmd == null ? DEFAULT_HELP_PAGE : cmd;
6768
}
6869

6970
private Writer getDestination() {
@@ -87,11 +88,11 @@ private String getQuitChar() {
8788

8889
private Reader getSource() throws CommandException, CommandValidationException {
8990
CLICommand command = CLICommand.getCommand(habitat, getCommandName());
90-
Reader r = command.getManPage();
91-
if (r == null) {
91+
final Reader reader = command.getManPage();
92+
if (reader == null) {
9293
throw new CommandException(strings.get("ManpageMissing", getCommandName()));
9394
}
94-
return expandManPage(r);
95+
return expandManPage(reader);
9596
}
9697

9798
private Reader getUserInput() {

nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/More.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
23
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
@@ -29,20 +30,21 @@
2930
*
3031
* This class provides a primitive more(1) functionality, paging through the given file, with an optional pagelength.
3132
*/
32-
33-
public class More {
33+
class More {
3434
private BufferedReader in;
3535
private BufferedWriter out;
3636
private String quit;
3737
private String prompt;
3838

39-
public More(int linesPerPage, Reader src, Writer dest, Reader fromUser, Writer toUser, String quitPrefix, String prompt)
40-
throws IOException {
39+
More(Reader fromUser, Writer toUser, String quitPrefix, String prompt) throws IOException {
4140
in = new BufferedReader(fromUser);
4241
out = new BufferedWriter(toUser);
4342
quit = quitPrefix;
4443
this.prompt = prompt;
4544

45+
}
46+
47+
void view(int linesPerPage, Reader src, Writer dest) throws IOException {
4648
Pager pager = new Pager(linesPerPage, src, dest);
4749
do {
4850
pager.nextPage();
@@ -53,7 +55,7 @@ public More(int linesPerPage, Reader src, Writer dest, Reader fromUser, Writer t
5355
* Return false iff the line read from the user starts with the quit character. This is a blocking call, waiting on
5456
* input from the user
5557
*/
56-
boolean wantsToContinue() throws IOException {
58+
private boolean wantsToContinue() throws IOException {
5759
out.write(prompt);
5860
out.newLine();
5961
out.flush();

nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/ProgramOptions.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2022, 2025 Contributors to the Eclipse Foundation
33
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -566,18 +566,12 @@ public void setInteractive(boolean interactive) {
566566
* @return the help
567567
*/
568568
public boolean isHelp() {
569-
boolean help = false;
570569
if (options.containsKey(HELP)) {
571570
String value = options.getOne(HELP);
572-
if (ok(value)) {
573-
help = Boolean.parseBoolean(value);
574-
} else {
575-
help = true;
576-
}
577-
} else {
578-
help = env.getBooleanOption(HELP);
571+
// --help=true or --help
572+
return ok(value) ? Boolean.parseBoolean(value) : true;
579573
}
580-
return help;
574+
return env.getBooleanOption(HELP);
581575
}
582576

583577
/**

nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFEmbeddedLauncher.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public void setup() throws GFLauncherException, MiniXmlParserException {
104104
setup = true;
105105
try {
106106
setupFromEnv();
107-
} catch (GFLauncherException gfle) {
108-
throw new GFLauncherException(GENERAL_MESSAGE + gfle.getMessage(), gfle);
107+
} catch (GFLauncherException e) {
108+
throw new GFLauncherException(GENERAL_MESSAGE + e.getMessage(), e);
109109
}
110110

111111
initCommandLine();
@@ -161,7 +161,7 @@ public void setup() throws GFLauncherException, MiniXmlParserException {
161161
}
162162

163163
@Override
164-
public String getLogFilename() throws GFLauncherException {
164+
public String getLogFilename() {
165165
return logFilename;
166166
}
167167

0 commit comments

Comments
 (0)