KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > freemarker > ScopesHashModel


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.freemarker;
6
7 import freemarker.template.ObjectWrapper;
8 import freemarker.template.SimpleHash;
9 import freemarker.template.TemplateModel;
10 import freemarker.template.TemplateModelException;
11
12 import javax.servlet.ServletContext JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpSession JavaDoc;
15
16 import com.opensymphony.xwork.util.OgnlValueStack;
17
18
19 /**
20  * Simple Hash model that also searches other scopes.
21  * <p/>
22  * If the key doesn't exist in this hash, this template model tries to
23  * resolve the key within the attributes of the following scopes,
24  * in the order stated: Request, Session, Servlet Context
25  */

26 public class ScopesHashModel extends SimpleHash {
27     //~ Instance fields ////////////////////////////////////////////////////////
28

29     private HttpServletRequest JavaDoc request;
30     private ObjectWrapper objectWraper;
31     private ServletContext JavaDoc servletContext;
32     private OgnlValueStack stack;
33     
34     //~ Constructors ///////////////////////////////////////////////////////////
35

36     public ScopesHashModel(ObjectWrapper objectWrapper, ServletContext JavaDoc context, HttpServletRequest JavaDoc request, OgnlValueStack stack) {
37         super(objectWrapper);
38         this.servletContext = context;
39         this.request = request;
40         this.stack = stack;
41     }
42
43     //~ Methods ////////////////////////////////////////////////////////////////
44

45     public TemplateModel get(String JavaDoc key) throws TemplateModelException {
46         // Lookup in default scope
47
TemplateModel model = super.get(key);
48
49         if (model != null) {
50             return model;
51         }
52
53         
54         if (stack != null) {
55             Object JavaDoc obj = stack.findValue(key);
56
57             if (obj != null) {
58                 return wrap(obj);
59             }
60         }
61
62         if (request != null) {
63             // Lookup in request scope
64
Object JavaDoc obj = request.getAttribute(key);
65
66             if (obj != null) {
67                 return wrap(obj);
68             }
69
70             // Lookup in session scope
71
HttpSession JavaDoc session = request.getSession(false);
72
73             if (session != null) {
74                 obj = session.getAttribute(key);
75
76                 if (obj != null) {
77                     return wrap(obj);
78                 }
79             }
80         }
81
82         if (servletContext != null) {
83             // Lookup in application scope
84
Object JavaDoc obj = servletContext.getAttribute(key);
85
86             if (obj != null) {
87                 return wrap(obj);
88             }
89         }
90
91         return null;
92     }
93 }
94
Popular Tags