Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WFLY-2899] Capture System.out and System.err before jboss-stdio capture them #5909

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions appclient/src/main/java/org/jboss/as/appclient/subsystem/Main.java
Expand Up @@ -23,6 +23,7 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -61,10 +62,13 @@
* @author Stuart Douglas
*/
public final class Main {
// Capture System.out and System.err before they are redirected by STDIO
private static final PrintStream STDOUT = System.out;
private static final PrintStream STDERR = System.err;


private static void usage() {
CommandLineArgumentUsageImpl.printUsage(System.out);
CommandLineArgumentUsageImpl.printUsage(STDOUT);
}

private Main() {
Expand Down Expand Up @@ -105,7 +109,7 @@ public static void main(String[] args) {
final List<String> clientArgs = options.clientArguments;

if (clientArgs.isEmpty()) {
System.err.println(MESSAGES.appClientNotSpecified());
STDERR.println(MESSAGES.appClientNotSpecified());
usage();
abort(null);
} else {
Expand Down Expand Up @@ -162,7 +166,7 @@ public ExtensibleConfigurationPersister createConfigurationPersister(ServerEnvir
private static void abort(Throwable t) {
try {
if (t != null) {
t.printStackTrace(System.err);
t.printStackTrace(STDERR);
}
} finally {
SystemExiter.exit(1);
Expand All @@ -187,7 +191,7 @@ public static ParsedOptions determineEnvironment(String[] args, Properties syste
} else if (CommandLineConstants.VERSION.equals(arg) || CommandLineConstants.SHORT_VERSION.equals(arg)
|| CommandLineConstants.OLD_VERSION.equals(arg) || CommandLineConstants.OLD_SHORT_VERSION.equals(arg)) {
productConfig = new ProductConfig(Module.getBootModuleLoader(), WildFlySecurityManager.getPropertyPrivileged(ServerEnvironment.HOME_DIR, null), null);
System.out.println(productConfig.getPrettyVersionString());
STDOUT.println(productConfig.getPrettyVersionString());
return null;
} else if (CommandLineConstants.HELP.equals(arg) || CommandLineConstants.SHORT_HELP.equals(arg) || CommandLineConstants.OLD_HELP.equals(arg)) {
usage();
Expand Down Expand Up @@ -258,7 +262,7 @@ public static ParsedOptions determineEnvironment(String[] args, Properties syste
appClientConfig = parseValue(arg, CommandLineConstants.APPCLIENT_CONFIG);
} else {
if (arg.startsWith("-")) {
System.out.println(MESSAGES.unknownOption(arg));
STDOUT.println(MESSAGES.unknownOption(arg));
usage();

return null;
Expand All @@ -267,7 +271,7 @@ public static ParsedOptions determineEnvironment(String[] args, Properties syste
clientArguments.add(arg);
}
} catch (IndexOutOfBoundsException e) {
System.err.println(MESSAGES.argumentExpected(arg));
STDERR.println(MESSAGES.argumentExpected(arg));
usage();
return null;
}
Expand Down Expand Up @@ -298,11 +302,11 @@ private static boolean processProperties(final String arg, final String urlSpec)
props.load(url.openConnection().getInputStream());
return true;
} catch (MalformedURLException e) {
System.err.println(MESSAGES.malformedUrl(arg));
STDERR.println(MESSAGES.malformedUrl(arg));
usage();
return false;
} catch (IOException e) {
System.err.println(MESSAGES.cannotLoadProperties(url));
STDERR.println(MESSAGES.cannotLoadProperties(url));
usage();
return false;
}
Expand Down
Expand Up @@ -66,6 +66,9 @@
* @author Brian Stansberry
*/
public final class Main {
// Capture System.out and System.err before they are redirected by STDIO
private static final PrintStream STDOUT = System.out;
private static final PrintStream STDERR = System.err;

private static final String PROCESS_NAME = "-D[Host Controller]";

Expand All @@ -87,7 +90,7 @@ public static void main(String[] args) throws IOException {
try {
StreamUtils.readFully(new Base64InputStream(System.in), authKey);
} catch (IOException e) {
System.err.println(MESSAGES.failedToReadAuthenticationKey(e));
STDERR.println(MESSAGES.failedToReadAuthenticationKey(e));
fail();
return;
}
Expand Down Expand Up @@ -178,7 +181,7 @@ private static void fail(){
}

private static void usage() {
CommandLineArgumentUsageImpl.printUsage(System.out);
CommandLineArgumentUsageImpl.printUsage(STDOUT);
}

/**
Expand Down Expand Up @@ -239,7 +242,7 @@ private static HostControllerEnvironment determineEnvironment(String[] args) {
try {
pmPort = Integer.valueOf(port);
} catch (NumberFormatException e) {
System.err.println(MESSAGES.invalidValue(CommandLineConstants.PROCESS_CONTROLLER_BIND_PORT, "Integer", port, usageNote()));
STDERR.println(MESSAGES.invalidValue(CommandLineConstants.PROCESS_CONTROLLER_BIND_PORT, "Integer", port, usageNote()));
return null;
}
} else if (arg.startsWith(CommandLineConstants.PROCESS_CONTROLLER_BIND_PORT)) {
Expand All @@ -257,7 +260,7 @@ private static HostControllerEnvironment determineEnvironment(String[] args) {
try {
pmAddress = InetAddress.getByName(addr);
} catch (UnknownHostException e) {
System.err.println(MESSAGES.unknownHostValue(CommandLineConstants.PROCESS_CONTROLLER_BIND_ADDR, addr, usageNote()));
STDERR.println(MESSAGES.unknownHostValue(CommandLineConstants.PROCESS_CONTROLLER_BIND_ADDR, addr, usageNote()));
return null;
}
} else if (arg.startsWith(CommandLineConstants.PROCESS_CONTROLLER_BIND_ADDR)) {
Expand Down Expand Up @@ -342,7 +345,7 @@ private static HostControllerEnvironment determineEnvironment(String[] args) {

int idx = arg.indexOf('=');
if (idx == arg.length() - 1) {
System.err.println(MESSAGES.argumentExpected(arg, usageNote()));
STDERR.println(MESSAGES.argumentExpected(arg, usageNote()));
return null;
}
String value = idx > -1 ? arg.substring(idx + 1) : checkValueIsNotAnArg(arg, args[++i]);
Expand All @@ -356,7 +359,7 @@ private static HostControllerEnvironment determineEnvironment(String[] args) {

int idx = arg.indexOf('=');
if (idx == arg.length() - 1) {
System.err.println(MESSAGES.argumentExpected(arg, usageNote()));
STDERR.println(MESSAGES.argumentExpected(arg, usageNote()));
return null;
}
String value = idx > -1 ? arg.substring(idx + 1) : args[++i];
Expand Down Expand Up @@ -387,7 +390,7 @@ private static HostControllerEnvironment determineEnvironment(String[] args) {

int idx = arg.indexOf('=');
if (idx == arg.length() - 1) {
System.err.println(MESSAGES.argumentExpected(arg, usageNote()));
STDERR.println(MESSAGES.argumentExpected(arg, usageNote()));
return null;
}
String value = idx > -1 ? arg.substring(idx + 1) : checkValueIsNotAnArg(arg, args[++i]);
Expand All @@ -411,7 +414,7 @@ private static HostControllerEnvironment determineEnvironment(String[] args) {

int idx = arg.indexOf('=');
if (idx == arg.length() - 1) {
System.err.println(MESSAGES.argumentExpected(arg, usageNote()));
STDERR.println(MESSAGES.argumentExpected(arg, usageNote()));
return null;
}
String value = idx > -1 ? arg.substring(idx + 1) : checkValueIsNotAnArg(arg, args[++i]);
Expand All @@ -427,11 +430,11 @@ private static HostControllerEnvironment determineEnvironment(String[] args) {
return null;
}
} else {
System.err.println(MESSAGES.invalidOption(arg, usageNote()));
STDERR.println(MESSAGES.invalidOption(arg, usageNote()));
return null;
}
} catch (IndexOutOfBoundsException e) {
System.err.println(MESSAGES.argumentExpected(arg, usageNote()));
STDERR.println(MESSAGES.argumentExpected(arg, usageNote()));
return null;
}
}
Expand All @@ -444,7 +447,7 @@ private static HostControllerEnvironment determineEnvironment(String[] args) {
private static String parseValue(final String arg, final String key) {
int splitPos = key.length();
if (arg.length() <= splitPos + 1 || arg.charAt(splitPos) != '=') {
System.err.println(MESSAGES.argumentHasNoValue(arg, usageNote()));
STDERR.println(MESSAGES.argumentHasNoValue(arg, usageNote()));
return null;
} else {
return arg.substring(splitPos + 1);
Expand All @@ -462,7 +465,7 @@ private static String parseValue(final String arg, final String key) {
*/
private static String checkValueIsNotAnArg(String argument, String value) {
if (value.startsWith("-")) {
System.err.println(MESSAGES.argumentHasNoValue(argument, usageNote()));
STDERR.println(MESSAGES.argumentHasNoValue(argument, usageNote()));
return null;
}
return value;
Expand All @@ -481,10 +484,10 @@ private static boolean processProperties(final String arg, final String urlSpec,
}
return true;
} catch (MalformedURLException e) {
System.err.println(MESSAGES.malformedUrl(arg, usageNote()));
STDERR.println(MESSAGES.malformedUrl(arg, usageNote()));
return false;
} catch (IOException e) {
System.err.println(MESSAGES.unableToLoadProperties(url, usageNote()));
STDERR.println(MESSAGES.unableToLoadProperties(url, usageNote()));
return false;
}
}
Expand All @@ -493,7 +496,7 @@ private static Integer parsePort(final String value, final String key) {
try {
return Integer.valueOf(value);
} catch (NumberFormatException e) {
System.err.println(MESSAGES.invalidValue(key, "Integer", value, usageNote()));
STDERR.println(MESSAGES.invalidValue(key, "Integer", value, usageNote()));
return null;
}
}
Expand All @@ -502,7 +505,7 @@ private static InetAddress parseAddress(final String value, final String key) {
try {
return InetAddress.getByName(value);
} catch (UnknownHostException e) {
System.err.println(MESSAGES.unknownHostValue(key, value, usageNote()));
STDERR.println(MESSAGES.unknownHostValue(key, value, usageNote()));
return null;
}
}
Expand Down Expand Up @@ -561,7 +564,7 @@ private static Map<String, String> getHostSystemProperties() {
}
}
} catch (Exception e) {
System.err.println(MESSAGES.cannotAccessJvmInputArgument(e));
STDERR.println(MESSAGES.cannotAccessJvmInputArgument(e));
}
return hostSystemProperties;
}
Expand Down Expand Up @@ -666,7 +669,7 @@ private void setBindAddress(String key, String value) {
bindAddress = InetAddress.getByName(value);
} catch (UnknownHostException e) {
parseFailed = true;
System.err.println(MESSAGES.invalidValue(key, "InetAddress", value, usageNote()));
STDERR.println(MESSAGES.invalidValue(key, "InetAddress", value, usageNote()));
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions server/src/main/java/org/jboss/as/server/Main.java
Expand Up @@ -24,6 +24,7 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
Expand Down Expand Up @@ -53,9 +54,12 @@
* @author Anil Saldhana
*/
public final class Main {
// Capture System.out and System.err before they are redirected by STDIO
private static final PrintStream STDOUT = System.out;
private static final PrintStream STDERR = System.err;

private static void usage() {
CommandLineArgumentUsageImpl.printUsage(System.out);
CommandLineArgumentUsageImpl.printUsage(STDOUT);
}

private Main() {
Expand Down Expand Up @@ -103,7 +107,7 @@ public static void main(String[] args) {
private static void abort(Throwable t) {
try {
if (t != null) {
t.printStackTrace(System.err);
t.printStackTrace(STDERR);
}
} finally {
SystemExiter.exit(ExitCodes.FAILED);
Expand All @@ -122,7 +126,7 @@ public static ServerEnvironment determineEnvironment(String[] args, Properties s
if (CommandLineConstants.VERSION.equals(arg) || CommandLineConstants.SHORT_VERSION.equals(arg)
|| CommandLineConstants.OLD_VERSION.equals(arg) || CommandLineConstants.OLD_SHORT_VERSION.equals(arg)) {
productConfig = new ProductConfig(Module.getBootModuleLoader(), WildFlySecurityManager.getPropertyPrivileged(ServerEnvironment.HOME_DIR, null), null);
System.out.println(productConfig.getPrettyVersionString());
STDOUT.println(productConfig.getPrettyVersionString());
return null;
} else if (CommandLineConstants.HELP.equals(arg) || CommandLineConstants.SHORT_HELP.equals(arg) || CommandLineConstants.OLD_HELP.equals(arg)) {
usage();
Expand Down Expand Up @@ -188,7 +192,7 @@ public static ServerEnvironment determineEnvironment(String[] args, Properties s

int idx = arg.indexOf('=');
if (idx == arg.length() - 1) {
System.err.println(ServerMessages.MESSAGES.noArgValue(arg));
STDERR.println(ServerMessages.MESSAGES.noArgValue(arg));
usage();
return null;
}
Expand All @@ -210,7 +214,7 @@ public static ServerEnvironment determineEnvironment(String[] args, Properties s

int idx = arg.indexOf('=');
if (idx == arg.length() - 1) {
System.err.println(ServerMessages.MESSAGES.valueExpectedForCommandLineOption(arg));
STDERR.println(ServerMessages.MESSAGES.valueExpectedForCommandLineOption(arg));
usage();
return null;
}
Expand Down Expand Up @@ -238,12 +242,12 @@ public static ServerEnvironment determineEnvironment(String[] args, Properties s
}
}
} else {
System.err.println(ServerMessages.MESSAGES.invalidCommandLineOption(arg));
STDERR.println(ServerMessages.MESSAGES.invalidCommandLineOption(arg));
usage();
return null;
}
} catch (IndexOutOfBoundsException e) {
System.err.println(ServerMessages.MESSAGES.valueExpectedForCommandLineOption(arg));
STDERR.println(ServerMessages.MESSAGES.valueExpectedForCommandLineOption(arg));
usage();
return null;
}
Expand Down Expand Up @@ -275,11 +279,11 @@ private static boolean processProperties(final String arg, final String urlSpec,
systemProperties.load(url.openConnection().getInputStream());
return true;
} catch (MalformedURLException e) {
System.err.println(ServerMessages.MESSAGES.malformedCommandLineURL(urlSpec, arg));
STDERR.println(ServerMessages.MESSAGES.malformedCommandLineURL(urlSpec, arg));
usage();
return false;
} catch (IOException e) {
System.err.println(ServerMessages.MESSAGES.unableToLoadProperties(url));
STDERR.println(ServerMessages.MESSAGES.unableToLoadProperties(url));
usage();
return false;
}
Expand Down Expand Up @@ -317,7 +321,7 @@ private static void processSecurityProperties(String secProperties, Properties s

int idx = token.indexOf('=');
if (idx == token.length() - 1) {
System.err.println(ServerMessages.MESSAGES.valueExpectedForCommandLineOption(secProperties));
STDERR.println(ServerMessages.MESSAGES.valueExpectedForCommandLineOption(secProperties));
usage();
return;
}
Expand Down