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.functions.dynamicEvaluation;
020
021import java.util.Iterator;
022import java.util.Map.Entry;
023
024import lucee.commons.lang.SystemOut;
025import lucee.runtime.Component;
026import lucee.runtime.ComponentScope;
027import lucee.runtime.ComponentSpecificAccess;
028import lucee.runtime.PageContext;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.Collection;
032import lucee.runtime.type.Collection.Key;
033import lucee.runtime.type.Struct;
034import lucee.runtime.type.UDF;
035import lucee.runtime.type.util.CollectionUtil;
036import lucee.runtime.type.util.ComponentUtil;
037import lucee.runtime.type.util.KeyConstants;
038
039public final class EvaluateComponent {
040        public static Object call(PageContext pc, String name, String md5, Struct sctThis) throws PageException {
041                return invoke(pc, name, md5, sctThis, null);
042                }
043        public static Object call(PageContext pc, String name, String md5, Struct sctThis, Struct sctVariables) throws PageException {
044                return invoke(pc, name, md5, sctThis, sctVariables);
045        }
046        public static Component invoke(PageContext pc, String name, String md5, Struct sctThis, Struct sctVariables) throws PageException {
047                // Load comp
048                Component comp=null;
049                try {
050                        comp = pc.loadComponent(name);
051                        if(!ComponentUtil.md5(comp).equals(md5)){       
052                                SystemOut.printDate(pc.getConfig().getErrWriter(),"component ["+name+"] in this enviroment has not the same interface as the component to load, it is possible that one off the components has Functions added dynamicly.");
053                                //throw new ExpressionException("component ["+name+"] in this enviroment has not the same interface as the component to load");
054                        }
055                }
056                catch (Exception e) {
057                        throw Caster.toPageException(e);
058                }
059                setInternalState(comp,sctThis,sctVariables);
060                return comp;
061        }
062        public static void setInternalState(Component comp, Struct sctThis, Struct sctVariables) throws PageException {
063                
064                // this 
065                // delete this scope data members
066                ComponentSpecificAccess cw = ComponentSpecificAccess.toComponentSpecificAccess(Component.ACCESS_PRIVATE,comp);
067                Collection.Key[] cwKeys = CollectionUtil.keys(cw);
068                Object member;
069                for(int i=0;i<cwKeys.length;i++) {
070                        member = cw.get(cwKeys[i]);
071                        if(member instanceof UDF) continue;
072            cw.removeEL(cwKeys[i]);
073                }
074                
075                // set this scope data members
076                Iterator<Entry<Key, Object>> it = sctThis.entryIterator();
077                Entry<Key, Object> e;
078                //keys = sctThis.keys();
079                while(it.hasNext()) {
080                        e=it.next();
081            comp.set(e.getKey(),e.getValue());
082                }
083                
084        // Variables
085        
086                ComponentScope scope = comp.getComponentScope();
087                
088                // delete variables scope data members
089                Key[] sKeys = CollectionUtil.keys(scope);
090                for(int i=0;i<sKeys.length;i++) {
091                        if(KeyConstants._this.equals(sKeys[i])) continue;
092                        if(scope.get(sKeys[i]) instanceof UDF) continue;
093                scope.removeEL(sKeys[i]);
094                }
095                
096                
097                // set variables scope data members
098                it=sctVariables.entryIterator();
099                //keys = sctVariables.keys();
100                        while(it.hasNext()) {
101                                e=it.next();
102                                scope.set(e.getKey(),e.getValue());
103                        }
104        
105        }
106}