View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
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   * <p>Runs MPT application.</p>
34   * <p>Return values:</p>
35   * <table>
36   * <tr><td>0</td><td>Success</td></tr>
37   * <tr><td>-1</td><td>Illegal Arguments</td></tr>
38   * <tr><td>1</td><td>Script not found</td></tr>
39   * <tr><td>1</td><td>Port not a number</td></tr>
40   * </table>
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         // -f <file> runs this script
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         // -p <port> runs against this port
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         // -h <host> runs against this host
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         // -s <shabang> sets shabang
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         // -v sets logging to verbose
138         options.addOption(OptionBuilder
139                     .withDescription("prints lots of logging")
140                     .withLongOpt("verbose")
141                     .isRequired(false)
142                     .create(VERBOSE_OPTION));
143     }
144 }