KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > utility > lang > UseBeanTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.taglibs.utility.lang;
17
18 import java.lang.reflect.Method JavaDoc;
19 import java.beans.*;
20 import java.io.IOException JavaDoc;
21
22 import javax.servlet.*;
23 import javax.servlet.http.HttpSession JavaDoc;
24 import javax.servlet.jsp.*;
25 import javax.servlet.jsp.tagext.*;
26
27 /**
28  * Generate code for useBean compatible with JSP 0.92. If the bean includes
29  * a method with the signature:
30  * <pre>
31  * public void processRequest(ServletRequest request);
32  * </pre>
33  * this method is called when the useBean tag is processed, with an argument
34  * representing the servlet request currently being serviced.
35  *
36  * @author Mandar Raje
37  */

38
39 public class UseBeanTag extends BodyTagSupport {
40   
41     String JavaDoc id;
42     String JavaDoc scope = "page";
43     String JavaDoc classname;
44     String JavaDoc type;
45     String JavaDoc beanName;
46     boolean processRequest = false;
47     boolean init = false;
48     Object JavaDoc beanObject;
49
50     public void setId(String JavaDoc val) {
51     this.id = val;
52     }
53     
54     public void setScope(String JavaDoc val) {
55     this.scope = val;
56     }
57
58     public void setClassname(String JavaDoc val) {
59     this.classname = val;
60     }
61
62     public void setType(String JavaDoc val) {
63     this.type = val;
64     }
65
66     public void setBeanName(String JavaDoc val) {
67     this.beanName = val;
68     }
69
70     public void setProcessRequest(boolean val) {
71     this.processRequest = val;
72     }
73
74     public void setInit(boolean val) {
75     this.init = val;
76     }
77
78     public void checkSyntax() throws JspException {
79     
80         // Check for mandatory attributes:
81
if ( id == null ) {
82         throw new JspException("useBean: id is missing");
83         }
84         
85         if (classname == null && type == null) {
86         throw new JspException ("useBean: both class and name attribute" +
87                           " can't be unspecified");
88         }
89
90         if (classname != null && beanName != null) {
91         throw new JspException ("useBean: can't specify both class and" +
92                           " beanName");
93         }
94     }
95   
96     public int doStartTag() throws JspException {
97
98         // check the syntax for useBean.
99
checkSyntax();
100         String JavaDoc clas = (this.classname == null)? type : this.classname;
101
102         try {
103         
104         // useBean idiosyncrasies.
105
if (type == null) type = clas;
106         
107         if (scope == null || scope.equals ("page")) {
108             
109             // locate the object.
110
beanObject = pageContext.getAttribute(id);
111
112             if (beanObject != null) {
113             if (!Class.forName(type).isInstance(beanObject))
114                 throw new JspException("ClassCastException: " +
115                            id + " " + clas);
116             return SKIP_BODY;
117             }
118             
119             synchronized(pageContext) {
120             beanObject = createBean();
121             if (processRequest == true)
122                 processReq(beanObject);
123             pageContext.setAttribute(id, beanObject);
124             setInit(true);
125             }
126             
127         } else if (scope.equals ("request")) {
128             
129             // locate the object.
130
beanObject =
131             pageContext.getAttribute(id, pageContext.REQUEST_SCOPE);
132
133             if (beanObject != null) {
134             if (!Class.forName(type).isInstance(beanObject))
135                 throw new JspException("ClassCastException: " +
136                            id + " " + clas);
137             return SKIP_BODY;
138             }
139             
140             synchronized((ServletRequest)
141                  pageContext.findAttribute(pageContext.REQUEST)) {
142             beanObject = createBean();
143             if (processRequest == true)
144                 processReq(beanObject);
145             pageContext.setAttribute(id, beanObject);
146             setInit(true);
147             }
148             
149         } else if (scope.equals ("session")) {
150             
151             // locate the object.
152
beanObject =
153             pageContext.getAttribute(id, pageContext.SESSION_SCOPE);
154
155             if (beanObject != null) {
156             if (!Class.forName(type).isInstance(beanObject))
157                 throw new JspException("ClassCastException: " +
158                            id + " " + clas);
159             return SKIP_BODY;
160             }
161             
162             synchronized((HttpSession JavaDoc)
163                  pageContext.findAttribute(pageContext.SESSION)) {
164             beanObject = createBean();
165             if (processRequest == true)
166                 processReq(beanObject);
167             pageContext.setAttribute(id, beanObject);
168             setInit(true);
169             }
170             
171         } else if (scope.equals ("application")) {
172             
173             // locate the object.
174
beanObject =
175             pageContext.getAttribute(id, pageContext.APPLICATION_SCOPE);
176
177             if (beanObject != null) {
178             if (!Class.forName(type).isInstance(beanObject))
179                 throw new JspException("ClassCastException: " +
180                            id + " " + clas);
181             return SKIP_BODY;
182             }
183             
184             synchronized((ServletContext)
185                  pageContext.findAttribute(pageContext.APPLICATION)) {
186             beanObject = createBean();
187             if (processRequest == true)
188                 processReq(beanObject);
189             pageContext.setAttribute(id, beanObject);
190             setInit(true);
191             }
192             
193         }
194         
195         // Check if the body needs to be evaluated.
196
if (init == false) return SKIP_BODY;
197         else return EVAL_BODY_TAG;
198         } catch (Exception JavaDoc ex) {
199         throw new JspException(ex.getMessage());
200         }
201     }
202
203     private Object JavaDoc createBean() throws JspException {
204
205     // Create the bean only if classname is specified.
206
// Otherwise bean must be located in the scope.
207
try {
208         if (classname != null || beanName != null) {
209         String JavaDoc className = (beanName != null) ? beanName : classname;
210         return (Beans.instantiate
211             (this.getClass().getClassLoader(),className));
212         }
213         else {
214         throw new JspException ("useBean: bean must be located in" +
215                     " the given scope.");
216         }
217     } catch (IOException JavaDoc iox) {
218         throw new JspException ("useBean: IO error while instantiating bean" +
219                        iox.getMessage());
220     } catch (ClassNotFoundException JavaDoc cnfex) {
221         throw new JspException ("useBean: class " + classname + " not found.");
222     }
223     }
224
225     public int doAfterBody() {
226     return SKIP_BODY;
227     }
228
229     public void processReq(Object JavaDoc bean) throws JspException {
230     try {
231         Class JavaDoc clazz = Class.forName("javax.servlet.ServletRequest");
232         Method JavaDoc method = bean.getClass().getMethod("processRequest",
233                           new Class JavaDoc[] { clazz });
234         method.invoke(bean, new Object JavaDoc[] {
235         pageContext.findAttribute(pageContext.REQUEST) });
236         
237     } catch(Exception JavaDoc ex) {
238         throw new JspException(ex.getMessage());
239     }
240     }
241 }
242
243
Popular Tags