KickJava   Java API By Example, From Geeks To Geeks.

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


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): Nicolas Tachker (Scalagent)
20  * Contributor(s):
21  */

22 package fr.dyade.aaa.agent.conf;
23
24 import java.io.*;
25 import java.util.*;
26
27 import org.objectweb.util.monolog.api.BasicLevel;
28 import org.objectweb.util.monolog.api.Logger;
29
30 public class A3CMLCluster implements Serializable {
31
32   public short sid = -1;
33   public String JavaDoc name = null;
34   public String JavaDoc jvmArgs = null;
35   public Hashtable servers = null;
36   public Hashtable properties = null;
37
38   public A3CMLCluster(short sid,
39                       String JavaDoc name) throws Exception JavaDoc {
40     this.sid = sid;
41     this.name = name;
42     this.properties = new Hashtable();
43     this.servers = new Hashtable();
44   }
45
46   /**
47    * Adds a server.
48    *
49    * @param server The description of added server.
50    * @exception DuplicateServerException
51    * If the server already exist.
52    */

53   public final void addServer(A3CMLServer server) throws DuplicateServerException {
54     Short JavaDoc id = new Short JavaDoc(server.sid);
55     if (servers.containsKey(id))
56       throw new DuplicateServerException("Duplicate server id. #" + server.sid);
57     server.name = "cluster_" + sid + "_" + server.sid;
58     server.sid = sid;
59     servers.put(id, server);
60   }
61   
62   /**
63    * Removes a server.
64    *
65    * @param sid The unique server identifier.
66    * @return The server description if exists.
67    * @exception UnknownServerException
68    * If the server does not exist.
69    */

70   public final A3CMLServer removeServer(short sid) throws UnknownServerException {
71     A3CMLServer server = null;
72     Short JavaDoc id = new Short JavaDoc(sid);
73     if (servers.containsKey(id))
74       server = (A3CMLServer) servers.remove(id);
75     else
76       throw new UnknownServerException("Unknown server id. #" + sid);
77     return server;
78   }
79   
80   /**
81    * Remove a server.
82    *
83    * @param name The server name.
84    * @return The server description if exists.
85    * @exception UnknownServerException
86    * If the server does not exist.
87    */

88   public final A3CMLServer removeServer(String JavaDoc name) throws UnknownServerException {
89     return removeServer(getServerIdByName(name));
90   }
91   
92   /**
93    * Returns true if the configuration contains a server with specified id.
94    *
95    * @param sid server id
96    * @return true if contain sid; false otherwise.
97    */

98   public final boolean containsServer(short sid) {
99     return servers.containsKey(new Short JavaDoc(sid));
100   }
101   
102   /**
103    * Gets a server identifier from its name.
104    *
105    * @param name The server name.
106    * @return The server identifier.
107    * @exception UnknownServerException
108    * If the server does not exist.
109    */

110   public short getServerIdByName(String JavaDoc name) throws UnknownServerException {
111     for (Enumeration s = servers.elements(); s.hasMoreElements(); ) {
112       A3CMLServer server = (A3CMLServer) s.nextElement();
113       if (server.name.equals(name)) return server.sid;
114     }
115     throw new UnknownServerException("Unknown server " + name);
116   }
117
118   /**
119    * Returns true if the configuration contains a server with specified name.
120    *
121    * @param name server name
122    * @return true if contain name; false otherwise.
123    */

124   public final boolean containsServer(String JavaDoc name) {
125     try {
126       getServerIdByName(name);
127     } catch (UnknownServerException exc) {
128       return false;
129     }
130     return true;
131   }
132
133   /**
134    * Returns the description of a server.
135    *
136    * @param name The server identifier.
137    * @return The server description if exist.
138    * @exception UnknownServerException
139    * If the server does not exist.
140    */

141   public final A3CMLServer getServer(short sid) throws UnknownServerException {
142     A3CMLServer server = (A3CMLServer) servers.get(new Short JavaDoc(sid));
143     if (server == null)
144       throw new UnknownServerException("Unknown server id. #" + sid);
145     return server;
146   }
147
148   /**
149    * Returns the description of a server.
150    *
151    * @param name The server name.
152    * @return The server description if exist.
153    * @exception UnknownServerException
154    * If the server does not exist.
155    */

156   public final A3CMLServer getServer(String JavaDoc name) throws UnknownServerException {
157     for (Enumeration s = servers.elements(); s.hasMoreElements(); ) {
158       A3CMLServer server = (A3CMLServer) s.nextElement();
159       if (server.name.equals(name)) return server;
160     }
161     throw new UnknownServerException("Unknown server id for server " + name);
162   }
163
164   /**
165    * add property
166    *
167    * @param prop A3CMLProperty
168    * @return the previous value of the specified prop.name in
169    * this hashtable, or null if it did not have one.
170    * @exception Exception
171    */

172   public final A3CMLProperty addProperty(A3CMLProperty prop) throws Exception JavaDoc {
173     return (A3CMLProperty) properties.put(prop.name, prop);
174   }
175
176   /**
177    * remove property
178    *
179    * @param name property name
180    * @return the value to which the name had been mapped in
181    * this hashtable, or null if the name did not have a mapping.
182    */

183   public final A3CMLProperty removeProperty(String JavaDoc name) {
184     return (A3CMLProperty) properties.remove(name);
185   }
186
187   /**
188    * contains property
189    *
190    * @param name property name
191    * @return true if contain name; false otherwise.
192    */

193   public final boolean containsProperty(String JavaDoc name) {
194     return properties.containsKey(name);
195   }
196
197   /**
198    * Returns the specified property.
199    */

200   public final A3CMLProperty getProperty(String JavaDoc name) {
201     return (A3CMLProperty) properties.get(name);
202   }
203
204   /**
205    * Get the JVM argument for a particular agent server identified by its id.
206    *
207    * @param id agent server identifier.
208    * @return the arguments as declared in configuration file
209    * @exception UnknownServerException
210    * The specified server does not exist.
211    */

212   public final String JavaDoc getJvmArgs(short sid) throws UnknownServerException {
213     A3CMLServer server = getServer(sid);
214     return server.getJvmArgs();
215   }
216   
217   /**
218    * Get the JVM argument for a particular agent server identified by its name.
219    *
220    * @param name agent server name.
221    * @return the arguments as declared in configuration file
222    * @exception UnknownServerException
223    * The specified server does not exist.
224    */

225   public final String JavaDoc getJvmArgs(String JavaDoc name) throws UnknownServerException {
226     A3CMLServer server = getServer(name);
227     return server.getJvmArgs();
228   }
229
230   public String JavaDoc toString() {
231     StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
232     strBuf.append("(").append(super.toString());
233     strBuf.append(",sid=").append(sid);
234     strBuf.append(",name=").append(name);
235     strBuf.append(",jvmArgs=").append(jvmArgs);
236     strBuf.append(",properties=").append(properties);
237     strBuf.append(",servers=").append(servers);
238     strBuf.append(")");
239
240     return strBuf.toString();
241   }
242   
243   public boolean equals(Object JavaDoc obj) {
244     if (obj == null) return false;
245
246     if (obj instanceof A3CMLCluster) {
247       A3CMLCluster cluster = (A3CMLCluster) obj;
248
249       if (sid == cluster.sid &&
250           name.equals(cluster.name) &&
251           servers.equals(cluster.servers) &&
252           properties.equals(cluster.properties))
253         return true;
254     }
255     return false;
256   }
257 }
258
Popular Tags