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.other;
020
021import java.io.IOException;
022
023import lucee.commons.lang.ClassUtil;
024import lucee.runtime.Component;
025import lucee.runtime.PageContext;
026import lucee.runtime.PageContextImpl;
027import lucee.runtime.exp.FunctionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.op.Caster;
031import lucee.runtime.op.Decision;
032import lucee.runtime.type.util.ListUtil;
033import lucee.transformer.bytecode.util.JavaProxyFactory;
034
035public class CreateDynamicProxy implements Function {
036        
037        private static final long serialVersionUID = -1787490871697335220L;
038
039        public static Object call(PageContext pc , Object oCFC,Object oInterfaces) throws PageException {
040                try {
041                        return _call(pc, oCFC, oInterfaces);
042                } catch (IOException e) {
043                        throw Caster.toPageException(e);
044                }
045        }
046        
047        public static Object _call(PageContext pc , Object oCFC,Object oInterfaces) throws PageException, IOException {
048                
049                // Component
050                Component cfc;
051                if(oCFC instanceof Component)
052                        cfc= (Component)oCFC;
053                else
054                        cfc=pc.loadComponent(Caster.toString(oCFC));
055                
056                // interfaces
057                String[] strInterfaces;
058                if(Decision.isArray(oInterfaces)) {
059                        strInterfaces=ListUtil.toStringArray(Caster.toArray(oInterfaces));
060                }
061                else {
062                        String list = Caster.toString(oInterfaces);
063                        strInterfaces=ListUtil.listToStringArray(list, ',');
064                }
065                strInterfaces=ListUtil.trimItems(strInterfaces);
066                
067                
068                ClassLoader cl = ((PageContextImpl)pc).getClassLoader();
069                Class[] interfaces=new Class[strInterfaces.length];
070                for(int i=0;i<strInterfaces.length;i++){
071                        interfaces[i]=ClassUtil.loadClass(cl, strInterfaces[i]);
072                        if(!interfaces[i].isInterface()) throw new FunctionException(pc, "CreateDynamicProxy", 2, "interfaces", "definition ["+strInterfaces[i]+"] is a class and not a interface");
073                }
074                
075                return JavaProxyFactory.createProxy(pc,cfc, null,interfaces);
076        }
077            
078}