KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > model > UseBeanTag


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.mvc.view.jsp.model;
8
9
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
12
13 import com.inversoft.util.ReflectionException;
14 import com.inversoft.util.ReflectionTools;
15 import com.inversoft.verge.mvc.MVCException;
16 import com.inversoft.verge.mvc.model.ModelResolution;
17 import com.inversoft.verge.mvc.model.web.WebMetaData;
18 import com.inversoft.verge.util.ScopeTools;
19
20
21 /**
22  * <p>
23  * This tag duplicates the functionality of the jsp:useBean
24  * tag except that it reduces the overhead of then using the
25  * included beans with the model tag library.
26  * </p>
27  *
28  * @author Brian Pontarelli
29  * @since 2.0
30  * @version 2.0
31  */

32 public class UseBeanTag extends TagSupport JavaDoc {
33
34     private String JavaDoc className;
35     private String JavaDoc type;
36     private String JavaDoc var;
37     private String JavaDoc scope;
38
39
40     /**
41      * Gets the className property value
42      *
43      * @return The className property value
44      */

45     public String JavaDoc getClassName() {
46         return className;
47     }
48
49     /**
50      * Sets the className property value
51      *
52      * @param className The className property value
53      */

54     public void setClassName(String JavaDoc className) {
55         this.className = className;
56     }
57
58     /**
59      * Gets the type property value
60      *
61      * @return The type property value
62      */

63     public String JavaDoc getType() {
64         return type;
65     }
66
67     /**
68      * Sets the type property value
69      *
70      * @param type The type property value
71      */

72     public void setType(String JavaDoc type) {
73         this.type = type;
74     }
75
76     /**
77      * Gets the var property value
78      *
79      * @return The var property value
80      */

81     public String JavaDoc getVar() {
82         return var;
83     }
84
85     /**
86      * Sets the var property value
87      *
88      * @param var The var property value
89      */

90     public void setVar(String JavaDoc var) {
91         this.var = var;
92     }
93
94     /**
95      * Gets the scope property value
96      *
97      * @return The scope property value
98      */

99     public String JavaDoc getScope() {
100         return scope;
101     }
102
103     /**
104      * Sets the scope property value
105      *
106      * @param scope The scope property value
107      */

108     public void setScope(String JavaDoc scope) {
109         this.scope = scope;
110     }
111
112     /**
113      * Creates the meta data, stores it. Checks if the bean already exists and
114      * if not, creates and stores it in the page context and the correct scope.
115      *
116      * @return Always EVAL_PAGE
117      * @throws JspException If there was an error
118      */

119     public int doStartTag() throws JspException JavaDoc {
120         try {
121             int scopeInt = ScopeTools.convertScope(scope);
122             int j2eeScope = ScopeTools.convertToJ2EE(scopeInt);
123             Object JavaDoc bean = pageContext.getAttribute(var, j2eeScope);
124
125             if (bean == null) {
126                 bean = ReflectionTools.instantiate(className);
127                 pageContext.setAttribute(var, bean, j2eeScope);
128                 pageContext.setAttribute(var, bean);
129             } else if (bean.getClass() != ReflectionTools.findClass(className)) {
130                 throw new JspException JavaDoc("Bean already exists in " + scope +
131                     " scope and is a different type then specified by useBean" +
132                     "tag");
133             }
134
135             WebMetaData md = new WebMetaData(var, className, scopeInt);
136             ModelResolution modelResolution = new ModelResolution(bean, md);
137             ModelResolutionRegistry.store(var, modelResolution, pageContext);
138         } catch (MVCException mvce) {
139             throw new JspException JavaDoc(mvce);
140         } catch (ReflectionException re) {
141             throw new JspException JavaDoc(re);
142         } catch (IllegalArgumentException JavaDoc iae) {
143             // Thrown from scope tools
144
throw new JspException JavaDoc(iae);
145         }
146
147         return EVAL_PAGE;
148     }
149 }
Popular Tags