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