001    /**
002     * Implements the Cold Fusion Function createobject
003     * FUTURE neue attr unterst�tzen
004     */
005    package railo.runtime.functions.other;
006    
007    import railo.commons.io.res.Resource;
008    import railo.commons.io.res.util.ResourceUtil;
009    import railo.commons.lang.ClassUtil;
010    import railo.commons.lang.StringUtil;
011    import railo.runtime.Component;
012    import railo.runtime.PageContext;
013    import railo.runtime.com.COMObject;
014    import railo.runtime.config.ConfigImpl;
015    import railo.runtime.exp.ExpressionException;
016    import railo.runtime.exp.FunctionNotSupported;
017    import railo.runtime.exp.PageException;
018    import railo.runtime.exp.SecurityException;
019    import railo.runtime.ext.function.Function;
020    import railo.runtime.java.JavaObject;
021    import railo.runtime.net.proxy.ProxyData;
022    import railo.runtime.net.proxy.ProxyDataImpl;
023    import railo.runtime.net.rpc.client.RPCClient;
024    import railo.runtime.op.Caster;
025    import railo.runtime.security.SecurityManager;
026    import railo.runtime.type.Array;
027    import railo.runtime.type.List;
028    import railo.runtime.type.Struct;
029    
030    public final class CreateObject implements Function {
031            public static Object call(PageContext pc , String cfcName) throws PageException {
032                    return call(pc,"component",cfcName,null,null);
033            }
034            public static Object call(PageContext pc , String type, String className) throws PageException {
035                    return call(pc,type,className,null,null);
036            }
037            public static Object call(PageContext pc , String type, String className, Object context) throws PageException {
038                    return call(pc,type,className,context,null);
039            }
040            public static Object call(PageContext pc , String type, String className, Object context, Object serverName) throws PageException {
041                    type=StringUtil.toLowerCase(type);
042                    
043                    
044                    // JAVA
045                            if(type.equals("java")) {
046                                checkAccess(pc,type);
047                                    return doJava(pc, className,Caster.toString(context),Caster.toString(serverName));
048                            }
049                    // COM
050                            if(type.equals("com")) {
051                                    return doCOM(pc,className);
052                            }
053            // Component
054                if(type.equals("component")) {
055                    return doComponent(pc,className);
056                }
057            // Webservice
058                if(type.equals("webservice") || type.equals("wsdl")) {
059                    String user=null;
060                    String pass=null;
061                    ProxyDataImpl proxy=null;
062                    if(context!=null){
063                            Struct args=(serverName!=null)?Caster.toStruct(serverName):Caster.toStruct(context);
064                            // basic security
065                            user=Caster.toString(args.get("username",null));
066                            pass=Caster.toString(args.get("password",null));
067                            
068                            // proxy
069                            String proxyServer=Caster.toString(args.get("proxyServer",null));
070                            String proxyPort=Caster.toString(args.get("proxyPort",null));
071                            String proxyUser=Caster.toString(args.get("proxyUser",null));
072                            if(StringUtil.isEmpty(proxyUser)) proxyUser=Caster.toString(args.get("proxyUsername",null));
073                            String proxyPassword=Caster.toString(args.get("proxyPassword",null));
074                            
075                            if(!StringUtil.isEmpty(proxyServer)){
076                                    proxy=new ProxyDataImpl(proxyServer,Caster.toIntValue(proxyPort,-1),proxyUser,proxyPassword);
077                            }                       
078                            
079                    }
080                    return doWebService(pc,className,user,pass,proxy);
081                }
082            // .net
083                if(type.equals(".net") || type.equals("dotnet")) {
084                    return doDotNet(pc,className);
085                }
086                            throw new ExpressionException("invalid argument for function createObject, first argument (type), " +
087                                            "must be (com, java, webservice or component) other types are not supported");
088                    
089            } 
090    
091        private static Object doDotNet(PageContext pc, String className) throws FunctionNotSupported {
092            throw new FunctionNotSupported("CreateObject","type .net");
093            }
094            private static void checkAccess(PageContext pc, String type) throws SecurityException {
095            if(pc.getConfig().getSecurityManager().getAccess(SecurityManager.TYPE_TAG_OBJECT)==SecurityManager.VALUE_NO) 
096                            throw new SecurityException("can't access function [createObject] with type ["+type+"]","access is prohibited by security manager");
097                    
098        }
099            
100             
101        public static Object doJava(PageContext pc,String className, String pathes, String delimeter) throws PageException {
102            if(pc.getConfig().getSecurityManager().getAccess(SecurityManager.TYPE_DIRECT_JAVA_ACCESS)==SecurityManager.VALUE_YES) {
103                    ConfigImpl ci = ((ConfigImpl)pc.getConfig());
104                    
105                    // load resources
106                    Resource[] reses=null;
107                    if(!StringUtil.isEmpty(pathes, true)) {
108                            if(StringUtil.isEmpty(delimeter))delimeter=",";
109                            Array arrPathes = List.listToArrayRemoveEmpty(pathes.trim(),delimeter);
110                            reses=new Resource[arrPathes.size()];
111                            for(int i=0;i<reses.length;i++) {
112                                    reses[i]=ResourceUtil.toResourceExisting(pc,Caster.toString(arrPathes.getE(i+1)));
113                            }
114                    }
115                    
116                    // load class
117                    try     {
118                            ClassLoader cl = reses==null?ci.getClassLoader():ci.getClassLoader(reses);
119                            Class clazz = ClassUtil.loadClass(cl,className);
120                            return new JavaObject((pc).getVariableUtil(),clazz);
121                    } 
122                            catch (Exception e) {
123                                    throw Caster.toPageException(e);
124                            }
125            }
126            throw new SecurityException("can't create Java Object ["+className+"], direct java access is deinied by security manager");
127            } 
128        
129        public static Object doCOM(PageContext pc,String className) {
130                    return new COMObject(className);
131            } 
132        
133        public static Component doComponent(PageContext pc,String className) throws PageException {
134            
135            return pc.loadComponent(className);
136        } 
137        
138        public static Object doWebService(PageContext pc,String wsdlUrl) throws PageException {
139            // TODO CF8 impl. alle neuen attribute f�r wsdl
140            return new RPCClient(wsdlUrl);
141        } 
142        
143        public static Object doWebService(PageContext pc,String wsdlUrl,String username,String password, ProxyData proxy) throws PageException {
144            // TODO CF8 impl. alle neuen attribute f�r wsdl
145            return new RPCClient(wsdlUrl,username,password,proxy);
146        } 
147    }