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.exp.ExpressionException;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.type.Collection;
011    import railo.runtime.type.KeyImpl;
012    import railo.runtime.type.Scope;
013    import railo.runtime.type.dt.DateTime;
014    import railo.runtime.type.util.StructSupport;
015    
016    /**
017     * caller scope
018     */
019    public final class CallerImpl extends StructSupport implements Caller  {
020    
021            private static final long serialVersionUID = -6228400815042475435L;
022            
023            private PageContext pc;
024        private Variables variablesScope;
025        private Scope localScope;
026        private Scope argumentsScope;
027        private boolean checkArgs;
028        
029        
030        /**
031         *
032         * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key)
033         */
034        public Object get(Collection.Key key) throws PageException  {
035            
036            char c=key.lowerCharAt(0);
037                    if('a'==c) {
038                            if(ScopeSupport.APPLICATION.equalsIgnoreCase(key))              return pc.applicationScope();
039                            else if(KeyImpl.ARGUMENTS.equalsIgnoreCase(key))                return pc.argumentsScope();
040                    }
041                    else if('c'==c) {
042                            if(ScopeSupport.CGI.equalsIgnoreCase(key))                                      return pc.cgiScope();
043                            if(ScopeSupport.COOKIE.equalsIgnoreCase(key))                           return pc.cookieScope();
044                            if(ScopeSupport.CLIENT.equalsIgnoreCase(key))                           return pc.clientScope();
045                            if(ScopeSupport.CLUSTER.equalsIgnoreCase(key))                          return pc.clusterScope(); 
046                    }
047                    else if('f'==c) {
048                            if(ScopeSupport.FORM.equalsIgnoreCase(key))                             return pc.formScope();
049                    }
050                    else if('r'==c) {
051                            if(ScopeSupport.REQUEST.equalsIgnoreCase(key))                          return pc.requestScope();
052                    }
053                    else if('l'==c) {
054                            if(KeyImpl.LOCAL.equalsIgnoreCase(key) && checkArgs)    return pc.localScope();
055                    }
056                    else if('s'==c) {
057                            if(ScopeSupport.SESSION.equalsIgnoreCase(key))                          return pc.sessionScope();
058                            if(KeyImpl.SERVER.equalsIgnoreCase(key))                                return pc.serverScope();
059                    }
060                    else if('u'==c) {
061                            if(ScopeSupport.URL.equalsIgnoreCase(key))                                      return pc.urlScope();
062                    }
063                    else if('v'==c) {
064                            if(KeyImpl.VARIABLES.equalsIgnoreCase(key))                     return variablesScope;
065                    }
066            
067            // upper variable scope
068            Object o;
069            
070            if(checkArgs) {
071                o=localScope.get(key,null);
072                if(o!=null) return o;
073                o=argumentsScope.get(key,null);
074                if(o!=null) return o;
075            }
076            o=variablesScope.get(key,null);
077            if(o!=null) return o;
078            
079            // get from cascaded scopes
080            o=pc.undefinedScope().getCascading(key);
081            if(o!=null) return o;
082            
083            /*
084            // get scopes
085            if(key.equalsIgnoreCase(VARIABLES)) {
086                return variablesScope;//new StructImpl(getMap());
087            }
088            
089            scope=VariableInterpreter.scopeKey2Int(key);
090            if(scope!=Scope.SCOPE_UNDEFINED)
091                return pc.scope(scope);
092            */
093            throw new ExpressionException("["+key.getString() +"] not found in caller scope");
094        }
095        
096        /**
097         *
098         * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key, java.lang.Object)
099         */
100        public Object get(Collection.Key key, Object defaultValue) {
101            
102            char c=key.lowerCharAt(0);
103                    if('a'==c) {
104                            if(ScopeSupport.APPLICATION.equalsIgnoreCase(key)){
105                                    try {
106                                            return pc.applicationScope();
107                                    } 
108                                    catch (PageException e) {}
109                            }
110                            else if(KeyImpl.ARGUMENTS.equalsIgnoreCase(key))                return pc.argumentsScope();
111                    }
112                    else if('c'==c) {
113                            if(ScopeSupport.CGI.equalsIgnoreCase(key))                                      return pc.cgiScope();
114                            if(ScopeSupport.COOKIE.equalsIgnoreCase(key))                           return pc.cookieScope();
115                            if(ScopeSupport.CLIENT.equalsIgnoreCase(key)){
116                                    try {
117                                            return pc.clientScope();
118                                    } 
119                                    catch (PageException e) {}
120                            }
121                            if(ScopeSupport.CLUSTER.equalsIgnoreCase(key)){
122                                    try {
123                                            return pc.clusterScope();
124                                    }
125                                    catch (PageException e) {} 
126                            }
127                    }
128                    else if('f'==c) {
129                            if(ScopeSupport.FORM.equalsIgnoreCase(key))                             return pc.formScope();
130                    }
131                    else if('r'==c) {
132                            if(ScopeSupport.REQUEST.equalsIgnoreCase(key))                          return pc.requestScope();
133                    }
134                    else if('l'==c) {
135                            if(KeyImpl.LOCAL.equalsIgnoreCase(key) && checkArgs)    return pc.localScope();
136                    }
137                    else if('s'==c) {
138                            if(ScopeSupport.SESSION.equalsIgnoreCase(key)){
139                                    try {
140                                            return pc.sessionScope();
141                                    } 
142                                    catch (PageException e) {}
143                            }
144                            if(KeyImpl.SERVER.equalsIgnoreCase(key)){
145                                    try {
146                                            return pc.serverScope();
147                                    } 
148                                    catch (PageException e) {}
149                            }
150                    }
151                    else if('u'==c) {
152                            if(ScopeSupport.URL.equalsIgnoreCase(key))                                      return pc.urlScope();
153                    }
154                    else if('v'==c) {
155                            if(KeyImpl.VARIABLES.equalsIgnoreCase(key))                     return variablesScope;
156                    }
157            
158            
159            
160            Object o;
161            if(checkArgs) {
162                o=localScope.get(key,null);
163                if(o!=null) return o;
164                o=argumentsScope.get(key,null);
165                if(o!=null) return o;
166            }
167            o=variablesScope.get(key,null);
168            if(o!=null) return o;
169            
170            
171            // get from cascaded scopes
172            o=pc.undefinedScope().getCascading(key);
173            if(o!=null) return o;
174            
175            return defaultValue;
176        }
177        
178        /**
179         * @see railo.runtime.type.Scope#initialize(railo.runtime.PageContext)
180         */
181        public void initialize(PageContext pc) {
182            this.pc=pc;
183        }
184    
185        /**
186         * @see railo.runtime.type.scope.Caller#setScope(railo.runtime.type.Scope, railo.runtime.type.Scope, railo.runtime.type.Scope, boolean)
187         */
188        public void setScope(Scope variablesScope, Scope localScope, Scope argumentsScope, boolean checkArgs) {
189            this.variablesScope = (Variables)variablesScope;
190            this.localScope = localScope;
191            this.argumentsScope = argumentsScope;
192            this.checkArgs = checkArgs;
193        }
194    
195        /**
196         * @see railo.runtime.type.Scope#isInitalized()
197         */
198        public boolean isInitalized() {
199            return pc!=null;
200        }
201    
202        /**
203         * @see railo.runtime.type.Scope#release()
204         */
205        public void release() {
206            this.pc=null;
207        }
208    
209        /**
210         * @see railo.runtime.type.Collection#size()
211         */
212        public int size() {
213            return variablesScope.size();
214        }
215    
216        /**
217         * @see railo.runtime.type.Collection#keysAsString()
218         */
219        public String[] keysAsString() {
220            return variablesScope.keysAsString();
221        }
222    
223        /**
224         * @see railo.runtime.type.Collection#keys()
225         */
226        public Collection.Key[] keys() {
227            return variablesScope.keys();
228        }
229    
230            /**
231             *
232             * @see railo.runtime.type.Collection#remove(railo.runtime.type.Collection.Key)
233             */
234            public Object remove(Collection.Key key) throws PageException {
235            if(checkArgs && localScope.containsKey(key))
236                return localScope.remove(key);
237            return variablesScope.remove(key);
238            }
239    
240            /**
241             *
242             * @see railo.runtime.type.Collection#removeEL(railo.runtime.type.Collection.Key)
243             */
244            public Object removeEL(Collection.Key key) {
245            if(checkArgs && localScope.containsKey(key))
246                return localScope.removeEL(key);
247            return variablesScope.removeEL(key);
248            }
249    
250        /**
251         * @see railo.runtime.type.Collection#clear()
252         */
253        public void clear() {
254            variablesScope.clear();
255        }
256    
257            public Object set(Key key, Object value) throws PageException {
258            if(checkArgs) {
259                if(localScope.containsKey(key))     return localScope.set(key,value);
260                if(argumentsScope.containsKey(key))  return argumentsScope.set(key,value);
261            }
262            return variablesScope.set(key,value);
263            }
264    
265            /**
266             * @see railo.runtime.type.Collection#setEL(railo.runtime.type.Collection.Key, java.lang.Object)
267             */
268            public Object setEL(Key key, Object value) {
269            if(checkArgs) {
270                if(localScope.containsKey(key))     return localScope.setEL(key,value);
271                if(argumentsScope.containsKey(key))  return argumentsScope.setEL(key,value);
272            }
273            return variablesScope.setEL(key,value);
274            }
275    
276        /**
277         * @see railo.runtime.type.Iteratorable#keyIterator()
278         */
279        public Iterator keyIterator() {
280            return variablesScope.keyIterator();
281        }
282        
283        /**
284         * @see railo.runtime.type.Collection#duplicate(boolean)
285         */
286        public Collection duplicate(boolean deepCopy) {
287            return variablesScope.duplicate(deepCopy);
288        }
289    
290            /**
291             *
292             * @see railo.runtime.type.Collection#containsKey(railo.runtime.type.Collection.Key)
293             */
294            public boolean containsKey(Collection.Key key) {
295            return get(key,null)!=null;
296            }
297    
298        /**
299             * @see railo.runtime.dump.Dumpable#toDumpData(railo.runtime.PageContext, int)
300             */
301            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
302                return variablesScope.toDumpData(pageContext, --maxlevel,dp);
303        }
304    
305        /**
306         * @see railo.runtime.op.Castable#castToString()
307         */
308        public String castToString() throws PageException {
309            return variablesScope.castToString();
310        }
311    
312        /**
313         * @see railo.runtime.op.Castable#castToString(java.lang.String)
314         */
315        public String castToString(String defaultValue) {
316            return variablesScope.castToString(defaultValue);
317        }
318        
319        /**
320         * @see railo.runtime.op.Castable#castToBooleanValue()
321         */
322        public boolean castToBooleanValue() throws PageException {
323            return variablesScope.castToBooleanValue();
324        }
325        
326        /**
327         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
328         */
329        public Boolean castToBoolean(Boolean defaultValue) {
330            return variablesScope.castToBoolean(defaultValue);
331        }
332    
333        /**
334         * @see railo.runtime.op.Castable#castToDoubleValue()
335         */
336        public double castToDoubleValue() throws PageException {
337            return variablesScope.castToDoubleValue();
338        }
339        
340        /**
341         * @see railo.runtime.op.Castable#castToDoubleValue(double)
342         */
343        public double castToDoubleValue(double defaultValue) {
344            return variablesScope.castToDoubleValue(defaultValue);
345        }
346    
347        /**
348         * @see railo.runtime.op.Castable#castToDateTime()
349         */
350        public DateTime castToDateTime() throws PageException {
351            return variablesScope.castToDateTime();
352        }
353        
354        /**
355         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
356         */
357        public DateTime castToDateTime(DateTime defaultValue) {
358            return variablesScope.castToDateTime(defaultValue);
359        }
360    
361    
362            /**
363             * @throws PageException 
364             * @see railo.runtime.op.Castable#compare(boolean)
365             */
366            public int compareTo(boolean b) throws PageException {
367                    return variablesScope.compareTo(b);
368            }
369    
370            /**
371             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
372             */
373            public int compareTo(DateTime dt) throws PageException {
374                    return variablesScope.compareTo(dt);
375            }
376    
377            /**
378             * @see railo.runtime.op.Castable#compareTo(double)
379             */
380            public int compareTo(double d) throws PageException {
381                    return variablesScope.compareTo(d);
382            }
383    
384            /**
385             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
386             */
387            public int compareTo(String str) throws PageException {
388                    return variablesScope.compareTo(str);
389            }
390    
391        /**
392         * @see railo.runtime.type.Scope#getType()
393         */
394        public int getType() {
395            return SCOPE_CALLER;
396        }
397    
398        /**
399         * @see railo.runtime.type.Scope#getTypeAsString()
400         */
401        public String getTypeAsString() {
402            return "caller";
403        }
404    
405            /**
406             * @see java.util.Map#containsValue(java.lang.Object)
407             */
408            public boolean containsValue(Object value) {
409                    return variablesScope.containsValue(value);
410            }
411    
412            /**
413             * @see java.util.Map#values()
414             */
415            public java.util.Collection values() {
416                    return variablesScope.values();
417            }
418    
419    
420        /** FUTURE add to intrface Caller
421             * @return the variablesScope
422             */
423            public Variables getVariablesScope() {
424                    return variablesScope;
425            }
426    
427            /**FUTURE add to intrface Caller
428             * @return the localScope
429             */
430            public Local getLocalScope() {
431                    return (Local)localScope;
432            }
433    
434            /**FUTURE add to intrface Caller
435             * @return the argumentsScope
436             */
437            public Argument getArgumentsScope() {
438                    return (Argument)argumentsScope;
439            }
440    }