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.type.util;
020
021import lucee.commons.lang.ExceptionUtil;
022import lucee.runtime.Component;
023import lucee.runtime.ComponentPro;
024import lucee.runtime.component.Member;
025import lucee.runtime.component.Property;
026import lucee.runtime.op.Caster;
027import lucee.runtime.type.Collection;
028import lucee.runtime.type.Collection.Key;
029
030public class ComponentProUtil {
031        
032        public static Property[] getProperties(Component c,boolean onlyPeristent, boolean includeBaseProperties, boolean preferBaseProperties, boolean inheritedMappedSuperClassOnly) {
033                ComponentPro cp = toComponentPro(c);
034                if(cp!=null) return cp.getProperties(onlyPeristent, includeBaseProperties,preferBaseProperties,inheritedMappedSuperClassOnly);
035                
036                // reflection
037                try{
038                        java.lang.reflect.Method getProperties = c.getClass().getMethod("getProperties", new Class[]{
039                                boolean.class,boolean.class,boolean.class,boolean.class});
040                        return (Property[]) getProperties.invoke(c, new Object[]{onlyPeristent,includeBaseProperties,preferBaseProperties,inheritedMappedSuperClassOnly});
041                }
042                catch(Throwable t){
043                        ExceptionUtil.rethrowIfNecessary(t);
044                        throw new RuntimeException(t);
045                }
046        }
047
048        public static Component getBaseComponent(Component c) { 
049                ComponentPro cp = toComponentPro(c);
050                if(cp!=null) return cp.getBaseComponent();
051                
052                // reflection
053                try{
054                        java.lang.reflect.Method getBaseComponent = c.getClass().getMethod("getBaseComponent", new Class[]{});
055                        return (Component) getBaseComponent.invoke(c, new Object[]{});
056                }
057                catch(Throwable t){
058                        ExceptionUtil.rethrowIfNecessary(t);
059                        throw new RuntimeException(t);
060                }
061        }
062
063        public static boolean isPersistent(Component c) {
064                ComponentPro cp = toComponentPro(c);
065                if(cp!=null) return cp.isPersistent();
066                
067                // reflection
068                try{
069                        java.lang.reflect.Method isPersistent = c.getClass().getMethod("isPersistent", new Class[]{});
070                        return Caster.toBooleanValue(isPersistent.invoke(c, new Object[]{}));
071                }
072                catch(Throwable t){
073                        ExceptionUtil.rethrowIfNecessary(t);
074                        throw new RuntimeException(t);
075                }
076        }
077        
078        public static Key[] keys(Component c, int access) {
079                ComponentPro cp = toComponentPro(c);
080                if(cp!=null) return cp.keys(access);
081                
082                // reflection
083                try{
084                        java.lang.reflect.Method keys = c.getClass().getMethod("keys", new Class[]{int.class});
085                        return (Key[]) keys.invoke(c, new Object[]{access});
086                }
087                catch(Throwable t){
088                        ExceptionUtil.rethrowIfNecessary(t);
089                        throw new RuntimeException(t);
090                }
091        }
092
093        public static Member getMember(Component c, int access,Collection.Key key, boolean dataMember,boolean superAccess) {
094                ComponentPro cp = toComponentPro(c);
095                if(cp!=null) return cp.getMember(access, key, dataMember, superAccess);
096                
097                // reflection
098                try{
099                        java.lang.reflect.Method getMember = c.getClass().getMethod("getMember", new Class[]{int.class,Collection.Key.class, boolean.class,boolean.class});
100                        return (Member)getMember.invoke(c, new Object[]{access,key,dataMember,superAccess});
101                }
102                catch(Throwable t){
103                        ExceptionUtil.rethrowIfNecessary(t);
104                        throw new RuntimeException(t);
105                }
106        }
107        
108        public static Object get(Component c, int access, Key key, Object defaultValue) {
109                ComponentPro cp = toComponentPro(c);
110                if(cp!=null) return cp.get(access, key, defaultValue);
111                
112                // reflection
113                try{
114                        java.lang.reflect.Method getMember = c.getClass().getMethod("get", new Class[]{int.class, Key.class, Object.class});
115                        return getMember.invoke(c, new Object[]{access,key,defaultValue});
116                }
117                catch(Throwable t){
118                        ExceptionUtil.rethrowIfNecessary(t);
119                        throw new RuntimeException(t);
120                }
121        }
122        
123        public static ComponentPro toComponentPro(Component cfc) {
124                if(cfc instanceof ComponentPro)return (ComponentPro) cfc;
125                
126                // check for method getComponent
127                try{
128                        java.lang.reflect.Method getComponent = cfc.getClass().getMethod("getComponent", new Class[]{});
129                        return toComponentPro((Component) getComponent.invoke(cfc, new Object[]{}));
130                }
131                catch(Throwable t){
132                        ExceptionUtil.rethrowIfNecessary(t);
133                        throw new RuntimeException(t);
134                }
135        }
136
137}