KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > L2ConfigForL1Object


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.config.schema;
6
7 import org.apache.xmlbeans.XmlException;
8 import org.apache.xmlbeans.XmlInteger;
9 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 JavaDoc 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, ConfigContext systemContext) {
34     Assert.assertNotNull(l2sContext);
35     Assert.assertNotNull(systemContext);
36
37     this.l2sContext = l2sContext;
38     this.systemContext = systemContext;
39
40     this.l2sContext.ensureRepositoryProvides(Servers.class);
41     this.systemContext.ensureRepositoryProvides(System JavaDoc.class);
42
43     this.defaultL2Data = new L2Data(DEFAULT_HOST, getL2IntDefault("server/dso-port"));
44
45     this.l2Data = new ObjectArrayXPathBasedConfigItem(this.l2sContext, ".", new L2Data[] { this.defaultL2Data }) {
46       protected Object JavaDoc fetchDataFromXmlObject(XmlObject xmlObject) {
47         Server[] l2Array = ((Servers) xmlObject).getServerArray();
48         L2Data[] data;
49
50         if (l2Array == null || l2Array.length == 0) {
51           data = new L2Data[] { defaultL2Data };
52         } else {
53           data = new L2Data[l2Array.length];
54
55           for (int i = 0; i < data.length; ++i) {
56             Server l2 = l2Array[i];
57             String JavaDoc host = l2.getHost();
58             if (host == null) host = l2.getName();
59
60             if (host == null) host = defaultL2Data.host();
61             int dsoPort = l2.getDsoPort() > 0 ? l2.getDsoPort() : defaultL2Data.dsoPort();
62
63             data[i] = new L2Data(host, dsoPort);
64           }
65         }
66
67         return data;
68       }
69     };
70   }
71
72   private int getL2IntDefault(String JavaDoc xpath) {
73     try {
74       return ((XmlInteger) l2sContext.defaultFor(xpath)).getBigIntegerValue().intValue();
75     } catch (XmlException xmle) {
76       throw Assert.failure("Can't fetch default for " + xpath + "?", xmle);
77     }
78   }
79
80   public ObjectArrayConfigItem l2Data() {
81     return this.l2Data;
82   }
83
84 }
85
Popular Tags