KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > ServerConfigHelper


1 /*
2  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): ScalAgent Distributed Technologies
20  * Contributor(s):
21  */

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 JavaDoc domainName,
44                            int routerId,
45                            int port)
46     throws Exception JavaDoc {
47     if (logger.isLoggable(BasicLevel.DEBUG))
48       logger.log(BasicLevel.DEBUG,
49                  "ServerConfigHelper.addDomain(" +
50                  domainName +
51                  ',' + routerId + ',' +
52                  port + ')');
53
54     // Check configuration consistency (may fail)
55
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 JavaDoc((short)routerId)) == null)
60       throw new Exception JavaDoc("Server not found: " + routerId);
61     
62     // Update the configuration (can't fail)
63
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       // Create and start the run-time entities (may fail)
79
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 JavaDoc exc) {
91         if (logger.isLoggable(BasicLevel.ERROR))
92           logger.log(BasicLevel.ERROR, "", exc);
93         // Rollback the network addition
94
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 JavaDoc domainName)
112     throws Exception JavaDoc {
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     // Check that there is only one server left inside the domain:
126
// the router. If there is more than one server, the domain
127
// can't be removed. The servers belonging to the domain must
128
// first be removed.
129
if (domain.servers.size() > 1)
130       throw new Exception JavaDoc("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     // Update the configuration and the run-time entities
138
// at the same time (can't fail)
139
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       // stop and delete the run-time entity
153
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 JavaDoc hostName,
165                         String JavaDoc domainName,
166                         int port,
167                         String JavaDoc name)
168     throws Exception JavaDoc {
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 JavaDoc(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 JavaDoc("Unknown gateway type: " +
208                         serverDesc.domain);
209       }
210     }
211
212     if (autoCommit) commit();
213   }
214
215   public void removeServer(int sid)
216     throws Exception JavaDoc {
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       // Check that the server doesn't belong to more than one
230
// domain.
231
if (a3cmlServer.networks.size() > 1)
232         throw new Exception JavaDoc(
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     //else do nothing (don't remove the local server)
260
}
261
262   public void addService(int sid,
263                          String JavaDoc className,
264                          String JavaDoc args)
265     throws Exception JavaDoc {
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 JavaDoc exc) {
285         a3cmlServer.removeService(className);
286       }
287     }
288
289     if (autoCommit) commit();
290   }
291
292   public void removeService(int sid,
293                             String JavaDoc className)
294     throws Exception JavaDoc {
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 JavaDoc {
312     A3CMLConfig a3cmlConfig = AgentServer.getConfig();
313     if (AgentServer.getTransaction() instanceof
314         fr.dyade.aaa.util.NullTransaction) {
315       String JavaDoc cfgDir = System.getProperty(AgentServer.CFG_DIR_PROPERTY,
316                                          AgentServer.DEFAULT_CFG_DIR);
317       String JavaDoc 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 JavaDoc {
334     public ServerIdAlreadyUsedException(String JavaDoc info) {
335       super(info);
336     }
337   }
338   
339   public static class NameAlreadyUsedException extends Exception JavaDoc {
340     public NameAlreadyUsedException(String JavaDoc info) {
341       super(info);
342     }
343   }
344
345   public static class StartFailureException extends Exception JavaDoc {
346     public StartFailureException(String JavaDoc info) {
347       super(info);
348     }
349   }
350 }
351
352     
353     
354
Popular Tags