01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.config.schema;
06:
07: import org.apache.xmlbeans.XmlException;
08: import org.apache.xmlbeans.XmlInteger;
09: import org.apache.xmlbeans.XmlObject;
10:
11: import com.tc.config.schema.context.ConfigContext;
12: import com.tc.config.schema.dynamic.ObjectArrayConfigItem;
13: import com.tc.config.schema.dynamic.ObjectArrayXPathBasedConfigItem;
14: import com.tc.util.Assert;
15: import com.terracottatech.config.Server;
16: import com.terracottatech.config.Servers;
17: import com.terracottatech.config.System;
18:
19: /**
20: * The standard implementation of {@link L2ConfigForL1}.
21: */
22: public class L2ConfigForL1Object implements L2ConfigForL1 {
23:
24: private static final String DEFAULT_HOST = "localhost";
25:
26: private final ConfigContext l2sContext;
27: private final ConfigContext systemContext;
28:
29: private final ObjectArrayConfigItem l2Data;
30:
31: private final L2Data defaultL2Data;
32:
33: public L2ConfigForL1Object(ConfigContext l2sContext,
34: ConfigContext systemContext) {
35: this (l2sContext, systemContext, null);
36: }
37:
38: public L2ConfigForL1Object(ConfigContext l2sContext,
39: ConfigContext systemContext, int[] dsoPorts) {
40: Assert.assertNotNull(l2sContext);
41: Assert.assertNotNull(systemContext);
42:
43: this .l2sContext = l2sContext;
44: this .systemContext = systemContext;
45:
46: this .l2sContext.ensureRepositoryProvides(Servers.class);
47: this .systemContext.ensureRepositoryProvides(System.class);
48:
49: this .defaultL2Data = new L2Data(DEFAULT_HOST,
50: getL2IntDefault("server/dso-port"));
51:
52: this .l2Data = new ObjectArrayXPathBasedConfigItem(
53: this .l2sContext, ".", new L2Data[] { defaultL2Data }) {
54: protected Object fetchDataFromXmlObject(XmlObject xmlObject) {
55: Server[] l2Array = ((Servers) xmlObject)
56: .getServerArray();
57: L2Data[] data;
58:
59: if (l2Array == null || l2Array.length == 0) {
60: data = new L2Data[] { defaultL2Data };
61: } else {
62: data = new L2Data[l2Array.length];
63:
64: for (int i = 0; i < data.length; ++i) {
65: Server l2 = l2Array[i];
66: String host = l2.getHost();
67: if (host == null)
68: host = l2.getName();
69:
70: if (host == null)
71: host = defaultL2Data.host();
72: int dsoPort = l2.getDsoPort() > 0 ? l2
73: .getDsoPort() : defaultL2Data.dsoPort();
74:
75: data[i] = new L2Data(host, dsoPort);
76: }
77: }
78:
79: return data;
80: }
81: };
82: }
83:
84: private int getL2IntDefault(String xpath) {
85: try {
86: return ((XmlInteger) l2sContext.defaultFor(xpath))
87: .getBigIntegerValue().intValue();
88: } catch (XmlException xmle) {
89: throw Assert.failure("Can't fetch default for " + xpath
90: + "?", xmle);
91: }
92: }
93:
94: public ObjectArrayConfigItem l2Data() {
95: return this.l2Data;
96: }
97:
98: }
|