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.tc.simulator.container;
005:
006: import org.apache.commons.cli.CommandLine;
007: import org.apache.commons.cli.HelpFormatter;
008: import org.apache.commons.cli.Option;
009: import org.apache.commons.cli.Options;
010: import org.apache.commons.cli.ParseException;
011: import org.apache.commons.cli.PosixParser;
012:
013: import com.tcsimulator.ContainerBuilder;
014:
015: import java.io.File;
016: import java.util.Arrays;
017: import java.util.Iterator;
018:
019: class CommandLineContainerBuilderConfig implements
020: ContainerBuilderConfig {
021: public static final String INTENSITY_OPTION = "intensity";
022: public static final String APP_EXEC_COUNT_OPTION = "app-exec-count";
023: public static final String GLOBAL_PARTICIPANT_COUNT_OPTION = "global-participant-count";
024: public static final String GLOBAL_CONTAINER_COUNT_OPTION = "global-container-count";
025: public static final String OUTPUT_OPTION = "output";
026: public static final String CONSOLE_OUTPUT_VALUE = "console";
027: public static final String APP_CONFIG_BUILDER_OPTION = "app-config-builder";
028: public static final String MASTER_OPTION = "master";
029: public static final String START_SERVER_OPTION = "start-server";
030: public static final String HELP_OPTION = "help";
031: public static final String CONTAINER_START_TIMEOUT_OPTION = "container-start-timeout";
032: public static final String APPLICATION_START_TIMEOUT_OPTION = "app-start-timeout";
033: public static final String APPLICATION_EXECUTION_TIMEOUT_OPTION = "app-exec-timeout";
034: public static final String DUMP_ERRORS_OPTION = "dump-errors";
035: public static final String DUMP_OUTPUT_OPTION = "dump-output";
036: public static final String AGGREGATE_OPTION = "aggregate";
037: public static final String STREAM_OPTION = "stream";
038: public static final String APPLICATION_CLASSNAME_OPTION = "application-classname";
039:
040: private final String[] args;
041: private final Options options;
042: private CommandLine commandLine;
043: private Option help;
044: private Option startServer;
045: private Option master;
046: private Option appConfigBuilder;
047: private Option output;
048: private Option globalParticipantCount;
049: private Option applicationExecutionCount;
050: private Option containerStartTimeout;
051: private Option applicationStartTimeout;
052: private Option applicationExecutionTimeout;
053: private Option dumpErrors;
054: private Option dumpOutput;
055: private Option aggregate;
056: private Option stream;
057: private Option intensity;
058: private Option globalContainerCount;
059: private Option applicationClassname;
060:
061: CommandLineContainerBuilderConfig(String[] args) {
062: this .args = args;
063: this .options = newOptions();
064: }
065:
066: public String toString() {
067: StringBuffer rv = new StringBuffer();
068:
069: for (Iterator iter = Arrays.asList(commandLine.getOptions())
070: .iterator(); iter.hasNext();) {
071: Option opt = (Option) iter.next();
072: rv.append(opt + "=");
073: String[] values = opt.getValues();
074: for (int i = 0; values != null && i < values.length; i++) {
075: rv.append(values[i]);
076: if (i < values.length - 1) {
077: rv.append(",");
078: }
079: }
080: rv.append("\n");
081: }
082: return rv.toString();
083: }
084:
085: boolean help() {
086: return asBoolean(this .help);
087: }
088:
089: public boolean startServer() {
090: return asBoolean(this .startServer);
091: }
092:
093: public boolean master() {
094: return asBoolean(this .master);
095: }
096:
097: public String appConfigBuilder() {
098: return asString(null, this .appConfigBuilder);
099: }
100:
101: public boolean outputToConsole() {
102: return CONSOLE_OUTPUT_VALUE.equals(asString(
103: CONSOLE_OUTPUT_VALUE, this .output));
104: }
105:
106: public boolean outputToFile() {
107: return !outputToConsole();
108: }
109:
110: public int globalParticipantCount() {
111: return asInt(-1, this .globalParticipantCount);
112: }
113:
114: public File outputFile() {
115: return new File(asString(CONSOLE_OUTPUT_VALUE, this .output));
116: }
117:
118: public int getApplicationExecutionCount() {
119: return asInt(1, this .applicationExecutionCount);
120: }
121:
122: public long getContainerStartTimeout() {
123: return asLong(5 * 60 * 1000, this .containerStartTimeout);
124: }
125:
126: public long getApplicationStartTimeout() {
127: return asLong(5 * 60 * 1000, this .applicationStartTimeout);
128: }
129:
130: public long getApplicationExecutionTimeout() {
131: return asLong(30 * 60 * 1000, this .applicationExecutionTimeout);
132: }
133:
134: public boolean dumpErrors() {
135: return asBoolean(this .dumpErrors);
136: }
137:
138: public boolean dumpOutput() {
139: return asBoolean(this .dumpOutput);
140: }
141:
142: public boolean aggregate() {
143: return asBoolean(this .aggregate);
144: }
145:
146: public boolean stream() {
147: return asBoolean(this .stream);
148: }
149:
150: public int globalContainerCount() {
151: return asInt(0, this .globalContainerCount);
152: }
153:
154: public int intensity() {
155: return asInt(1, this .intensity);
156: }
157:
158: private Options newOptions() {
159: OptionMaker maker = new OptionMaker();
160: Options rv = new Options();
161: this .help = maker.withLongOpt(HELP_OPTION).withDescription(
162: "Prints this message.").create('h');
163: rv.addOption(this .help);
164:
165: this .startServer = maker.withLongOpt(START_SERVER_OPTION)
166: .withDescription("Start the DSO server.").create('a');
167: rv.addOption(this .startServer);
168:
169: this .master = maker.withLongOpt(MASTER_OPTION).withDescription(
170: "Designate this instance as the master.").create('b');
171: rv.addOption(this .master);
172:
173: this .appConfigBuilder = maker
174: .withLongOpt(APP_CONFIG_BUILDER_OPTION)
175: .withDescription(
176: "The classname of the application configuration builder (required)")
177: .withValueSeparator().hasArg().isRequired().create('c');
178: rv.addOption(this .appConfigBuilder);
179:
180: this .output = maker.withLongOpt(OUTPUT_OPTION).withDescription(
181: "Output sink. ('console' | <filename>)")
182: .withValueSeparator().hasArg().create('d');
183: rv.addOption(this .output);
184:
185: this .globalParticipantCount = maker.withLongOpt(
186: GLOBAL_PARTICIPANT_COUNT_OPTION).withDescription(
187: "Total number of application instances cluster-wide")
188: .hasArg().isRequired().create('e');
189: rv.addOption(this .globalParticipantCount);
190:
191: this .applicationExecutionCount = maker
192: .withLongOpt(APP_EXEC_COUNT_OPTION)
193: .withDescription(
194: "Number of application instances to start in this container.")
195: .hasArg().create('f');
196: rv.addOption(this .applicationExecutionCount);
197:
198: this .containerStartTimeout = maker.withLongOpt(
199: CONTAINER_START_TIMEOUT_OPTION).withDescription(
200: "Timeout for all containers to start.").hasArg()
201: .create('g');
202: rv.addOption(this .containerStartTimeout);
203:
204: this .applicationStartTimeout = maker
205: .withLongOpt(APPLICATION_START_TIMEOUT_OPTION)
206: .withDescription(
207: "Timeout for all application instances within a given container to start.")
208: .hasArg().create('h');
209: rv.addOption(this .applicationStartTimeout);
210:
211: this .applicationExecutionTimeout = maker
212: .withLongOpt(APPLICATION_EXECUTION_TIMEOUT_OPTION)
213: .withDescription(
214: "Timeout for all application instances within a given container to execute.")
215: .hasArg().create('i');
216: rv.addOption(this .applicationExecutionTimeout);
217:
218: this .dumpErrors = maker.withLongOpt(DUMP_ERRORS_OPTION)
219: .withDescription(
220: "Dump errors to System.err as they occur.")
221: .create('j');
222: rv.addOption(this .dumpErrors);
223:
224: this .dumpOutput = maker.withLongOpt(DUMP_OUTPUT_OPTION)
225: .withDescription("Dump results as they are reported.")
226: .create('k');
227: rv.addOption(this .dumpOutput);
228:
229: this .aggregate = maker
230: .withLongOpt(AGGREGATE_OPTION)
231: .withDescription(
232: "Aggregate output for batch send when run is complete.")
233: .create('l');
234: rv.addOption(this .aggregate);
235:
236: this .stream = maker.withLongOpt(STREAM_OPTION).withDescription(
237: "Stream output immediately.").create('m');
238: rv.addOption(stream);
239:
240: this .intensity = maker.withLongOpt(INTENSITY_OPTION)
241: .withDescription("Intensity of test.").hasArg().create(
242: 'n');
243: rv.addOption(intensity);
244:
245: this .globalContainerCount = maker.withLongOpt(
246: GLOBAL_CONTAINER_COUNT_OPTION).withDescription(
247: "The total number of Containers int this test.")
248: .hasArg().create('o');
249: rv.addOption(this .globalContainerCount);
250:
251: this .applicationClassname = maker.withLongOpt(
252: APPLICATION_CLASSNAME_OPTION).withDescription(
253: "Name of the test application.").withValueSeparator()
254: .hasArg().create('p');
255: rv.addOption(this .applicationClassname);
256:
257: return rv;
258: }
259:
260: private boolean asBoolean(Option opt) {
261: return commandLine.hasOption(opt.getLongOpt())
262: || commandLine.hasOption(opt.getOpt());
263: }
264:
265: private int asInt(int defaultValue, Option opt) {
266: String value = asString(null, opt);
267: return (value == null) ? defaultValue : Integer.parseInt(value);
268: }
269:
270: private long asLong(long defaultValue, Option opt) {
271: String value = asString(null, opt);
272: return (value == null) ? defaultValue : Long.parseLong(value);
273: }
274:
275: private String asString(String defaultValue, Option opt) {
276: String value;
277: // value = (opt.hasLongOpt()) ? commandLine.getOptionValue(opt.getLongOpt()) : commandLine
278: // .getOptionValue(opt.getOpt());
279: value = commandLine.getOptionValue(opt.getOpt());
280: return (value == null) ? defaultValue : value;
281: }
282:
283: void parse() throws ParseException {
284: this .commandLine = new PosixParser().parse(options, args);
285: }
286:
287: void usage() {
288: HelpFormatter formatter = new HelpFormatter();
289: formatter.printHelp("java <options> "
290: + ContainerBuilder.class.getName(), options);
291: }
292:
293: public String applicationClassname() {
294: return asString(null, this.applicationClassname);
295: }
296: }
|