001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tcsimulator;
005:
006: import com.tc.config.schema.builder.DSOApplicationConfigBuilder;
007: import com.tc.config.schema.test.ConfigBuilderFactoryImpl;
008: import com.tc.config.schema.test.L1ConfigBuilder;
009: import com.tc.config.schema.test.L2ConfigBuilder;
010: import com.tc.config.schema.test.SystemConfigBuilder;
011: import com.tc.config.schema.test.TerracottaConfigBuilder;
012: import com.tc.object.config.ConfigVisitor;
013: import com.tc.object.config.DSOApplicationConfig;
014: import com.tc.object.config.DSOApplicationConfigImpl;
015: import com.tc.simulator.distrunner.ArgParser;
016: import com.tcsimulator.distrunner.ServerSpec;
017:
018: import java.io.File;
019: import java.io.FileWriter;
020: import java.io.IOException;
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: public final class ConfigWriter {
025:
026: private static final String LICENSE_FILENAME = "license.lic";
027:
028: private final Collection classesToVisit;
029: private final ServerSpec sSpec;
030: private final File licenseFile;
031: private final File serverHome;
032: private final File configFile;
033:
034: public ConfigWriter(ServerSpec sSpec, Collection classesToVisit,
035: Sandbox sandbox) {
036: this .sSpec = sSpec;
037: this .classesToVisit = classesToVisit;
038: this .serverHome = sandbox.getServerHome();
039: this .configFile = sandbox.getConfigFile();
040:
041: this .licenseFile = new File(sSpec.getTestHome(),
042: LICENSE_FILENAME);
043: if (!licenseFile.exists()) {
044: throw new RuntimeException(
045: "No license file: "
046: + licenseFile
047: + ". Try this: cp src/test.data/licenses/license-for-tests.lic "
048: + licenseFile);
049: }
050: }
051:
052: public void writeConfigFile() throws IOException {
053: String fileSeparator = System.getProperty("file.separator");
054:
055: TerracottaConfigBuilder configBuilder = TerracottaConfigBuilder
056: .newMinimalInstance();
057:
058: SystemConfigBuilder system = configBuilder.getSystem();
059: system
060: .setConfigurationModel(SystemConfigBuilder.CONFIG_MODEL_PRODUCTION);
061:
062: L1ConfigBuilder l1Builder = L1ConfigBuilder
063: .newMinimalInstance();
064: l1Builder.setLogs("client-logs-%i");
065: configBuilder.setClient(l1Builder);
066:
067: L2ConfigBuilder l2Builder = L2ConfigBuilder
068: .newMinimalInstance();
069: l2Builder.setName(sSpec.getHostName());
070: l2Builder.setData(this .serverHome + fileSeparator
071: + "server-data");
072: l2Builder.setLogs(this .serverHome + fileSeparator
073: + "server-logs");
074: int undefined = ArgParser.getUndefinedNumber();
075: if (sSpec.getJmxPort() != undefined) {
076: l2Builder.setJMXPort(sSpec.getJmxPort());
077: }
078: if (sSpec.getDsoPort() != undefined) {
079: l2Builder.setDSOPort(sSpec.getDsoPort());
080: }
081: configBuilder.getServers().setL2s(
082: new L2ConfigBuilder[] { l2Builder });
083:
084: DSOApplicationConfigBuilder appConfigBuilder = configBuilder
085: .getApplication().getDSO();
086:
087: DSOApplicationConfig cfg = new DSOApplicationConfigImpl(
088: new ConfigBuilderFactoryImpl());
089:
090: // NOTE: am I missing something? Why would you instrument/autolock everything then visit each app config to dicide what to instrument
091: //cfg.addWriteAutolock("* *..*.*(..)");
092: //cfg.addIncludePattern("*..*");
093:
094: ConfigVisitor configVisitor = new ConfigVisitor();
095: for (Iterator i = classesToVisit.iterator(); i.hasNext();) {
096: configVisitor.visitDSOApplicationConfig(cfg, (Class) i
097: .next());
098: }
099:
100: cfg.writeTo(appConfigBuilder);
101:
102: FileWriter cfgOut = new FileWriter(configFile);
103: System.out
104: .println("*******************************************************************************************");
105: System.out.println(configBuilder.toString());
106: System.out
107: .println("*******************************************************************************************");
108: cfgOut.write(configBuilder.toString());
109: cfgOut.flush();
110: cfgOut.close();
111: }
112:
113: }
|