001    package railo.runtime.functions.dynamicEvaluation;
002    
003    import java.util.Iterator;
004    import java.util.Map.Entry;
005    
006    import railo.commons.lang.SystemOut;
007    import railo.runtime.Component;
008    import railo.runtime.ComponentScope;
009    import railo.runtime.ComponentWrap;
010    import railo.runtime.PageContext;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.op.Caster;
013    import railo.runtime.type.Collection;
014    import railo.runtime.type.Collection.Key;
015    import railo.runtime.type.Struct;
016    import railo.runtime.type.UDF;
017    import railo.runtime.type.util.CollectionUtil;
018    import railo.runtime.type.util.ComponentUtil;
019    import railo.runtime.type.util.KeyConstants;
020    
021    public final class EvaluateComponent {
022            public static Object call(PageContext pc, String name, String md5, Struct sctThis) throws PageException {
023                    return call(pc, name, md5, sctThis, null);
024            }
025            public static Object call(PageContext pc, String name, String md5, Struct sctThis, Struct sctVariables) throws PageException {
026                    
027                    // Load comp
028                    Component comp=null;
029                    try {
030                            comp = pc.loadComponent(name);
031                            if(!ComponentUtil.md5(comp).equals(md5)){       
032                                    SystemOut.printDate(pc.getConfig().getErrWriter(),"component ["+name+"] in this enviroment has not the same interface as the component to load");
033                                    //throw new ExpressionException("component ["+name+"] in this enviroment has not the same interface as the component to load");
034                            }
035                    }
036                    catch (Exception e) {
037                            throw Caster.toPageException(e);
038                    }
039                    
040                    
041                    
042                    setInternalState(comp,sctThis,sctVariables);
043                    
044                    
045            
046            return comp;
047            }
048            public static void setInternalState(Component comp, Struct sctThis, Struct sctVariables) throws PageException {
049                    
050                    // this 
051                    // delete this scope data members
052                    ComponentWrap cw = ComponentWrap.toComponentWrap(Component.ACCESS_PRIVATE,comp);
053                    Collection.Key[] cwKeys = CollectionUtil.keys(cw);
054                    Object member;
055                    for(int i=0;i<cwKeys.length;i++) {
056                            member = cw.get(cwKeys[i]);
057                            if(member instanceof UDF) continue;
058                cw.removeEL(cwKeys[i]);
059                    }
060                    
061                    // set this scope data members
062                    Iterator<Entry<Key, Object>> it = sctThis.entryIterator();
063                    Entry<Key, Object> e;
064                    //keys = sctThis.keys();
065                    while(it.hasNext()) {
066                            e=it.next();
067                comp.set(e.getKey(),e.getValue());
068                    }
069                    
070            // Variables
071            
072                    ComponentScope scope = comp.getComponentScope();
073                    
074                    // delete variables scope data members
075                    Key[] sKeys = CollectionUtil.keys(scope);
076                    for(int i=0;i<sKeys.length;i++) {
077                            if(KeyConstants._this.equals(sKeys[i])) continue;
078                            if(scope.get(sKeys[i]) instanceof UDF) continue;
079                    scope.removeEL(sKeys[i]);
080                    }
081                    
082                    
083                    // set variables scope data members
084                    it=sctVariables.entryIterator();
085                    //keys = sctVariables.keys();
086                            while(it.hasNext()) {
087                                    e=it.next();
088                                    scope.set(e.getKey(),e.getValue());
089                            }
090            
091            }
092    }