1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 import jgroup.util.Network;
27
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public final class DistributedSystemConfig
47 implements ConfigurationObject
48 {
49
50
51
52
53
54 private static final String DOMAIN_TAG_NAME = "Domain";
55 private static final String HOST_TAG_NAME = "Host";
56 private static final String NAME_ATTRIB = "name";
57 private static final String ADDRESS_ATTRIB = "address";
58 private static final String PORT_ATTRIB = "port";
59 private static final String JDAEMONS_ATTRIB = "jdaemons";
60
61
62
63
64
65
66
67 private static final HostSet allHosts = new HostSet();
68
69
70 private static Host localHost = null;
71
72
73 private static Domain localDomain = null;
74
75
76
77
78
79
80
81 private DomainSet domains = new DomainSet();
82
83
84
85
86
87
88
89
90
91 public DistributedSystemConfig()
92 {
93 }
94
95
96
97
98
99
100
101
102
103 public void parse(Element elm)
104 throws ConfigurationException
105 {
106
107 NodeList domainList = elm.getChildNodes();
108
109
110 for (int i=1; i <= domainList.getLength(); i++) {
111 Node node = domainList.item(i);
112 if (node != null && !node.getNodeName().startsWith("#")) {
113 elm = (Element) node;
114 if (ConfigParser.checkTag(elm, DOMAIN_TAG_NAME))
115 parseDomain(elm);
116 }
117 }
118
119
120
121
122 if (localHost != null && localHost.getAddress().isLoopbackAddress()) {
123 throw new ConfigurationException("Cannot use loopback as a configured server address");
124 }
125 }
126
127
128
129
130
131
132 private void parseDomain(Element elm)
133 throws ConfigurationException
134 {
135
136
137
138 String domainname = ConfigParser.getAttrib(elm, NAME_ATTRIB, true);
139 if ("-".equals(domainname))
140 domainname = "";
141 String address = ConfigParser.getAttrib(elm, ADDRESS_ATTRIB, true);
142 int port = ConfigParser.getIntAttrib(elm, PORT_ATTRIB, false);
143 int jdaemons = ConfigParser.getIntAttrib(elm, JDAEMONS_ATTRIB, false);
144
145
146 Domain domain = null;
147 try {
148 domain = new Domain(domainname, address, port, jdaemons);
149 } catch (UnknownHostException e) {
150 throw new ConfigurationException("Unknown multicast address: " + address);
151 } catch (IllegalArgumentException e) {
152 throw new ConfigurationException(e.getMessage());
153 }
154
155
156 if (!domains.addDomain(domain))
157 throw new ConfigurationException("Multiple definitions of domain: " + domain);
158
159
160 NodeList hostList = elm.getChildNodes();
161
162 for (int i=1; i <= hostList.getLength(); i++) {
163 Node node = hostList.item(i);
164 if (node != null && !node.getNodeName().startsWith("#")) {
165 elm = (Element)node;
166 if (ConfigParser.checkTag(elm, HOST_TAG_NAME))
167 parseHost(domain, elm);
168 }
169 }
170 }
171
172
173 private void parseHost(Domain domain, Element elm)
174 throws ConfigurationException
175 {
176 String hostname = ConfigParser.getAttrib(elm, NAME_ATTRIB, true);
177 int port = ConfigParser.getIntAttrib(elm, PORT_ATTRIB, true);
178
179
180 Host host = null;
181 try {
182 host = new Host(hostname, domain, port);
183 } catch(UnknownHostException e) {
184 throw new ConfigurationException("Unknown host: " + hostname);
185 }
186
187
188 if (!domain.addHost(host))
189 throw new ConfigurationException("Multple definitions of host: " + host);
190
191
192 allHosts.addHost(host);
193
194
195 if (host.isLocal()) {
196 localHost = host;
197 localDomain = domain;
198 }
199 }
200
201
202
203
204
205
206
207
208
209 public DomainSet getDomainSet()
210 {
211 return domains;
212 }
213
214
215
216
217
218 public HostSet getAllHosts()
219 {
220 return allHosts;
221 }
222
223
224
225
226
227 public int size()
228 {
229 return allHosts.size();
230 }
231
232
233
234
235
236 public int numOfDomains()
237 {
238 return domains.size();
239 }
240
241
242
243
244
245
246
247
248
249 public static Host getHost(MemberId member)
250 {
251 return allHosts.getHost(member);
252 }
253
254
255
256
257
258 public static Host getHost(InetAddress inetAdr)
259 {
260 return allHosts.getHost(inetAdr);
261 }
262
263
264
265
266
267 public static Host getLocalHost()
268 {
269 return localHost;
270 }
271
272
273
274
275
276
277 public static Domain getLocalDomain()
278 {
279 return localDomain;
280 }
281
282
283
284
285
286
287 public boolean containsLocalHost()
288 {
289 return localHost != null;
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303 public static void setLocalHost()
304 throws UnknownHostException
305 {
306 if (localHost == null) {
307 localDomain = new Domain(Network.getLocalDomainName());
308 localHost = new Host(Network.getLocalMachineName(), localDomain, 1991);
309 allHosts.addHost(localHost);
310 System.out.println("hosts: " + allHosts);
311 }
312 }
313
314
315
316
317
318
319
320
321 public static boolean updateAll()
322 {
323 HostSet availHosts = allHosts.getAvailHosts();
324 return !availHosts.containsAll(allHosts);
325 }
326
327
328
329
330
331
332 public int hashCode()
333 {
334 return domains.hashCode() ^ allHosts.hashCode();
335 }
336
337
338
339
340
341 public String toString()
342 {
343 return toString(false);
344 }
345
346
347
348
349
350
351 public String toString(boolean full)
352 {
353 StringBuilder buf = new StringBuilder();
354 buf.append("[DistributedSystemConfig:\n");
355 buf.append(domains.toString(full));
356 buf.append("]");
357 return buf.toString();
358 }
359
360 }