1 22 package fr.dyade.aaa.agent; 23 24 import java.util.*; 25 import java.io.*; 26 27 import fr.dyade.aaa.agent.conf.*; 28 29 import org.objectweb.util.monolog.api.BasicLevel; 30 import org.objectweb.util.monolog.api.Logger; 31 32 public class ServerConfigHelper { 33 34 private static Logger logger = Debug.getLogger( 35 "fr.dyade.aaa.agent.ServerConfigHelper"); 36 37 private boolean autoCommit; 38 39 public ServerConfigHelper(boolean autoCommit) { 40 this.autoCommit = autoCommit; 41 } 42 43 public boolean addDomain(String domainName, 44 int routerId, 45 int port) 46 throws Exception { 47 if (logger.isLoggable(BasicLevel.DEBUG)) 48 logger.log(BasicLevel.DEBUG, 49 "ServerConfigHelper.addDomain(" + 50 domainName + 51 ',' + routerId + ',' + 52 port + ')'); 53 54 A3CMLConfig a3cmlConfig = AgentServer.getConfig(); 56 if (a3cmlConfig.domains.get(domainName) != null) 57 throw new NameAlreadyUsedException( 58 "Domain name already used: " + domainName); 59 if (a3cmlConfig.servers.get(new Short ((short)routerId)) == null) 60 throw new Exception ("Server not found: " + routerId); 61 62 A3CMLDomain domain = new A3CMLDomain( 64 domainName, 65 fr.dyade.aaa.agent.SimpleNetwork.class.getName()); 66 a3cmlConfig.addDomain(domain); 67 A3CMLServer a3cmlServer = a3cmlConfig.getServer((short)routerId); 68 domain.addServer(a3cmlServer); 69 A3CMLNetwork a3cmlNetwork = new A3CMLNetwork(domainName, port); 70 a3cmlServer.addNetwork(a3cmlNetwork); 71 72 A3CMLServer root = a3cmlConfig.getServer( 73 AgentServer.getServerId()); 74 a3cmlConfig.configure(root); 75 76 boolean res = false; 77 if (routerId == AgentServer.getServerId()) { 78 Network network = 80 (Network) fr.dyade.aaa.agent.SimpleNetwork.class.newInstance(); 81 AgentServer.addConsumer(domainName, network); 82 83 try { 84 short[] sids = new short[1]; 85 sids[0] = (short)routerId; 86 network.init(domainName, 87 port, 88 sids); 89 network.start(); 90 } catch (Exception exc) { 91 if (logger.isLoggable(BasicLevel.ERROR)) 92 logger.log(BasicLevel.ERROR, "", exc); 93 AgentServer.removeConsumer(domainName); 95 96 a3cmlServer.removeNetwork(domainName); 97 a3cmlConfig.removeDomain(domainName); 98 a3cmlConfig.configure(root); 99 100 throw new StartFailureException(exc.getMessage()); 101 } 102 103 res = true; 104 } 105 106 if (autoCommit) commit(); 107 108 return res; 109 } 110 111 public boolean removeDomain(String domainName) 112 throws Exception { 113 if (logger.isLoggable(BasicLevel.DEBUG)) 114 logger.log(BasicLevel.DEBUG, 115 "ServerConfigHelper.removeDomain(" + 116 domainName + ')'); 117 118 A3CMLConfig a3cmlConfig = AgentServer.getConfig(); 119 A3CMLDomain domain = a3cmlConfig.getDomain(domainName); 120 121 if (logger.isLoggable(BasicLevel.DEBUG)) 122 logger.log(BasicLevel.DEBUG, 123 " -> domain.servers = " + domain.servers); 124 125 if (domain.servers.size() > 1) 130 throw new Exception ("Can't remove domain: it contains more than one server."); 131 132 A3CMLServer router = null; 133 if (domain.servers.size() == 1) { 134 router = (A3CMLServer) domain.servers.elementAt(0); 135 } 136 137 a3cmlConfig.removeDomain(domainName); 140 141 if (router != null) { 142 router.removeNetwork(domainName); 143 } 144 145 A3CMLServer root = a3cmlConfig.getServer(AgentServer.getServerId()); 146 a3cmlConfig.configure(root); 147 148 boolean res = false; 149 if (router != null && 150 router.sid == AgentServer.getServerId()) { 151 152 AgentServer.removeConsumer(domainName); 154 155 res = true; 156 } 157 158 if (autoCommit) commit(); 159 160 return res; 161 } 162 163 public void addServer(int sid, 164 String hostName, 165 String domainName, 166 int port, 167 String name) 168 throws Exception { 169 if (logger.isLoggable(BasicLevel.DEBUG)) 170 logger.log(BasicLevel.DEBUG, 171 "ServerConfigHelper.addServer(" + 172 sid + ',' + 173 hostName + ',' + 174 domainName + ',' + 175 port + ',' + 176 name + ')'); 177 A3CMLConfig a3cmlConfig = AgentServer.getConfig(); 178 if (a3cmlConfig.servers.get(new Integer (sid)) != null) 179 throw new ServerIdAlreadyUsedException( 180 "Server id already used: " + sid); 181 182 A3CMLDomain domain = 183 (A3CMLDomain) a3cmlConfig.getDomain(domainName); 184 185 A3CMLServer server = new A3CMLServer((short)sid, name, hostName); 186 a3cmlConfig.addServer(server); 187 188 A3CMLNetwork network = new A3CMLNetwork(domainName, port); 189 server.addNetwork(network); 190 domain.addServer(server); 191 server.domain = domainName; 192 193 A3CMLServer root = a3cmlConfig.getServer(AgentServer.getServerId()); 194 a3cmlConfig.configure(root); 195 196 ServerDesc serverDesc = new ServerDesc( 197 (short)sid, 198 name, 199 hostName, 200 -1); 201 AgentServer.addServerDesc(serverDesc); 202 AgentServer.initServerDesc(serverDesc, server); 203 if (serverDesc.gateway == serverDesc.sid) { 204 if (serverDesc.domain instanceof Network) { 205 ((Network) serverDesc.domain).addServer((short)sid); 206 } else { 207 throw new Error ("Unknown gateway type: " + 208 serverDesc.domain); 209 } 210 } 211 212 if (autoCommit) commit(); 213 } 214 215 public void removeServer(int sid) 216 throws Exception { 217 if (logger.isLoggable(BasicLevel.DEBUG)) 218 logger.log(BasicLevel.DEBUG, 219 "ServerConfigHelper.removeServer(" + 220 sid + ')'); 221 if (sid != AgentServer.getServerId()) { 222 A3CMLConfig a3cmlConfig = AgentServer.getConfig(); 223 A3CMLServer a3cmlServer = a3cmlConfig.getServer((short)sid); 224 225 if (logger.isLoggable(BasicLevel.DEBUG)) 226 logger.log(BasicLevel.DEBUG, 227 " -> server.networks = " + a3cmlServer.networks); 228 229 if (a3cmlServer.networks.size() > 1) 232 throw new Exception ( 233 "Can't remove server: it belongs to more than one domain."); 234 235 a3cmlConfig.removeServer((short)sid); 236 237 A3CMLServer root = a3cmlConfig.getServer(AgentServer.getServerId()); 238 a3cmlConfig.configure(root); 239 240 ServerDesc servDesc = 241 AgentServer.removeServerDesc((short)sid); 242 243 if (servDesc.domain instanceof Network) { 244 Network nw = (Network) servDesc.domain; 245 nw.delServer(servDesc.sid); 246 } 247 248 for (Enumeration e = AgentServer.elementsServerDesc(); 249 e.hasMoreElements(); ) { 250 ServerDesc sd = (ServerDesc)e.nextElement(); 251 if (sd.gateway == sid) { 252 sd.gateway = -1; 253 sd.domain = null; 254 } 255 } 256 257 if (autoCommit) commit(); 258 } 259 } 261 262 public void addService(int sid, 263 String className, 264 String args) 265 throws Exception { 266 if (logger.isLoggable(BasicLevel.DEBUG)) 267 logger.log(BasicLevel.DEBUG, 268 "ServerConfigHelper.addService(" + 269 sid + ',' + className + ',' + args + ')'); 270 A3CMLConfig a3cmlConfig = AgentServer.getConfig(); 271 A3CMLServer a3cmlServer = a3cmlConfig.getServer((short)sid); 272 A3CMLService a3cmlService = new A3CMLService(className, args); 273 a3cmlServer.addService(a3cmlService); 274 275 if (sid == AgentServer.getServerId()) { 276 try { 277 ServiceManager.register(className, args); 278 ServiceDesc desc = 279 (ServiceDesc) ServiceManager.manager.registry.get( 280 className); 281 if (! desc.running) { 282 ServiceManager.start(desc); 283 } 284 } catch (Exception exc) { 285 a3cmlServer.removeService(className); 286 } 287 } 288 289 if (autoCommit) commit(); 290 } 291 292 public void removeService(int sid, 293 String className) 294 throws Exception { 295 if (logger.isLoggable(BasicLevel.DEBUG)) 296 logger.log(BasicLevel.DEBUG, 297 "ServerConfigHelper.addService(" + 298 sid + ',' + className + ')'); 299 A3CMLConfig a3cmlConfig = AgentServer.getConfig(); 300 A3CMLServer a3cmlServer = a3cmlConfig.getServer((short)sid); 301 302 if (sid == AgentServer.getServerId()) { 303 ServiceManager.stop(className); 304 } 305 306 a3cmlServer.removeService(className); 307 308 if (autoCommit) commit(); 309 } 310 311 public void commit() throws Exception { 312 A3CMLConfig a3cmlConfig = AgentServer.getConfig(); 313 if (AgentServer.getTransaction() instanceof 314 fr.dyade.aaa.util.NullTransaction) { 315 String cfgDir = System.getProperty(AgentServer.CFG_DIR_PROPERTY, 316 AgentServer.DEFAULT_CFG_DIR); 317 String cfgFile = System.getProperty(AgentServer.CFG_FILE_PROPERTY, 318 AgentServer.DEFAULT_CFG_FILE); 319 FileOutputStream fos = new FileOutputStream( 320 new File(cfgDir, cfgFile)); 321 PrintWriter out = new PrintWriter(fos); 322 A3CML.toXML(a3cmlConfig, out); 323 out.flush(); 324 fos.flush(); 325 fos.getFD().sync(); 326 out.close(); 327 fos.close(); 328 } else { 329 a3cmlConfig.save(); 330 } 331 } 332 333 public static class ServerIdAlreadyUsedException extends Exception { 334 public ServerIdAlreadyUsedException(String info) { 335 super(info); 336 } 337 } 338 339 public static class NameAlreadyUsedException extends Exception { 340 public NameAlreadyUsedException(String info) { 341 super(info); 342 } 343 } 344 345 public static class StartFailureException extends Exception { 346 public StartFailureException(String info) { 347 super(info); 348 } 349 } 350 } 351 352 353 354 | Popular Tags |