📄 agentadmin.java
字号:
/* * Copyright (C) 2002 - 2004 ScalAgent Distributed Technologies * Copyright (C) 2004 - France Telecom R&D * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. * * Initial developer(s): ScalAgent Distributed Technologies */package fr.dyade.aaa.agent;import java.util.*;import java.lang.*;import org.objectweb.util.monolog.api.BasicLevel;import org.objectweb.util.monolog.api.Logger;import fr.dyade.aaa.admin.cmd.*;import fr.dyade.aaa.admin.script.*;import fr.dyade.aaa.agent.conf.*;final public class AgentAdmin extends Agent { static Logger logmon; // current operation public static final int NONE = 0; public static final int CONFIGURED = 1; public static final int STOPED = 2; public static final int STARTED = 3; static String[] statusName = {"NONE", "CONFIGURED", "STOPED", "STARTED"}; /** silence use for idempotence */ private boolean silence = false; /** configuration */ private transient A3CMLConfig a3cmlConfig = null; /** start script */ private StartScript startScript = null; /** stop script */ private StopScript stopScript = null; /** script use to rollback a configuration */ private Script rollback = null; /** server id counter */ private short maxId = 0; /** get default AgentId of AgentAdmin */ public static AgentId getDefault(short serverId) { return new AgentId(serverId, serverId, AgentId.AdminIdStamp); } /** get AgentId of AgentServer */ public static AgentId getDefault() { return getDefault(AgentServer.getServerId()); } /** * Initializes the package as a well known service. * <p> * Creates a <code>AgentAdmin</code> agent with the well known stamp * <code>AgentId.AdminIdStamp</code>. * * @param args parameters from the configuration file * @param firstTime <code>true</code> when agent server starts anew * * @exception Exception * unspecialized exception */ public static void init(String args, boolean firstTime) throws Exception { // Get the logging monitor from current server MonologMonitorFactory logmon = Debug.getLogger("fr.dyade.aaa.agent.Admin"); if (logmon.isLoggable(BasicLevel.DEBUG)) logmon.log(BasicLevel.DEBUG, "AgentAdmin.init(" + args + ", " + firstTime + ")"); if (! firstTime) return; short maxId; if (args == null) { maxId = -1; } else { try { maxId = Short.parseShort(args); } catch (Exception exc) { if (logmon.isLoggable(BasicLevel.WARN)) logmon.log(BasicLevel.WARN, "", exc); maxId = -1; } } AgentAdmin agentAdmin = new AgentAdmin(maxId); agentAdmin.deploy(); } public static void stopService() { // Does nothing } protected void agentInitialize(boolean firstTime) throws Exception { super.agentInitialize(firstTime); } /** * Creates a local administration agent. */ public AgentAdmin(short maxId) { super("AgentAdmin#" + AgentServer.getServerId(), true, AgentId.adminId); this.maxId = maxId; } /** * Reacts to <code>AgentAdmin</code> specific notifications. * Analyzes the notification request code, then do the appropriate * work. By default calls <code>react</code> from base class. * Handled notification types are : * <code>AdminRequest</code>, * <code>AdminStartStopNot</code> * * @param from agent sending notification * @param not notification to react to * * @exception Exception * unspecialized exception */ public void react(AgentId from, Notification not) throws Exception { if (logmon.isLoggable(BasicLevel.DEBUG)) logmon.log(BasicLevel.DEBUG, "\n\n\nAgentAdmin.react(" + from + "," + not + ")"); if (not instanceof AdminRequestNot) { doReact(from, (AdminRequestNot) not); } else if (not instanceof AdminStartStopNot) { doReact(from, (AdminStartStopNot) not); } else { super.react(from, not); } } /** * doReact to <code>AdminRequestNot</code> notifications. * * @param from agent sending AdminRequestNot * @param not AdminRequestNot notification * * @exception Exception */ private void doReact(AgentId from, AdminRequestNot not) throws Exception { if (logmon.isLoggable(BasicLevel.DEBUG)) logmon.log(BasicLevel.DEBUG, "AgentAdmin.doReact(AdminRequestNot)"); startScript = new StartScript(); stopScript = new StopScript(); rollback = new Script(); silence = not.silence; try { // configuration phase doReact(not.script); } catch (Exception exc) { logmon.log(BasicLevel.WARN, "AgentAdmin.react", exc); if (exc instanceof ExceptionCmd) { // send exception to agent sending AdminRequestNot. AdminReplyNot reply = new AdminReplyNot((ExceptionCmd) exc); reply.setContext(not.getContext()); reply.status = NONE; sendTo(from, reply); // remove all i do (rollback) doReact(rollback); startScript = null; stopScript = null; rollback = null; } else throw exc; } if (not.autoStart) { // start/stop configuration in same reaction AdminStartStopNot startstop = new AdminStartStopNot(); startstop.setContext(not.getContext()); startstop.startScript = startScript; startstop.stopScript = stopScript; doReact(from, startstop); } else { // reply to agent sending AdminRequestNot. // set in AdminReplyNot startScript and stopScript. // use to start this configuration. AdminReplyNot reply = new AdminReplyNot(); reply.setContext(not.getContext()); reply.startScript = startScript; reply.stopScript = stopScript; reply.status = CONFIGURED; sendTo(from, reply); } } /** * doReact to <code>AdminStartStopNot</code> notifications. * * @param from agent sending AdminStartStopNot * @param not AdminStartStopNot notification * * @exception Exception */ private void doReact(AgentId from, AdminStartStopNot not) throws Exception { if (logmon.isLoggable(BasicLevel.DEBUG)) logmon.log(BasicLevel.DEBUG, "AgentAdmin.doReact(AdminStartStopNot)\n startScript = " + startScript); AdminAckStartStopNot ack = new AdminAckStartStopNot(); ack.setContext(not.getContext()); ack.status = CONFIGURED; try { startScript = not.startScript; stopScript = not.stopScript; // first execute stop script if (stopScript != null) { stop(); ack.status = STOPED; } // second execute start script if (startScript != null) { start(); ack.status = STARTED; } // startScript should be null ack.startScript = startScript; // stopScript contains the complementary // of initial startScript. ack.stopScript = stopScript; sendTo(from, ack); } catch (Exception exc) { logmon.log(BasicLevel.WARN, "AgentAdmin.doReact(AdminStartStopNot) Exception : " + exc); if (exc instanceof ExceptionCmd) { ack.exc = (ExceptionCmd) exc; // startScript should not be empty // depend where exception is catch ack.startScript = startScript; ack.stopScript = stopScript; sendTo(from, ack); } else throw exc; } } /** execute start script */ private void start() throws Exception { if (logmon.isLoggable(BasicLevel.DEBUG)) logmon.log(BasicLevel.DEBUG, "AgentAdmin.start()\nstartScript = " + startScript); if (startScript == null) return; Vector toRemove = new Vector(); try { // add serverDesc for (Enumeration e = startScript.serverDesc.elements(); e.hasMoreElements();) AgentServer.addServerDesc((ServerDesc) e.nextElement()); // start network, server and service. for (Enumeration e = startScript.elements(); e.hasMoreElements();) { StartAdminCmd cmd = (StartAdminCmd) e.nextElement();// if (cmd instanceof UpdateNetworkPortCmd) {// doReact((UpdateNetworkPortCmd) cmd);// } else if (cmd instanceof StartNetworkCmd) { doReact((StartNetworkCmd) cmd); } else if (cmd instanceof StartServiceCmd) { doReact((StartServiceCmd) cmd); } else if (cmd instanceof StartServerCmd) { doReact((StartServerCmd) cmd); } // add command done. toRemove.addElement(cmd); } // remove from startScript all done command. for (Enumeration e = toRemove.elements(); e.hasMoreElements();) { StartAdminCmd cmd = (StartAdminCmd) e.nextElement(); startScript.remove(cmd); } if (startScript.size() == 0) startScript = null; } catch (Exception exc) { // remove from startScript all done command. for (Enumeration e = toRemove.elements(); e.hasMoreElements();) { StartAdminCmd cmd = (StartAdminCmd) e.nextElement(); startScript.remove(cmd); } if (startScript.size() == 0) startScript = null; throw exc; } } /** execute stop script */ private void stop() throws Exception { if (logmon.isLoggable(BasicLevel.DEBUG)) logmon.log(BasicLevel.DEBUG, "AgentAdmin.stop()\nstopScript = " + stopScript); if (stopScript == null) return; Vector toRemove = new Vector(); try { for (Enumeration e = stopScript.elements(); e.hasMoreElements();) { StopAdminCmd cmd = (StopAdminCmd) e.nextElement(); if (cmd instanceof StopNetworkCmd) { doReact((StopNetworkCmd) cmd); } else if (cmd instanceof StopServiceCmd) { doReact((StopServiceCmd) cmd); } // add command done. toRemove.addElement(cmd); } // remove from stopScript all done command. for (Enumeration e = toRemove.elements(); e.hasMoreElements();) { StopAdminCmd cmd = (StopAdminCmd) e.nextElement(); stopScript.remove(cmd); } if (stopScript.size() == 0) stopScript = null; } catch (Exception exc) { // remove from stopScript all done command. for (Enumeration e = toRemove.elements(); e.hasMoreElements();) { StartAdminCmd cmd = (StartAdminCmd) e.nextElement(); startScript.remove(cmd); } if (startScript.size() == 0) startScript = null; throw exc; } } /** * execute configuration script * * @param script Script * * @exception Exception */ private void doReact(Script script) throws Exception { if (logmon.isLoggable(BasicLevel.DEBUG)) logmon.log(BasicLevel.DEBUG, "AgentAdmin.doReact(" + script + ")"); if (script.newConfig) { a3cmlConfig = new A3CMLConfig(); } else { // keep AgentServer configuration a3cmlConfig = AgentServer.getConfig(); } if (a3cmlConfig == null) { throw new ExceptionCmd("a3cmlConfig is null"); } for (Enumeration e = script.elements(); e.hasMoreElements();) { AdminCmd cmd = (AdminCmd) e.nextElement(); if (cmd instanceof NewDomainCmd) { doReact((NewDomainCmd) cmd); } else if (cmd instanceof NewServerCmd) { doReact((NewServerCmd) cmd); } else if (cmd instanceof NewServiceCmd) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -