gov.llnl.lc.smt.command.console.SmtConsole.java Source code

Java tutorial

Introduction

Here is the source code for gov.llnl.lc.smt.command.console.SmtConsole.java

Source

/************************************************************
 * Copyright (c) 2015, Lawrence Livermore National Security, LLC.
 * Produced at the Lawrence Livermore National Laboratory.
 * Written by Timothy Meier, meier3@llnl.gov, All rights reserved.
 * LLNL-CODE-673346
 *
 * This file is part of the OpenSM Monitoring Service (OMS) package.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (as published by
 * the Free Software Foundation) version 2.1 dated February 1999.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * OUR NOTICE AND TERMS AND CONDITIONS OF THE GNU GENERAL PUBLIC LICENSE
 *
 * Our Preamble Notice
 *
 * A. This notice is required to be provided under our contract with the U.S.
 * Department of Energy (DOE). This work was produced at the Lawrence Livermore
 * National Laboratory under Contract No.  DE-AC52-07NA27344 with the DOE.
 *
 * B. Neither the United States Government nor Lawrence Livermore National
 * Security, LLC nor any of their employees, makes any warranty, express or
 * implied, or assumes any liability or responsibility for the accuracy,
 * completeness, or usefulness of any information, apparatus, product, or
 * process disclosed, or represents that its use would not infringe privately-
 * owned rights.
 *
 * C. Also, reference herein to any specific commercial products, process, or
 * services by trade name, trademark, manufacturer or otherwise does not
 * necessarily constitute or imply its endorsement, recommendation, or favoring
 * by the United States Government or Lawrence Livermore National Security,
 * LLC. The views and opinions of authors expressed herein do not necessarily
 * state or reflect those of the United States Government or Lawrence Livermore
 * National Security, LLC, and shall not be used for advertising or product
 * endorsement purposes.
 *
 *        file: SmtConsole.java
 *
 *  Created on: Mar 20, 2013
 *      Author: meier3
 ********************************************************************/
package gov.llnl.lc.smt.command.console;

import java.util.Map;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;

import gov.llnl.lc.smt.command.SmtCommand;
import gov.llnl.lc.smt.command.config.SmtConfig;
import gov.llnl.lc.smt.props.SmtProperty;

/**********************************************************************
 * The SmtCommand version of an SmtConsoleManager.  Its primary purpose
 * is to handle the various modes of operation of the SmtConsole through
 * the use of the standard configuration methods.
 * <p>
 * @see  SmtConsoleManager
 *
 * @author meier3
 * 
 * @version Mar 20, 2013 12:08:04 PM
 **********************************************************************/
public class SmtConsole extends SmtCommand {

    /************************************************************
     * Method Name:
     *  parseCommands
     **/
    /**
     * Describe the method here
     *
     * @see gov.llnl.lc.smt.command.SmtCommandInterface#parseCommands(java.util.Map, org.apache.commons.cli.CommandLine)
     *
     * @param config
     * @param line
     * @return
     ***********************************************************/

    @Override
    public boolean parseCommands(Map<String, String> config, CommandLine line) {
        // set the command and sub-command
        config.put(SmtProperty.SMT_COMMAND.getName(), this.getClass().getName());

        // parse (only) the command specific options
        SmtProperty sp = SmtProperty.SMT_REUSE;
        if (line.hasOption(sp.getName())) {
            config.put(sp.getName(), line.getOptionValue(sp.getName()));
        }

        sp = SmtProperty.SMT_UPDATE_PERIOD;
        if (line.hasOption(sp.getName())) {
            config.put(sp.getName(), line.getOptionValue(sp.getName()));
        }

        sp = SmtProperty.SMT_UPDATE_MULTIPLIER;
        if (line.hasOption(sp.getName())) {
            config.put(sp.getName(), line.getOptionValue(sp.getName()));
        }

        sp = SmtProperty.SMT_WRAP_DATA;
        if (line.hasOption(sp.getName())) {
            config.put(sp.getName(), line.getOptionValue(sp.getName()));
        }

        sp = SmtProperty.SMT_PLAY_CONTROL;
        if (line.hasOption(sp.getName())) {
            config.put(sp.getName(), "true");
        }

        sp = SmtProperty.SMT_FILTER_FILE;
        if (line.hasOption(sp.getName())) {
            config.put(sp.getName(), line.getOptionValue(sp.getName()));
        }

        sp = SmtProperty.SMT_READ_OMS_HISTORY;
        if (line.hasOption(sp.getName())) {
            // save this, only if its a valid file
            boolean status = putHistoryProperty(config, line.getOptionValue(sp.getName()));
            config.put(SmtProperty.SMT_SUBCOMMAND.getName(), sp.getName());

            if (status)
                config.put(SmtProperty.SMT_FILE_NAME.getName(),
                        convertSpecialFileName(line.getOptionValue(sp.getName())));
        }

        return true;
    }

