KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > util > WebBean


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.util;
8
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.jsp.PageContext JavaDoc;
12
13 import com.inversoft.beans.BeanException;
14 import com.inversoft.beans.JavaBean;
15
16
17 /**
18  * <p>
19  * This class can be used to describe and access a JavaBean
20  * inside the web container. This class can reference beans
21  * in the page, request, session or context scopes. This
22  * class is also an instance of the {@link JavaBean JavaBean}
23  * class so that it can be used for accessing properties of
24  * the bean.
25  * </p>
26  *
27  * @author Brian Pontarelli
28  * @since 2.0
29  * @version 2.0
30  */

31 public class WebBean extends JavaBean {
32
33     private String JavaDoc id;
34     private int scope;
35
36
37     /**
38      * Constructs a new web bean. This method only calls the initialize method
39      *
40      * @param id The key into the page, request, session of context scoped
41      * objects, where the bean is stored (or to be stored)
42      * @param scope An integer that describes the scope of the bean. This value
43      * must be one of the _INT constants in the {@link ScopeConstants
44      * ScopeConstants} interface.
45      * @param klass The Class object for the bean given. This will be used
46      * when the bean is constructed (if necessary) as well as used to
47      * create the JavaBean instance
48      * @asserts If any of the parameters are null or the scope int is invalid
49      */

50     public WebBean(String JavaDoc id, int scope, Class JavaDoc klass) throws BeanException {
51         super(klass);
52         initialize(id, scope);
53     }
54
55     /**
56      * Constructs a new web bean. This method only calls the initialize method
57      *
58      * @param id The key into the page, request, session of context scoped
59      * objects, where the bean is stored (or to be stored)
60      * @param scope An integer that describes the scope of the bean. This value
61      * must be one of the _INT constants in the {@link ScopeConstants
62      * ScopeConstants} interface.
63      * @param className The fully qualified name of the Class object for the
64      * bean given. This will be used when the bean is constructed (if
65      * necessary) as well as used to create the JavaBean instance
66      * @asserts If any of the parameters are null or the scope int is invalid
67      */

68     public WebBean(String JavaDoc id, int scope, String JavaDoc className) throws BeanException {
69         super(className);
70         initialize(id, scope);
71     }
72
73     /**
74      * <p>
75      * Constructs a new WebBean without specifying the id, scope and Class. These
76      * need to be setup by calling the initialize method before the WebBean is
77      * usable.
78      * </p>
79      *
80      * <p>
81      * This allows sub-classes to contain more information in the id field, which
82      * can be parsed out and still initialized
83      * </p>
84      */

85     protected WebBean() {
86     }
87
88
89     /**
90      * Initializes the WebBean using the given id, scope and klass
91      *
92      * @param id The key into the page, request, session of context scoped
93      * objects, where the bean is stored (or to be stored)
94      * @param scope An integer that describes the scope of the bean. This value
95      * must be one of the _INT constants in the {@link ScopeConstants
96      * ScopeConstants} interface.
97      * @asserts If any of the parameters are null or the scope int is invalid
98      */

99     protected void initialize(String JavaDoc id, int scope) throws BeanException {
100
101         assert (id != null) : "id == null";
102         if (scope != ScopeConstants.PAGE_INT &&
103                 scope != ScopeConstants.REQUEST_INT &&
104                 scope != ScopeConstants.SESSION_INT &&
105                 scope != ScopeConstants.APPLICATION_INT) {
106             throw new BeanException("Invalid scope value");
107         }
108
109         this.id = id;
110         this.scope = scope;
111     }
112
113     /**
114      * Returns the id that the bean is stored under
115      *
116      * @return The id that the bean is stored under
117      */

118     public String JavaDoc getID() {
119         return id;
120     }
121
122     /**
123      * Returns the scope of the bean
124      *
125      * @return The scope int, which is defined in ScopeConstants
126      */

127     public int getScope() {
128         return scope;
129     }
130
131     /**
132      * Gets the scope of the item as a String
133      *
134      * @return Returns the scope of the item
135      */

136     public String JavaDoc getScopeStr() {
137         return ScopeTools.convertScope(scope);
138     }
139
140     /**
141      * Returns the instance of the bean
142      *
143      * @param pageContext The PageContext used for looking up the bean if it
144      * is in the page scope
145      * @param request The HttpServletRequest used for looking up the bean if
146      * it is in the request, session or application scopes
147      * @return The instance of the bean from the correct scope or a new instance
148      * of the bean which has been added to the correct scope
149      * @throws BeanException If there was a problem during instantiation or if
150      * the scope contains an object that is not the correct class
151      */

152     public Object JavaDoc getInstance(PageContext JavaDoc pageContext, HttpServletRequest JavaDoc request)
153     throws BeanException {
154
155         Object JavaDoc bean = null;
156         switch (scope) {
157             case ScopeConstants.PAGE_INT:
158                 if (pageContext == null) {
159                     assert (false) : "Can't get page scope without PageContext";
160                 }
161                 bean = pageContext.getAttribute(id);
162                 break;
163             case ScopeConstants.REQUEST_INT:
164                 bean = request.getAttribute(id);
165                 break;
166             case ScopeConstants.SESSION_INT:
167                 bean = request.getSession().getAttribute(id);
168                 break;
169             case ScopeConstants.APPLICATION_INT:
170                 bean = request.getSession().getServletContext().getAttribute(id);
171                 break;
172         }
173
174         if (bean == null) {
175             bean = instantiate();
176             switch (scope) {
177                 case ScopeConstants.PAGE_INT:
178                     if (pageContext == null) {
179                         assert (false) : "Can't get page scope without PageContext";
180                     }
181                     pageContext.setAttribute(id, bean);
182                     break;
183                 case ScopeConstants.REQUEST_INT:
184                     request.setAttribute(id, bean);
185                     break;
186                 case ScopeConstants.SESSION_INT:
187                     request.getSession().setAttribute(id, bean);
188                     break;
189                 case ScopeConstants.APPLICATION_INT:
190                     request.getSession().getServletContext().setAttribute(id, bean);
191                     break;
192             }
193         }
194
195         return bean;
196     }
197
198     /**
199      * Returns the instance of the bean
200      *
201      * @param pageContext The PageContext used for looking up the bean if it
202      * is in the page, request, session or application scopes
203      * @return The instance of the bean from the correct scope or a new instance
204      * of the bean which has been added to the correct scope
205      * @throws BeanException If there was a problem during instantiation or if
206      * the scope contains an object that is not the correct class
207      */

208     public final Object JavaDoc getInstance(PageContext JavaDoc pageContext) throws BeanException {
209         return getInstance(pageContext, (HttpServletRequest JavaDoc) pageContext.getRequest());
210     }
211
212     /**
213      * Returns the instance of the bean
214      *
215      * @param request The HttpServletRequest used for looking up the bean if
216      * it is in the request, session or application scopes
217      * @return The instance of the bean from the correct scope or a new instance
218      * of the bean which has been added to the correct scope
219      * @throws BeanException If there was a problem during instantiation or if
220      * the scope contains an object that is not the correct class
221      */

222     public final Object JavaDoc getInstance(HttpServletRequest JavaDoc request) throws BeanException {
223         return getInstance(null, request);
224     }
225
226     /**
227      * Returns a WebBeanProperty for the given property string
228      *
229      * @param propertyName The property String
230      */

231     public WebBeanProperty getWebBeanProperty(String JavaDoc propertyName)
232     throws BeanException {
233         return new WebBeanProperty(propertyName, this);
234     }
235 }
236
Popular Tags