Source code: com/aendvari/tethys/tag/basic/BeanTag.java
1 /*
2 * BeanTag.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.tethys.tag.basic;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14
15 import javax.servlet.*;
16 import javax.servlet.http.*;
17 import javax.servlet.jsp.*;
18 import javax.servlet.jsp.tagext.*;
19
20 import com.aendvari.common.model.*;
21 import com.aendvari.common.util.ResourceLoader;
22
23 import com.aendvari.tethys.*;
24 import com.aendvari.tethys.tag.*;
25 import com.aendvari.tethys.tag.model.*;
26 import com.aendvari.tethys.context.*;
27
28 import com.aendvari.griffin.bean.transform.BeanTransformer;
29
30 import com.aendvari.common.util.ServletUtil;
31
32 import java.lang.reflect.*;
33 import java.lang.NoSuchMethodException;
34 import java.lang.IllegalAccessException;
35
36
37
38 /**
39 * <p>Implementation class for the "bean" tag.</p>
40 *
41 * @author Scott Milne
42 *
43 */
44
45 public class BeanTag extends ModelTreeTag
46 {
47 /* Variables */
48
49 /** the type of the <code>Bean</code> class */
50 public String type;
51
52 /** the scope in which to save the define */
53 protected String scope;
54
55 /** true/false if the variable should be saved into page scope. See BeanTEI. */
56 protected String bean;
57
58
59 /* Constructor */
60
61 public BeanTag()
62 {
63 super();
64
65 type = null;
66 scope = null;
67 bean = null;
68 }
69
70 /* Attributes */
71
72 public String getType() { return type; }
73 public void setType( String type ) { this.type = type; }
74
75 public String getScope() { return scope ;}
76 public void setScope( String scope ) { this.scope = scope; }
77
78 public String getBean() { return bean ;}
79 public void setBean( String setBean ) { this.bean = setBean; }
80
81
82 public int doStartTag() throws JspTagException
83 {
84 // get the request
85 HttpServletRequest request =
86 (HttpServletRequest)pageContext.getRequest();
87
88 // get the session
89 HttpSession session = request.getSession(true);
90
91 // get the servlet context
92 ServletContext servletContext = pageContext.getServletContext();
93
94 // check for the scope tag
95 if(
96 (scope.compareToIgnoreCase(ServletUtil.Scope.Session)!=0) &&
97 (scope.compareToIgnoreCase(ServletUtil.Scope.Page)!=0) &&
98 (scope.compareToIgnoreCase(ServletUtil.Scope.Request)!=0)
99 )
100 {
101 throw new JspTagException("DefineTag: \"scope\" must be either \"session\", \"page\", \"application\" or \"request\"");
102 }
103
104 // now create and load the modelTree and contextMap
105 Object valueObject = null;
106 try
107 {
108 // establish tag context
109 establishModelContext();
110
111 // get the value from the path
112 ModelNode modelNode = getModelNode(path, true);
113
114 // lookup the class object by name
115 Class objectType = Class.forName(type);
116
117 Class[] listParam = {};
118 Object[] listArgs = {};
119
120 // create an instance of the given class
121 Constructor constructor = objectType.getConstructor(listParam);
122 valueObject = constructor.newInstance(listArgs);
123 valueObject = BeanTransformer.updateBeanFromModel(valueObject, modelNode);
124 }
125 /*
126 * ClassNotFoundException
127 * NoSuchMethodException
128 * IllegalAccessException
129 *
130 */
131 catch (Exception e)
132 {
133 throw new JspTagException("BeanTag:" + e.toString());
134 }
135
136
137 // set the object into the given scope
138
139 int inScope = PageContext.PAGE_SCOPE;
140
141 // store the value where it's needed
142 if( (scope.compareToIgnoreCase(ServletUtil.Scope.Session)==0) )
143 {
144 inScope = PageContext.SESSION_SCOPE;
145 }
146 else if( (scope.compareToIgnoreCase(ServletUtil.Scope.Request)==0) )
147 {
148 inScope = PageContext.REQUEST_SCOPE;
149 }
150 else if( (scope.compareToIgnoreCase(ServletUtil.Scope.Page)==0) )
151 {
152 inScope = PageContext.PAGE_SCOPE;
153 }
154 else if( (scope.compareToIgnoreCase(ServletUtil.Scope.Application)==0) )
155 {
156 inScope = PageContext.APPLICATION_SCOPE;
157 }
158
159 // Expose this value as a scripting variable
160 pageContext.setAttribute(getName(), valueObject, inScope);
161
162 // skip the body of the tag
163 return EVAL_BODY_INCLUDE;
164 }
165
166 /**
167 * Release all allocated resources.
168 */
169
170 public void release()
171 {
172 super.release();
173
174 type = null;
175 scope = null;
176 bean = null;
177 }
178 }
179