KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
25  * The class <code>Server</code> describes an agent server.
26  */

27 public class A3CMLServer implements Serializable {
28   public short sid = -1;
29   public String JavaDoc name = null;
30   public String JavaDoc hostname = null;
31   /** Domain (1st hop) used to access this server from current node. */
32   public String JavaDoc domain = null;
33   /**
34    * Communication port if the server is directly accessible by the root
35    * server.
36    */

37   public int port = -1;
38   public Hashtable nat = null;
39   public Vector networks = null;
40   public Vector services = null;
41   public String JavaDoc jvmArgs = null;
42   public Hashtable properties = null;
43   /**
44    * True if the server is already visited during configuration phase.
45    */

46   public boolean visited = false;
47   /**
48    * For persistent server, Id. of router (1st hop) used to access the
49    * server from current node. if -1 the server is not accessible. This
50    * value is fixed during configuration phase.
51    */

52   public short gateway = -1;
53   /**
54    * Logical distance between this server and the root one, i.e. the number
55    * of hops to reach it from the local one.
56    */

57   public int hops = -1;
58
59   public A3CMLServer(short sid,
60                      String JavaDoc name,
61                      String JavaDoc hostname) throws Exception JavaDoc {
62     this.sid = sid;
63     if ((name == null) || (name.length() == 0))
64       this.name = "s#" + sid;
65     else
66       this.name = name;
67     this.hostname = hostname;
68     this.services = new Vector();
69     this.networks = new Vector();
70   }
71
72   public void addNetwork(A3CMLNetwork newNetwork) throws Exception JavaDoc {
73     for (int i = 0; i < networks.size(); i++) {
74       A3CMLNetwork network = (A3CMLNetwork)networks.elementAt(i);
75       if (network.domain.equals(newNetwork.domain)) {
76         throw new Exception JavaDoc("Network " + newNetwork.domain + "already added");
77       }
78     }
79     networks.addElement(newNetwork);
80   }
81
82   public void removeNetwork(String JavaDoc domainName) {
83     for (int i = 0; i < networks.size(); i++) {
84       A3CMLNetwork network = (A3CMLNetwork)networks.elementAt(i);
85       if (network.domain.equals(domainName)) {
86         networks.removeElementAt(i);
87       }
88     }
89   }
90
91   public void addService(A3CMLService newService) throws Exception JavaDoc {
92     for (int i = 0; i < services.size(); i++) {
93       A3CMLService service = (A3CMLService)services.elementAt(i);
94       if (service.classname.equals(newService.classname)) {
95         throw new Exception JavaDoc("Service " + newService.classname + "already added");
96       }
97     }
98     services.addElement(newService);
99   }
100
101   public void removeService(String JavaDoc serviceClassName) {
102     for (int i = 0; i < services.size(); i++) {
103       A3CMLService service = (A3CMLService)services.elementAt(i);
104       if (service.classname.equals(serviceClassName)) {
105         services.removeElementAt(i);
106       }
107     }
108   }
109
110   public A3CMLProperty addProperty(A3CMLProperty prop) {
111     if (properties == null)
112       properties = new Hashtable();
113     return (A3CMLProperty) properties.put(prop.name, prop);
114   }
115   
116   public A3CMLProperty getProperty(String JavaDoc name) {
117     if (properties == null) return null;
118     return (A3CMLProperty) properties.get(name);
119   }
120   
121   public A3CMLProperty removeProperty(String JavaDoc name) {
122     if (properties != null)
123       return (A3CMLProperty) properties.remove(name);
124     return null;
125   }
126   
127   public boolean containsProperty(String JavaDoc name) {
128     if (properties != null)
129       return properties.containsKey(name);
130     return false;
131   }
132
133   public A3CMLNat addNat(A3CMLNat natElement) {
134     if (nat == null)
135       nat = new Hashtable();
136     return (A3CMLNat) nat.put(new Short JavaDoc(natElement.sid), natElement);
137   }
138
139   public A3CMLNat getNat(short sid) {
140     if (nat == null) return null;
141     return (A3CMLNat) nat.get(new Short JavaDoc(sid));
142   }
143   
144   public A3CMLNat removeNat(short sid) {
145     if (nat != null)
146       return (A3CMLNat) nat.remove(new Short JavaDoc(sid));
147     return null;
148   }
149   
150   public boolean containsNat(short sid) {
151     if (nat != null)
152       return nat.containsKey(new Short JavaDoc(sid));
153     return false;
154   }
155   public final String JavaDoc getJvmArgs() {
156     if (jvmArgs != null) return jvmArgs;
157     return "";
158   }
159
160   public final A3CMLService getService(String JavaDoc classname) throws UnknownServiceException {
161     if (services != null) {
162       for (int i = services.size() - 1; i >= 0; i--) {
163     A3CMLService service = (A3CMLService) services.elementAt(i);
164     if (service.classname.equals(classname))
165       return service;
166       }
167     }
168     throw new UnknownServiceException("Unknown service \"" + classname +
169                                       "\" on server#" + sid);
170   }
171
172   public final String JavaDoc getServiceArgs(String JavaDoc classname) throws UnknownServiceException {
173     if (services != null) {
174       for (int i = services.size() -1; i >=0; i--) {
175     A3CMLService service = (A3CMLService) services.elementAt(i);
176     if (service.classname.equals(classname))
177       return service.args;
178       }
179     }
180     throw new UnknownServiceException("Unknown service \"" + classname +
181                                       "\" on server#" + sid);
182   }
183
184   public A3CMLNetwork getNetwork(String JavaDoc domainName) {
185     for (int i = networks.size() -1; i >=0; i--) {
186       A3CMLNetwork nw = (A3CMLNetwork) networks.elementAt(i);
187       if (nw.domain.equals(domainName)) {
188         return nw;
189       }
190     }
191     return null;
192   }
193
194   public A3CMLServer duplicate(Hashtable context) throws Exception JavaDoc {
195     A3CMLServer clone = null;
196     Short JavaDoc serverSid = new Short JavaDoc(sid);
197     if (!context.containsKey(serverSid)) {
198       clone = duplicate();
199       context.put(serverSid,clone);
200     } else
201       clone = (A3CMLServer) context.get(serverSid);
202     return clone;
203   }
204
205   public A3CMLServer duplicate() throws Exception JavaDoc {
206     A3CMLServer clone = new A3CMLServer(sid,
207                                         name,
208                                         hostname);
209     if (networks != null) {
210       for (Enumeration n = networks.elements(); n.hasMoreElements(); )
211         clone.networks.addElement(
212           ((A3CMLNetwork) n.nextElement()).duplicate());
213     }
214     clone.gateway = gateway;
215     clone.domain = domain;
216     clone.port = port;
217
218     // super class
219
clone.visited = visited;
220     if (services != null) {
221       for (Enumeration e = services.elements(); e.hasMoreElements(); )
222         clone.services.addElement(
223           ((A3CMLService) e.nextElement()).duplicate());
224     }
225     if (properties != null) {
226       for (Enumeration e = properties.keys(); e.hasMoreElements(); ) {
227         
228         String JavaDoc pName = (String JavaDoc) e.nextElement();
229         A3CMLProperty prop = (A3CMLProperty) properties.get(pName);
230         if (prop != null) {
231           if (clone.properties == null) clone.properties = new Hashtable();
232           clone.properties.put(pName, prop.duplicate());
233         }
234       }
235     }
236     if (nat != null) {
237       for (Enumeration e = nat.elements(); e.hasMoreElements(); )
238         clone.addNat(
239           ((A3CMLNat) e.nextElement()).duplicate());
240     }
241     clone.jvmArgs = jvmArgs;
242
243     return clone;
244   }
245
246   public String JavaDoc toString() {
247     StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
248     strBuf.append("(");
249     strBuf.append(super.toString());
250     strBuf.append(",name=").append(name);
251     strBuf.append(",sid=").append(sid);
252     strBuf.append(",hostname=").append(hostname);
253     strBuf.append(",port=").append(port);
254     strBuf.append(",domain=").append(domain);
255     strBuf.append(",visited=").append(visited);
256     strBuf.append(",networks=").append(networks);
257     strBuf.append(",jvmArgs=").append(jvmArgs);
258     strBuf.append(",services=").append(services);
259     strBuf.append(",properties=").append(properties);
260     strBuf.append(",nat=").append(nat);
261     strBuf.append(",gateway=").append(gateway);
262     strBuf.append(",hops=").append(hops);
263     strBuf.append(")");
264     return strBuf.toString();
265   }
266
267   public boolean equals(Object JavaDoc obj) {
268     if (obj == null) return false;
269
270     if (obj instanceof A3CMLServer) {
271       A3CMLServer server = (A3CMLServer) obj;
272       if ((sid == server.sid) &&
273           name.equals(server.name) &&
274           ((hostname == server.hostname) ||
275            ((hostname != null) && hostname.equals(server.hostname))) &&
276           ((domain == server.domain) ||
277            ((domain != null) && domain.equals(server.domain))) &&
278           (port == server.port) &&
279           services.equals(server.services) &&
280           ((jvmArgs == server.jvmArgs) ||
281            ((jvmArgs != null) && jvmArgs.equals(server.jvmArgs))) &&
282           ((properties == server.properties) ||
283            ((properties != null) && properties.equals(server.properties))) &&
284           ((nat == server.nat) ||
285            ((nat != null) && nat.equals(server.nat))) &&
286           networks.equals(server.networks) &&
287           (visited == server.visited) &&
288           (gateway == server.gateway))
289         return true;
290     }
291     return false;
292   }
293 }
294
Popular Tags