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.DefaultParser;
027import org.apache.commons.cli.HelpFormatter;
028import org.apache.commons.cli.Option;
029import org.apache.commons.cli.Options;
030import org.apache.commons.cli.ParseException;
031import org.apache.james.util.Port;
032
033/**
034 * <p>Runs MPT application.</p>
035 * <p>Return values:</p>
036 * <table>
037 * <tr><td>0</td><td>Success</td></tr>
038 * <tr><td>-1</td><td>Illegal Arguments</td></tr>
039 * <tr><td>1</td><td>Script not found</td></tr>
040 * <tr><td>1</td><td>Port not a number</td></tr>
041 * </table>
042 */
043public class Main {
044
045    
046    private static final int FILE_NOT_FOUND = 1;
047    private static final int PORT_NOT_A_NUMBER = 2;
048    
049    private static final String FILE_OPTION = "f";
050    private static final String PORT_OPTION = "p";
051    private static final String HOST_OPTION = "h";
052    private static final String SHABANG_OPTION = "s";
053    private static final String VERBOSE_OPTION = "v";
054
055    public static void main(String[] args) throws Exception {
056        Options options = buildOptions();
057        
058        try {
059            
060            CommandLineParser parser = new DefaultParser();
061            CommandLine cmd = parser.parse(options, args);
062            runCommand(cmd);
063            
064        } catch (ParseException e) {
065            System.out.println(e.getMessage());
066            new HelpFormatter().printHelp("mpt", options);
067            System.exit(-1);
068        }
069        
070    }
071
072    private static void runCommand(CommandLine cmd) throws Exception {
073        boolean verbose = Boolean.parseBoolean(cmd.getOptionValue(VERBOSE_OPTION, Boolean.toString(false)));
074        File file = new File(cmd.getOptionValue(FILE_OPTION));
075        if (file.exists()) {
076            try {
077                Port port = new Port(Integer.parseInt(cmd.getOptionValue(PORT_OPTION)));    
078                String host = cmd.getOptionValue(HOST_OPTION, "localhost");
079                String shabang = cmd.getOptionValue(SHABANG_OPTION, null);
080                RunScript runner = new RunScript(file, port, host, shabang, verbose);
081                runner.run();
082                
083            } catch (NumberFormatException e) {
084                System.out.println("Port must be numeric");
085                System.exit(PORT_NOT_A_NUMBER);
086            }
087        } else {
088            System.out.println("Script not found");
089            System.exit(FILE_NOT_FOUND);
090        }
091    }
092
093    private static Options buildOptions() {
094        Options options = new Options();
095        
096        addRunScriptOptions(options);
097        
098        return options;
099    }
100
101    private static void addRunScriptOptions(Options options) {
102        // -f <file> runs this script
103        options.addOption(Option.builder(FILE_OPTION)
104                    .argName("file")
105                    .hasArg()
106                    .desc("run this script")
107                    .longOpt("file")
108                    .required()
109                    .build());
110        
111        // -p <port> runs against this port
112        options.addOption(Option.builder(PORT_OPTION)
113                    .argName("port")
114                    .hasArg()
115                    .desc("runs against this port")
116                    .longOpt("port")
117                    .required()
118                    .build());
119        
120        // -h <host> runs against this host
121        options.addOption(Option.builder(HOST_OPTION)
122                    .argName("host")
123                    .hasArg()
124                    .desc("runs against this host (defaults to localhost)")
125                    .longOpt("host")
126                    .required(false)
127                    .build());
128        // -s <shabang> sets shabang
129        options.addOption(Option.builder(SHABANG_OPTION)
130                    .argName("shabang")
131                    .hasArg()
132                    .desc("sets shabang (defaults to empty)")
133                    .longOpt("shabang")
134                    .required(false)
135                    .build());
136        // -v sets logging to verbose
137        options.addOption(Option.builder(VERBOSE_OPTION)
138                    .desc("prints lots of logging")
139                    .longOpt("verbose")
140                    .required(false)
141                    .build());
142    }
143}