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.runtime.net.flex;
020
021import javax.servlet.ServletException;
022
023import lucee.loader.engine.CFMLEngine;
024import lucee.loader.engine.CFMLEngineFactory;
025import lucee.loader.engine.EngineChangeListener;
026import lucee.runtime.util.BlazeDS;
027import flex.messaging.FlexContext;
028import flex.messaging.config.ConfigMap;
029import flex.messaging.messages.Message;
030import flex.messaging.services.ServiceAdapter;
031
032// FUTURE make this class independent from flex.messaging... so that the loader no longer need the flex jar
033
034/**
035 * Lucee implementation of the ServiceAdapter, forward all BlazeDS Request to the CFMLEngine. 
036 */
037public class LuceeAdapter extends ServiceAdapter implements EngineChangeListener {
038        
039        public static final short LOWER_CASE=0;
040        public static final short UPPER_CASE=1;
041        public static final short ORIGINAL_CASE=2;
042        
043        
044        private CFMLEngine engine;
045        private ConfigMap properties;
046        private BlazeDS util;
047    
048        public void initialize(String id, ConfigMap properties) {
049                super.initialize(id, properties);
050        this.properties=properties;
051        try{
052                // we call this because otherwse they does not exist (bug in BlazeDS)
053                ConfigMap propertyCases = properties.getPropertyAsMap("property-case", null);
054                if(propertyCases!=null){
055                    propertyCases.getPropertyAsBoolean("force-cfc-lowercase", false);
056                    propertyCases.getPropertyAsBoolean("force-query-lowercase", false);
057                    propertyCases.getPropertyAsBoolean("force-struct-lowercase", false);
058                }
059                ConfigMap access = properties.getPropertyAsMap("access", null);
060                if(access!=null){
061                    access.getPropertyAsBoolean("use-mappings", false);
062                    access.getPropertyAsString("method-access-level","remote");
063                }
064        }
065        catch(Throwable t){
066                        if(t instanceof ThreadDeath) throw (ThreadDeath)t;}
067        
068    }
069        
070        
071        /**
072         * @see flex.messaging.services.ServiceAdapter#invoke(flex.messaging.messages.Message)
073         */
074        public Object invoke(Message message){
075                try {
076                        if(util==null){
077                                util = (BlazeDS)getEngine().getBlazeDSUtil();
078                                util.init(properties);
079                        }
080                        return util.invoke(this,message);
081                } 
082                catch (Exception e) {e.printStackTrace();
083                        throw new RuntimeException(e);
084                }
085    }
086    
087    /**
088     * load (if needed) and return the CFMLEngine
089     * @return CFML Engine
090     */
091    private CFMLEngine getEngine() {
092        if(engine==null){
093                try {CFMLEngineFactory.getInstance();
094                                engine=CFMLEngineFactory.getInstance(FlexContext.getServletConfig(),this);
095                        } 
096                catch (Throwable t) {
097                                if(t instanceof ThreadDeath) throw (ThreadDeath)t;
098                                throw new RuntimeException(t);
099                        }
100        }
101        return engine;
102        }
103
104        /**
105     * @see lucee.loader.engine.EngineChangeListener#onUpdate(lucee.loader.engine.CFMLEngine)
106     */
107    public void onUpdate(CFMLEngine newEngine) {
108        try {
109            engine=CFMLEngineFactory.getInstance(FlexContext.getServletConfig(),this);
110        } catch (ServletException e) {
111            engine=newEngine;
112        }
113    }
114}