1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.taglib;
16
17 import javax.servlet.jsp.PageContext;
18 import javax.servlet.jsp.JspTagException;
19 import javax.servlet.jsp.tagext.BodyTagSupport;
20 /**
21 * This is a base class for tag extensions using context and scopes
22 * for retrieving and storing beans.
23 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
24 * @version $Revision: 1.2 $
25 */
26 public abstract class ContextTag extends BodyTagSupport{
27
28
29
30 /**
31 * Find a bean specified by its name from context within a specified scope.
32 * @param name Name of the bean to retrieve
33 * @param scope Scope to look for the bean
34 * @throws javax.servlet.jsp.JspTagException scope is not an existing scope
35 */
36 protected Object findBeanInScope(String name, String scope) throws JspTagException{
37 Object obj = null;
38
39 if (scope.equalsIgnoreCase("page"))
40 obj = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
41 else if (scope.equalsIgnoreCase("request"))
42 obj = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
43 else if (scope.equalsIgnoreCase("session"))
44 obj = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
45 else if (scope.equalsIgnoreCase("application"))
46 obj = pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
47 else
48 throw new JspTagException("'" + scope + "' is not an existing scope !");
49
50 return obj;
51 }
52
53 /**
54 * Store a bean into context using a specified name within a specified scope
55 * @param bean The bean to store within scope
56 * @param name Name to use for storing bean within context
57 * @param scope The scope of context to use for registration
58 */
59 protected void storeBeanInScope(Object bean, String name, String scope){
60
61 if ("request".equalsIgnoreCase(scope))
62 pageContext.setAttribute(name, bean, PageContext.REQUEST_SCOPE);
63 else if ("session".equalsIgnoreCase(scope))
64 pageContext.setAttribute(name, bean, PageContext.SESSION_SCOPE);
65 else if ("application".equalsIgnoreCase(scope))
66 pageContext.setAttribute(name, bean, PageContext.APPLICATION_SCOPE);
67 else
68 pageContext.setAttribute(name, bean, PageContext.PAGE_SCOPE);
69 }
70 }