1 /*
2 * Copyright (c) 1998-2002 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.relacs.config;
20
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23
24 import jgroup.core.ConfigurationException;
25 import jgroup.core.MemberId;
26
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29 import org.w3c.dom.NodeList;
30
31
32 /**
33 * The <code>ClientsConfig</code> object contains the set of clients
34 * on which client applications may be started.
35 *
36 * @author Hein Meling
37 * @since Jgroup 2.1
38 */
39 public final class ClientsConfig
40 implements ConfigurationObject
41 {
42
43 ////////////////////////////////////////////////////////////////////////////////////////////
44 // Tags
45 ////////////////////////////////////////////////////////////////////////////////////////////
46
47 private static final String DOMAIN_TAG_NAME = "Domain";
48 private static final String HOST_TAG_NAME = "Host";
49 private static final String NAME_ATTRIB = "name";
50 private static final String ADDRESS_ATTRIB = "address";
51 private static final String PORT_ATTRIB = "port";
52
53
54 ////////////////////////////////////////////////////////////////////////////////////////////
55 // Static fields
56 ////////////////////////////////////////////////////////////////////////////////////////////
57
58 /** Set of all hosts in this distributed system; independent of domains. */
59 private static final HostSet allHosts = new HostSet();
60
61 /** The endpoint considered to be the local host. */
62 private static Host localHost = null;
63
64 /** The endpoint considered to be the local domain. */
65 private static Domain localDomain = null;
66
67
68 ////////////////////////////////////////////////////////////////////////////////////////////
69 // Fields
70 ////////////////////////////////////////////////////////////////////////////////////////////
71
72 /** Set of domains in this distributed system */
73 private DomainSet domains = new DomainSet();
74
75
76 ////////////////////////////////////////////////////////////////////////////////////////////
77 // Constructors
78 ////////////////////////////////////////////////////////////////////////////////////////////
79
80 /**
81 * Constructs an empty distributed system.
82 */
83 public ClientsConfig()
84 {
85 }
86
87
88 ////////////////////////////////////////////////////////////////////////////////////////////
89 // Methods from ConfigurationObject
90 ////////////////////////////////////////////////////////////////////////////////////////////
91
92 /**
93 * Parses the specified element.
94 */
95 public void parse(Element elm)
96 throws ConfigurationException
97 {
98 /* Nodes within DS should be Domain tags. */
99 NodeList domainList = elm.getChildNodes();
100
101 /* Parse the list of domains */
102 for (int i=1; i <= domainList.getLength(); i++) {
103 Node node = domainList.item(i);
104 if (node != null && !node.getNodeName().startsWith("#")) {
105 elm = (Element) node;
106 if (ConfigParser.checkTag(elm, DOMAIN_TAG_NAME))
107 parseDomain(elm);
108 }
109 }
110 }
111
112
113 ////////////////////////////////////////////////////////////////////////////////////////////
114 // Private methods for parsing
115 ////////////////////////////////////////////////////////////////////////////////////////////
116
117 private void parseDomain(Element elm)
118 throws ConfigurationException
119 {
120 /*
121 * Read attributes; "-" in the domain name corresponds to empty
122 */
123 String domainname = ConfigParser.getAttrib(elm, NAME_ATTRIB, true);
124 if ("-".equals(domainname))
125 domainname = "";
126 String address = ConfigParser.getAttrib(elm, ADDRESS_ATTRIB, true);
127 int port = ConfigParser.getIntAttrib(elm, PORT_ATTRIB, false);
128
129 /* Create a new domain object with the parsed information */
130 Domain domain = null;
131 try {
132 domain = new Domain(domainname, address, port);
133 } catch (UnknownHostException e) {
134 throw new ConfigurationException("Unknown multicast address: " + address);
135 } catch (IllegalArgumentException e) {
136 throw new ConfigurationException(e.getMessage());
137 }
138
139 /* Add the domain to the system */
140 if (!domains.addDomain(domain))
141 throw new ConfigurationException("Multiple definitions of domain: " + domain);
142
143 /* Nodes within the domain should be host tags. */
144 NodeList hostList = elm.getChildNodes();
145 /* Parse the list of hosts */
146 for (int i=1; i <= hostList.getLength(); i++) {
147 Node node = hostList.item(i);
148 if (node != null && !node.getNodeName().startsWith("#")) {
149 elm = (Element)node;
150 if (ConfigParser.checkTag(elm, HOST_TAG_NAME))
151 parseHost(domain, elm);
152 }
153 }
154 }
155
156
157 private void parseHost(Domain domain, Element elm)
158 throws ConfigurationException
159 {
160 String hostname = ConfigParser.getAttrib(elm, NAME_ATTRIB, true);
161 int port = ConfigParser.getIntAttrib(elm, PORT_ATTRIB, true);
162
163 /* Create a new host object with the parsed information */
164 Host host = null;
165 try {
166 host = new Host(hostname, domain, port);
167 } catch(UnknownHostException e) {
168 throw new ConfigurationException("Unknown host: " + hostname);
169 }
170
171 /* Add the host to this domain */
172 if (!domain.addHost(host))
173 throw new ConfigurationException("Multple definitions of host: " + host);
174
175 /* Add the host to the all hosts set */
176 allHosts.addHost(host);
177
178 /* Check if this host is a local host */
179 if (host.isLocal()) {
180 localHost = host;
181 localDomain = domain;
182 }
183 }
184
185
186 ////////////////////////////////////////////////////////////////////////////////////////////
187 // Access methods
188 ////////////////////////////////////////////////////////////////////////////////////////////
189
190 /**
191 * Returns the parsed set of domains.
192 */
193 public DomainSet getDomainSet()
194 {
195 return domains;
196 }
197
198
199 /**
200 * Returns a set of all hosts parsed; independent of domain.
201 */
202 public HostSet getAllHosts()
203 {
204 return allHosts;
205 }
206
207
208 /**
209 * Returns the size of all hosts parsed; independent of domain.
210 */
211 public int size()
212 {
213 return allHosts.size();
214 }
215
216
217 /**
218 * Returns the number of domains parsed.
219 */
220 public int numOfDomains()
221 {
222 return domains.size();
223 }
224
225
226 ////////////////////////////////////////////////////////////////////////////////////////////
227 // Access methods - only for server side
228 ////////////////////////////////////////////////////////////////////////////////////////////
229
230 /**
231 * Returns the host object associated with the given member.
232 */
233 public static Host getHost(MemberId member)
234 {
235 return allHosts.getHost(member);
236 }
237
238
239 /**
240 * Returns the host object associated with the given internet address.
241 */
242 public static Host getHost(InetAddress inetAdr)
243 {
244 return allHosts.getHost(inetAdr);
245 }
246
247
248 /**
249 * Returns the endpoint that is considered local to this host.
250 */
251 public Host getLocalHost()
252 {
253 return localHost;
254 }
255
256
257 /**
258 * Returns the domain endpoint that is considered local to this
259 * domain.
260 */
261 public Domain getLocalDomain()
262 {
263 return localDomain;
264 }
265
266
267 /**
268 * Returns true if the local host is specified in the distributed
269 * system configuration; false is returned otherwise.
270 */
271 public boolean containsLocalHost()
272 {
273 return localHost != null;
274 }
275
276
277 ////////////////////////////////////////////////////////////////////////////////////////////
278 // Override methods in Object
279 ////////////////////////////////////////////////////////////////////////////////////////////
280
281 public int hashCode()
282 {
283 return domains.hashCode() ^ allHosts.hashCode();
284 }
285
286
287 /**
288 * Returns a string representation of this object
289 */
290 public String toString()
291 {
292 return toString(false);
293 }
294
295 /**
296 * Returns a string representation of this object;
297 *
298 * @param full If true, prints also the complete host set for each domain.
299 */
300 public String toString(boolean full)
301 {
302 StringBuilder buf = new StringBuilder();
303 buf.append("[ClientsConfig:\n");
304 buf.append(domains.toString(full));
305 buf.append("]");
306 return buf.toString();
307 }
308
309 } // END ClientsConfig