KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > conf > A3CMLConfig


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 package fr.dyade.aaa.agent.conf;
20
21 import java.io.*;
22 import java.util.*;
23
24 import org.objectweb.util.monolog.api.BasicLevel;
25 import org.objectweb.util.monolog.api.Logger;
26
27 import fr.dyade.aaa.util.Transaction;
28 import fr.dyade.aaa.agent.Debug;
29 import fr.dyade.aaa.agent.AgentServer;
30
31 public class A3CMLConfig implements Serializable {
32   /** use serialVersionUID for interoperability */
33   private static final long serialVersionUID = -2497621374376654935L;
34
35   /** Hashtable of all domains */
36   public Hashtable domains = null;
37   /** Hashtable of all servers (persitent and transient) */
38   public Hashtable servers = null;
39   /** Hashtable of all global properties */
40   public Hashtable properties = null;
41   /** Hashtable of all clusters */
42   public Hashtable clusters = null;
43
44   public A3CMLConfig() {
45     domains = new Hashtable();
46     servers = new Hashtable();
47     properties = new Hashtable();
48     clusters = new Hashtable();
49   }
50
51   /**
52    * Adds a domain.
53    *
54    * @param domain The description of added domain.
55    * @exception DuplicateDomainException
56    * If the domain already exist.
57    */

58   public final void addDomain(A3CMLDomain domain) throws DuplicateDomainException {
59     if (domains.containsKey(domain.name))
60       throw new DuplicateDomainException("Duplicate domain " + domain.name);
61     domains.put(domain.name, domain);
62   }
63
64   /**
65    * Removes a domain.
66    *
67    * @param name The domain name.
68    * @return The domain description if exist.
69    * @exception UnknownDomainException
70    * If the domain don't exist.
71    */

72   public final A3CMLDomain removeDomain(String JavaDoc name) throws UnknownDomainException {
73     A3CMLDomain domain = null;
74     if (domains.containsKey(name))
75       domain = (A3CMLDomain) domains.remove(name);
76     else
77       throw new UnknownDomainException("Unknown domain " + name);
78     return domain;
79   }
80
81   /**
82    * Returns true if it exists a domain with this name, false otherwise.
83    *
84    * @param name The domain name.
85    * @return True if the domain is declared, false otherwise.
86    */

87   public final boolean containsDomain(String JavaDoc name) {
88     return domains.containsKey(name);
89   }
90
91   /**
92    * Returns the description of a domain.
93    *
94    * @param name The domain name.
95    * @return The domain description if exist.
96    * @exception UnknownDomainException
97    * If the domain don't exist.
98    */

99   public final A3CMLDomain getDomain(String JavaDoc name) throws UnknownDomainException {
100     A3CMLDomain domain = (A3CMLDomain) domains.get(name);
101     if (domain == null)
102       throw new UnknownDomainException("Unknown domain " + name);
103     return domain;
104   }
105
106   /**
107    * Adds a cluster.
108    *
109    * @param cluster The description of added cluster.
110    * @exception DuplicateClusterException
111    * If the cluster already exist.
112    */

113   public final void addCluster(A3CMLCluster cluster) throws DuplicateClusterException {
114     Short JavaDoc id = new Short JavaDoc(cluster.sid);
115     if (clusters.containsKey(id))
116       throw new DuplicateClusterException("Duplicate cluster " + cluster.sid);
117     clusters.put(id, cluster);
118   }
119
120   /**
121    * Returns the description of a cluster.
122    *
123    * @param sid The cluster identifier.
124    * @return The cluster description if exist.
125    * @exception UnknownClusterException
126    * If the cluster does not exist.
127    */

128   public final A3CMLCluster getCluster(short sid) throws UnknownClusterException {
129     A3CMLCluster cluster = (A3CMLCluster) clusters.get(new Short JavaDoc(sid));
130     if (cluster == null)
131       throw new UnknownClusterException("Unknown cluster id. #" + sid);
132     return cluster;
133   }
134   
135   /**
136    * Returns the description of a cluster.
137    *
138    * @param name The cluster name.
139    * @return The cluster description if exist.
140    * @exception UnknownClusterException
141    * If the cluster does not exist.
142    */

143   public final A3CMLCluster getCluster(String JavaDoc name) throws UnknownClusterException {
144     for (Enumeration c = clusters.elements(); c.hasMoreElements(); ) {
145       A3CMLCluster cluster = (A3CMLCluster) c.nextElement();
146       if (cluster.name.equals(name)) return cluster;
147     }
148     throw new UnknownClusterException("Unknown cluster id for cluster " + name);
149   }
150
151   /**
152    * Gets a cluster identifier from its name.
153    *
154    * @param name The cluster name.
155    * @return The cluster identifier.
156    * @exception UnknownClusterException
157    * If the cluster does not exist.
158    */

159   public short getClusterIdByName(String JavaDoc name) throws UnknownClusterException {
160     for (Enumeration c = clusters.elements(); c.hasMoreElements(); ) {
161       A3CMLCluster cluster = (A3CMLCluster) c.nextElement();
162       if (cluster.name.equals(name)) return cluster.sid;
163     }
164     throw new UnknownClusterException("Unknown cluster " + name);
165   }
166
167
168   /**
169    * Returns true if the configuration contains a cluster with specified name.
170    *
171    * @param name cluster name
172    * @return true if contain name; false otherwise.
173    */

174   public final boolean containsCluster(String JavaDoc name) {
175     try {
176       getClusterIdByName(name);
177     } catch (UnknownClusterException exc) {
178       return false;
179     }
180     return true;
181   }
182
183   /**
184    * Returns true if the configuration contains a cluster with specified id.
185    *
186    * @param sid cluster id
187    * @return true if contain sid; false otherwise.
188    */

189   public final boolean containsCluster(short sid) {
190     return clusters.containsKey(new Short JavaDoc(sid));
191   }
192
193   /**
194    * Removes a cluster.
195    *
196    * @param sid The unique cluster identifier.
197    * @return The cluster description if exists.
198    * @exception UnknownClusterException
199    * If the server does not exist.
200    */

201   public final A3CMLCluster removeCluster(short sid) throws UnknownClusterException {
202     A3CMLCluster cluster = null;
203     Short JavaDoc id = new Short JavaDoc(sid);
204     if (clusters.containsKey(id))
205       cluster = (A3CMLCluster) clusters.remove(id);
206     else
207       throw new UnknownClusterException("Unknown cluster id. #" + sid);
208     return cluster;
209   }
210   
211   /**
212    * Remove a cluster.
213    *
214    * @param name The cluster name.
215    * @return The cluster description if exists.
216    * @exception UnknownClusterException
217    * If the server does not exist.
218    */

219   public final A3CMLCluster removeCluster(String JavaDoc name) throws UnknownClusterException {
220     return removeCluster(getClusterIdByName(name));
221   }
222
223   /**
224    * Adds a server.
225    *
226    * @param server The description of added server.
227    * @exception DuplicateServerException
228    * If the server already exist.
229    */

230   public final void addServer(A3CMLServer server) throws DuplicateServerException {
231     Short JavaDoc id = new Short JavaDoc(server.sid);
232     if (servers.containsKey(id))
233       throw new DuplicateServerException("Duplicate server id. #" + server.sid);
234     servers.put(id, server);
235   }
236   
237   /**
238    * Removes a server.
239    *
240    * @param sid The unique server identifier.
241    * @return The server description if exists.
242    * @exception UnknownServerException
243    * If the server does not exist.
244    */

245   public final A3CMLServer removeServer(short sid) throws UnknownServerException {
246     A3CMLServer server = null;
247     Short JavaDoc id = new Short JavaDoc(sid);
248     if (servers.containsKey(id)) {
249       server = (A3CMLServer) servers.remove(id);
250     } else {
251       throw new UnknownServerException("Unknown server id. #" + sid);
252     }
253
254     for (int i = 0; i < server.networks.size(); i++) {
255       A3CMLNetwork network = (A3CMLNetwork)server.networks.elementAt(i);
256       A3CMLDomain domain = (A3CMLDomain)domains.get(network.domain);
257       domain.removeServer(sid);
258     }
259
260     return server;
261   }
262   
263   /**
264    * Remove a server.
265    *
266    * @param name The server name.
267    * @return The server description if exists.
268    * @exception UnknownServerException
269    * If the server does not exist.
270    */

271   public final A3CMLServer removeServer(String JavaDoc name) throws UnknownServerException {
272     return removeServer(getServerIdByName(name));
273   }
274   
275   /**
276    * Returns true if the configuration contains a server with specified id.
277    *
278    * @param sid server id
279    * @return true if contain sid; false otherwise.
280    */

281   public final boolean containsServer(short sid) {
282     return servers.containsKey(new Short JavaDoc(sid));
283   }
284   
285   /**
286    * Gets a server identifier from its name.
287    *
288    * @param name The server name.
289    * @return The server identifier.
290    * @exception UnknownServerException
291    * If the server does not exist.
292    */

293   public short getServerIdByName(String JavaDoc name) throws UnknownServerException {
294     for (Enumeration s = servers.elements(); s.hasMoreElements(); ) {
295       A3CMLServer server = (A3CMLServer) s.nextElement();
296       if (server.name.equals(name)) return server.sid;
297     }
298     throw new UnknownServerException("Unknown server " + name);
299   }
300
301   /**
302    * Returns true if the configuration contains a server with specified name.
303    *
304    * @param name server name
305    * @return true if contain name; false otherwise.
306    */

307   public final boolean containsServer(String JavaDoc name) {
308     try {
309       getServerIdByName(name);
310     } catch (UnknownServerException exc) {
311       return false;
312     }
313     return true;
314   }
315
316   /**
317    * Returns the description of a server.
318    *
319    * @param sid The server identifier.
320    * @return The server description if exist.
321    * @exception UnknownServerException
322    * If the server does not exist.
323    */

324   public final A3CMLServer getServer(short sid) throws UnknownServerException {
325     return getServer(sid, AgentServer.NULL_ID);
326   }
327
328   /**
329    * Returns the description of a server.
330    *
331    * @param sid The server identifier.
332    * @param cid The cluster identifier.
333    * @return The server description if exist.
334    * @exception UnknownServerException
335    * If the server does not exist.
336    */

337   public final A3CMLServer getServer(short sid, short cid) throws UnknownServerException {
338     A3CMLServer server = null;
339     if (cid == AgentServer.NULL_ID)
340       server = (A3CMLServer) servers.get(new Short JavaDoc(sid));
341     else {
342       try {
343         A3CMLCluster cluster = getCluster(sid);
344         server = cluster.getServer(cid);
345       } catch (Exception JavaDoc exc) {
346         throw new UnknownServerException(exc.getMessage());
347       }
348     }
349     
350     if (server == null)
351       throw new UnknownServerException("Unknown server id. #" + sid);
352     return server;
353   }
354
355   /**
356    * Returns the description of a server.
357    *
358    * @param name The server name.
359    * @return The server description if exist.
360    * @exception UnknownServerException
361    * If the server does not exist.
362    */

363   public final A3CMLServer getServer(String JavaDoc name) throws UnknownServerException {
364     for (Enumeration s = servers.elements(); s.hasMoreElements(); ) {
365       A3CMLServer server = (A3CMLServer) s.nextElement();
366       if (server.name.equals(name)) return server;
367     }
368     throw new UnknownServerException("Unknown server id for server " + name);
369   }
370
371   /**
372    * add property
373    *
374    * @param prop A3CMLProperty
375    * @return the previous value of the specified prop.name in
376    * this hashtable, or null if it did not have one.
377    * @exception Exception
378    */

379   public final A3CMLProperty addProperty(A3CMLProperty prop) throws Exception JavaDoc {
380     return (A3CMLProperty) properties.put(prop.name, prop);
381   }
382
383   /**
384    * remove property
385    *
386    * @param name property name
387    * @return the value to which the name had been mapped in
388    * this hashtable, or null if the name did not have a mapping.
389    */

390   public final A3CMLProperty removeProperty(String JavaDoc name) {
391     return (A3CMLProperty) properties.remove(name);
392   }
393
394   /**
395    * contains property
396    *
397    * @param name property name
398    * @return true if contain name; false otherwise.
399    */

400   public final boolean containsProperty(String JavaDoc name) {
401     return properties.containsKey(name);
402   }
403
404   /**
405    * Returns the specified property.
406    */

407   public final A3CMLProperty getProperty(String JavaDoc name) {
408     return (A3CMLProperty) properties.get(name);
409   }
410
411   /**
412    * Returns the specified property.
413    */

414   public final A3CMLProperty getProperty(String JavaDoc name, short sid, short cid)
415     throws Exception JavaDoc {
416     A3CMLProperty prop = null;
417     if (cid == AgentServer.NULL_ID) {
418       A3CMLServer server = getServer(sid);
419       prop = (A3CMLProperty) server.getProperty(name);
420     } else {
421       A3CMLCluster cluster = getCluster(sid);
422       A3CMLServer server = cluster.getServer(cid);
423       prop = (A3CMLProperty) server.getProperty(name);
424       if (prop == null)
425         prop = (A3CMLProperty) cluster.getProperty(name);
426     }
427     return prop;
428   }
429
430   /**
431    * Get the JVM argument for a particular agent server identified by its id.
432    *
433    * @param id agent server identifier.
434    * @return the arguments as declared in configuration file
435    * @exception UnknownServerException
436    * The specified server does not exist.
437    */

438   public final String JavaDoc getJvmArgs(short sid) throws UnknownServerException {
439     A3CMLServer server = getServer(sid);
440     return server.getJvmArgs();
441   }
442   
443   /**
444    * Get the JVM argument for a particular agent server identified by its name.
445    *
446    * @param name agent server name.
447    * @return the arguments as declared in configuration file
448    * @exception UnknownServerException
449    * The specified server does not exist.
450    */

451   public final String JavaDoc getJvmArgs(String JavaDoc name) throws UnknownServerException {
452     A3CMLServer server = getServer(name);
453     return server.getJvmArgs();
454   }
455
456   /**
457    * Get the argument strings for a particular service on a particular
458    * agent server identified by its id.
459    *
460    * @param sid agent server id.
461    * @param classname the service class name.
462    * @return the arguments as declared.
463    * @exception UnknownServerException
464    * The specified server does not exist.
465    * @exception UnknownServiceException
466    * The specified service is not declared on this server.
467    */

468   public final String JavaDoc
469       getServiceArgs(short sid, String JavaDoc classname)
470     throws UnknownServerException, UnknownServiceException {
471     A3CMLServer server = getServer(sid);
472     return server.getServiceArgs(classname);
473   }
474
475   /**
476    * Get the argument strings for a particular service on a particular
477    * agent server identified by its name.
478    *
479    * @param sid agent server name.
480    * @param classname the service class name.
481    * @return the arguments as declared.
482    * @exception UnknownServerException
483    * The specified server does not exist.
484    * @exception UnknownServiceException
485    * The specified service is not declared on this server.
486    */

487   public final String JavaDoc
488       getServiceArgs(String JavaDoc name, String JavaDoc classname)
489     throws UnknownServerException, UnknownServiceException {
490     A3CMLServer server = getServer(name);
491     return server.getServiceArgs(classname);
492   }
493
494   /* -+-+-+- -+-+-+- */
495   /* -+-+-+- Code needed for configuration. -+-+-+- */
496   /* -+-+-+- -+-+-+- */
497
498   /**
499    * Adapts the current configuration to the specified persistent server.
500    */

501   public void configure(A3CMLServer root) throws Exception JavaDoc {
502     short rootid = root.sid;
503     Vector toExplore = new Vector();
504
505     // Temporary fix, reset visited and gateway fields
506
reset();
507     
508     // Search alls directly accessible domains.
509
for (Enumeration n = root.networks.elements(); n.hasMoreElements();) {
510       A3CMLNetwork network = (A3CMLNetwork) n.nextElement();
511
512       A3CMLDomain domain = (A3CMLDomain) domains.get(network.domain);
513       domain.gateway = rootid;
514       domain.hops = 1;
515       toExplore.addElement(domain);
516
517       Log.logger.log(BasicLevel.DEBUG,
518                      "configure - toExplore.add(" + domain + ")");
519     }
520     
521     root.visited = true;
522     root.gateway = -1;
523     root.hops = 0;
524     root.domain = "local";
525
526     while (toExplore.size() > 0) {
527       A3CMLDomain domain = (A3CMLDomain) toExplore.elementAt(0);
528       toExplore.removeElementAt(0);
529       A3CMLServer gateway = (A3CMLServer) servers.get(new Short JavaDoc(domain.gateway));
530
531       if (Log.logger.isLoggable(BasicLevel.DEBUG))
532         Log.logger.log(BasicLevel.DEBUG, "configure - explore(" + domain + ")");
533
534       // Parse all nodes of this domain
535
for (Enumeration s = domain.servers.elements();
536        s.hasMoreElements();) {
537     A3CMLServer server = (A3CMLServer) s.nextElement();
538
539         if (server.visited) continue;
540
541         if (Log.logger.isLoggable(BasicLevel.DEBUG))
542           Log.logger.log(BasicLevel.DEBUG, "configure - explore(" + server + ")");
543
544         server.visited = true;
545         if (domain.gateway == rootid) {
546           // The server is directly accessible from root
547
server.gateway = -1;
548           server.domain = domain.name;
549         } else {
550           server.gateway = domain.gateway;
551           server.domain = gateway.domain;
552         }
553         server.hops = domain.hops;
554
555         // If the server is a router then add the accessible domains
556
// to the list.
557
for (Enumeration n = server.networks.elements();
558              n.hasMoreElements();) {
559           A3CMLNetwork network = (A3CMLNetwork) n.nextElement();
560           A3CMLDomain d2 = (A3CMLDomain) domains.get(network.domain);
561
562           if (Log.logger.isLoggable(BasicLevel.DEBUG))
563             Log.logger.log(BasicLevel.DEBUG, "configure - parse(" + d2 + ")");
564
565           if (d2 == domain) {
566             if (Log.logger.isLoggable(BasicLevel.DEBUG))
567               Log.logger.log(BasicLevel.DEBUG, "configure - setPort(" + network.port + ")");
568             // The server is directly accessible from root server by
569
// this network interface; fixes the communication port
570
// for this server.
571

572             // AF 03/11/2004 - It seems in fact the domain is the one we are
573
// exploring, so if the server is directly accessible its listen
574
// port is the one of this network...
575
server.port = network.port;
576             continue;
577           }
578
579           // If the domain is already explored then there is more
580
// than one route to this domain.
581

582           //if (d2.gateway != -1)
583
// throw new Exception("more than one route to: " + domain);
584

585           // if (d2.hops != -1)
586
// throw new Exception("more than one route to: " + domain);
587
d2.hops = domain.hops +1;
588
589           // The domain is not already explored.
590
if (server.gateway == -1)
591             d2.gateway = server.sid; // the server is directly accessible
592
else
593             d2.gateway = server.gateway; // the server itself is routed
594
toExplore.addElement(d2);
595
596           if (Log.logger.isLoggable(BasicLevel.DEBUG))
597             Log.logger.log(BasicLevel.DEBUG, "configure - toExplore.add(" + d2 + ")");
598         }
599       }
600     }
601
602     // verify that all declared servers are accessible
603
for (Enumeration s = servers.elements(); s.hasMoreElements();) {
604       A3CMLServer server = (A3CMLServer) s.nextElement();
605       if (Log.logger.isLoggable(BasicLevel.DEBUG))
606         Log.logger.log(BasicLevel.DEBUG, "configure - verify " + server);
607         if (! server.visited)
608           throw new Exception JavaDoc(server + " inaccessible");
609     }
610
611     // Search alls directly accessible domains, then set special routes
612
// for HttpNetworks.
613
for (Enumeration n = root.networks.elements(); n.hasMoreElements();) {
614       A3CMLNetwork network = (A3CMLNetwork) n.nextElement();
615       A3CMLDomain domain = (A3CMLDomain) domains.get(network.domain);
616
617       if (("fr.dyade.aaa.agent.HttpNetwork".equals(domain.network)) ||
618           ("fr.dyade.aaa.agent.HttpsNetwork".equals(domain.network))) {
619
620         // First search for the listen server..
621
short router = -1;
622         for (int i=0; i<domain.servers.size(); i++) {
623           A3CMLServer server = (A3CMLServer) domain.servers.elementAt(i);
624           if ((server.port > 0) && (server.sid != rootid)) {
625             router = server.sid;
626             break;
627           }
628         }
629         // .. then set the gateway for all clients.
630
if (router != -1) {
631           for (int i=0; i<domain.servers.size(); i++) {
632             A3CMLServer server = (A3CMLServer) domain.servers.elementAt(i);
633             if ((server.sid != router) && (server.sid != rootid))
634               server.gateway = router;
635           }
636         }
637       }
638     }
639
640   }
641
642   /**
643    * Gets configuration of agent servers by a domain from a Config object.
644    * This method fills the object graph configuration in the <code>Config</code>
645    * object.
646    *
647    * @param domainName domain name
648    * @return the <code>Config</code> object if file exists and is
649    * correct, null otherwise.
650    *
651    * @exception Exception
652    * unspecialized exception when reading and parsing the configuration file
653    */

654   public A3CMLConfig getDomainConfig(String JavaDoc domainName) throws Exception JavaDoc {
655     if (Log.logger.isLoggable(BasicLevel.DEBUG))
656       Log.logger.log(BasicLevel.DEBUG,
657                      "Config.getDomainConfig(" + domainName + ")");
658
659     A3CMLConfig domainConf = new A3CMLConfig();
660
661     // add domain "domainName" in domainConf.
662
A3CMLDomain dom = getDomain(domainName).duplicate();
663     domainConf.addDomain(dom);
664     
665     // add persistent server in domainConf.
666
for (int i = 0; i < dom.servers.size(); i++) {
667       A3CMLServer server = (A3CMLServer) dom.servers.elementAt(i);
668       domainConf.servers.put(new Short JavaDoc(server.sid),server);
669     }
670
671     // add global properties in domainConf.
672
for (Enumeration p = properties.elements(); p.hasMoreElements(); ) {
673       A3CMLProperty property = (A3CMLProperty) p.nextElement();
674       domainConf.addProperty(((A3CMLProperty) property).duplicate());
675     }
676
677     try {
678       // for Admin Domain
679
// add domain "ADMIN_DOMAIN" in domainConf.
680
A3CMLDomain d0 = getDomain(AgentServer.ADMIN_DOMAIN);
681       domainConf.addDomain(new A3CMLDomain(d0.name,d0.network));
682       A3CMLServer s0 = (A3CMLServer) domainConf.getServer(AgentServer.getServerId());
683       d0 = domainConf.getDomain(AgentServer.ADMIN_DOMAIN);
684       d0.addServer(s0);
685       for (int i = 0; i < s0.networks.size(); ) {
686         A3CMLNetwork network = (A3CMLNetwork) s0.networks.elementAt(i);
687         if (!(network.domain.equals(AgentServer.ADMIN_DOMAIN) ||
688               network.domain.equals(domainName))) {
689           s0.networks.removeElement(network);
690         } else
691           i++;
692       }
693     } catch (UnknownServerException exc) {
694       if (Log.logger.isLoggable(BasicLevel.DEBUG))
695         Log.logger.log(BasicLevel.DEBUG, "", exc);
696     }
697
698     return domainConf;
699   }
700
701   /**
702    * Gets configuration of agent servers by a list of domain from a Config object.
703    * This method fills the object graph configuration in the <code>Config</code>
704    * object.
705    *
706    * @param domainName list of domain name
707    * @return the <code>Config</code> object if file exists and is
708    * correct, null otherwise.
709    *
710    * @exception Exception
711    * unspecialized exception when reading and parsing the configuration file
712    */

713   public A3CMLConfig getDomainConfig(String JavaDoc[] listDomainName) throws Exception JavaDoc {
714     if (Log.logger.isLoggable(BasicLevel.DEBUG))
715       Log.logger.log(BasicLevel.DEBUG,
716                      "Config.getDomainConfig(" + listDomainName + ")");
717
718     Hashtable context = new Hashtable();
719
720     A3CMLConfig domainConf = new A3CMLConfig();
721     Vector domainList = new Vector();
722     for (int i = 0; i < listDomainName.length; i++)
723       domainList.addElement(listDomainName[i]);
724     
725     for (int n = 0; n < listDomainName.length; n++) {
726       String JavaDoc domainName = listDomainName[n];
727
728       // add domain "domainName" in domainConf.
729
A3CMLDomain dom = getDomain(domainName).duplicate(context);
730       domainConf.addDomain(dom);
731       
732       // add persistent server in domainConf.
733
for (int i = 0; i < dom.servers.size(); i++) {
734         A3CMLServer server = (A3CMLServer) dom.servers.elementAt(i);
735         for (int j = 0; j < server.networks.size(); ) {
736           A3CMLNetwork network = (A3CMLNetwork) server.networks.elementAt(j);
737           if (!(network.domain.equals(AgentServer.ADMIN_DOMAIN) ||
738                 network.domain.equals("transient") ||
739                 domainList.contains(network.domain))) {
740             server.networks.removeElement(network);
741           } else
742             j++;
743         }
744         domainConf.servers.put(new Short JavaDoc(server.sid),server);
745       }
746     }
747       
748     // add global properties in domainConf.
749
for (Enumeration p = properties.elements(); p.hasMoreElements(); ) {
750       A3CMLProperty property = (A3CMLProperty) p.nextElement();
751       domainConf.addProperty(((A3CMLProperty) property).duplicate());
752     }
753     
754     try {
755       // for Admin Domain
756
// add domain "ADMIN_DOMAIN" in domainConf.
757
A3CMLDomain d0 = getDomain(AgentServer.ADMIN_DOMAIN);
758       domainConf.addDomain(new A3CMLDomain(d0.name,d0.network));
759       A3CMLServer s0 = (A3CMLServer) domainConf.getServer(AgentServer.getServerId());
760       d0 = domainConf.getDomain(AgentServer.ADMIN_DOMAIN);
761       d0.addServer(s0);
762       for (int i = 0; i < s0.networks.size(); ) {
763         A3CMLNetwork network = (A3CMLNetwork) s0.networks.elementAt(i);
764         if (!(network.domain.equals(AgentServer.ADMIN_DOMAIN) ||
765               domainList.contains(network.domain))) {
766           s0.networks.removeElement(network);
767         } else
768           i++;
769       }
770     } catch (UnknownServerException exc) {
771       if (Log.logger.isLoggable(BasicLevel.DEBUG))
772         Log.logger.log(BasicLevel.DEBUG, "", exc);
773     }
774
775     return domainConf;
776   }
777   
778   /**
779    * save configuration of agent servers (Config)
780    * in a serialized file.
781    *
782    * @exception IOException
783    * @see AgentServer.DEFAULT_SER_CFG_FILE
784    */

785   public void save() throws IOException {
786     if (Log.logger.isLoggable(BasicLevel.DEBUG))
787       Log.logger.log(BasicLevel.DEBUG, "Config.save(" + this + ")");
788
789     AgentServer.getTransaction().save(this, AgentServer.DEFAULT_SER_CFG_FILE);
790   }
791     
792   /**
793    * read object from a serialized file,
794    * in cfgDir if null, search object in
795    * path used to load classes
796    *
797    * @param cfgDir read obj in this directory
798    * @param cfgFileName serialized file name
799    * @exception Exception
800    */

801   public static A3CMLConfig load() throws Exception JavaDoc {
802     // Get the logging monitor from current server MonoLog.loggeritorFactory
803
if (Log.logger.isLoggable(BasicLevel.DEBUG))
804       Log.logger.log(BasicLevel.DEBUG, "Config.load()");
805     
806     A3CMLConfig a3config = (A3CMLConfig) AgentServer.getTransaction().load(AgentServer.DEFAULT_SER_CFG_FILE);
807
808     if (a3config == null) {
809       Log.logger.log(BasicLevel.WARN,
810                      "Unable to find configuration file.");
811       throw new IOException("Unable to find configuration file .");
812     }
813     
814     if (Log.logger.isLoggable(BasicLevel.DEBUG))
815       Log.logger.log(BasicLevel.DEBUG, "Config.load : a3cmlconfig = " + a3config);
816     return a3config;
817   }
818
819   /**
820    * Gets a <code>A3CMLConfig</code> serialialized object from file.
821    *
822    * @param path path of serialized configuration file
823    * @return the <code>A3CMLConfig</code> object if file exists and is
824    * correct, null otherwise.
825    *
826    * @exception Exception
827    * unspecialized exception when reading and parsing the configuration file
828    */

829   public static A3CMLConfig getConfig(String JavaDoc path) throws Exception JavaDoc {
830     // Get the logging monitor from current server MonoLog.loggeritorFactory
831
if (Log.logger.isLoggable(BasicLevel.DEBUG))
832       Log.logger.log(BasicLevel.DEBUG, "Config.load(" + path + ")");
833     
834     A3CMLConfig a3config = null;
835     
836     File cfgFile = new File(path);
837     if (cfgFile.exists() && cfgFile.isFile()) {
838       if ((cfgFile.length() == 0)) {
839         Log.logger.log(BasicLevel.ERROR,
840                        " \"" + cfgFile.getPath() + "\", is empty.");
841         throw new IOException(" \"" + cfgFile.getPath() + "\", is empty.");
842       }
843       
844       FileInputStream fis = null;
845       try {
846         fis = new FileInputStream(cfgFile);
847         ObjectInputStream ois = new ObjectInputStream(fis);
848         a3config = (A3CMLConfig) ois.readObject();
849       } catch (Exception JavaDoc exc) {
850         Log.logger.log(BasicLevel.WARN, "Can't load configuration: " + path, exc);
851       } finally {
852         if (fis != null) fis.close();
853       }
854
855       if (Log.logger.isLoggable(BasicLevel.DEBUG))
856         Log.logger.log(BasicLevel.DEBUG,
857                        "Config.load : a3cmlconfig = " + a3config);
858       return a3config;
859     }
860
861     //search a3config in path used to load classes.
862
ClassLoader JavaDoc classLoader = null;
863     InputStream is = null;
864     try {
865       classLoader = A3CMLConfig.class.getClassLoader();
866       if (classLoader != null) {
867         Log.logger.log(BasicLevel.WARN,
868                        "Trying to find [" + path + "] using " +
869                        classLoader + " class loader.");
870         is = classLoader.getResourceAsStream(path);
871       }
872     } catch(Throwable JavaDoc t) {
873       Log.logger.log(BasicLevel.WARN,
874                      "Can't find [" + path + "] using " +
875                      classLoader + " class loader.", t);
876       is = null;
877     }
878     if (is == null) {
879       // Last ditch attempt: get the resource from the system class path.
880
Log.logger.log(BasicLevel.WARN,
881                      "Trying to find serialized config using ClassLoader.getSystemResource().");
882       is = ClassLoader.getSystemResourceAsStream(path);
883     }
884     if (is != null) {
885       ObjectInputStream ois = new ObjectInputStream(is);
886       a3config = (A3CMLConfig) ois.readObject();
887     }
888
889     if (a3config == null) {
890       Log.logger.log(BasicLevel.WARN,
891                      "Unable to find configuration file: " + path);
892       throw new IOException("Unable to find configuration file: " + path);
893     }
894     
895     if (Log.logger.isLoggable(BasicLevel.DEBUG))
896       Log.logger.log(BasicLevel.DEBUG, "Config.load : a3cmlconfig = " + a3config);
897     return a3config;
898   }
899
900 // ### save
901
// Transaction transaction = AgentServer.getTransaction();
902
// if (transaction != null) {
903
// // use transaction to save this obj
904
// if (Log.logger.isLoggable(BasicLevel.DEBUG))
905
// Log.logger.log(BasicLevel.DEBUG,
906
// "Config.save with AgentServer.transaction");
907
// // AgentServer.getTransaction().save(obj,cfgDir,cfgFileName);
908
// } else {
909
// if (Log.logger.isLoggable(BasicLevel.DEBUG))
910
// Log.logger.log(BasicLevel.DEBUG,
911
// "Config.save without transaction");
912
// File file = new File(cfgDir, cfgFileName);
913
// File temp = new File(cfgDir, cfgFileName+"_temp");
914

915 // if (cfgDir != null) {
916
// File dir = new File(cfgDir);
917
// if (!dir.exists()) dir.mkdir();
918
// }
919

920 // if (file.exists())
921
// file.renameTo(temp);
922

923 // // Save the current state of the object.
924
// FileOutputStream fos = null;
925
// try {
926
// fos = new FileOutputStream(file);
927
// ObjectOutputStream oos = new ObjectOutputStream(fos);
928
// oos.writeObject(obj);
929
// oos.flush();
930
// fos.getFD().sync();
931
// fos.close();
932
// fos = null;
933
// temp.delete();
934
// temp = null;
935
// } finally {
936
// if (fos != null) fos.close();
937
// }
938
// }
939

940 // if (Log.logger.isLoggable(BasicLevel.DEBUG)) {
941
// try {
942
// A3CML.toXML(this, null, "debugServers.xml");
943
// } catch (Exception exc) {}
944
// }
945

946   public String JavaDoc toString() {
947     StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
948     strBuf.append("(").append(super.toString());
949     strBuf.append(",properties=").append(properties);
950     strBuf.append(",domains=").append(domains);
951     strBuf.append(",servers=").append(servers);
952     strBuf.append(",clusters=").append(clusters);
953     strBuf.append(")");
954
955     return strBuf.toString();
956   }
957   
958   /* -+-+-+- -+-+-+- */
959   /* -+-+-+- This code below is needed for historic reason. -+-+-+- */
960   /* -+-+-+- It is used in mediation chain in order to find -+-+-+- */
961   /* -+-+-+- the corresponding TcpServer services. -+-+-+- */
962   /* -+-+-+- This code below is needed for historic reason. -+-+-+- */
963   /* -+-+-+- -+-+-+- */
964
965   /**
966    * Gets the argument strings for a particular service running on a server
967    * identified by its host (searchs on all servers and associated transient).
968    *
969    * @param hostname hostname
970    * @param className the service class name
971    * @return the arguments as declared in configuration file
972    * @exception UnknownServiceException
973    * The specified service is not declared on this server.
974    */

975   public final String JavaDoc getServiceArgsHost(String JavaDoc hostname,
976                                          String JavaDoc classname) throws Exception JavaDoc {
977     for (Enumeration s = servers.elements(); s.hasMoreElements(); ) {
978       A3CMLServer server = (A3CMLServer) s.nextElement();
979       if (server.hostname.equals(hostname)) {
980     try {
981       String JavaDoc args = getServiceArgs(server.sid, classname);
982       return args;
983     } catch (Exception JavaDoc exc) {}
984       }
985     }
986     throw new UnknownServiceException("Unknown service \"" + classname +
987                                       "\" on host " + hostname);
988   }
989
990   public boolean equals(Object JavaDoc obj) {
991     if (obj == null) return false;
992
993     if (obj instanceof A3CMLConfig) {
994       A3CMLConfig config = (A3CMLConfig) obj;
995
996       if (domains.equals(config.domains) &&
997           servers.equals(config.servers) &&
998           properties.equals(config.properties))
999         return true;
1000    }
1001    return false;
1002  }
1003
1004  /**
1005   * reset visited and gateway fields.
1006   */

1007  public void reset() {
1008    for (Enumeration s = servers.elements(); s.hasMoreElements(); ) {
1009      A3CMLServer server = (A3CMLServer) s.nextElement();
1010      server.visited = false;
1011      server.gateway = (short) -1;
1012    }
1013    for (Enumeration d = domains.elements(); d.hasMoreElements(); ) {
1014      A3CMLDomain domain = (A3CMLDomain) d.nextElement();
1015      domain.gateway = (short) -1;
1016    }
1017  }
1018}
1019
Popular Tags