001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.test.activepassive;
006:
007: import org.apache.commons.io.FileUtils;
008:
009: import com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory;
010: import com.tc.config.schema.test.ApplicationConfigBuilder;
011: import com.tc.config.schema.test.HaConfigBuilder;
012: import com.tc.config.schema.test.L2ConfigBuilder;
013: import com.tc.config.schema.test.L2SConfigBuilder;
014: import com.tc.config.schema.test.SystemConfigBuilder;
015: import com.tc.config.schema.test.TerracottaConfigBuilder;
016: import com.tc.logging.TCLogger;
017: import com.tc.logging.TCLogging;
018:
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.PrintWriter;
023:
024: public class ActivePassiveServerConfigCreator {
025: public static final String DEV_MODE = "development";
026: public static final String PROD_MODE = "production";
027:
028: private static TCLogger logger = TCLogging
029: .getTestingLogger(ActivePassiveServerConfigCreator.class);
030: private final int serverCount;
031: private final int[] dsoPorts;
032: private final int[] jmxPorts;
033: private final int[] l2GroupPorts;
034: private final String[] serverNames;
035: private final String serverPersistence;
036: private final boolean serverDiskless;
037: private final String configModel;
038: private final File configFile;
039: private final File tempDir;
040: private final TestTVSConfigurationSetupManagerFactory configFactory;
041: private final String[] dataLocations;
042:
043: public ActivePassiveServerConfigCreator(int serverCount,
044: int[] dsoPorts, int[] jmxPorts, int[] l2GroupPorts,
045: String[] serverNames, String serverPersistence,
046: boolean serverDiskless, String configModel,
047: File configFile, File tempDir,
048: TestTVSConfigurationSetupManagerFactory configFactory) {
049: this .serverCount = serverCount;
050: this .dsoPorts = dsoPorts;
051: this .jmxPorts = jmxPorts;
052: this .l2GroupPorts = l2GroupPorts;
053: this .serverNames = serverNames;
054: this .serverPersistence = serverPersistence;
055: this .serverDiskless = serverDiskless;
056: this .configModel = configModel;
057: this .configFile = configFile;
058: this .tempDir = tempDir;
059: this .configFactory = configFactory;
060: dataLocations = new String[serverCount];
061:
062: checkPersistenceAndDiskLessMode();
063: }
064:
065: public String getDataLocation(int i) {
066: if (i < 1 && i > dataLocations.length) {
067: throw new AssertionError("Invalid index=[" + i
068: + "]... there are [" + dataLocations.length
069: + "] servers involved in this test.");
070: }
071: if (serverDiskless) {
072: return dataLocations[i];
073: } else {
074: return dataLocations[0];
075: }
076: }
077:
078: private void checkPersistenceAndDiskLessMode() {
079: if (!serverDiskless
080: && serverPersistence
081: .equals(ActivePassivePersistenceMode.TEMPORARY_SWAP_ONLY)) {
082: throw new AssertionError(
083: "The servers are not running in diskless mode so persistence mode should be set to permanent-store");
084: }
085: }
086:
087: private void checkConfigurationModel() {
088: if (!configModel.equals(DEV_MODE)
089: && !configModel.equals(PROD_MODE)) {
090: throw new AssertionError("Unknown operating mode.");
091: }
092: }
093:
094: private void cleanDataDirectory(String dataLocation)
095: throws IOException {
096: File dbDir = new File(dataLocation);
097: logger.info("DBHome: " + dbDir.getAbsolutePath());
098: if (dbDir.exists()) {
099: FileUtils.cleanDirectory(dbDir);
100: }
101: }
102:
103: public void writeL2Config() throws Exception {
104: checkConfigurationModel();
105: SystemConfigBuilder system = SystemConfigBuilder
106: .newMinimalInstance();
107: system.setConfigurationModel(configModel);
108:
109: String dataLocationHome = tempDir.getAbsolutePath()
110: + File.separator + "server-data";
111: cleanDataDirectory(dataLocationHome);
112: String logLocationHome = tempDir.getAbsolutePath()
113: + File.separator + "server-logs" + File.separator;
114:
115: boolean gcEnabled = configFactory.getGCEnabled();
116: boolean gcVerbose = configFactory.getGCVerbose();
117: int gcIntervalInSec = configFactory.getGCIntervalInSec();
118:
119: L2ConfigBuilder[] l2s = new L2ConfigBuilder[serverCount];
120: for (int i = 0; i < serverCount; i++) {
121: L2ConfigBuilder l2 = new L2ConfigBuilder();
122: if (serverDiskless) {
123: dataLocations[i] = dataLocationHome + File.separator
124: + "server-" + i;
125: l2.setData(dataLocations[i]);
126: } else {
127: l2.setData(dataLocationHome);
128: if (dataLocations[0] == null) {
129: dataLocations[0] = dataLocationHome;
130: }
131: }
132: l2.setLogs(logLocationHome + "server-" + i);
133: l2.setName(serverNames[i]);
134: l2.setDSOPort(dsoPorts[i]);
135: l2.setJMXPort(jmxPorts[i]);
136: l2.setL2GroupPort(l2GroupPorts[i]);
137: l2.setPersistenceMode(serverPersistence);
138: l2.setGCEnabled(gcEnabled);
139: l2.setGCVerbose(gcVerbose);
140: l2.setGCInterval(gcIntervalInSec);
141: l2s[i] = l2;
142: }
143: HaConfigBuilder ha = new HaConfigBuilder();
144: if (this .serverDiskless) {
145: ha
146: .setMode(HaConfigBuilder.HA_MODE_NETWORKED_ACTIVE_PASSIVE);
147: } else {
148: ha
149: .setMode(HaConfigBuilder.HA_MODE_DISK_BASED_ACTIVE_PASSIVE);
150: }
151:
152: L2SConfigBuilder l2sConfigbuilder = new L2SConfigBuilder();
153: l2sConfigbuilder.setL2s(l2s);
154: l2sConfigbuilder.setHa(ha);
155:
156: ApplicationConfigBuilder app = ApplicationConfigBuilder
157: .newMinimalInstance();
158:
159: TerracottaConfigBuilder configBuilder = new TerracottaConfigBuilder();
160: configBuilder.setSystem(system);
161: configBuilder.setServers(l2sConfigbuilder);
162: configBuilder.setApplication(app);
163:
164: String configAsString = configBuilder.toString();
165: System.err.println("Writing config to file:"
166: + configFile.getAbsolutePath() + configAsString);
167:
168: FileOutputStream fileOutputStream = new FileOutputStream(
169: configFile);
170: PrintWriter out = new PrintWriter((fileOutputStream));
171: out.println(configAsString);
172: out.flush();
173: out.close();
174: }
175: }
|