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