1 4 package com.tc.simulator.container; 5 6 import org.apache.commons.cli.CommandLine; 7 import org.apache.commons.cli.HelpFormatter; 8 import org.apache.commons.cli.Option; 9 import org.apache.commons.cli.Options; 10 import org.apache.commons.cli.ParseException; 11 import org.apache.commons.cli.PosixParser; 12 13 import com.tcsimulator.ContainerBuilder; 14 15 import java.io.File ; 16 import java.util.Arrays ; 17 import java.util.Iterator ; 18 19 class CommandLineContainerBuilderConfig implements ContainerBuilderConfig { 20 public static final String INTENSITY_OPTION = "intensity"; 21 public static final String APP_EXEC_COUNT_OPTION = "app-exec-count"; 22 public static final String GLOBAL_PARTICIPANT_COUNT_OPTION = "global-participant-count"; 23 public static final String GLOBAL_CONTAINER_COUNT_OPTION = "global-container-count"; 24 public static final String OUTPUT_OPTION = "output"; 25 public static final String CONSOLE_OUTPUT_VALUE = "console"; 26 public static final String APP_CONFIG_BUILDER_OPTION = "app-config-builder"; 27 public static final String MASTER_OPTION = "master"; 28 public static final String START_SERVER_OPTION = "start-server"; 29 public static final String HELP_OPTION = "help"; 30 public static final String CONTAINER_START_TIMEOUT_OPTION = "container-start-timeout"; 31 public static final String APPLICATION_START_TIMEOUT_OPTION = "app-start-timeout"; 32 public static final String APPLICATION_EXECUTION_TIMEOUT_OPTION = "app-exec-timeout"; 33 public static final String DUMP_ERRORS_OPTION = "dump-errors"; 34 public static final String DUMP_OUTPUT_OPTION = "dump-output"; 35 public static final String AGGREGATE_OPTION = "aggregate"; 36 public static final String STREAM_OPTION = "stream"; 37 public static final String APPLICATION_CLASSNAME_OPTION = "application-classname"; 38 39 private final String [] args; 40 private final Options options; 41 private CommandLine commandLine; 42 private Option help; 43 private Option startServer; 44 private Option master; 45 private Option appConfigBuilder; 46 private Option output; 47 private Option globalParticipantCount; 48 private Option applicationExecutionCount; 49 private Option containerStartTimeout; 50 private Option applicationStartTimeout; 51 private Option applicationExecutionTimeout; 52 private Option dumpErrors; 53 private Option dumpOutput; 54 private Option aggregate; 55 private Option stream; 56 private Option intensity; 57 private Option globalContainerCount; 58 private Option applicationClassname; 59 60 CommandLineContainerBuilderConfig(String [] args) { 61 this.args = args; 62 this.options = newOptions(); 63 } 64 65 public String toString() { 66 StringBuffer rv = new StringBuffer (); 67 68 for (Iterator iter = Arrays.asList(commandLine.getOptions()).iterator(); iter.hasNext();) { 69 Option opt = (Option) iter.next(); 70 rv.append(opt + "="); 71 String [] values = opt.getValues(); 72 for (int i = 0; values != null && i < values.length; i++) { 73 rv.append(values[i]); 74 if (i < values.length - 1) { 75 rv.append(","); 76 } 77 } 78 rv.append("\n"); 79 } 80 return rv.toString(); 81 } 82 83 boolean help() { 84 return asBoolean(this.help); 85 } 86 87 public boolean startServer() { 88 return asBoolean(this.startServer); 89 } 90 91 public boolean master() { 92 return asBoolean(this.master); 93 } 94 95 public String appConfigBuilder() { 96 return asString(null, this.appConfigBuilder); 97 } 98 99 public boolean outputToConsole() { 100 return CONSOLE_OUTPUT_VALUE.equals(asString(CONSOLE_OUTPUT_VALUE, this.output)); 101 } 102 103 public boolean outputToFile() { 104 return !outputToConsole(); 105 } 106 107 public int globalParticipantCount() { 108 return asInt(-1, this.globalParticipantCount); 109 } 110 111 public File outputFile() { 112 return new File (asString(CONSOLE_OUTPUT_VALUE, this.output)); 113 } 114 115 public int getApplicationExecutionCount() { 116 return asInt(1, this.applicationExecutionCount); 117 } 118 119 public long getContainerStartTimeout() { 120 return asLong(5 * 60 * 1000, this.containerStartTimeout); 121 } 122 123 public long getApplicationStartTimeout() { 124 return asLong(5 * 60 * 1000, this.applicationStartTimeout); 125 } 126 127 public long getApplicationExecutionTimeout() { 128 return asLong(30 * 60 * 1000, this.applicationExecutionTimeout); 129 } 130 131 public boolean dumpErrors() { 132 return asBoolean(this.dumpErrors); 133 } 134 135 public boolean dumpOutput() { 136 return asBoolean(this.dumpOutput); 137 } 138 139 public boolean aggregate() { 140 return asBoolean(this.aggregate); 141 } 142 143 public boolean stream() { 144 return asBoolean(this.stream); 145 } 146 147 public int globalContainerCount() { 148 return asInt(0, this.globalContainerCount); 149 } 150 151 public int intensity() { 152 return asInt(1, this.intensity); 153 } 154 155 private Options newOptions() { 156 OptionMaker maker = new OptionMaker(); 157 Options rv = new Options(); 158 this.help = maker.withLongOpt(HELP_OPTION).withDescription("Prints this message.").create('h'); 159 rv.addOption(this.help); 160 161 this.startServer = maker.withLongOpt(START_SERVER_OPTION).withDescription("Start the DSO server.").create('a'); 162 rv.addOption(this.startServer); 163 164 this.master = maker.withLongOpt(MASTER_OPTION).withDescription("Designate this instance as the master.") 165 .create('b'); 166 rv.addOption(this.master); 167 168 this.appConfigBuilder = maker.withLongOpt(APP_CONFIG_BUILDER_OPTION) 169 .withDescription("The classname of the application configuration builder (required)").withValueSeparator() 170 .hasArg().isRequired().create('c'); 171 rv.addOption(this.appConfigBuilder); 172 173 this.output = maker.withLongOpt(OUTPUT_OPTION).withDescription("Output sink. ('console' | <filename>)") 174 .withValueSeparator().hasArg().create('d'); 175 rv.addOption(this.output); 176 177 this.globalParticipantCount = maker.withLongOpt(GLOBAL_PARTICIPANT_COUNT_OPTION) 178 .withDescription("Total number of application instances cluster-wide").hasArg().isRequired().create('e'); 179 rv.addOption(this.globalParticipantCount); 180 181 this.applicationExecutionCount = maker.withLongOpt(APP_EXEC_COUNT_OPTION) 182 .withDescription("Number of application instances to start in this container.").hasArg().create('f'); 183 rv.addOption(this.applicationExecutionCount); 184 185 this.containerStartTimeout = maker.withLongOpt(CONTAINER_START_TIMEOUT_OPTION) 186 .withDescription("Timeout for all containers to start.").hasArg().create('g'); 187 rv.addOption(this.containerStartTimeout); 188 189 this.applicationStartTimeout = maker.withLongOpt(APPLICATION_START_TIMEOUT_OPTION) 190 .withDescription("Timeout for all application instances within a given container to start.").hasArg() 191 .create('h'); 192 rv.addOption(this.applicationStartTimeout); 193 194 this.applicationExecutionTimeout = maker.withLongOpt(APPLICATION_EXECUTION_TIMEOUT_OPTION) 195 .withDescription("Timeout for all application instances within a given container to execute.").hasArg() 196 .create('i'); 197 rv.addOption(this.applicationExecutionTimeout); 198 199 this.dumpErrors = maker.withLongOpt(DUMP_ERRORS_OPTION).withDescription("Dump errors to System.err as they occur.") 200 .create('j'); 201 rv.addOption(this.dumpErrors); 202 203 this.dumpOutput = maker.withLongOpt(DUMP_OUTPUT_OPTION).withDescription("Dump results as they are reported.") 204 .create('k'); 205 rv.addOption(this.dumpOutput); 206 207 this.aggregate = maker.withLongOpt(AGGREGATE_OPTION) 208 .withDescription("Aggregate output for batch send when run is complete.").create('l'); 209 rv.addOption(this.aggregate); 210 211 this.stream = maker.withLongOpt(STREAM_OPTION).withDescription("Stream output immediately.").create('m'); 212 rv.addOption(stream); 213 214 this.intensity = maker.withLongOpt(INTENSITY_OPTION).withDescription("Intensity of test.").hasArg().create('n'); 215 rv.addOption(intensity); 216 217 this.globalContainerCount = maker.withLongOpt(GLOBAL_CONTAINER_COUNT_OPTION) 218 .withDescription("The total number of Containers int this test.").hasArg().create('o'); 219 rv.addOption(this.globalContainerCount); 220 221 this.applicationClassname = maker.withLongOpt(APPLICATION_CLASSNAME_OPTION) 222 .withDescription("Name of the test application.").withValueSeparator().hasArg().create('p'); 223 rv.addOption(this.applicationClassname); 224 225 return rv; 226 } 227 228 private boolean asBoolean(Option opt) { 229 return commandLine.hasOption(opt.getLongOpt()) || commandLine.hasOption(opt.getOpt()); 230 } 231 232 private int asInt(int defaultValue, Option opt) { 233 String value = asString(null, opt); 234 return (value == null) ? defaultValue : Integer.parseInt(value); 235 } 236 237 private long asLong(long defaultValue, Option opt) { 238 String value = asString(null, opt); 239 return (value == null) ? defaultValue : Long.parseLong(value); 240 } 241 242 private String asString(String defaultValue, Option opt) { 243 String value; 244 value = commandLine.getOptionValue(opt.getOpt()); 247 return (value == null) ? defaultValue : value; 248 } 249 250 void parse() throws ParseException { 251 this.commandLine = new PosixParser().parse(options, args); 252 } 253 254 void usage() { 255 HelpFormatter formatter = new HelpFormatter(); 256 formatter.printHelp("java <options> " + ContainerBuilder.class.getName(), options); 257 } 258 259 public String applicationClassname() { 260 return asString(null, this.applicationClassname); 261 } 262 } | Popular Tags |