Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ private int processApplications(List<Application> applications, final List<Strin
final int lengthBefore = output.length();
output.append(BORDER_CHARACTER)
.append(" ")
.append("No applications deployed. Listening on ")
.append(String.join(", ", listenerUrls))
.append("No applications deployed")
.append(listenerUrls.isEmpty()
? ""
: ". Listening on " + String.join(", ", listenerUrls))
.append(" ")
.append(BORDER_CHARACTER);
int lengthAfter = output.length();
Expand All @@ -124,8 +126,10 @@ private int processApplications(List<Application> applications, final List<Strin
output.append(BORDER_CHARACTER)
.append(" ")
.append(app.getName())
.append(" deployed at ")
.append(appUrls)
.append(" deployed")
.append( appUrls.isEmpty()
? ""
: " at: " + appUrls)
.append(" ")
.append(BORDER_CHARACTER);
int lengthAfter = output.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -80,6 +81,11 @@ public class UberMain {

GlassFish glassFish;
CommandRunner commandRunner;
String fatalErrorMessage = null;
String goodByeMessage = "GlassFish shut down. See you soon!";
boolean shutdownRequested = false;
// to be used in a shutdown hook when handlers are cleared already
Handler[] shutdownLogHandlers;

public static void main(String... args) throws IOException, GlassFishException {
final Arguments arguments = new CommandLineParser().parse(args);
Expand All @@ -88,11 +94,43 @@ public static void main(String... args) throws IOException, GlassFishException {
} else {
// When running off the uber jar don't add extras module URLs to classpath.
EmbeddedGlassFishRuntimeBuilder.addModuleJars = false;
new UberMain().run(arguments);
logger.info(() -> "GlassFish started. Welcome!");
UberMain uberMain = new UberMain();
try {
uberMain.run(arguments);
if (uberMain.isShutdownRequested()) {
uberMain.stopGlassFish();
exit(0);
}
} catch (Throwable ex) {
logger.log(SEVERE, ex.getMessage());
logger.log(FINE, ex.getMessage(), ex);
uberMain.stopGlassFish();
exit(1);
}
}
}

public UberMain() {
this.shutdownLogHandlers = Logger.getLogger("").getHandlers();
}



public void run(Arguments arguments) throws GlassFishException {
try {
runInternal(arguments);
} catch (Throwable ex) {
fatalErrorMessage = ex.getMessage();
throw ex;
}
}

public boolean isShutdownRequested() {
return shutdownRequested;
}

private void runInternal(Arguments arguments) throws GlassFishException {
addShutdownHook(); // handle Ctrt-C.

GlassFishProperties gfProps = arguments.glassFishProperties;
Expand Down Expand Up @@ -121,7 +159,9 @@ public void run(Arguments arguments) throws GlassFishException {

if (arguments.shutdown) {
logger.log(INFO, () -> "Shutting down after startup as requested");
exit(0);
shutdownRequested = true;
goodByeMessage = "GlassFish shut down after startup as requested";
return;
}

switch (glassFish.getStatus()) {
Expand Down Expand Up @@ -187,24 +227,23 @@ private void executeCommandFromString(String stringCommand) {
commandParams[i - 1] = split[i].trim();
}
}
try {
CommandResult result = commandParams == null
? commandRunner.run(command) : commandRunner.run(command, commandParams);
switch (result.getExitStatus()) {
case SUCCESS:
logger.log(INFO, () -> "SUCCESS: " + result.getOutput());
break;
default:
if (result.getFailureCause() != null) {
throw result.getFailureCause();
CommandResult result = commandParams == null
? commandRunner.run(command) : commandRunner.run(command, commandParams);
switch (result.getExitStatus()) {
case SUCCESS:
logger.log(INFO, () -> "SUCCESS: " + result.getOutput());
break;
default:
if (result.getFailureCause() != null) {
if (result.getFailureCause() instanceof RuntimeException runtimeException) {
throw runtimeException;
} else {
throw new RuntimeException("Command completed with " + result.getExitStatus() + ": "
+ result.getOutput() + ". Command was: " + stringCommand);
throw new RuntimeException(result.getFailureCause());
}
}
} catch (Throwable ex) {
logger.log(SEVERE, ex.getMessage());
logger.log(FINE, ex.getMessage(), ex);
} else {
throw new RuntimeException("Command completed with " + result.getExitStatus() + ": "
+ result.getOutput() + ". Command was: " + stringCommand);
}
}
}

Expand All @@ -213,23 +252,38 @@ private void addShutdownHook() {

@Override
public void run() {
if (glassFish != null) {
stopGlassFish();
// We restore the handlers because they are already removed from the logger in the shutdown hook
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite certainly bad idea, I will remember later why.
Btw logging should not be used for communication with the user, but we have this problem all over the sources. I already created an issue for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not communication with the user, it’s a proper log message at the end. It can happen in Kubernetes for example, if the service is misconfigured.

what else do you suggest? System.err?

for (Handler handler : shutdownLogHandlers) {
logger.addHandler(handler);
}

stopGlassFish();

for (Handler handler : logger.getHandlers()) {
logger.removeHandler(handler);
}
}
});
}

private void stopGlassFish() {
try {
glassFish.stop();
} catch (GlassFishException ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
public void stopGlassFish() {
if (glassFish != null) {
try {
glassFish.dispose();
glassFish.stop();
} catch (GlassFishException ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
glassFish.dispose();
glassFish = null;
} catch (GlassFishException ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
}
if (fatalErrorMessage != null) {
logger.severe(() -> "GlassFish shut down unexpectedly, due to an error: " + fatalErrorMessage);
} else {
logger.info(goodByeMessage);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.junit.jupiter.api.Test;
import org.jvnet.hk2.config.types.Property;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
*
* @author Ondro Mihalyi
Expand All @@ -47,6 +49,19 @@ public void getInfoAfterStartup() throws GlassFishException {
String info = new InfoPrinter().getInfoAfterStartup(List.of(app("/app1"), app("application")),
List.of(listener(8080, false), listener(8181, true)));
System.out.println(info);
String[] lines = info.split("\n");
assertTrue(lines.length == 6, "Number of lines should be 6 but is " + lines.length);
assertThatLinesAreEquallyLong(lines);
}

@Test
public void getInfoAfterStartup_noListeners() throws GlassFishException {
String info = new InfoPrinter().getInfoAfterStartup(List.of(app("/app1"), app("application")),
List.of());
System.out.println(info);
String[] lines = info.split("\n");
assertTrue(lines.length == 6, "Number of lines should be 6 but is " + lines.length);
assertThatLinesAreEquallyLong(lines);
}


Expand All @@ -55,8 +70,28 @@ public void getInfoAfterStartup_noApps() throws GlassFishException {
String info = new InfoPrinter().getInfoAfterStartup(List.of(),
List.of(listener(8080, false), listener(8181, true)));
System.out.println(info);
String[] lines = info.split("\n");
assertTrue(lines.length == 5, "Number of lines should be 6 but is " + lines.length);
assertThatLinesAreEquallyLong(lines);
}

@Test
public void getInfoAfterStartup_noApps_noListeners() throws GlassFishException {
String info = new InfoPrinter().getInfoAfterStartup(List.of(),
List.of());
System.out.println(info);
String[] lines = info.split("\n");
assertTrue(lines.length == 5, "Number of lines should be 6 but is " + lines.length);
assertThatLinesAreEquallyLong(lines);
}

private void assertThatLinesAreEquallyLong(String[] lines) {
int lineLength = lines[0].length();
for (int i = 1; i < lines.length; i++) {
int thisLineLength = lines[i].length();
assertTrue(thisLineLength == lineLength, "All lines should as long as the first line. The first line is " + lineLength + " long while the line number " + i + " is " + thisLineLength + " long");
}
}

private MockListener listener(int port, boolean secure) {
return new MockListener(port, secure);
Expand Down