Search in sources :

Example 1 with SupervisorMBean

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;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) TreeTable(org.apache.sis.util.collection.TreeTable) IOException(java.io.IOException) Date(java.util.Date) About(org.apache.sis.setup.About) ObjectName(javax.management.ObjectName) LogRecord(java.util.logging.LogRecord) JMXConnector(javax.management.remote.JMXConnector) SupervisorMBean(org.apache.sis.internal.system.SupervisorMBean) MBeanServerConnection(javax.management.MBeanServerConnection)

Aggregations

IOException (java.io.IOException)1 Date (java.util.Date)1 LogRecord (java.util.logging.LogRecord)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 ObjectName (javax.management.ObjectName)1 JMXConnector (javax.management.remote.JMXConnector)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1 SupervisorMBean (org.apache.sis.internal.system.SupervisorMBean)1 About (org.apache.sis.setup.About)1 TreeTable (org.apache.sis.util.collection.TreeTable)1