📄 agentserver.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): Dyade * Contributor(s): ScalAgent Distributed Technologies */package fr.dyade.aaa.agent;import java.io.*;import java.util.*;import org.objectweb.util.monolog.api.BasicLevel;import org.objectweb.util.monolog.api.Logger;import org.objectweb.util.monolog.api.LoggerFactory;import fr.dyade.aaa.util.*;import fr.dyade.aaa.agent.conf.*;import fr.dyade.aaa.util.management.MXWrapper;/** * The <code>AgentServer</code> class manages the global configuration * of an agent server. It reads the configuration file, then it creates * and configure {@link Engine <code>Engine</code>}, * {@link Channel <code>Channel</code>}, * {@link Network <code>Network</code>s}. * This class contains the main method for AgentServer, for example to * acivate a server you have to run this class with two parameters: the * server id. and the path of the root of persistancy. You can also use * a specialized main calling methods initi and start. * <p><hr> * To start the agents server an XML configuration file describing the * architecture of the agent platform is needed. By default, this file is * the a3servers.xml file and it should be located inside the running * directory where the server is launched. Each server must use the same * configuration file.<p> * The configuration file contains a <code>config</code> element, that * is essentially made up of <code>domain</code>s elements, and servers * (<code>server</code>s elements): * <ul> * <li>Each domain of the configuration is described by an XML element with * attributes giving the name and the classname for <code>Network</code> * implementation (class {@link SimpleNetwork <code>SimpleNetwork</code>} * by default). * <li>Each server is described by an XML element with attributes giving * the id, the name (optional) and the node (the <code>hostname</code> * attribute describes the name or the IP address of this node) * <ul> * <li>Each persistent server must be part of a domain (at least one), to * do this you need to define a <code>network</code> element with attributes * giving the domain's name (<code>domain</code> attribute) and the * communication port (<code>port</code> attribute). * <li>A service can be declared on any of these servers by inserting a * <code>service</code> element describing it. * </ul> * <li>Additonnaly, you can define property for the global configuration * or for a particular server, it depends where you define it: in the * <code>config</code> element or in a server one. * </ul> * Each server that is part of two domains is named a "router", be careful, * it should have only one route between two domains. If it is not true the * configuration failed. * <p><hr> * A simple example of a3servers.xml follows: * <p><blockquote><pre> * <?xml version="1.0"?> * <!DOCTYPE config SYSTEM "a3config.dtd"> * * <config> * <domain name="D1"/> * <domain name="D2" class="fr.dyade.aaa.agent.PoolCnxNetwork"/> * * <property name="D2.nbMaxCnx" value="1"\> * * <server id="0" name="S0" hostname="acores"> * <network domain="D1" port="16300"/> * <service class=\"fr.dyade.aaa.ns.NameService\" args=\"\"/> * <property name="A3DEBUG_PROXY" value="true"\> * </server> * * <server id="2" name="S2" hostname="bermudes"> * <network domain="D1" port="16310"/> * <network domain="D2" port="16312"/> * </server> * * <server id="3" name="S3" hostname="baleares"> * <network domain="D2" port="16320"/> * </server> * </config> * </pre></blockquote> * <p> * This file described a 2 domains configuration D1 and D2, D1 with default * network protocol and D2 with the <code>PoolCnxNetwork</code> one, and 4 * servers: * <ul> * <li>The first server (id 0 and name "S0") is hosted by acores, it is * in domain D1, and listen on port 16300. It defines a service and a * property. * <li>The second server (id 2) is hosted by bermudes and it is the router * between D1 and D2. * <li>The last server is a persistent one, it is hosted on baleares and it * runs in domain D2. * </ul> * At the beginning of the file, there is a global property that defines * the maximum number of connection handled by each server of domain D2. * <hr> * @see Engine * @see Channel * @see Network * @see MessageQueue * @see fr.dyade.aaa.util.Transaction */public final class AgentServer { public final static short NULL_ID = -1; public final static String ADMIN_DOMAIN = "D0"; public final static String ADMIN_SERVER = "s0"; private static short serverId = NULL_ID; private static Logger logmon = null; public final static String CFG_DIR_PROPERTY = "fr.dyade.aaa.agent.A3CONF_DIR"; public final static String DEFAULT_CFG_DIR = null; public final static String CFG_FILE_PROPERTY = "fr.dyade.aaa.agent.A3CONF_FILE"; public final static String DEFAULT_CFG_FILE = "a3servers.xml"; public final static String DEFAULT_SER_CFG_FILE = "a3cmlconfig"; public final static String CFG_NAME_PROPERTY = "fr.dyade.aaa.agent.A3CONF_NAME"; public final static String DEFAULT_CFG_NAME = "default"; public final static String A3CMLWRP_PROPERTY = "fr.dyade.aaa.agent.A3CMLWrapper"; public final static String DEFAULT_A3CMLWRP = "fr.dyade.aaa.agent.conf.A3CMLSaxWrapper"; static ThreadGroup tgroup = null; public static ThreadGroup getThreadGroup() { return tgroup; } /** * Static reference to the engine. Used in <code>Channel.sendTo</code> to * know if the method is called from a react or no. * <p><hr> * AF: I think we must supress this dependency in order to be able to run * multiples engine. */ static Engine engine = null; /** * Returns the agent server engine. */ public static Engine getEngine() { return engine; } /** Static reference to the transactional monitor. */ private static Transaction transaction = null; /** * Returns the agent server transaction context. */ public static Transaction getTransaction() { return transaction; } private static JGroups jgroups = null; private static short clusterId = NULL_ID; private static ConfigController configController; public static ConfigController getConfigController() { return configController; } /** * Static references to all messages consumers initialized in this * agent server (including <code>Engine</code>). */ private static Hashtable consumers = null; static void addConsumer(String domain, MessageConsumer cons) throws Exception { if (consumers.containsKey(domain)) throw new Exception("Consumer for domain " + domain + " already exist"); consumers.put(domain, cons); try { MXWrapper.registerMBean(cons, "AgentServer", "server=" + getName() + ",cons=" + cons.getName()); } catch (Exception exc) { logmon.log(BasicLevel.ERROR, getName() + " jmx failed", exc); } } static Enumeration getConsumers() { if (consumers == null) return null; else return consumers.elements(); } static MessageConsumer getConsumer(String domain) throws Exception { if (! consumers.containsKey(domain)) throw new Exception("Unknown consumer for domain " + domain); return (MessageConsumer) consumers.get(domain); } static void removeConsumer(String domain) { MessageConsumer cons = (MessageConsumer) consumers.remove(domain); if (cons != null) cons.stop(); try { MXWrapper.unregisterMBean("AgentServer", "server=" + getName() + ",cons=" + cons.getName()); } catch (Exception exc) { logmon.log(BasicLevel.ERROR, getName() + " jmx failed", exc); } } /** Static reference to the configuration. */ private static A3CMLConfig a3config = null; /** * Set the agent server configuration. Be careful, this method cannot * be called after initialization. * * @param a3config A3CMLConfig * @exception Exception Server is already initialized. */ public final static void setConfig(A3CMLConfig a3config) throws Exception { setConfig(a3config, false); } final static void setConfig(A3CMLConfig a3config, boolean force) throws Exception { if (! force) { synchronized(status) { if (status.value != Status.INSTALLED) throw new Exception("cannot set config, bad status: " + status.value); } } AgentServer.a3config = a3config; } /** * Returns the agent server configuration. * * @return agent server configuration (A3CMLConfig) */ public final static A3CMLConfig getConfig() throws Exception { if (a3config == null) throw new Exception("Server not configured"); return a3config; } /** * Gets configuration of agent servers for a domain from the current * A3CMLConfig object. This method fills the object graph configuration * in a <code>A3CMLConfig</code> object. * * @param domains list of domain's names * @return a <code>A3CMLConfig</code> object. */ public static A3CMLConfig getAppConfig(String[] domains) throws Exception { return getConfig().getDomainConfig(domains); } public final static short getServerId() { return serverId; } public final static short getClusterId() { return clusterId; } private static String name = null; public final static String getName() { return name; } /** * Returns the identifier of the agent server which name is specified. * * @param name the name of the agent server * @return the identifier of the agent server * @exception Exception if the server name is unknown. */ public static short getServerIdByName(String name) throws Exception { return getConfig().getServerIdByName(name); } /** * Searches for the property with the specified key in the server property * list. * * @param key the hashtable key. * @return the value with the specified key value. */ public static String getProperty(String key) { return System.getProperty(key); } /** * Searches for the property with the specified key in the server property * list. * * @param key the hashtable key. * @param value a default value. * @return the value with the specified key value. */ public static String getProperty(String key, String value) { return System.getProperty(key, value); } /** * Determines the integer value of the server property with the * specified name. * * @param key property name. * @return the Integer value of the property. */ public static Integer getInteger(String key) { try { return Integer.getInteger(key); } catch (Exception exc) { return null; } } /** * Determines the integer value of the server property with the * specified name. * * @param key property name. * @param value a default value. * @return the Integer value of the property. */ public static Integer getInteger(String key, int value) { try { return Integer.getInteger(key, value); } catch (Exception exc) { return null; } } /** Static description of all known agent servers in ascending order. */ private static ServersHT servers = null; static void addServerDesc(ServerDesc desc) throws Exception { if (desc == null) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -