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.object.config;
06:
07: import org.apache.commons.lang.StringUtils;
08:
09: import com.tc.config.schema.L2ConfigForL1.L2Data;
10: import com.tc.config.schema.dynamic.ConfigItem;
11: import com.tc.config.schema.dynamic.DerivedConfigItem;
12: import com.tc.logging.CustomerLogging;
13: import com.tc.logging.TCLogger;
14: import com.tc.net.core.ConnectionInfo;
15: import com.tc.util.Assert;
16: import com.tc.util.stringification.OurStringBuilder;
17:
18: /**
19: * Returns a {@link ConnectionInfo} array from the L2 data.
20: */
21: public class ConnectionInfoConfigItem extends DerivedConfigItem {
22: static TCLogger consoleLogger = CustomerLogging.getConsoleLogger();
23:
24: public ConnectionInfoConfigItem(ConfigItem l2DataConfigItem) {
25: super (new ConfigItem[] { l2DataConfigItem });
26: }
27:
28: protected Object createValueFrom(ConfigItem[] fromWhich) {
29: ConnectionInfo[] out;
30:
31: String serversProperty = System.getProperty("tc.server");
32: if (serversProperty != null
33: && (serversProperty = serversProperty.trim()) != null
34: && serversProperty.length() > 0) {
35: consoleLogger.info("tc.server: " + serversProperty);
36:
37: String[] serverDescs = StringUtils.split(serversProperty,
38: ",");
39: int count = serverDescs.length;
40:
41: out = new ConnectionInfo[count];
42: for (int i = 0; i < count; i++) {
43: String[] serverDesc = StringUtils.split(serverDescs[i],
44: ":");
45: String host = serverDesc.length > 0 ? serverDesc[0]
46: : "localhost";
47: int dsoPort = 9510;
48:
49: if (serverDesc.length == 2) {
50: try {
51: dsoPort = Integer.parseInt(serverDesc[1]);
52: } catch (NumberFormatException nfe) {
53: consoleLogger
54: .warn("Cannot parse port for tc.server element '"
55: + serverDescs[i]
56: + "'; Using default of 9510.");
57: }
58: }
59:
60: out[i] = new ConnectionInfo(host, dsoPort);
61: }
62: } else {
63: Assert.eval(fromWhich.length == 1);
64:
65: L2Data[] l2Data = (L2Data[]) fromWhich[0].getObject();
66: out = new ConnectionInfo[l2Data.length];
67:
68: for (int i = 0; i < out.length; ++i) {
69: out[i] = new ConnectionInfo(l2Data[i].host(), l2Data[i]
70: .dsoPort());
71: }
72: }
73:
74: return out;
75: }
76:
77: public String toString() {
78: return new OurStringBuilder(this,
79: OurStringBuilder.COMPACT_STYLE).appendSuper(
80: super.toString()).toString();
81: }
82: }
|