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