001    package railo.runtime.functions.other;
002    
003    import java.io.IOException;
004    
005    import railo.commons.lang.ClassUtil;
006    import railo.runtime.Component;
007    import railo.runtime.PageContext;
008    import railo.runtime.PageContextImpl;
009    import railo.runtime.exp.FunctionException;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.ext.function.Function;
012    import railo.runtime.op.Caster;
013    import railo.runtime.op.Decision;
014    import railo.runtime.type.util.ListUtil;
015    import railo.transformer.bytecode.util.JavaProxyFactory;
016    
017    public class CreateDynamicProxy implements Function {
018            
019            private static final long serialVersionUID = -1787490871697335220L;
020    
021            public static Object call(PageContext pc , Object oCFC,Object oInterfaces) throws PageException {
022                    try {
023                            return _call(pc, oCFC, oInterfaces);
024                    } catch (IOException e) {
025                            throw Caster.toPageException(e);
026                    }
027            }
028            
029            public static Object _call(PageContext pc , Object oCFC,Object oInterfaces) throws PageException, IOException {
030                    
031                    // Component
032                    Component cfc;
033                    if(oCFC instanceof Component)
034                            cfc= (Component)oCFC;
035                    else
036                            cfc=pc.loadComponent(Caster.toString(oCFC));
037                    
038                    // interfaces
039                    String[] strInterfaces;
040                    if(Decision.isArray(oInterfaces)) {
041                            strInterfaces=ListUtil.toStringArray(Caster.toArray(oInterfaces));
042                    }
043                    else {
044                            String list = Caster.toString(oInterfaces);
045                            strInterfaces=ListUtil.listToStringArray(list, ',');
046                    }
047                    strInterfaces=ListUtil.trimItems(strInterfaces);
048                    
049                    
050                    ClassLoader cl = ((PageContextImpl)pc).getClassLoader();
051                    Class[] interfaces=new Class[strInterfaces.length];
052                    for(int i=0;i<strInterfaces.length;i++){
053                            interfaces[i]=ClassUtil.loadClass(cl, strInterfaces[i]);
054                            if(!interfaces[i].isInterface()) throw new FunctionException(pc, "CreateDynamicProxy", 2, "interfaces", "definition ["+strInterfaces[i]+"] is a class and not a interface");
055                    }
056                    
057                    return JavaProxyFactory.createProxy(pc,cfc, null,interfaces);
058            }
059                
060    }