Package net.helipilot50.stocktrade.systemmonitor

Source Code of net.helipilot50.stocktrade.systemmonitor.NodeAgent

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package net.helipilot50.stocktrade.systemmonitor;

import java.io.IOException;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import net.helipilot50.stocktrade.framework.Array_Of_Object;
import net.helipilot50.stocktrade.framework.ErrorMgr;
import net.helipilot50.stocktrade.framework.FrameworkUtils;
import net.helipilot50.stocktrade.framework.LogMgr;
import net.helipilot50.stocktrade.framework.Stream;
import net.helipilot50.stocktrade.framework.TextData;
import net.helipilot50.stocktrade.framework.UsageException;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.jmx.export.naming.ObjectNamingStrategy;


/**
* The Node agent represents a Node Manager, which is responsible for providing access to and control over a single node within the environment.
* A JEE deployed application has no concept of a Forte node manager and this agent (MBean) is included to provide "Forte like" semantics and is not fully implemented.
*/
@SuppressWarnings("serial")
public class NodeAgent extends SystemAgent {

    public static final int MOD_LOGGER_REMOTE_INDEX = 1;
    public static final int MOD_LOG4J_REMOTE_INDEX = 2;
    public static final int EXEC_CMD_REMOTE_INDEX = 3;
    public static final int SET_ENV_REMOTE_INDEX = 4;

    public NodeAgent(ObjectNamingStrategy strategy) {
        super(strategy);
        try {
            ObjectName objectName  = SystemAgent.getStrategy().getObjectName(this, "NodeAgent_" + FrameworkUtils.getNodeName().toString());
            this.setObjectName(objectName);

        } catch (MalformedObjectNameException e) {
            UsageException errorVar = new UsageException("Cannot create NodeAgent", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (NullPointerException e) {
            UsageException errorVar = new UsageException("Cannot create NodeAgent", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
        this.setState(Constants.SM_ONLINE);

        this.setName(FrameworkUtils.getNodeName());

        int systemID = getLastIID();

        ConfigValueInst archName = new ConfigValueInst();
        archName.getName().setValue("Architecture");
        archName.updateData(new TextData(System.getProperty("os.arch")));
        archName.setIsReadOnly(true);
        archName.setInstrumentID(systemID + 1);
        this.addInstrument(archName);

        ConfigValueInst modelNode = new ConfigValueInst();
        modelNode.getName().setValue("ModelNode");
        modelNode.updateData(new TextData("false"));
        modelNode.setIsReadOnly(true);
        modelNode.setInstrumentID(systemID + 2);
        this.addInstrument(modelNode);


    }

    @Override
    public void initCmdProcessor() {
        super.initCmdProcessor();
        int systemIndex = CommandIndexValues.AGENT_CMD_LASTVALUE;
        this.getCommandProcessor().addCommand("ModLogggerRemote",
                    "s",
                    (short)systemIndex + MOD_LOGGER_REMOTE_INDEX,
                    "Syntax: ModLoggerRemote +(trc:lo:25) where + or - toggles the flag",
                    1,
                    "flags",
                    "",
                    "Mod logger");
        this.getCommandProcessor().addCommand("ModLog4JRemote",
                "s,s",
                (short)systemIndex + MOD_LOG4J_REMOTE_INDEX,
                "Syntax: ModLog4JRemote Logger=org.springframework Level=DEBUG|INFO|ERROR|WARN",
                2,
                "Logger,Level",
                "",
                "Mod log4J");

        this.getCommandProcessor().addCommand("ExecCmdRemote",
                "s",
                (short)systemIndex + EXEC_CMD_REMOTE_INDEX,
                "Syntax: ExecCmdRemote opsys_command ",
                1,
                "opsys_command",
                "",
                "Utility");
//        this.getCommandProcessor().addCommand("SetEnvRemote",
//                "s,s",
//                (short)systemIndex + SET_ENV_REMOTE_INDEX,
//                "Syntax: SetEnvRemote env_variable new_value",
//                2,
//                "env_variable,new_value",
//                "",
//                "Utility");
    }

    @Override
    public Object processCmdRequest(short cmdIndex, Array_Of_Object<Object> parameters,
            Stream outStream) {
            int systemIndex = CommandIndexValues.AGENT_CMD_LASTVALUE;
            int index = cmdIndex - systemIndex;
            switch (index){
            case MOD_LOGGER_REMOTE_INDEX:{
                TextData flags = (TextData)parameters.get(0);
                modLoggerRemote(flags.toString());
                return null;
            }
            case MOD_LOG4J_REMOTE_INDEX:{
              TextData logger = (TextData)parameters.get(0);
                TextData level = (TextData)parameters.get(1);
                modLog4JRemote(logger.toString(), level.toString());
                return null;
            }
            case EXEC_CMD_REMOTE_INDEX:{
                try {
                  TextData command = (TextData)parameters.get(0);
                    Runtime.getRuntime().exec(command.toString());
                    return null;
                } catch (IOException e) {
                    return e.getMessage();
                }
            }
                default:
                    return super.processCmdRequest(cmdIndex, parameters, outStream);   
            }

    }
    private void modLoggerRemote(String flags){
        LogMgr.getInstance().modifyFlags(flags);
    }
    private void modLog4JRemote(String logger, String level){
        /*
         * e.g.
         * bob=DEBUG
         * mary=INFO
         *
         */
        Logger.getLogger(logger).setLevel(Level.toLevel(level));
    }
    private int lastIID = initialIID;
  @Override
  public int getLastIID() {
    return lastIID;
  }

  @Override
  public void setLastIID(int id) {
    this.lastIID = id;
  }

  @Override
  public String getDescription() {
    return "Information pertaining to the node on which this application is running, such as the node name";
  }
}
TOP

Related Classes of net.helipilot50.stocktrade.systemmonitor.NodeAgent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.