1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.mpt.app;
21
22 import java.io.File;
23
24 import org.apache.commons.cli.CommandLine;
25 import org.apache.commons.cli.CommandLineParser;
26 import org.apache.commons.cli.GnuParser;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.apache.commons.cli.OptionBuilder;
29 import org.apache.commons.cli.Options;
30 import org.apache.commons.cli.ParseException;
31
32
33
34
35
36
37
38
39
40
41
42 public class Main {
43
44
45 private static final int FILE_NOT_FOUND = 1;
46 private static final int PORT_NOT_A_NUMBER = 2;
47
48 private static final char FILE_OPTION = 'f';
49 private static final char PORT_OPTION = 'p';
50 private static final char HOST_OPTION = 'h';
51 private static final char SHABANG_OPTION = 's';
52 private static final char VERBOSE_OPTION = 'v';
53
54 public static final void main(final String[] args) throws Exception {
55 final Options options = buildOptions();
56
57 try {
58
59 CommandLineParser parser = new GnuParser();
60 CommandLine cmd = parser.parse(options, args);
61 runCommand(cmd);
62
63 } catch (ParseException e) {
64 System.out.println(e.getMessage());
65 new HelpFormatter().printHelp( "mpt", options );
66 System.exit(-1);
67 }
68
69 }
70
71 private static void runCommand(CommandLine cmd) throws Exception {
72 final boolean verbose = Boolean.parseBoolean(cmd.getOptionValue(VERBOSE_OPTION, Boolean.toString(false)));
73 final File file = new File(cmd.getOptionValue(FILE_OPTION));
74 if (file.exists()) {
75 try {
76 final int port = Integer.parseInt(cmd.getOptionValue(PORT_OPTION));
77 final String host = cmd.getOptionValue(HOST_OPTION, "localhost");
78 final String shabang = cmd.getOptionValue(SHABANG_OPTION, null);
79 RunScript runner = new RunScript(file, port, host, shabang, verbose);
80 runner.run();
81
82 } catch (NumberFormatException e) {
83 System.out.println("Port must be numeric");
84 System.exit(PORT_NOT_A_NUMBER);
85 }
86 } else {
87 System.out.println("Script not found");
88 System.exit(FILE_NOT_FOUND);
89 }
90 }
91
92 @SuppressWarnings("static-access")
93 private static Options buildOptions() {
94 final Options options = new Options();
95
96 addRunScriptOptions(options);
97
98 return options;
99 }
100
101 @SuppressWarnings("static-access")
102 private static void addRunScriptOptions(final Options options) {
103
104 options.addOption(OptionBuilder
105 .withArgName("file")
106 .hasArg()
107 .withDescription("run this script")
108 .withLongOpt("file")
109 .isRequired()
110 .create(FILE_OPTION));
111
112
113 options.addOption(OptionBuilder
114 .withArgName("port")
115 .hasArg()
116 .withDescription("runs against this port")
117 .withLongOpt("port")
118 .isRequired()
119 .create(PORT_OPTION));
120
121
122 options.addOption(OptionBuilder
123 .withArgName("host")
124 .hasArg()
125 .withDescription("runs against this host (defaults to localhost)")
126 .withLongOpt("host")
127 .isRequired(false)
128 .create(HOST_OPTION));
129
130 options.addOption(OptionBuilder
131 .withArgName("shabang")
132 .hasArg()
133 .withDescription("sets shabang (defaults to empty)")
134 .withLongOpt("shabang")
135 .isRequired(false)
136 .create(SHABANG_OPTION));
137
138 options.addOption(OptionBuilder
139 .withDescription("prints lots of logging")
140 .withLongOpt("verbose")
141 .isRequired(false)
142 .create(VERBOSE_OPTION));
143 }
144 }