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.rmi.RemoteException;
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.servlet.ServletException;
027
028import lucee.cli.servlet.ServletConfigImpl;
029import lucee.cli.servlet.ServletContextImpl;
030import lucee.loader.engine.CFMLEngine;
031import lucee.loader.engine.CFMLEngineFactory;
032
033public class CLIInvokerImpl implements CLIInvoker {
034
035        private ServletConfigImpl servletConfig;
036        private CFMLEngine engine;
037        private long lastAccess;
038
039        public CLIInvokerImpl(File root, String servletName) throws ServletException{
040
041                Map<String,Object> attributes  = new HashMap<String, Object>();
042                Map<String, String> initParams = new HashMap<String, String>();
043
044                String param = System.getProperty("lucee.cli.config");
045
046                if (param != null && !param.isEmpty()) {
047
048                        initParams.put("lucee-web-directory",    new File(param, "lucee-web").getAbsolutePath());
049                        initParams.put("lucee-server-directory", new File(param).getAbsolutePath());    // will create a subfolder named lucee-server
050                }
051                else {
052
053                        initParams.put("lucee-server-directory", new File(root, "WEB-INF").getAbsolutePath());
054                }
055
056                ServletContextImpl servletContext = new ServletContextImpl(root, attributes, initParams, 1, 0);
057                servletConfig = new ServletConfigImpl(servletContext, servletName);
058                engine = CFMLEngineFactory.getInstance(servletConfig);
059        }
060        
061        @Override
062        public void invoke(Map<String, String> config) throws RemoteException {
063
064                try {
065
066                        engine.cli(config, servletConfig);
067                        lastAccess = System.currentTimeMillis();
068                } catch (Throwable t) {
069                        if(t instanceof ThreadDeath) throw (ThreadDeath)t;
070                        throw new RemoteException("failed to call CFML Engine", t);
071                }
072        }
073
074        public long lastAccess() {
075                return lastAccess;
076        }
077
078}