KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > taglib > ConstantsTag


1 package org.appfuse.webapp.taglib;
2
3 import java.lang.reflect.AccessibleObject JavaDoc;
4 import java.lang.reflect.Field JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import javax.servlet.jsp.JspException JavaDoc;
9 import javax.servlet.jsp.PageContext JavaDoc;
10 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.appfuse.Constants;
15
16
17 /**
18  * <p>This class is designed to put all the public variables in a class to a
19  * specified scope - designed for exposing a Constants class to Tag
20  * Libraries.</p>
21  *
22  * <p>It is designed to be used as follows:
23  * <pre>&lt;tag:constants /&gt;</pre>
24  * </p>
25  *
26  * <p>Optional values are "className" (fully qualified) and "scope".</p>
27  *
28  * <p>
29  * <a HREF="BaseAction.java.htm"><i>View Source</i></a>
30  * </p>
31  *
32  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
33  * @version $Revision: 1.7 $ $Date: 2006-01-29 22:00:13 -0700 (Sun, 29 Jan 2006) $
34  *
35  * @jsp.tag name="constants" bodycontent="empty"
36  * tei-class="org.appfuse.webapp.taglib.ConstantsTei"
37  */

38 public class ConstantsTag extends TagSupport JavaDoc {
39     private static final long serialVersionUID = 3258417209566116146L;
40     private final Log log = LogFactory.getLog(ConstantsTag.class);
41     
42     /**
43      * The class to expose the variables from.
44      */

45     public String JavaDoc clazz = Constants.class.getName();
46
47     /**
48      * The scope to be put the variable in.
49      */

50     protected String JavaDoc scope = null;
51
52     /**
53      * The single variable to expose.
54      */

55     protected String JavaDoc var = null;
56
57     public int doStartTag() throws JspException JavaDoc {
58         // Using reflection, get the available field names in the class
59
Class JavaDoc c = null;
60         int toScope = PageContext.PAGE_SCOPE;
61
62         if (scope != null) {
63             toScope = getScope(scope);
64         }
65
66         try {
67             c = Class.forName(clazz);
68         } catch (ClassNotFoundException JavaDoc cnf) {
69             log.error("ClassNotFound - maybe a typo?");
70             throw new JspException JavaDoc(cnf.getMessage());
71         }
72
73         try {
74             // if var is null, expose all variables
75
if (var == null) {
76                 Field JavaDoc[] fields = c.getDeclaredFields();
77
78                 AccessibleObject.setAccessible(fields, true);
79
80                 for (int i = 0; i < fields.length; i++) {
81                     /*if (log.isDebugEnabled()) {
82                        log.debug("putting '" + fields[i].getName() + "=" +
83                                  fields[i].get(this) + "' into " + scope +
84                                  " scope");
85                        }*/

86                     pageContext.setAttribute(fields[i].getName(),
87                                              fields[i].get(this), toScope);
88                 }
89             } else {
90                 try {
91                     Object JavaDoc value = c.getField(var).get(this);
92                     pageContext.setAttribute(c.getField(var).getName(), value,
93                                              toScope);
94                 } catch (NoSuchFieldException JavaDoc nsf) {
95                     log.error(nsf.getMessage());
96                     throw new JspException JavaDoc(nsf);
97                 }
98             }
99         } catch (IllegalAccessException JavaDoc iae) {
100             log.error("Illegal Access Exception - maybe a classloader issue?");
101             throw new JspException JavaDoc(iae);
102         }
103
104         // Continue processing this page
105
return (SKIP_BODY);
106     }
107
108     /**
109      * @jsp.attribute
110      */

111     public void setClassName(String JavaDoc clazz) {
112         this.clazz = clazz;
113     }
114
115     public String JavaDoc getClassName() {
116         return this.clazz;
117     }
118
119     /**
120      * @jsp.attribute
121      */

122     public void setScope(String JavaDoc scope) {
123         this.scope = scope;
124     }
125
126     public String JavaDoc getScope() {
127         return (this.scope);
128     }
129
130     /**
131      * @jsp.attribute
132      */

133     public void setVar(String JavaDoc var) {
134         this.var = var;
135     }
136
137     public String JavaDoc getVar() {
138         return (this.var);
139     }
140
141     /**
142      * Release all allocated resources.
143      */

144     public void release() {
145         super.release();
146         clazz = null;
147         scope = Constants.class.getName();
148     }
149     
150     // ~========== From Struts' TagUtils class =====================
151

152     /**
153      * Maps lowercase JSP scope names to their PageContext integer constant
154      * values.
155      */

156     private static final Map JavaDoc scopes = new HashMap JavaDoc();
157
158     /**
159      * Initialize the scope names map and the encode variable with the
160      * Java 1.4 method if available.
161      */

162     static {
163         scopes.put("page", new Integer JavaDoc(PageContext.PAGE_SCOPE));
164         scopes.put("request", new Integer JavaDoc(PageContext.REQUEST_SCOPE));
165         scopes.put("session", new Integer JavaDoc(PageContext.SESSION_SCOPE));
166         scopes.put("application", new Integer JavaDoc(PageContext.APPLICATION_SCOPE));
167     }
168     
169     /**
170      * Converts the scope name into its corresponding PageContext constant value.
171      * @param scopeName Can be "page", "request", "session", or "application" in any
172      * case.
173      * @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).
174      * @throws JspException if the scopeName is not a valid name.
175      */

176     public int getScope(String JavaDoc scopeName) throws JspException JavaDoc {
177         Integer JavaDoc scope = (Integer JavaDoc) scopes.get(scopeName.toLowerCase());
178
179         if (scope == null) {
180             throw new JspException JavaDoc("Scope '" + scopeName + "' not a valid option");
181         }
182
183         return scope.intValue();
184     }
185 }
186
Popular Tags