001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.cli;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.servlet.ServletException;
027import javax.servlet.jsp.JspException;
028
029import lucee.loader.util.Util;
030
031public class CLI {
032
033/**
034 * Config
035 * 
036 * webroot - webroot directory
037 * servlet-name - name of the servlet (default:CFMLServlet)
038 * server-name - server name (default:localhost)
039 * uri - host/scriptname/query
040 * cookie - cookies (same pattern as query string)
041 * form - form (same pattern as query string)
042 */
043        
044        
045        /**
046         * @param args
047         * @throws JspException 
048         */
049        public static void main(String[] args) throws ServletException, IOException, JspException {
050
051                Map<String, String> config = toMap(args);
052                
053                System.setProperty("lucee.cli.call", "true");
054
055                boolean useRMI = "true".equalsIgnoreCase(config.get("rmi"));
056
057                File root;
058                String param = config.get("webroot");
059                if (Util.isEmpty(param, true)) {
060
061                        root = new File(".");        // working directory that the java command was called from
062                        config.put("webroot", root.getAbsolutePath());
063                }
064                else {
065
066                        root = new File(param);
067                        root.mkdirs();
068                }
069
070//              System.out.println("set webroot to: " + root.getAbsolutePath());
071
072                String servletName = config.get("servlet-name");
073
074                if (Util.isEmpty(servletName, true))
075                        servletName = "CFMLServlet";
076
077                if (useRMI) {
078
079                        CLIFactory factory = new CLIFactory(root, servletName, config);
080                        factory.setDaemon(false);
081                        factory.start();
082                }
083                else {
084
085                        CLIInvokerImpl invoker=new CLIInvokerImpl(root, servletName);
086                        invoker.invoke(config);
087                }
088        }
089
090        private static Map<String, String> toMap(String[] args) {
091
092                int index;
093                String raw,key,value;
094
095                Map<String, String> config = new HashMap<String, String>();
096
097                if (args != null && args.length > 0) {
098
099                        for (int i=0; i<args.length; i++) {
100
101                                raw = args[i].trim();
102                                if (raw.startsWith("-"))
103                                        raw = raw.substring(1);
104
105                                if (!raw.isEmpty()) {
106
107                                        index = raw.indexOf('=');
108                                        if (index == -1) {
109                                                key = raw;
110                                                value = "";
111                                        }
112                                        else {
113                                                key = raw.substring(0, index).trim();
114                                                value = raw.substring(index + 1).trim();
115                                        }
116
117                                        config.put(key.toLowerCase(), value);
118                                }
119                        }
120                }
121
122                return config;
123        }
124}