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