📄 configcontroller.java
字号:
/* * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies * Copyright (C) 2004 France Telecom R&D * Copyright (C) 1996 - 2000 BULL * Copyright (C) 1996 - 2000 INRIA * * 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 * Contributor(s): */package fr.dyade.aaa.agent;import java.util.*;import java.io.*;import fr.dyade.aaa.util.Transaction;import fr.dyade.aaa.agent.conf.*;import org.objectweb.util.monolog.api.BasicLevel;import org.objectweb.util.monolog.api.Logger;public class ConfigController { public final static String ADMIN_SERVER = "fr.dyade.aaa.agent.ADMIN_SERVER"; public final static String SERVER_COUNTER = "serverCounter"; private static Logger logger = Debug.getLogger( "fr.dyade.aaa.agent.ConfigController"); public static class Status { public static final int FREE = 0; public static final int CONFIG = 1; public static String[] names = { "FREE", "CONFIG"}; public static String toString(int status) { return names[status]; } } private short serverCounter; private A3CMLConfig currentA3cmlConfig; private A3CMLConfig a3cmlConfig; private int status; private Vector newServers; private Vector stopScript; private Vector startScript; ConfigController() throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.<init>()"); // DF: must be improved. The admin server id can be // higher than zero. if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, " -> AgentServer.getServerId() = " + AgentServer.getServerId()); if (AgentServer.getServerId() == 0) { Transaction transaction = AgentServer.getTransaction(); Short counter = (Short)transaction.load(SERVER_COUNTER); if (counter != null) { serverCounter = counter.shortValue(); } else { serverCounter = 1; } } else { serverCounter = -1; } if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, " -> serverCounter = " + serverCounter); setStatus(Status.FREE); } private void setStatus(int status) { this.status = status; } public synchronized void beginConfig() throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.beginConfig()"); while (status != Status.FREE) { try { wait(); } catch (InterruptedException exc) { } } currentA3cmlConfig = AgentServer.getConfig(); // Copy the configuration in order to enable rollback. // Use serialization. The rollback could also // be done with an "undo script". ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(currentA3cmlConfig); oos.flush(); oos.close(); byte[] bytes = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); a3cmlConfig = (A3CMLConfig)ois.readObject(); newServers = new Vector(); startScript = new Vector(); stopScript = new Vector(); setStatus(Status.CONFIG); } public synchronized void commitConfig() throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.commitConfig()"); checkStatus(Status.CONFIG); AgentServer.setConfig(a3cmlConfig, true); try { A3CMLServer root = a3cmlConfig.getServer(AgentServer.getServerId()); a3cmlConfig.configure(root); stop(); addNewServers(); start(); commit(); } catch (Exception exc) { if (logger.isLoggable(BasicLevel.ERROR)) logger.log(BasicLevel.ERROR, "", exc); rollback(); throw exc; } finally { setStatus(Status.FREE); notify(); } } private void commit() { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.commit()"); try { Transaction transaction = AgentServer.getTransaction(); transaction.begin(); a3cmlConfig.save(); transaction.save( new Short(serverCounter), SERVER_COUNTER); transaction.commit(); transaction.release(); } catch (Exception exc) { throw new Error(exc.toString()); } } private void rollback() throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.rollback()"); for (int i = 0; i < newServers.size(); i++) { ServerDesc sd = (ServerDesc)newServers.elementAt(i); AgentServer.removeServerDesc(sd.sid); } AgentServer.setConfig(currentA3cmlConfig, true); } public synchronized void release() { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.release()"); if (status == Status.CONFIG) { setStatus(Status.FREE); notify(); } } private void stop() throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.stop()"); for (int i = 0; i < stopScript.size(); i++) { Object cmd = stopScript.elementAt(i); if (cmd instanceof StopNetworkCmd) { exec((StopNetworkCmd)cmd); } } } private void exec(StopNetworkCmd cmd) throws Exception { stopNetwork(cmd.domainName); } private void addNewServers() throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.addNewServers()"); for (int i = 0; i < newServers.size(); i++) { ServerDesc sd = (ServerDesc)newServers.elementAt(i); AgentServer.addServerDesc(sd); } } private void start() throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.start()"); int i = 0; try { for (i = 0; i < startScript.size(); i++) { Object cmd = startScript.elementAt(i); if (cmd instanceof StartNetworkCmd) { exec((StartNetworkCmd)cmd); } else if (cmd instanceof StartServiceCmd) { exec((StartServiceCmd)cmd); } else if (cmd instanceof ReconfigureClientNetworkCmd) { exec((ReconfigureClientNetworkCmd)cmd); } else if (cmd instanceof ReconfigureServerNetworkCmd) { exec((ReconfigureServerNetworkCmd)cmd); } } } catch (Exception exc) { int size = i + 1; for (int j = 0; j < size; j++) { Object cmd = startScript.elementAt(j); if (cmd instanceof StartNetworkCmd) { rollback((StartNetworkCmd)cmd); } else if (cmd instanceof StartServiceCmd) { rollback((StartServiceCmd)cmd); } else if (cmd instanceof ReconfigureClientNetworkCmd) { rollback((ReconfigureClientNetworkCmd)cmd); } else if (cmd instanceof ReconfigureServerNetworkCmd) { rollback((ReconfigureServerNetworkCmd)cmd); } } throw exc; } // 'Start server' implies that the consumers // have been added (done by 'start network'). try { for (i = 0; i < startScript.size(); i++) { Object cmd = startScript.elementAt(i); if (cmd instanceof StartServerCmd) { exec((StartServerCmd)cmd); } } } catch (Exception exc) { int size = i + 1; for (int j = 0; j < size; j++) { Object cmd = startScript.elementAt(j); if (cmd instanceof StartServerCmd) { rollback((StartServerCmd)cmd); } } } } private void exec(StartServerCmd cmd) throws Exception { startServer(cmd.sid); } private void exec(StartNetworkCmd cmd) throws Exception { startNetwork(cmd.domainName); } private void exec(StartServiceCmd cmd) throws Exception { startService(cmd.serviceClassName, cmd.args); } private void exec(ReconfigureClientNetworkCmd cmd) throws Exception { reconfigureClientNetwork(cmd.sid, cmd.domainName, cmd.port); } private void exec(ReconfigureServerNetworkCmd cmd) throws Exception { reconfigureServerNetwork(cmd.domainName, cmd.port); } private void rollback(StartServerCmd cmd) throws Exception { ServerDesc servDesc = AgentServer.getServerDesc(cmd.sid); deleteServer(servDesc); } private void rollback(StartNetworkCmd cmd) throws Exception { stopNetwork(cmd.domainName); } private void rollback(StartServiceCmd cmd) throws Exception { stopService(cmd.serviceClassName); } private void rollback(ReconfigureClientNetworkCmd cmd) throws Exception { // Do nothing } private void rollback(ReconfigureServerNetworkCmd cmd) throws Exception { // Do nothing } private synchronized void checkStatus(int expectedStatus) throws Exception { if (status != expectedStatus) { throw new Exception("Illegal status: " + Status.toString(status) + " expected: " + Status.toString(expectedStatus)); } } public void addDomain(String name, String className) throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.addDomain(" + name + ',' + className + ')'); checkStatus(Status.CONFIG); try { a3cmlConfig.addDomain(new A3CMLDomain(name, className)); } catch (Exception exc) { // idempotent } } public int addServer(String name, String hostName, short id) throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.addServer(" + name + ',' + hostName + ',' + id + ')'); checkStatus(Status.CONFIG); if (! a3cmlConfig.containsServer(name)) { if (id < 0) { if (serverCounter > -1) { id = serverCounter++; } else { throw new Exception("Missing server id"); } } a3cmlConfig.addServer(new A3CMLServer(id, name, hostName)); ServerDesc serverDesc = new ServerDesc(id, name, hostName, -1); serverDesc.gateway = id; newServers.addElement(serverDesc); } else { // idempotent } if (id != AgentServer.getServerId()) { startScript.addElement( new StartServerCmd(id)); } return id; } public void addService(String serverName, String serviceClassName, String args) throws Exception { if (logger.isLoggable(BasicLevel.DEBUG)) logger.log(BasicLevel.DEBUG, "ConfigController.addService(" + serverName + ',' + serviceClassName + ',' + args + ')'); checkStatus(Status.CONFIG); A3CMLServer server = a3cmlConfig.getServer(serverName); A3CMLService newService = new A3CMLService( serviceClassName, args); try { server.addService(newService); } catch (Exception exc) { // Idempotent } short sid = a3cmlConfig.getServerIdByName(serverName); if (sid == AgentServer.getServerId()) { startScript.addElement( new StartServiceCmd(serviceClassName, args)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -