1 16 package org.apache.myfaces.el; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import javax.faces.context.ExternalContext; 22 import javax.faces.context.FacesContext; 23 import javax.faces.el.ReferenceSyntaxException; 24 import javax.faces.el.VariableResolver; 25 26 import org.apache.myfaces.config.ManagedBeanBuilder; 27 import org.apache.myfaces.config.RuntimeConfig; 28 import org.apache.myfaces.config.element.ManagedBean; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 34 75 public class VariableResolverImpl 76 extends VariableResolver 77 { 78 80 private static final Log log = LogFactory.getLog(VariableResolverImpl.class); 81 82 84 public static final Map s_standardImplicitObjects = new HashMap (32); 85 static { 86 s_standardImplicitObjects.put( 87 "applicationScope", 88 new ImplicitObject() 89 { 90 public Object get(FacesContext facesContext) 91 { 92 return facesContext.getExternalContext().getApplicationMap(); 93 } 94 }); 95 s_standardImplicitObjects.put( 96 "cookie", 97 new ImplicitObject() 98 { 99 public Object get(FacesContext facesContext) 100 { 101 return facesContext.getExternalContext().getRequestCookieMap(); 102 } 103 }); 104 s_standardImplicitObjects.put( 105 "facesContext", 106 new ImplicitObject() 107 { 108 public Object get(FacesContext facesContext) 109 { 110 return facesContext; 111 } 112 }); 113 s_standardImplicitObjects.put( 114 "header", 115 new ImplicitObject() 116 { 117 public Object get(FacesContext facesContext) 118 { 119 return facesContext.getExternalContext().getRequestHeaderMap(); 120 } 121 }); 122 s_standardImplicitObjects.put( 123 "headerValues", 124 new ImplicitObject() 125 { 126 public Object get(FacesContext facesContext) 127 { 128 return facesContext.getExternalContext().getRequestHeaderValuesMap(); 129 } 130 }); 131 s_standardImplicitObjects.put( 132 "initParam", 133 new ImplicitObject() 134 { 135 public Object get(FacesContext facesContext) 136 { 137 return facesContext.getExternalContext().getInitParameterMap(); 138 } 139 }); 140 s_standardImplicitObjects.put( 141 "param", 142 new ImplicitObject() 143 { 144 public Object get(FacesContext facesContext) 145 { 146 return facesContext.getExternalContext().getRequestParameterMap(); 147 } 148 }); 149 s_standardImplicitObjects.put( 150 "paramValues", 151 new ImplicitObject() 152 { 153 public Object get(FacesContext facesContext) 154 { 155 return facesContext.getExternalContext().getRequestParameterValuesMap(); 156 } 157 }); 158 s_standardImplicitObjects.put( 159 "requestScope", 160 new ImplicitObject() 161 { 162 public Object get(FacesContext facesContext) 163 { 164 return facesContext.getExternalContext().getRequestMap(); 165 } 166 }); 167 s_standardImplicitObjects.put( 168 "sessionScope", 169 new ImplicitObject() 170 { 171 public Object get(FacesContext facesContext) 172 { 173 return facesContext.getExternalContext().getSessionMap(); 174 } 175 }); 176 s_standardImplicitObjects.put( 177 "view", 178 new ImplicitObject() 179 { 180 public Object get(FacesContext facesContext) 181 { 182 return facesContext.getViewRoot(); 183 } 184 }); 185 } 186 187 202 protected final Map _implicitObjects = new HashMap (32); 203 { 204 _implicitObjects.putAll(s_standardImplicitObjects); 205 } 206 207 protected static final Map s_standardScopes = new HashMap (16); 208 static { 209 s_standardScopes.put( 210 "request", 211 new Scope() 212 { 213 public void put(ExternalContext extContext, String name, Object obj) 214 { 215 extContext.getRequestMap().put(name, obj); 216 } 217 }); 218 s_standardScopes.put( 219 "session", 220 new Scope() 221 { 222 public void put(ExternalContext extContext, String name, Object obj) 223 { 224 extContext.getSessionMap().put(name, obj); 225 } 226 }); 227 s_standardScopes.put( 228 "application", 229 new Scope() 230 { 231 public void put(ExternalContext extContext, String name, Object obj) 232 { 233 extContext.getApplicationMap().put(name, obj); 234 } 235 }); 236 s_standardScopes.put( 237 "none", 238 new Scope() 239 { 240 public void put(ExternalContext extContext, String name, Object obj) 241 { 242 } 244 }); 245 } 246 247 260 protected final Map _scopes = new HashMap (16); 261 { 262 _scopes.putAll(s_standardScopes); 263 } 264 265 269 private RuntimeConfig _runtimeConfig; 270 271 private ManagedBeanBuilder beanBuilder = new ManagedBeanBuilder(); 272 273 274 276 public Object resolveVariable(FacesContext facesContext, String name) 277 { 278 if ((name == null) || (name.length() == 0)) 279 { 280 throw new ReferenceSyntaxException("Varible name is null or empty"); 281 } 282 283 Object implicitObject = _implicitObjects.get(name); 285 if (implicitObject != null) 286 { 287 if (implicitObject instanceof ImplicitObject) 288 { 289 return ((ImplicitObject) implicitObject).get(facesContext); 291 } 292 else 293 { 294 return implicitObject; 296 } 297 } 298 299 ExternalContext externalContext = facesContext.getExternalContext(); 300 301 Map requestMap = externalContext.getRequestMap(); 303 Object obj = requestMap.get(name); 304 if (obj != null) 305 { 306 return obj; 307 } 308 309 obj = externalContext.getSessionMap().get(name); 311 if (obj != null) 312 { 313 return obj; 314 } 315 316 obj = externalContext.getApplicationMap().get(name); 318 if (obj != null) 319 { 320 return obj; 321 } 322 323 ManagedBean mbc = getRuntimeConfig(facesContext).getManagedBean(name); 325 326 if (mbc != null) 327 { 328 obj = beanBuilder.buildManagedBean(facesContext, mbc); 329 330 String scopeKey = mbc.getManagedBeanScope(); 332 333 Scope scope = (Scope) _scopes.get(scopeKey); 335 if (scope == null) 336 { 337 log.error("Managed bean '" + name + "' has illegal scope: " 338 + scopeKey); 339 } 340 else 341 { 342 scope.put(externalContext, name, obj); 343 } 344 345 return obj; 346 } 347 348 log.warn("Variable '" + name + "' could not be resolved."); 349 return null; 350 } 351 352 protected RuntimeConfig getRuntimeConfig(FacesContext facesContext) 353 { 354 if (_runtimeConfig == null) 355 { 356 _runtimeConfig = RuntimeConfig.getCurrentInstance(facesContext.getExternalContext()); 357 } 358 return _runtimeConfig; 359 } 360 } 361 362 363 interface ImplicitObject 364 { 365 367 public Object get(FacesContext facesContext); 368 } 369 370 371 interface Scope 372 { 373 375 public void put(ExternalContext extContext, String name, Object obj); 376 } 377 | Popular Tags |