Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -124,9 +124,12 @@ private int processApplications(List<Application> applications, final List<Strin
output.append(BORDER_CHARACTER)
.append(" ")
.append(app.getName())
.append(" deployed at ")
.append(appUrls)
.append(" ")
.append(" deployed");
if (!appUrls.isEmpty()) {
output.append(" at ")
.append(appUrls);
}
output.append(" ")
.append(BORDER_CHARACTER);
int lengthAfter = output.length();
int lineLength = lengthAfter - lengthBefore;
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