    /************************************************************
     * Method Name:
     *  doCommand
     **/
    /**
     * Describe the method here
     *
     * @see gov.llnl.lc.smt.command.SmtCommand#doCommand(gov.llnl.lc.smt.command.config.SmtConfig)
     *
     * @param config
     * @return
     * @throws Exception
     ***********************************************************/

    @Override
    public boolean doCommand(SmtConfig config) throws Exception {
        // this is the console command, and ultimately we want to be able
        // to operate off an instance of the the service (OFFLINE) or
        // dynamically with changing data

        // ALSO:  a mode where a connection is established and used or
        // reused until done.  As opposed to a new connection each time
        // new data is desired or refreshed.  Both have pros/cons, so
        // provide both.

        initServiceUpdater(config);

        // check for white or black list, and init
        initFilter(smtConfig.getConfigMap());

        SmtConsoleManager mgr = SmtConsoleManager.getInstance();

        try {
            // give the manager the updater, so it can initialize itself, then done!
            mgr.initManager(UpdateService, SmtScreenType.SCN_CONTENTS, this);
        } catch (Exception e) {
            logger.severe("Could not initialize the SmtConsoleManager with the update service");
            e.printStackTrace();
        }

        return true;
    }

    /************************************************************
     * Method Name:
     *  init
     **/
    /**
     * Describe the method here
     *
     * @see gov.llnl.lc.smt.command.SmtCommand#init()
     *
     * @return
     ***********************************************************/

    @Override
    public boolean init() {
        USAGE = "[-h=<host url>] [-pn=<port num>]";
        HEADER = "smt-console - An interactive console (curses application)";

        // create and initialize the common options for this command
        initCommonOptions();

        // initialize the command specific options
        SmtProperty sp = SmtProperty.SMT_REUSE;
        Option re_use = OptionBuilder.hasArg(true).hasArgs(1).withArgName(sp.getArgName()).withValueSeparator('=')
                .withDescription(sp.getDescription()).withLongOpt(sp.getName()).create(sp.getShortName());

        sp = SmtProperty.SMT_UPDATE_PERIOD;
        Option uPeriod = OptionBuilder.hasArg(true).hasArgs(1).withArgName(sp.getArgName()).withValueSeparator('=')
                .withDescription(sp.getDescription()).withLongOpt(sp.getName()).create(sp.getShortName());

        sp = SmtProperty.SMT_UPDATE_MULTIPLIER;
        Option multi = OptionBuilder.hasArg(true).hasArgs(1).withArgName(sp.getArgName()).withValueSeparator('=')
                .withDescription(sp.getDescription()).withLongOpt(sp.getName()).create(sp.getShortName());

        sp = SmtProperty.SMT_WRAP_DATA;
        Option wrap = OptionBuilder.hasArg(true).hasArgs(1).withArgName(sp.getArgName()).withValueSeparator('=')
                .withDescription(sp.getDescription()).withLongOpt(sp.getName()).create(sp.getShortName());

        sp = SmtProperty.SMT_READ_OMS_HISTORY;
        Option rHist = OptionBuilder.hasArg(true).hasArgs(1).withArgName(sp.getArgName()).withValueSeparator('=')
                .withDescription(sp.getDescription()).withLongOpt(sp.getName()).create(sp.getShortName());

        sp = SmtProperty.SMT_PLAY_CONTROL;
        Option play = new Option(sp.getShortName(), sp.getName(), false, sp.getDescription());

        sp = SmtProperty.SMT_FILTER_FILE;
        Option fF = OptionBuilder.hasArg(true).hasArgs(1).withArgName(sp.getArgName()).withValueSeparator('=')
                .withDescription(sp.getDescription()).withLongOpt(sp.getName()).create(sp.getShortName());

        options.addOption(re_use);
        options.addOption(uPeriod);
        options.addOption(multi);
        options.addOption(wrap);
        options.addOption(rHist);
        options.addOption(fF);

        options.addOption(play);

        return true;
    }

    /************************************************************
     * Method Name:
     *  main
     **/
    /**
     * Describe the method here
     *
     * @see     describe related java objects
     *
     * @param args
     * @throws Exception 
     ***********************************************************/
    public static void main(String[] args) throws Exception {
        System.exit((new SmtConsole().execute(args)) ? 0 : -1);
    }

}