001    package railo.cli;
002    
003    import java.io.File;
004    import java.io.IOException;
005    import java.util.HashMap;
006    import java.util.Map;
007    
008    import javax.servlet.ServletException;
009    import javax.servlet.jsp.JspException;
010    
011    import railo.loader.util.Util;
012    
013    public class CLI {
014            
015            private static boolean useRMI=false;
016    /**
017     * Config
018     * 
019     * webroot - webroot directory
020     * servlet-name - name of the servlet (default:CFMLServlet)
021     * server-name - server name (default:localhost)
022     * uri - host/scriptname/query
023     * cookie - cookies (same pattern as query string)
024     * form - form (same pattern as query string)
025     */
026            
027            
028            /**
029             * @param args
030             * @throws JspException 
031             */
032            public static void main(String[] args) throws ServletException, IOException, JspException {
033                    Map<String,String> config=toMap(args);
034                    
035                    System.setProperty("railo.cli.call", "true");
036                    
037                    // webroot
038                    String strWebroot=config.get("webroot");
039                    if(Util.isEmpty(strWebroot,true)) throw new IOException("missing webroot configuration");
040                    File root=new File(strWebroot);
041                    root.mkdirs();
042                    
043                    // servletNane
044                    String servletName=config.get("servlet-name");
045                    if(Util.isEmpty(servletName,true))servletName="CFMLServlet";
046                    if(useRMI){
047                            CLIFactory factory = new CLIFactory(root,servletName,config);
048                            factory.setDaemon(false);
049                            factory.start();
050                    }
051                    else {
052                            CLIInvokerImpl invoker=new CLIInvokerImpl(root, servletName);
053                            invoker.invoke(config);
054                    }
055                    //Map<String,Object> attributes=new HashMap<String, Object>();
056                    //Map<String, String> initParameters=new HashMap<String, String>();
057                    //initParameters.put("railo-server-directory", new File(root,"server").getAbsolutePath());
058                    
059                    
060                    //ServletContextImpl servletContext = new ServletContextImpl(root, attributes, initParameters, 1, 0);
061                    //ServletConfigImpl servletConfig = new ServletConfigImpl(servletContext, servletName);
062                    //CFMLEngine engine = CFMLEngineFactory.getInstance(servletConfig);
063                    //engine.cli(config,servletConfig);
064                    
065    
066            }
067    // java railo-cli.jar -config=.../railo-web.xml.cfm -uri=/susi/index.cfm?test=1 -form=name=susi -cgi=user_agent=urs -output=.../test.txt ...
068    
069            private static Map<String, String> toMap(String[] args) {
070                    int index;
071                    Map<String, String> config=new HashMap<String, String>();
072                    String raw,key,value;
073                    if(args!=null)for(int i=0;i<args.length;i++){
074                            raw=args[i].trim();
075                            if(Util.isEmpty(raw, true)) continue;
076                            if(raw.startsWith("-"))raw=raw.substring(1).trim();
077                            index=raw.indexOf('=');
078                            if(index==-1) {
079                                    key=raw;
080                                    value="";
081                            }
082                            else {
083                                    key=raw.substring(0,index).trim();
084                                    value=raw.substring(index+1).trim();
085                            }
086                            config.put(key.toLowerCase(), value);
087                    }
088                    return config;
089            }
090    }