use of org.apache.sis.internal.system.SupervisorMBean in project sis by apache.
the class AboutCommand method run.
/**
* Prints the information to the output stream.
*
* @return 0 on success, or an exit code if the command failed for a reason other than an uncaught Java exception.
* @throws Exception if an error occurred while executing the sub-command.
*/
@Override
public int run() throws Exception {
DataDirectory.quiet();
/*
* Check the number of arguments, which can be 0 or 1. If present,
* the argument is the name and port number of a remote machine.
*
* In the current implementation, the --brief option is supported only on the local machine.
*/
final boolean brief = options.containsKey(Option.BRIEF);
if (hasUnexpectedFileCount(0, brief ? 0 : 1)) {
return Command.INVALID_ARGUMENT_EXIT_CODE;
}
String[] warnings = null;
final String configuration;
if (brief && files.isEmpty()) {
configuration = Vocabulary.getResources(locale).getString(Vocabulary.Keys.Version_2, "Apache SIS", Version.SIS);
} else {
final EnumSet<About> sections = EnumSet.allOf(About.class);
if (!options.containsKey(Option.VERBOSE)) {
sections.remove(About.LIBRARIES);
}
if (files.isEmpty()) {
/*
* Provide information about the local SIS installation.
*/
configuration = About.configuration(sections, locale, timezone).toString();
} else {
/*
* Provide information about a remote SIS installation. Those information are accessible
* only if explicitely enabled at JVM startup time.
*
* Tutorial: http://docs.oracle.com/javase/tutorial/jmx/remote/custom.html
*/
final String address = files.get(0);
final String path = toRemoteURL(address);
final long time = System.nanoTime();
final TreeTable table;
try {
final JMXServiceURL url = new JMXServiceURL(path);
try (JMXConnector jmxc = JMXConnectorFactory.connect(url)) {
final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
final SupervisorMBean bean = JMX.newMBeanProxy(mbsc, new ObjectName(Supervisor.NAME), SupervisorMBean.class);
table = bean.configuration(sections, locale, timezone);
warnings = bean.warnings(locale);
}
} catch (IOException e) {
error(Errors.format(Errors.Keys.CanNotConnectTo_1, path), e);
return Command.IO_EXCEPTION_EXIT_CODE;
}
/*
* Logs a message telling how long it took to receive the reply.
* Sometime the delay gives a hint about the server charge.
*/
// In seconds.
double delay = (System.nanoTime() - time) / (double) StandardDateFormat.NANOS_PER_SECOND;
if (delay >= 0.1) {
final double scale = (delay >= 10) ? 1 : (delay >= 1) ? 10 : 100;
delay = Math.rint(delay * scale) / scale;
}
final LogRecord record = Messages.getResources(locale).getLogRecord(Level.INFO, Messages.Keys.ConfigurationOf_3, address, new Date(), delay);
record.setLoggerName(Loggers.APPLICATION);
Logging.log(Command.class, "main", record);
/*
* Replace the root node label from "Local configuration" to "Remote configuration"
* before to get the string representation of the configuration as a tree-table.
*/
table.getRoot().setValue(TableColumn.NAME, Vocabulary.getResources(locale).getString(Vocabulary.Keys.RemoteConfiguration));
configuration = table.toString();
}
}
out.println(configuration);
if (warnings != null) {
out.println();
if (colors) {
out.print(X364.BACKGROUND_RED.sequence());
out.print(X364.BOLD.sequence());
out.print(' ');
}
Vocabulary.getResources(locale).appendLabel(Vocabulary.Keys.Warnings, out);
if (colors) {
out.print(' ');
out.println(X364.RESET.sequence());
out.print(X364.FOREGROUND_RED.sequence());
} else {
out.println();
}
for (final String warning : warnings) {
out.println(warning);
}
if (colors) {
out.print(X364.FOREGROUND_DEFAULT.sequence());
}
}
out.flush();
return 0;
}
Aggregations