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.java;
020
021import java.util.Calendar;
022import java.util.Date;
023import java.util.Iterator;
024import java.util.Map;
025import java.util.Map.Entry;
026
027import lucee.commons.io.DevNullOutputStream;
028import lucee.runtime.Component;
029import lucee.runtime.PageContext;
030import lucee.runtime.config.ConfigWeb;
031import lucee.runtime.engine.ThreadLocalPageContext;
032import lucee.runtime.exp.PageException;
033import lucee.runtime.exp.PageRuntimeException;
034import lucee.runtime.op.Caster;
035import lucee.runtime.op.Decision;
036import lucee.runtime.thread.ThreadUtil;
037import lucee.runtime.type.Array;
038import lucee.runtime.type.Query;
039import lucee.runtime.type.QueryColumn;
040import lucee.runtime.type.Struct;
041import lucee.runtime.type.StructImpl;
042import lucee.runtime.type.util.ArrayUtil;
043
044/**
045 * creates a Java Proxy for components, so you can use componets as java classes following a certain interface or class
046 */
047public class JavaProxy {
048        
049        public static Object call(ConfigWeb config,Component cfc, String methodName, Object... arguments) {
050                try {
051                        
052                        PageContext pc = ThreadLocalPageContext.get();
053                        if(pc==null) {
054                                //PageSource ps = cfc.getPageSource();
055                                pc=ThreadUtil.createPageContext(
056                                                config, 
057                                                DevNullOutputStream.DEV_NULL_OUTPUT_STREAM, 
058                                                "Lucee", "/", "", null, null, null, null);
059                                pc.addPageSource(cfc.getPageSource(), true);
060                                
061                        }
062                        
063                        return cfc.call(
064                                        ThreadLocalPageContext.get(), 
065                                        methodName, 
066                                        arguments);
067                } catch (PageException pe) {
068                        throw new PageRuntimeException(pe);
069                }
070        }
071
072        public static boolean toBoolean(Object obj) {
073                try {
074                        return Caster.toBooleanValue(obj);
075                } catch (PageException pe) {
076                        throw new PageRuntimeException(pe);
077                }
078        }
079
080        public static float toFloat(Object obj) {
081                try {
082                        return Caster.toFloatValue(obj);
083                } catch (PageException pe) {
084                        throw new PageRuntimeException(pe);
085                }
086        }
087
088        public static int toInt(Object obj) {
089                try {
090                        return Caster.toIntValue(obj);
091                } catch (PageException pe) {
092                        throw new PageRuntimeException(pe);
093                }
094        }
095
096        public static double toDouble(Object obj) {
097                try {
098                        return Caster.toDoubleValue(obj);
099                } catch (PageException pe) {
100                        throw new PageRuntimeException(pe);
101                }
102        }
103
104        public static long toLong(Object obj) {
105                try {
106                        return Caster.toLongValue(obj);
107                } catch (PageException pe) {
108                        throw new PageRuntimeException(pe);
109                }
110        }
111
112        public static char toChar(Object obj) {
113                try {
114                        return Caster.toCharValue(obj);
115                } catch (PageException pe) {
116                        throw new PageRuntimeException(pe);
117                }
118        }
119
120        public static byte toByte(Object obj) {
121                try {
122                        return Caster.toByteValue(obj);
123                } catch (PageException pe) {
124                        throw new PageRuntimeException(pe);
125                }
126        }
127
128        public static short toShort(Object obj) {
129                try {
130                        return Caster.toShortValue(obj);
131                } catch (PageException pe) {
132                        throw new PageRuntimeException(pe);
133                }
134        }
135
136        public static Object to(Object obj, Class clazz) {
137                return obj;
138        }
139        
140
141        public static Object toCFML(boolean value) {
142                return value?Boolean.TRUE:Boolean.FALSE;
143        }
144        public static Object toCFML(byte value) {
145                return Caster.toDouble(value);
146        }
147        public static Object toCFML(char value) {
148                return new String(new char[]{value});
149        }
150        public static Object toCFML(double value) {
151                return Caster.toDouble(value);
152        }
153        public static Object toCFML(float value) {
154                return Caster.toDouble(value);
155        }
156        public static Object toCFML(int value) {
157                return Caster.toDouble(value);
158        }
159        public static Object toCFML(long value) {
160                return Caster.toDouble(value);
161        }
162        public static Object toCFML(short value) {
163                return Caster.toDouble(value);
164        }
165
166        public static Object toCFML(Object value) {
167                try {
168                        return _toCFML(value);
169                } catch (PageException e) {
170                        return value;
171                }
172        }
173        public static Object _toCFML(Object value) throws PageException {
174                if(value instanceof Date || value instanceof Calendar) {// do not change to caster.isDate
175                return Caster.toDate(value,null);
176        }
177                if(value instanceof Object[]) {
178                Object[] arr=(Object[]) value;
179                if(!ArrayUtil.isEmpty(arr)){
180                        boolean allTheSame=true;
181                        // byte
182                        if(arr[0] instanceof Byte){
183                                for(int i=1;i<arr.length;i++){
184                                        if(!(arr[i] instanceof Byte)){
185                                                allTheSame=false;
186                                                break;
187                                        }
188                                }
189                                if(allTheSame){
190                                        byte[] bytes=new byte[arr.length];
191                                        for(int i=0;i<arr.length;i++){
192                                        bytes[i]=Caster.toByteValue(arr[i]);
193                                }
194                                        return bytes;
195                                }
196                        }
197                }
198        }
199        if(value instanceof Byte[]) {
200                Byte[] arr=(Byte[]) value;
201                if(!ArrayUtil.isEmpty(arr)){
202                                byte[] bytes=new byte[arr.length];
203                                for(int i=0;i<arr.length;i++){
204                                bytes[i]=arr[i].byteValue();
205                        }
206                                return bytes;
207                }
208        }
209        if(value instanceof byte[]) {
210                return value;
211        }
212        if(Decision.isArray(value)) {
213            Array a = Caster.toArray(value);
214            int len=a.size();
215            Object o;
216            for(int i=1;i<=len;i++) {
217                o=a.get(i,null);
218                if(o!=null)a.setEL(i,toCFML(o));
219            }
220            return a;
221        }
222        if(value instanceof Map) {
223                Struct sct = new StructImpl();
224            Iterator it=((Map)value).entrySet().iterator();
225            Map.Entry entry;
226            while(it.hasNext()) {
227                entry=(Entry) it.next();
228                sct.setEL(Caster.toString(entry.getKey()),toCFML(entry.getValue()));
229            }
230            return sct;
231                
232                
233                //return StructUtil.copyToStruct((Map)value);
234        }
235        if(Decision.isQuery(value)) {
236            Query q = Caster.toQuery(value);
237            int recorcount=q.getRecordcount();
238            String[] strColumns = q.getColumns();
239            
240            QueryColumn col;
241            int row;
242            for(int i=0;i<strColumns.length;i++) {
243                col=q.getColumn(strColumns[i]);
244                for(row=1;row<=recorcount;row++) {
245                    col.set(row,toCFML(col.get(row,null)));
246                }
247            }
248            return q;
249        }
250                return value;
251        }
252        
253        
254        
255        
256}