KickJava   Java API By Example, From Geeks To Geeks.

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


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

25 package fr.dyade.aaa.agent;
26
27 import java.util.*;
28 import java.io.*;
29
30 import fr.dyade.aaa.util.Transaction;
31
32 import fr.dyade.aaa.agent.conf.*;
33
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.objectweb.util.monolog.api.Logger;
36
37 public class ConfigController {
38
39   public final static String JavaDoc ADMIN_SERVER =
40       "fr.dyade.aaa.agent.ADMIN_SERVER";
41
42   public final static String JavaDoc SERVER_COUNTER = "serverCounter";
43
44   private static Logger logger = Debug.getLogger(
45     "fr.dyade.aaa.agent.ConfigController");
46
47   public static class Status {
48     public static final int FREE = 0;
49     public static final int CONFIG = 1;
50
51     public static String JavaDoc[] names = {
52       "FREE", "CONFIG"};
53     
54     public static String JavaDoc toString(int status) {
55       return names[status];
56     }
57   }
58
59   private short serverCounter;
60
61   private A3CMLConfig currentA3cmlConfig;
62
63   private A3CMLConfig a3cmlConfig;
64
65   private int status;
66
67   private Vector newServers;
68
69   private Vector stopScript;
70
71   private Vector startScript;
72
73   ConfigController() throws Exception JavaDoc {
74     if (logger.isLoggable(BasicLevel.DEBUG))
75       logger.log(BasicLevel.DEBUG,
76                  "ConfigController.<init>()");
77     // DF: must be improved. The admin server id can be
78
// higher than zero.
79
if (logger.isLoggable(BasicLevel.DEBUG))
80       logger.log(BasicLevel.DEBUG,
81                  " -> AgentServer.getServerId() = " +
82                  AgentServer.getServerId());
83     if (AgentServer.getServerId() == 0) {
84       Transaction transaction = AgentServer.getTransaction();
85       Short JavaDoc counter = (Short JavaDoc)transaction.load(SERVER_COUNTER);
86       if (counter != null) {
87         serverCounter = counter.shortValue();
88       } else {
89         serverCounter = 1;
90       }
91     } else {
92       serverCounter = -1;
93     }
94     if (logger.isLoggable(BasicLevel.DEBUG))
95       logger.log(BasicLevel.DEBUG,
96                  " -> serverCounter = " + serverCounter);
97     
98     setStatus(Status.FREE);
99   }
100
101   private void setStatus(int status) {
102     this.status = status;
103   }
104   
105   public synchronized void beginConfig() throws Exception JavaDoc {
106     if (logger.isLoggable(BasicLevel.DEBUG))
107       logger.log(BasicLevel.DEBUG,
108                  "ConfigController.beginConfig()");
109     while (status != Status.FREE) {
110       try {
111         wait();
112       } catch (InterruptedException JavaDoc exc) {
113       }
114     }
115     
116     currentA3cmlConfig = AgentServer.getConfig();
117     
118     // Copy the configuration in order to enable rollback.
119
// Use serialization. The rollback could also
120
// be done with an "undo script".
121
ByteArrayOutputStream baos = new ByteArrayOutputStream();
122     ObjectOutputStream oos = new ObjectOutputStream(baos);
123     oos.writeObject(currentA3cmlConfig);
124     oos.flush();
125     oos.close();
126     byte[] bytes = baos.toByteArray();
127     ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
128     ObjectInputStream ois = new ObjectInputStream(bais);
129     a3cmlConfig = (A3CMLConfig)ois.readObject();
130     
131     newServers = new Vector();
132     startScript = new Vector();
133     stopScript = new Vector();
134     setStatus(Status.CONFIG);
135   }
136
137   public synchronized void commitConfig() throws Exception JavaDoc {
138     if (logger.isLoggable(BasicLevel.DEBUG))
139       logger.log(BasicLevel.DEBUG,
140                  "ConfigController.commitConfig()");
141     checkStatus(Status.CONFIG);
142
143     AgentServer.setConfig(a3cmlConfig, true);
144
145     try {
146       A3CMLServer root = a3cmlConfig.getServer(AgentServer.getServerId());
147       a3cmlConfig.configure(root);
148       
149       stop();
150       
151       addNewServers();
152
153       start();
154
155       commit();
156     } catch (Exception JavaDoc exc) {
157       if (logger.isLoggable(BasicLevel.ERROR))
158         logger.log(BasicLevel.ERROR, "", exc);
159       rollback();
160       throw exc;
161     } finally {
162       setStatus(Status.FREE);
163       notify();
164     }
165   }
166
167   private void commit() {
168     if (logger.isLoggable(BasicLevel.DEBUG))
169       logger.log(BasicLevel.DEBUG,
170                  "ConfigController.commit()");
171
172     try {
173       Transaction transaction = AgentServer.getTransaction();
174       transaction.begin();
175       a3cmlConfig.save();
176       transaction.save(
177         new Short JavaDoc(serverCounter),
178         SERVER_COUNTER);
179       transaction.commit();
180       transaction.release();
181     } catch (Exception JavaDoc exc) {
182       throw new Error JavaDoc(exc.toString());
183     }
184   }
185
186   private void rollback() throws Exception JavaDoc {
187     if (logger.isLoggable(BasicLevel.DEBUG))
188       logger.log(BasicLevel.DEBUG,
189                  "ConfigController.rollback()");
190     
191     for (int i = 0; i < newServers.size(); i++) {
192       ServerDesc sd = (ServerDesc)newServers.elementAt(i);
193       AgentServer.removeServerDesc(sd.sid);
194     }
195
196     AgentServer.setConfig(currentA3cmlConfig, true);
197   }
198
199   public synchronized void release() {
200     if (logger.isLoggable(BasicLevel.DEBUG))
201       logger.log(BasicLevel.DEBUG,
202                  "ConfigController.release()");
203     
204     if (status == Status.CONFIG) {
205       setStatus(Status.FREE);
206       notify();
207     }
208   }
209
210   private void stop() throws Exception JavaDoc {
211     if (logger.isLoggable(BasicLevel.DEBUG))
212       logger.log(BasicLevel.DEBUG,
213                  "ConfigController.stop()");
214     for (int i = 0; i < stopScript.size(); i++) {
215       Object JavaDoc cmd = stopScript.elementAt(i);
216       if (cmd instanceof StopNetworkCmd) {
217         exec((StopNetworkCmd)cmd);
218       }
219     }
220   }
221   
222   private void exec(StopNetworkCmd cmd) throws Exception JavaDoc {
223     stopNetwork(cmd.domainName);
224   }
225
226   private void addNewServers() throws Exception JavaDoc {
227     if (logger.isLoggable(BasicLevel.DEBUG))
228       logger.log(BasicLevel.DEBUG,
229                  "ConfigController.addNewServers()");
230     for (int i = 0; i < newServers.size(); i++) {
231       ServerDesc sd = (ServerDesc)newServers.elementAt(i);
232       AgentServer.addServerDesc(sd);
233     }
234   }
235   
236   private void start() throws Exception JavaDoc {
237     if (logger.isLoggable(BasicLevel.DEBUG))
238       logger.log(BasicLevel.DEBUG,
239                  "ConfigController.start()");
240     int i = 0;
241     try {
242       for (i = 0; i < startScript.size(); i++) {
243         Object JavaDoc cmd = startScript.elementAt(i);
244         if (cmd instanceof StartNetworkCmd) {
245           exec((StartNetworkCmd)cmd);
246         } else if (cmd instanceof StartServiceCmd) {
247           exec((StartServiceCmd)cmd);
248         } else if (cmd instanceof ReconfigureClientNetworkCmd) {
249           exec((ReconfigureClientNetworkCmd)cmd);
250         } else if (cmd instanceof ReconfigureServerNetworkCmd) {
251           exec((ReconfigureServerNetworkCmd)cmd);
252         }
253       }
254     } catch (Exception JavaDoc exc) {
255       int size = i + 1;
256       for (int j = 0; j < size; j++) {
257         Object JavaDoc cmd = startScript.elementAt(j);
258         if (cmd instanceof StartNetworkCmd) {
259           rollback((StartNetworkCmd)cmd);
260         } else if (cmd instanceof StartServiceCmd) {
261           rollback((StartServiceCmd)cmd);
262         } else if (cmd instanceof ReconfigureClientNetworkCmd) {
263           rollback((ReconfigureClientNetworkCmd)cmd);
264         } else if (cmd instanceof ReconfigureServerNetworkCmd) {
265           rollback((ReconfigureServerNetworkCmd)cmd);
266         }
267       }
268       throw exc;
269     }
270     
271     // 'Start server' implies that the consumers
272
// have been added (done by 'start network').
273
try {
274       for (i = 0; i < startScript.size(); i++) {
275         Object JavaDoc cmd = startScript.elementAt(i);
276         if (cmd instanceof StartServerCmd) {
277           exec((StartServerCmd)cmd);
278         }
279       }
280     } catch (Exception JavaDoc exc) {
281       int size = i + 1;
282       for (int j = 0; j < size; j++) {
283         Object JavaDoc cmd = startScript.elementAt(j);
284         if (cmd instanceof StartServerCmd) {
285           rollback((StartServerCmd)cmd);
286         }
287       }
288     }
289   }
290
291   private void exec(StartServerCmd cmd) throws Exception JavaDoc {
292     startServer(cmd.sid);
293   }
294
295   private void exec(StartNetworkCmd cmd) throws Exception JavaDoc {
296     startNetwork(cmd.domainName);
297   }
298
299   private void exec(StartServiceCmd cmd) throws Exception JavaDoc {
300     startService(cmd.serviceClassName,
301                  cmd.args);
302   }
303
304   private void exec(ReconfigureClientNetworkCmd cmd) throws Exception JavaDoc {
305     reconfigureClientNetwork(cmd.sid,
306                              cmd.domainName,
307                              cmd.port);
308   }
309   
310   private void exec(ReconfigureServerNetworkCmd cmd) throws Exception JavaDoc {
311     reconfigureServerNetwork(cmd.domainName,
312                              cmd.port);
313   }
314
315   private void rollback(StartServerCmd cmd) throws Exception JavaDoc {
316     ServerDesc servDesc = AgentServer.getServerDesc(cmd.sid);
317     deleteServer(servDesc);
318   }
319
320   private void rollback(StartNetworkCmd cmd) throws Exception JavaDoc {
321     stopNetwork(cmd.domainName);
322   }
323
324   private void rollback(StartServiceCmd cmd) throws Exception JavaDoc {
325     stopService(cmd.serviceClassName);
326   }
327
328   private void rollback(ReconfigureClientNetworkCmd cmd) throws Exception JavaDoc {
329     // Do nothing
330
}
331   
332   private void rollback(ReconfigureServerNetworkCmd cmd) throws Exception JavaDoc {
333     // Do nothing
334
}
335
336   private synchronized void checkStatus(int expectedStatus)
337     throws Exception JavaDoc {
338     if (status != expectedStatus) {
339       throw new Exception JavaDoc("Illegal status: " + Status.toString(status)
340           + " expected: " + Status.toString(expectedStatus));
341     }
342   }
343
344   public void addDomain(String JavaDoc name, String JavaDoc className)
345     throws Exception JavaDoc {
346     if (logger.isLoggable(BasicLevel.DEBUG))
347       logger.log(BasicLevel.DEBUG,
348                  "ConfigController.addDomain(" + name + ',' + className + ')');
349     checkStatus(Status.CONFIG);
350     try {
351       a3cmlConfig.addDomain(new A3CMLDomain(name, className));
352     } catch (Exception JavaDoc exc) {
353       // idempotent
354
}
355   }
356
357   public int addServer(String JavaDoc name, String JavaDoc hostName, short id)
358     throws Exception JavaDoc {
359     if (logger.isLoggable(BasicLevel.DEBUG))
360       logger.log(BasicLevel.DEBUG,
361                  "ConfigController.addServer(" +
362                  name + ',' + hostName + ',' + id + ')');
363     
364     checkStatus(Status.CONFIG);
365     if (! a3cmlConfig.containsServer(name)) {
366       if (id < 0) {
367         if (serverCounter > -1) {
368           id = serverCounter++;
369         } else {
370           throw new Exception JavaDoc("Missing server id");
371         }
372       }
373       a3cmlConfig.addServer(new A3CMLServer(id, name, hostName));
374       ServerDesc serverDesc = new ServerDesc(id, name, hostName, -1);
375       serverDesc.gateway = id;
376       newServers.addElement(serverDesc);
377     } else {
378       // idempotent
379
}
380     
381     if (id != AgentServer.getServerId()) {
382       startScript.addElement(
383         new StartServerCmd(id));
384     }
385
386     return id;
387   }
388
389   public void addService(String JavaDoc serverName,
390                          String JavaDoc serviceClassName,
391                          String JavaDoc args) throws Exception JavaDoc {
392     if (logger.isLoggable(BasicLevel.DEBUG))
393       logger.log(BasicLevel.DEBUG,
394                  "ConfigController.addService(" +
395                  serverName + ',' +
396                  serviceClassName + ',' +
397                  args + ')');
398     checkStatus(Status.CONFIG);
399     A3CMLServer server = a3cmlConfig.getServer(serverName);
400     A3CMLService newService = new A3CMLService(
401       serviceClassName,
402       args);
403     try {
404       server.addService(newService);
405     } catch (Exception JavaDoc exc) {
406       // Idempotent
407
}
408
409     short sid = a3cmlConfig.getServerIdByName(serverName);
410     if (sid == AgentServer.getServerId()) {
411       startScript.addElement(
412         new StartServiceCmd(serviceClassName, args));
413     }
414   }
415   
416   public void addNetwork(String JavaDoc serverName,
417                          String JavaDoc domainName,
418                          int port) throws Exception JavaDoc {
419     if (logger.isLoggable(BasicLevel.DEBUG))
420       logger.log(BasicLevel.DEBUG,
421                  "ConfigController.addNetwork(" +
422                  serverName + ',' +
423                  domainName + ',' +
424                  port + ')');
425     short sid = a3cmlConfig.getServerIdByName(serverName);
426     addNetwork(sid, domainName, port);
427   }
428
429   public void addNetwork(short serverId,
430                          String JavaDoc domainName,
431                          int port) throws Exception JavaDoc {
432     if (logger.isLoggable(BasicLevel.DEBUG))
433       logger.log(BasicLevel.DEBUG,
434                  "ConfigController.addNetwork(" +
435                  serverId + ',' +
436                  domainName + ',' +
437                  port + ')');
438     checkStatus(Status.CONFIG);
439     A3CMLServer server = a3cmlConfig.getServer(serverId);
440     A3CMLNetwork newNetwork = new A3CMLNetwork(domainName, port);
441     try {
442       server.addNetwork(newNetwork);
443       A3CMLDomain domain =
444         (A3CMLDomain) a3cmlConfig.getDomain(domainName);
445       domain.addServer(server);
446     } catch (Exception JavaDoc exc) {
447       // Idempotent
448
}
449     
450     if (serverId == AgentServer.getServerId()) {
451       startScript.add(
452         new StartNetworkCmd(domainName));
453     }
454   }
455
456   public void setNetworkPort(String JavaDoc serverName,
457                              String JavaDoc domainName,
458                              int port) throws Exception JavaDoc {
459     if (logger.isLoggable(BasicLevel.DEBUG))
460       logger.log(BasicLevel.DEBUG,
461                  "ConfigController.setNetworkPort(" +
462                  serverName + ',' +
463                  domainName + ',' +
464                  port + ')');
465     checkStatus(Status.CONFIG);
466     A3CMLServer server = a3cmlConfig.getServer(serverName);
467     A3CMLNetwork network = server.getNetwork(domainName);
468     if (network != null) {
469       network.port = port;
470     } else {
471       throw new Exception JavaDoc("Unknown network");
472     }
473     
474     if (server.sid == AgentServer.getServerId()) {
475       startScript.add(
476         new ReconfigureServerNetworkCmd(domainName, port));
477     } else {
478       startScript.add(
479         new ReconfigureClientNetworkCmd(
480           server.sid, domainName, port));
481     }
482   }
483
484   public void setJVMArgs(String JavaDoc serverName,
485                          String JavaDoc jvmArgs) throws Exception JavaDoc {
486     if (logger.isLoggable(BasicLevel.DEBUG))
487       logger.log(BasicLevel.DEBUG,
488                  "ConfigController.setJVMArgs(" +
489                  serverName + ',' +
490                  jvmArgs + ')');
491     checkStatus(Status.CONFIG);
492     A3CMLServer server = a3cmlConfig.getServer(serverName);
493     server.jvmArgs = jvmArgs;
494   }
495
496   public void setProperty(String JavaDoc propName,
497                           String JavaDoc value) throws Exception JavaDoc {
498     if (logger.isLoggable(BasicLevel.DEBUG))
499       logger.log(BasicLevel.DEBUG,
500                  "ConfigController.setProperty(" +
501                  propName + ',' +
502                  value + ')');
503     checkStatus(Status.CONFIG);
504     a3cmlConfig.addProperty(
505       new A3CMLProperty(propName, value));
506   }
507
508   public void setServerProperty(String JavaDoc serverName,
509                                 String JavaDoc propName,
510                                 String JavaDoc value) throws Exception JavaDoc {
511     if (logger.isLoggable(BasicLevel.DEBUG))
512       logger.log(BasicLevel.DEBUG,
513                  "ConfigController.setServerProperty(" +
514                  serverName + ',' +
515                  propName + +',' +
516                  value + ')');
517     checkStatus(Status.CONFIG);
518     A3CMLServer server = a3cmlConfig.getServer(serverName);
519     server.addProperty(new A3CMLProperty(propName,
520                                          value));
521   }
522
523   public void setServerNat(String JavaDoc serverName,
524                            String JavaDoc translatedServerName,
525                            String JavaDoc translationHostName,
526                            int translationPort) throws Exception JavaDoc {
527     if (logger.isLoggable(BasicLevel.DEBUG))
528       logger.log(BasicLevel.DEBUG,
529                  "ConfigController.setServerNat(" +
530                  serverName + ',' +
531                  translatedServerName + ',' +
532                  translationHostName + ',' +
533                  translationPort + ')');
534     checkStatus(Status.CONFIG);
535     A3CMLServer server = a3cmlConfig.getServer(serverName);
536     short sid = a3cmlConfig.getServerIdByName(translatedServerName);
537     server.addNat(new A3CMLNat(sid, translationHostName,
538                                translationPort));
539   }
540
541   public void removeDomain(String JavaDoc domainName) throws Exception JavaDoc {
542     if (logger.isLoggable(BasicLevel.DEBUG))
543       logger.log(BasicLevel.DEBUG,
544                  "ConfigController.removeDomain(" + domainName + ')');
545     checkStatus(Status.CONFIG);
546     try {
547       Vector serversToRemove = new Vector();
548       A3CMLDomain domain = a3cmlConfig.getDomain(domainName);
549       for (int i = 0; i < domain.servers.size(); i++) {
550         A3CMLServer server = (A3CMLServer) domain.servers.elementAt(i);
551         if (server.networks.size() == 1) {
552           serversToRemove.addElement(server);
553         } else {
554           removeNetwork(server.name, domainName);
555         }
556       }
557       
558       for (int i = 0; i < serversToRemove.size(); i++) {
559         A3CMLServer server =
560           (A3CMLServer)serversToRemove.elementAt(i);
561         if (server.sid != AgentServer.getServerId()) {
562           removeServer(server.name);
563         } else {
564           // Remove the network
565
removeNetwork(server.name, domainName);
566         }
567       }
568       
569       a3cmlConfig.removeDomain(domainName);
570     } catch (fr.dyade.aaa.agent.conf.UnknownDomainException exc) {
571       // Do nothing (idempotency)
572
}
573   }
574
575   public void removeNetwork(String JavaDoc serverName,
576                             String JavaDoc domainName) throws Exception JavaDoc {
577     if (logger.isLoggable(BasicLevel.DEBUG))
578       logger.log(BasicLevel.DEBUG,
579                  "ConfigController.removeNetwork(" +
580                  serverName + ',' +
581                  domainName + ')');
582     checkStatus(Status.CONFIG);
583     A3CMLServer server = a3cmlConfig.getServer(serverName);
584     server.removeNetwork(domainName);
585
586     if (server.sid == AgentServer.getServerId()) {
587       stopScript.addElement(new StopNetworkCmd(domainName));
588     }
589   }
590
591   public void removeServer(String JavaDoc serverName) throws Exception JavaDoc {
592     checkStatus(Status.CONFIG);
593     try {
594       A3CMLServer server = a3cmlConfig.getServer(serverName);
595       ServerDesc servDesc = AgentServer.getServerDesc(server.sid);
596       removeServer(servDesc);
597     } catch (fr.dyade.aaa.agent.conf.UnknownServerException exc) {
598       // The server has already been removed.
599
// This may happen e.g. if the domain used
600
// to communicate with it has been deleted.
601
// Idempotent: do nothing
602
}
603   }
604
605   public void removeServer(short serverId) throws Exception JavaDoc {
606     if (logger.isLoggable(BasicLevel.DEBUG))
607       logger.log(BasicLevel.DEBUG,
608                  "ConfigController.removeServer(" +
609                  serverId + ')');
610     try {
611       checkStatus(Status.CONFIG);
612       ServerDesc servDesc = AgentServer.getServerDesc(serverId);
613       removeServer(servDesc);
614     } catch (fr.dyade.aaa.agent.conf.UnknownServerException exc) {
615       // The server has already been removed.
616
// This may happen e.g. if the domain used
617
// to communicate with it has been deleted.
618
// Idempotent: do nothing
619
}
620   }
621
622   private void removeServer(ServerDesc servDesc) throws Exception JavaDoc {
623     if (servDesc.sid != AgentServer.getServerId()) {
624       deleteServer(servDesc);
625       a3cmlConfig.removeServer(servDesc.sid);
626     } else {
627       // else don't remove the local server
628
throw new Exception JavaDoc("Can't remove local server");
629     }
630   }
631
632   private void deleteServer(ServerDesc servDesc) throws Exception JavaDoc {
633     if (servDesc.domain instanceof Network)
634       ((Network) servDesc.domain).delServer(servDesc.sid);
635     AgentServer.removeServerDesc(servDesc.sid);
636     for (Enumeration e = AgentServer.elementsServerDesc();
637          e.hasMoreElements(); ) {
638       ServerDesc sd = (ServerDesc)e.nextElement();
639       if (sd.gateway == servDesc.sid) {
640         sd.gateway = -1;
641         sd.domain = null;
642       }
643     }
644   }
645   
646   public void removeService(String JavaDoc serverName,
647                             String JavaDoc serviceClassName) throws Exception JavaDoc {
648     checkStatus(Status.CONFIG);
649     A3CMLServer server = a3cmlConfig.getServer(serverName);
650     server.removeService(serviceClassName);
651   }
652   
653   private void stopNetwork(String JavaDoc domainName) throws Exception JavaDoc {
654     AgentServer.removeConsumer(domainName);
655   }
656
657   private void stopService(String JavaDoc serviceClassName) throws Exception JavaDoc {
658     ServiceManager.stop(serviceClassName);
659   }
660
661   private void startNetwork(String JavaDoc domainName) throws Exception JavaDoc {
662     if (logger.isLoggable(BasicLevel.DEBUG))
663       logger.log(BasicLevel.DEBUG,
664                  "ConfigController.startNetwork(" +
665                  domainName + ')');
666     A3CMLServer a3cmlServer = a3cmlConfig.getServer(AgentServer.getServerId());
667     A3CMLNetwork a3cmlNetwork = a3cmlServer.getNetwork(domainName);
668     if (a3cmlNetwork == null) throw new Exception JavaDoc(
669       "Unknown network " + domainName);
670     try {
671       Network network =
672         (Network) AgentServer.getConsumer(domainName);
673       network.start();
674     } catch (Exception JavaDoc exc) {
675       A3CMLDomain a3cmlDomain =
676         (A3CMLDomain) a3cmlConfig.getDomain(domainName);
677       Network network =
678         (Network) Class.forName(a3cmlDomain.network).newInstance();
679       short[] domainSids = new short[a3cmlDomain.servers.size()];
680       for (int i = 0; i < domainSids.length; i++) {
681         domainSids[i] =
682           ((A3CMLServer) a3cmlDomain.servers.elementAt(i)).sid;
683       }
684       AgentServer.addConsumer(domainName, network);
685       if (logger.isLoggable(BasicLevel.DEBUG))
686         logger.log(BasicLevel.DEBUG,
687                    " -> init network " + a3cmlDomain.name
688                    + ',' + a3cmlNetwork.port);
689       network.init(a3cmlDomain.name,
690                    a3cmlNetwork.port,
691                    domainSids);
692       network.start();
693     }
694   }
695
696   private void reconfigureServerNetwork(String JavaDoc domainName,
697                                         int port) throws Exception JavaDoc {
698     if (logger.isLoggable(BasicLevel.DEBUG))
699       logger.log(BasicLevel.DEBUG,
700                  "ConfigController.reconfigureServerNetwork(" +
701                  domainName + ',' +
702                  port + ')');
703     Network network =
704       (Network) AgentServer.getConsumer(domainName);
705     network.stop();
706     network.setPort(port);
707     network.start();
708   }
709
710   private void reconfigureClientNetwork(short sid,
711                                         String JavaDoc domainName,
712                                         int port) throws Exception JavaDoc {
713     if (logger.isLoggable(BasicLevel.DEBUG))
714       logger.log(BasicLevel.DEBUG,
715                  "ConfigController.reconfigureClientNetwork(" +
716                  sid + ',' +
717                  domainName + ',' +
718                  port + ')');
719     A3CMLServer a3cmlServer = a3cmlConfig.getServer(sid);
720     ServerDesc serverDesc =
721       AgentServer.getServerDesc(sid);
722     if (domainName.equals(serverDesc.getDomainName())) {
723       serverDesc.updateSockAddr(
724         serverDesc.getHostname(),
725         port);
726     }
727   }
728
729   private void startService(String JavaDoc serviceClassName,
730                             String JavaDoc args) throws Exception JavaDoc {
731     if (logger.isLoggable(BasicLevel.DEBUG))
732       logger.log(BasicLevel.DEBUG,
733                  "ConfigController.startService(" +
734                  serviceClassName + ',' +
735                  args + ')');
736     A3CMLServer a3cmlServer =
737       a3cmlConfig.getServer(AgentServer.getServerId());
738     ServiceManager.register(
739      serviceClassName, args);
740     ServiceDesc desc =
741       (ServiceDesc) ServiceManager.manager.registry.get(
742         serviceClassName);
743     if (desc.running) return;
744     ServiceManager.start(desc);
745   }
746
747   private void startServer(short sid) throws Exception JavaDoc {
748     if (logger.isLoggable(BasicLevel.DEBUG))
749       logger.log(BasicLevel.DEBUG,
750                  "ConfigController.startServer(" + sid + ')');
751     A3CMLServer server = a3cmlConfig.getServer(sid);
752     ServerDesc desc = AgentServer.getServerDesc(sid);
753     AgentServer.initServerDesc(desc, server);
754     if (desc.gateway == desc.sid) {
755       if (desc.domain instanceof Network) {
756         ((Network) desc.domain).addServer(server.sid);
757       } else {
758         throw new Error JavaDoc("Unknown gateway type: " + desc.domain);
759       }
760     }
761   }
762
763   static class StartServerCmd {
764     public short sid;
765
766     public StartServerCmd(short sid) {
767       this.sid = sid;
768     }
769   }
770
771   static class StartServiceCmd {
772     public String JavaDoc serviceClassName;
773     public String JavaDoc args;
774
775     public StartServiceCmd(String JavaDoc serviceClassName,
776                            String JavaDoc args) {
777       this.serviceClassName = serviceClassName;
778       this.args = args;
779     }
780   }
781
782   static class StartNetworkCmd {
783     public String JavaDoc domainName;
784
785     public StartNetworkCmd(String JavaDoc domainName) {
786       this.domainName = domainName;
787     }
788   }
789
790   static class StopNetworkCmd {
791     public String JavaDoc domainName;
792     
793     public StopNetworkCmd(String JavaDoc domainName) {
794       this.domainName = domainName;
795     }
796   }
797
798   static class ReconfigureClientNetworkCmd {
799     public short sid;
800     public String JavaDoc domainName;
801     public int port;
802     
803     public ReconfigureClientNetworkCmd(
804       short sid,
805       String JavaDoc domainName,
806       int port) {
807       this.sid = sid;
808       this.domainName = domainName;
809       this.port = port;
810     }
811   }
812
813   static class ReconfigureServerNetworkCmd {
814     public String JavaDoc domainName;
815     public int port;
816     
817     public ReconfigureServerNetworkCmd(
818       String JavaDoc domainName,
819       int port) {
820       this.domainName = domainName;
821       this.port = port;
822     }
823   }
824 }
825
Popular Tags