001    package railo.runtime.com;
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.dump.DumpTable;
009    import railo.runtime.dump.DumpTablePro;
010    import railo.runtime.dump.SimpleDumpData;
011    import railo.runtime.exp.ExpressionException;
012    import railo.runtime.exp.PageException;
013    import railo.runtime.type.Collection;
014    import railo.runtime.type.Iteratorable;
015    import railo.runtime.type.Objects;
016    import railo.runtime.type.Struct;
017    import railo.runtime.type.dt.DateTime;
018    
019    import com.jacob.com.Dispatch;
020    import com.jacob.com.Variant;
021    
022    /**
023     * 
024     */
025    public final class COMObject implements Objects, Iteratorable {
026    
027            private String name;
028        private Dispatch dispatch;
029        private Variant parent;
030    
031        /**
032             * Public Constructor of the class
033             * @param dispatch
034         * @throws ExpressionException 
035             */
036            public COMObject(String dispatch) {
037                    //if(!SystemUtil.isWindows()) throw new ExpressionException("Com Objects are only supported in Windows Enviroments");
038                    this.name=dispatch;
039                    this.dispatch=new Dispatch(dispatch);
040            }
041            
042            /**
043             * Private Constructor of the class for sub Objects
044             * @param parent
045             * @param dispatch
046             * @param name
047             */
048            COMObject(Variant parent,Dispatch dispatch, String name) {
049                    this.parent=parent;
050                    this.name=name;
051                    this.dispatch=dispatch;
052            }
053    
054        /**
055         * @see railo.runtime.type.ContextCollection#get(railo.runtime.PageContext, java.lang.String)
056         */
057        public Object get(PageContext pc, String propertyName) throws PageException {
058            return COMUtil.toObject(this,Dispatch.call(dispatch,propertyName),propertyName);
059        }
060    
061            public Object get(PageContext pc, Collection.Key key) throws PageException {
062                    return get(pc, key.getString());
063            }
064    
065        /**
066         * @see railo.runtime.type.Objects#get(railo.runtime.PageContext, java.lang.String, java.lang.Object)
067         */
068        public Object get(PageContext pc, String propertyName, Object defaultValue) {
069            return COMUtil.toObject(this,Dispatch.call(dispatch,propertyName),propertyName,defaultValue);
070        }
071    
072            public Object get(PageContext pc, Collection.Key key, Object defaultValue) {
073                    return get(pc, key.getString(), defaultValue);
074            }
075    
076        /**
077         * @see railo.runtime.type.Objects#set(railo.runtime.PageContext, java.lang.String, java.lang.Object)
078         */
079        public Object set(PageContext pc, String propertyName, Object value) {
080            return setEL(pc,propertyName,value);
081        }
082    
083            public Object set(PageContext pc, Collection.Key propertyName, Object value) throws PageException {
084                    return setEL(pc,propertyName.toString(),value);
085            }
086    
087        /**
088         * @see railo.runtime.type.Objects#setEL(railo.runtime.PageContext, java.lang.String, java.lang.Object)
089         */
090        public Object setEL(PageContext pc, String propertyName, Object value) {
091                    Dispatch.put(dispatch,propertyName,value);
092                    return value;
093        }
094    
095            /**
096             *
097             * @see railo.runtime.type.Objects#setEL(railo.runtime.PageContext, railo.runtime.type.Collection.Key, java.lang.Object)
098             */
099            public Object setEL(PageContext pc, Collection.Key propertyName, Object value) {
100                    return setEL(pc, propertyName.toString(), value);
101            }
102    
103        /**
104         * @see railo.runtime.type.Objects#call(railo.runtime.PageContext, java.lang.String, java.lang.Object[])
105         */
106        public Object call(PageContext pc, String methodName, Object[] args) throws PageException {
107            Object[] arr=new Object[args.length];
108                    for(int i=0;i<args.length;i++) {
109                            if(args[i] instanceof COMObject)arr[i]=((COMObject)args[i]).dispatch;
110                            else arr[i]=args[i];
111                    }       
112                    return COMUtil.toObject(this,Dispatch.callN(dispatch,methodName,arr),methodName);
113        }
114    
115            public Object call(PageContext pc, Collection.Key methodName, Object[] arguments) throws PageException {
116                    return call(pc, methodName.getString(), arguments) ;
117            }
118    
119        /**
120         * @see railo.runtime.type.Objects#callWithNamedValues(railo.runtime.PageContext, java.lang.String, railo.runtime.type.Struct)
121         */
122        public Object callWithNamedValues(PageContext pc, String methodName, Struct args) throws PageException {
123    //      TODO gibt es hier eine bessere m�glichkeit?
124            Collection.Key[] keys = args.keys();
125            Object[] values=new Object[keys.length];
126            for(int i=0;i<keys.length;i++) {
127                values[i]=args.get(keys[i],null);
128            }   
129            return call(pc,methodName,values);
130        }
131    
132            /**
133             *
134             * @see railo.runtime.type.Objects#callWithNamedValues(railo.runtime.PageContext, railo.runtime.type.Collection.Key, railo.runtime.type.Struct)
135             */
136            public Object callWithNamedValues(PageContext pc, Collection.Key methodName, Struct args) throws PageException {
137                    return callWithNamedValues(pc, methodName.getString(), args);
138            }
139    
140        /* *
141         * @see railo.runtime.reflection.wrapper.ObjectWrapper#getEmbededObject()
142         * /
143        public Object getEmbededObject() throws PageException {
144            return dispatch;
145        }*/
146    
147        /* *
148         * @see railo.runtime.reflection.wrapper.ObjectWrapper#getEmbededObjectEL()
149         * /
150        public Object getEmbededObjectEL() {
151            return dispatch;
152        }*/
153    
154        /**
155         * @see railo.runtime.type.Objects#isInitalized()
156         */
157        public boolean isInitalized() {
158            return true;
159        }
160    
161        /**
162             * @see railo.runtime.dump.Dumpable#toDumpData(railo.runtime.PageContext, int)
163             */
164            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
165                    DumpTable table = new DumpTablePro("com","#ff3300","#ff9966","#660000");
166                    table.appendRow(1,new SimpleDumpData("COM Object"),new SimpleDumpData(name));
167                    return table;
168        }
169    
170        /**
171         * @see railo.runtime.op.Castable#castToString()
172         */
173        public String castToString() throws ExpressionException {
174            throw new ExpressionException("can't cast Com Object to a String");
175        }
176    
177        /**
178         * @see railo.runtime.op.Castable#castToString(java.lang.String)
179         */
180        public String castToString(String defaultValue) {
181            return defaultValue;
182        }
183    
184        /**
185         * @see railo.runtime.op.Castable#castToBooleanValue()
186         */
187        public boolean castToBooleanValue() throws ExpressionException {
188            throw new ExpressionException("can't cast Com Object to a boolean value");
189        }
190        
191        /**
192         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
193         */
194        public Boolean castToBoolean(Boolean defaultValue) {
195            return defaultValue;
196        }
197    
198        /**
199         * @see railo.runtime.op.Castable#castToDoubleValue()
200         */
201        public double castToDoubleValue() throws ExpressionException {
202            throw new ExpressionException("can't cast Com Object to a number");
203        }
204        
205        /**
206         * @see railo.runtime.op.Castable#castToDoubleValue(double)
207         */
208        public double castToDoubleValue(double defaultValue) {
209            return defaultValue;
210        }
211    
212        /**
213         * @see railo.runtime.op.Castable#castToDateTime()
214         */
215        public DateTime castToDateTime() throws ExpressionException {
216            throw new ExpressionException("can't cast Com Object to a Date");
217        }
218        
219        /**
220         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
221         */
222        public DateTime castToDateTime(DateTime defaultValue) {
223            return defaultValue;
224        }
225        
226        /**
227         * @return Returns the name.
228         */
229        public String getName() {
230            return name;
231        }
232        /**
233         * @return Returns the dispatch.
234         */
235        public Dispatch getDispatch() {
236            return dispatch;
237        }
238        /**
239         * @return Returns the parent.
240         */
241        public Variant getParent() {
242            return parent;
243        }
244    
245        /**
246         * release the com Object
247         */
248        public void release() {
249            dispatch.safeRelease();
250        }
251    
252            /**
253             * @see railo.runtime.op.Castable#compare(boolean)
254             */
255            public int compareTo(boolean b) throws ExpressionException {
256                    throw new ExpressionException("can't compare Com Object with a boolean value");
257            }
258    
259            /**
260             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
261             */
262            public int compareTo(DateTime dt) throws PageException {
263                    throw new ExpressionException("can't compare Com Object with a DateTime Object");
264            }
265    
266            /**
267             * @see railo.runtime.op.Castable#compareTo(double)
268             */
269            public int compareTo(double d) throws PageException {
270                    throw new ExpressionException("can't compare Com Object with a numeric value");
271            }
272    
273            /**
274             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
275             */
276            public int compareTo(String str) throws PageException {
277                    throw new ExpressionException("can't compare Com Object with a String");
278            }
279        
280            /**
281             * @see railo.runtime.type.Iteratorable#iterator()
282             */
283            public Iterator iterator() {
284                    return valueIterator();
285            }
286            
287        /**
288         * @see railo.runtime.type.Iteratorable#keyIterator()
289         */
290        public Iterator keyIterator() {
291            return new COMKeyWrapperIterator(this);
292        }
293    
294        /**
295         * @see railo.runtime.type.Iteratorable#valueIterator()
296         */
297        public Iterator valueIterator() {
298            return new COMValueWrapperIterator(this);
299        }
300    }