001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.mpt.app;
021
022import java.io.File;
023
024import org.apache.commons.cli.CommandLine;
025import org.apache.commons.cli.CommandLineParser;
026import org.apache.commons.cli.GnuParser;
027import org.apache.commons.cli.HelpFormatter;
028import org.apache.commons.cli.OptionBuilder;
029import org.apache.commons.cli.Options;
030import org.apache.commons.cli.ParseException;
031
032/**
033 * <p>Runs MPT application.</p>
034 * <p>Return values:</p>
035 * <table>
036 * <tr><td>0</td><td>Success</td></tr>
037 * <tr><td>-1</td><td>Illegal Arguments</td></tr>
038 * <tr><td>1</td><td>Script not found</td></tr>
039 * <tr><td>1</td><td>Port not a number</td></tr>
040 * </table>
041 */
042public class Main {
043
044    
045    private static final int FILE_NOT_FOUND = 1;
046    private static final int PORT_NOT_A_NUMBER = 2;
047    
048    private static final char FILE_OPTION = 'f';
049    private static final char PORT_OPTION = 'p';
050    private static final char HOST_OPTION = 'h';
051    private static final char SHABANG_OPTION = 's';
052    private static final char VERBOSE_OPTION = 'v';
053
054    public static final void main(String[] args) throws Exception {
055        Options options = buildOptions();
056        
057        try {
058            
059            CommandLineParser parser = new GnuParser();
060            CommandLine cmd = parser.parse(options, args);
061            runCommand(cmd);
062            
063        } catch (ParseException e) {
064            System.out.println(e.getMessage());
065            new HelpFormatter().printHelp( "mpt", options );
066            System.exit(-1);
067        }
068        
069    }
070
071    private static void runCommand(CommandLine cmd) throws Exception {
072        boolean verbose = Boolean.parseBoolean(cmd.getOptionValue(VERBOSE_OPTION, Boolean.toString(false)));
073        File file = new File(cmd.getOptionValue(FILE_OPTION));
074        if (file.exists()) {
075            try {
076                int port = Integer.parseInt(cmd.getOptionValue(PORT_OPTION));    
077                String host = cmd.getOptionValue(HOST_OPTION, "localhost");
078                String shabang = cmd.getOptionValue(SHABANG_OPTION, null);
079                RunScript runner = new RunScript(file, port, host, shabang, verbose);
080                runner.run();
081                
082            } catch (NumberFormatException e) {
083                System.out.println("Port must be numeric");
084                System.exit(PORT_NOT_A_NUMBER);
085            }
086        } else {
087            System.out.println("Script not found");
088            System.exit(FILE_NOT_FOUND);
089        }
090    }
091
092    private static Options buildOptions() {
093        Options options = new Options();
094        
095        addRunScriptOptions(options);
096        
097        return options;
098    }
099
100    @SuppressWarnings("static-access")
101    private static void addRunScriptOptions(Options options) {
102        // -f <file> runs this script
103        options.addOption(OptionBuilder
104                    .withArgName("file")
105                    .hasArg()
106                    .withDescription("run this script")
107                    .withLongOpt("file")
108                    .isRequired()
109                    .create(FILE_OPTION));
110        
111        // -p <port> runs against this port
112        options.addOption(OptionBuilder
113                    .withArgName("port")
114                    .hasArg()
115                    .withDescription("runs against this port")
116                    .withLongOpt("port")
117                    .isRequired()
118                    .create(PORT_OPTION));
119        
120        // -h <host> runs against this host
121        options.addOption(OptionBuilder
122                    .withArgName("host")
123                    .hasArg()
124                    .withDescription("runs against this host (defaults to localhost)")
125                    .withLongOpt("host")
126                    .isRequired(false)
127                    .create(HOST_OPTION));
128        // -s <shabang> sets shabang
129        options.addOption(OptionBuilder
130                    .withArgName("shabang")
131                    .hasArg()
132                    .withDescription("sets shabang (defaults to empty)")
133                    .withLongOpt("shabang")
134                    .isRequired(false)
135                    .create(SHABANG_OPTION));
136        // -v sets logging to verbose
137        options.addOption(OptionBuilder
138                    .withDescription("prints lots of logging")
139                    .withLongOpt("verbose")
140                    .isRequired(false)
141                    .create(VERBOSE_OPTION));
142    }
143}