1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.tiles.taglib.util;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.Map;
26 import java.util.HashMap;
27
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.PageContext;
30
31 import org.apache.commons.beanutils.PropertyUtils;
32 import org.apache.struts.Globals;
33 import org.apache.struts.tiles.taglib.ComponentConstants;
34 import org.apache.struts.tiles.ComponentContext;
35 import org.apache.struts.tiles.ComponentDefinition;
36 import org.apache.struts.tiles.DefinitionsFactoryException;
37 import org.apache.struts.tiles.FactoryNotFoundException;
38 import org.apache.struts.tiles.NoSuchDefinitionException;
39 import org.apache.struts.tiles.TilesUtil;
40
41
42
43
44
45
46
47
48 public class TagUtils {
49
50
51 public static final boolean debug = true;
52
53
54
55
56
57 private static final Map scopes = new HashMap();
58
59
60
61
62
63 static {
64 scopes.put("page", new Integer(PageContext.PAGE_SCOPE));
65 scopes.put("request", new Integer(PageContext.REQUEST_SCOPE));
66 scopes.put("session", new Integer(PageContext.SESSION_SCOPE));
67 scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE));
68 }
69
70
71
72
73
74
75
76
77
78 public static int getScope(String scopeName, int defaultValue) throws JspException {
79 if (scopeName == null) {
80 return defaultValue;
81 }
82
83 if (scopeName.equalsIgnoreCase("component")) {
84 return ComponentConstants.COMPONENT_SCOPE;
85
86 } else if (scopeName.equalsIgnoreCase("template")) {
87 return ComponentConstants.COMPONENT_SCOPE;
88
89 } else if (scopeName.equalsIgnoreCase("tile")) {
90 return ComponentConstants.COMPONENT_SCOPE;
91
92 } else {
93 return getScope(scopeName);
94 }
95 }
96
97
98
99
100
101
102
103
104 public static int getScope(String scopeName) throws JspException {
105 Integer scope = (Integer) scopes.get(scopeName.toLowerCase());
106
107 if (scope == null) {
108
109 throw new JspException("Unable to retrieve the scope "+scopeName);
110 }
111
112 return scope.intValue();
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public static Object retrieveBean(String beanName, String scopeName, PageContext pageContext)
128 throws JspException {
129
130 if (scopeName == null) {
131 return findAttribute(beanName, pageContext);
132 }
133
134
135 int scope = getScope(scopeName, PageContext.PAGE_SCOPE);
136
137
138 return getAttribute(beanName, scope, pageContext);
139 }
140
141
142
143
144
145
146
147
148 public static Object findAttribute(String beanName, PageContext pageContext) {
149 ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest());
150
151 if (compContext != null) {
152 Object attribute = compContext.findAttribute(beanName, pageContext);
153 if (attribute != null) {
154 return attribute;
155 }
156 }
157
158
159 return pageContext.findAttribute(beanName);
160 }
161
162
163
164
165
166
167
168
169
170 public static Object getAttribute(String beanName, int scope, PageContext pageContext) {
171 if (scope == ComponentConstants.COMPONENT_SCOPE) {
172 ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest());
173 return compContext.getAttribute(beanName);
174 }
175 return pageContext.getAttribute(beanName, scope);
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public static Object getRealValueFromBean(
196 String beanName,
197 String beanProperty,
198 String beanScope,
199 PageContext pageContext)
200 throws JspException {
201
202 try {
203 Object realValue;
204 Object bean = retrieveBean(beanName, beanScope, pageContext);
205 if (bean != null && beanProperty != null) {
206 realValue = PropertyUtils.getProperty(bean, beanProperty);
207 } else {
208 realValue = bean;
209 }
210 return realValue;
211
212 } catch (NoSuchMethodException ex) {
213 throw new JspException(
214 "Error - component.PutAttributeTag : Error while retrieving value from bean '"
215 + beanName
216 + "' with property '"
217 + beanProperty
218 + "' in scope '"
219 + beanScope
220 + "'. (exception : "
221 + ex.getMessage(), ex);
222
223 } catch (InvocationTargetException ex) {
224 throw new JspException(
225 "Error - component.PutAttributeTag : Error while retrieving value from bean '"
226 + beanName
227 + "' with property '"
228 + beanProperty
229 + "' in scope '"
230 + beanScope
231 + "'. (exception : "
232 + ex.getMessage(), ex);
233
234 } catch (IllegalAccessException ex) {
235 throw new JspException(
236 "Error - component.PutAttributeTag : Error while retrieving value from bean '"
237 + beanName
238 + "' with property '"
239 + beanProperty
240 + "' in scope '"
241 + beanScope
242 + "'. (exception : "
243 + ex.getMessage(), ex);
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259 public static void setAttribute(
260 PageContext pageContext,
261 String name,
262 Object value,
263 String scope)
264 throws JspException {
265
266 if (scope == null)
267 pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE);
268 else if (scope.equalsIgnoreCase("page"))
269 pageContext.setAttribute(name, value, PageContext.PAGE_SCOPE);
270 else if (scope.equalsIgnoreCase("request"))
271 pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE);
272 else if (scope.equalsIgnoreCase("session"))
273 pageContext.setAttribute(name, value, PageContext.SESSION_SCOPE);
274 else if (scope.equalsIgnoreCase("application"))
275 pageContext.setAttribute(name, value, PageContext.APPLICATION_SCOPE);
276 else {
277 throw new JspException("Error - bad scope name '" + scope + "'");
278 }
279 }
280
281
282
283
284
285
286
287
288
289
290 public static void setAttribute(PageContext pageContext, String name, Object beanValue)
291 throws JspException {
292 pageContext.setAttribute(name, beanValue, PageContext.REQUEST_SCOPE);
293 }
294
295
296
297
298
299
300
301 public static void saveException(PageContext pageContext, Throwable exception) {
302 pageContext.setAttribute(Globals.EXCEPTION_KEY, exception, PageContext.REQUEST_SCOPE);
303 }
304
305
306
307
308
309
310
311 public static ComponentDefinition getComponentDefinition(String name, PageContext pageContext)
312 throws JspException {
313
314 try {
315 return TilesUtil.getDefinition(
316 name,
317 pageContext.getRequest(),
318 pageContext.getServletContext());
319
320 } catch (NoSuchDefinitionException ex) {
321 throw new JspException(
322 "Error : Can't get component definition for '"
323 + name
324 + "'. Check if this name exist in component definitions.",ex);
325 } catch (FactoryNotFoundException ex) {
326 throw new JspException(ex);
327
328 } catch (DefinitionsFactoryException ex) {
329 if (debug)
330 ex.printStackTrace();
331
332 saveException(pageContext, ex);
333 throw new JspException(ex);
334 }
335 }
336
337 }