001    package railo.runtime.type.scope;
002    
003    import java.util.Iterator;
004    
005    import railo.runtime.PageContext;
006    import railo.runtime.dump.DumpData;
007    import railo.runtime.dump.DumpProperties;
008    import railo.runtime.engine.ThreadLocalPageContext;
009    import railo.runtime.exp.ExpressionException;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.exp.PageRuntimeException;
012    import railo.runtime.java.JavaObject;
013    import railo.runtime.reflection.Reflector;
014    import railo.runtime.type.Collection;
015    import railo.runtime.type.KeyImpl;
016    import railo.runtime.type.Objects;
017    import railo.runtime.type.Struct;
018    import railo.runtime.type.dt.DateTime;
019    import railo.runtime.type.it.KeyIterator;
020    import railo.runtime.type.util.StructSupport;
021    
022    public final class ObjectStruct extends StructSupport implements Struct,Objects {
023    
024    
025            private JavaObject jo;
026    
027            public ObjectStruct(Object o) {
028                    if(o instanceof JavaObject) this.jo=(JavaObject) o;
029                    else this.jo=new JavaObject(ThreadLocalPageContext.get().getVariableUtil(),o);
030            }
031    
032            public ObjectStruct(JavaObject jo) {
033                    this.jo=jo;
034            }
035    
036            /**
037             *
038             * @see railo.runtime.java.JavaObject#call(railo.runtime.PageContext, java.lang.String, java.lang.Object[])
039             */
040            public Object call(PageContext pc, String methodName, Object[] arguments) throws PageException {
041                    return jo.call(pc, methodName, arguments);
042            }
043    
044            /**
045             *
046             * @see railo.runtime.java.JavaObject#call(railo.runtime.PageContext, railo.runtime.type.Collection.Key, java.lang.Object[])
047             */
048            public Object call(PageContext pc, Key methodName, Object[] arguments) throws PageException {
049                    return jo.call(pc, methodName, arguments);
050            }
051    
052            /**
053             *
054             * @see railo.runtime.java.JavaObject#callWithNamedValues(railo.runtime.PageContext, java.lang.String, railo.runtime.type.Struct)
055             */
056            public Object callWithNamedValues(PageContext pc, String methodName, Struct args) throws PageException {
057                    return jo.callWithNamedValues(pc, methodName, args);
058            }
059    
060            /**
061             *
062             * @see railo.runtime.java.JavaObject#callWithNamedValues(railo.runtime.PageContext, railo.runtime.type.Collection.Key, railo.runtime.type.Struct)
063             */
064            public Object callWithNamedValues(PageContext pc, Key methodName, Struct args) throws PageException {
065                    return jo.callWithNamedValues(pc, methodName, args);
066            }
067    
068            /**
069             *
070             * @see railo.runtime.java.JavaObject#get(railo.runtime.PageContext, java.lang.String)
071             */
072            public Object get(PageContext pc, String propertyName) throws PageException {
073                    return jo.get(pc, propertyName);
074            }
075    
076            /**
077             *
078             * @see railo.runtime.java.JavaObject#get(railo.runtime.PageContext, railo.runtime.type.Collection.Key)
079             */
080            public Object get(PageContext pc, Key key) throws PageException {
081                    return jo.get(pc, key);
082            }
083    
084            /**
085             *
086             * @see railo.runtime.java.JavaObject#get(railo.runtime.PageContext, java.lang.String, java.lang.Object)
087             */
088            public Object get(PageContext pc, String propertyName, Object defaultValue) {
089                    return jo.get(pc, propertyName, defaultValue);
090            }
091    
092            /**
093             *
094             * @see railo.runtime.java.JavaObject#get(railo.runtime.PageContext, railo.runtime.type.Collection.Key, java.lang.Object)
095             */
096            public Object get(PageContext pc, Key key, Object defaultValue) {
097                    return jo.get(pc, key, defaultValue);
098            }
099    
100            /**
101             *
102             * @see railo.runtime.java.JavaObject#isInitalized()
103             */
104            public boolean isInitalized() {
105                    return jo.isInitalized();
106            }
107    
108            /**
109             *
110             * @see railo.runtime.java.JavaObject#set(railo.runtime.PageContext, java.lang.String, java.lang.Object)
111             */
112            public Object set(PageContext pc, String propertyName, Object value) throws PageException {
113                    return jo.set(pc, propertyName, value);
114            }
115    
116            /**
117             *
118             * @see railo.runtime.java.JavaObject#set(railo.runtime.PageContext, railo.runtime.type.Collection.Key, java.lang.Object)
119             */
120            public Object set(PageContext pc, Key propertyName, Object value) throws PageException {
121                    return jo.set(pc, propertyName, value);
122            }
123    
124            /**
125             *
126             * @see railo.runtime.java.JavaObject#setEL(railo.runtime.PageContext, java.lang.String, java.lang.Object)
127             */
128            public Object setEL(PageContext pc, String propertyName, Object value) {
129                    return jo.setEL(pc, propertyName, value);
130            }
131    
132            /**
133             *
134             * @see railo.runtime.java.JavaObject#setEL(railo.runtime.PageContext, railo.runtime.type.Collection.Key, java.lang.Object)
135             */
136            public Object setEL(PageContext pc, Key propertyName, Object value) {
137                    return jo.setEL(pc, propertyName, value);
138            }
139            
140            /**
141             * @see railo.runtime.type.Collection#clear()
142             */
143            public void clear() {
144                    //throw new PageRuntimeException(new ExpressionException("can't clear fields from object ["+objects.getClazz().getName()+"]"));
145            }
146    
147            public Collection duplicate(boolean deepCopy) {
148                    throw new PageRuntimeException(new ExpressionException("can't clone object of type ["+jo.getClazz().getName()+"]"));
149                    //return null;
150            }
151    
152            
153    
154            /**
155             * @see railo.runtime.type.Collection#containsKey(java.lang.String)
156             */
157            public boolean containsKey(Key key) {
158                    return Reflector.hasPropertyIgnoreCase(jo.getClazz(), key.getString());
159            }
160    
161            /**
162             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key)
163             */
164            public Object get(Key key) throws PageException {
165                    return jo.get(ThreadLocalPageContext.get(), key);
166            }
167    
168            /**
169             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key, java.lang.Object)
170             */
171            public Object get(Key key, Object defaultValue) {
172                    return jo.get(ThreadLocalPageContext.get(), key,defaultValue);
173            }
174    
175            /**
176             * @see railo.runtime.type.Collection#keys()
177             */
178            public Key[] keys() {
179                    String[] strKeys = keysAsString();
180                    Key[] keys=new Key[strKeys.length];
181                    for(int i=0;i<strKeys.length;i++) {
182                            keys[i]=KeyImpl.init(strKeys[i]);
183                    }
184                    return keys;
185            }
186    
187            /**
188             * @see railo.runtime.type.Collection#keysAsString()
189             */
190            public String[] keysAsString() {
191                    return Reflector.getPropertyKeys(jo.getClazz());
192            }
193    
194            /**
195             * @see railo.runtime.type.Collection#remove(railo.runtime.type.Collection.Key)
196             */
197            public Object remove(Key key) throws PageException {
198                    throw new ExpressionException("can't remove field ["+key.getString()+"] from object ["+jo.getClazz().getName()+"]");
199            }
200    
201            /**
202             * @see railo.runtime.type.Collection#removeEL(railo.runtime.type.Collection.Key)
203             */
204            public Object removeEL(Key key) {
205                    return null;
206            }
207    
208            /**
209             * @see railo.runtime.type.Collection#set(railo.runtime.type.Collection.Key, java.lang.Object)
210             */
211            public Object set(Key key, Object value) throws PageException {
212                    return jo.set(ThreadLocalPageContext.get(), key, value);
213            }
214    
215            /**
216             * @see railo.runtime.type.Collection#setEL(railo.runtime.type.Collection.Key, java.lang.Object)
217             */
218            public Object setEL(Key key, Object value) {
219                    return jo.setEL(ThreadLocalPageContext.get(), key, value);
220            }
221    
222            /**
223             * @see railo.runtime.type.Collection#size()
224             */
225            public int size() {
226                    return keysAsString().length;
227            }
228    
229            /**
230             * @see railo.runtime.dump.Dumpable#toDumpData(railo.runtime.PageContext, int)
231             */
232            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
233                    return jo.toDumpData(pageContext, maxlevel,dp);
234            }
235    
236            /**
237             * @see railo.runtime.type.Iteratorable#keyIterator()
238             */
239            public Iterator keyIterator() {
240                    return new KeyIterator(keys());
241            }
242    
243            /**
244             * @see railo.runtime.op.Castable#castToBooleanValue()
245             */
246            public boolean castToBooleanValue() throws PageException {
247                    return jo.castToBooleanValue();
248            }
249        
250        /**
251         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
252         */
253        public Boolean castToBoolean(Boolean defaultValue) {
254            return jo.castToBoolean(defaultValue);
255        }
256    
257            /**
258             * @see railo.runtime.op.Castable#castToDateTime()
259             */
260            public DateTime castToDateTime() throws PageException {
261                    return jo.castToDateTime();
262            }
263        
264        /**
265         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
266         */
267        public DateTime castToDateTime(DateTime defaultValue) {
268            return jo.castToDateTime(defaultValue);
269        }
270    
271            /**
272             * @see railo.runtime.op.Castable#castToDoubleValue()
273             */
274            public double castToDoubleValue() throws PageException {
275                    return jo.castToDoubleValue();
276            }
277        
278        /**
279         * @see railo.runtime.op.Castable#castToDoubleValue(double)
280         */
281        public double castToDoubleValue(double defaultValue) {
282            return jo.castToDoubleValue(defaultValue);
283        }
284    
285            /**
286             * @see railo.runtime.op.Castable#castToString()
287             */
288            public String castToString() throws PageException {
289                    return jo.castToString();
290            }
291            
292            /**
293             * @see railo.runtime.type.util.StructSupport#castToString(java.lang.String)
294             */
295            public String castToString(String defaultValue) {
296                    return jo.castToString(defaultValue);
297            }
298    
299            /**
300             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
301             */
302            public int compareTo(String str) throws PageException {
303                    return jo.compareTo(str);
304            }
305    
306            /**
307             * @see railo.runtime.op.Castable#compareTo(boolean)
308             */
309            public int compareTo(boolean b) throws PageException {
310                    return jo.compareTo(b);
311            }
312    
313            /**
314             * @see railo.runtime.op.Castable#compareTo(double)
315             */
316            public int compareTo(double d) throws PageException {
317                    return jo.compareTo(d);
318            }
319    
320            /**
321             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
322             */
323            public int compareTo(DateTime dt) throws PageException {
324                    return jo.compareTo(dt);
325            }
326    }