001    /**
002     * Implements the CFML Function getmetadata
003     */
004    package railo.runtime.functions.other;
005    
006    import railo.runtime.Component;
007    import railo.runtime.PageContext;
008    import railo.runtime.exp.FunctionException;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.ext.function.Function;
011    import railo.runtime.img.Image;
012    import railo.runtime.java.JavaObject;
013    import railo.runtime.op.Caster;
014    import railo.runtime.type.KeyImpl;
015    import railo.runtime.type.ObjectWrap;
016    import railo.runtime.type.Query;
017    import railo.runtime.type.Struct;
018    import railo.runtime.type.StructImpl;
019    import railo.runtime.type.UDF;
020    
021    public final class GetMetaData implements Function {
022    
023            private static final long serialVersionUID = -3787469574373656167L;
024    
025            // TODO support enties more deeply
026            public static Object call(PageContext pc ) throws PageException {
027            Component ac = pc.getActiveComponent();
028            if(ac!=null) {
029                    return call(pc , ac);
030                }
031                
032                    return new StructImpl();
033            }
034            
035            public static Object call(PageContext pc , Object object) throws PageException {
036                    return call(pc, object, false);
037            }
038            
039            public static Object call(PageContext pc , Object object,boolean source) throws PageException {
040                    if(object instanceof JavaObject){
041                            return call(pc,((JavaObject)object).getClazz(),source);
042                    }
043                    else if(object instanceof ObjectWrap){
044                            return call(pc,((ObjectWrap)object).getEmbededObject(),source);
045                    }
046                    
047                    if(!source){
048                            // Component
049                            if(object instanceof Component) {
050                                    return getMetaData((Component)object,pc);
051                                    //return ((Component)object).getMetaData(pc);
052                            }
053                            // UDF
054                            if(object instanceof UDF) {
055                                    return ((UDF)object).getMetaData(pc);
056                            }
057                            // Query
058                    else if(object instanceof Query) {
059                        return ((Query)object).getMetaDataSimple();
060                    }
061                            // Image
062                    else if(object instanceof Image) {
063                        return ((Image)object).info();
064                    }
065                            
066                            return object.getClass();
067                    }
068                    
069                    String str = Caster.toString(object,null);
070                    if(str==null)throw new FunctionException(pc,"GetMetaData",1,"object","must be a string when second argument is true");
071            return pc.undefinedScope().getScope(KeyImpl.init(str));
072                    
073            }
074    
075            public static Struct getMetaData(Component cfc, PageContext pc) throws PageException {
076                    return cfc.getMetaData(pc);
077            }
078    
079            /*private static Map<String,Struct> datas=new ReferenceMap(ReferenceMap.SOFT,ReferenceMap.SOFT);
080            
081            public static Struct getMetaData(ComponentPro cfc,PageContext pc) throws PageException {
082                    String key=createKey(cfc.getPageSource());
083                    Struct meta = key==null?null:datas.get(key);
084                    if(meta==null){
085                            meta=cfc.getMetaData(pc);
086                            datas.put(key, meta);
087                    }
088                    return meta;
089            }
090            public static String createKey(PageSource ps) throws PageException {
091                    Page p = ((PageSourceImpl)ps).getPage();
092                    if(p==null) return null;
093                    String key=ps.getMapping().getConfig().getId()+":"+p.getSourceLastModified()+":"+ps.getDisplayPath();
094                    return key;
095            }
096    
097            public static Struct getMetaData(PageSource ps) throws PageException {
098                    String key=createKey(ps);
099                    return key==null?null:datas.get(key);
100            }*/
101    }