View Javadoc

1   package com.panogenesis.webapp.taglib;
2   
3   import java.lang.reflect.AccessibleObject;
4   import java.lang.reflect.Field;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import javax.servlet.jsp.JspException;
9   import javax.servlet.jsp.PageContext;
10  import javax.servlet.jsp.tagext.TagSupport;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import com.panogenesis.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.html"><i>View Source</i></a>
30   * </p>
31   *
32   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
33   * @version $Revision: 1.5 $ $Date: 2004/09/30 04:41:19 $
34   *
35   * @jsp.tag name="constants" bodycontent="empty"
36   *  tei-class="com.panogenesis.webapp.taglib.ConstantsTei"
37   */
38  public class ConstantsTag extends TagSupport {
39      private final Log log = LogFactory.getLog(ConstantsTag.class);
40      
41      /***
42       * The class to expose the variables from.
43       */
44      public String clazz = Constants.class.getName();
45  
46      /***
47       * The scope to be put the variable in.
48       */
49      protected String scope = null;
50  
51      /***
52       * The single variable to expose.
53       */
54      protected String var = null;
55  
56      public int doStartTag() throws JspException {
57          // Using reflection, get the available field names in the class
58          Class c = null;
59          int toScope = PageContext.PAGE_SCOPE;
60  
61          if (scope != null) {
62              toScope = getScope(scope);
63          }
64  
65          try {
66              c = Class.forName(clazz);
67          } catch (ClassNotFoundException cnf) {
68              log.error("ClassNotFound - maybe a typo?");
69              throw new JspException(cnf.getMessage());
70          }
71  
72          try {
73              // if var is null, expose all variables
74              if (var == null) {
75                  Field[] fields = c.getDeclaredFields();
76  
77                  AccessibleObject.setAccessible(fields, true);
78  
79                  for (int i = 0; i < fields.length; i++) {
80                      /*if (log.isDebugEnabled()) {
81                         log.debug("putting '" + fields[i].getName() + "=" +
82                                   fields[i].get(this) + "' into " + scope +
83                                   " scope");
84                         }*/
85                      pageContext.setAttribute(fields[i].getName(),
86                                               fields[i].get(this), toScope);
87                  }
88              } else {
89                  try {
90                      String value = (String) c.getField(var).get(this);
91                      pageContext.setAttribute(c.getField(var).getName(), value,
92                                               toScope);
93                  } catch (NoSuchFieldException nsf) {
94                      log.error(nsf.getMessage());
95                      throw new JspException(nsf);
96                  }
97              }
98          } catch (IllegalAccessException iae) {
99              log.error("Illegal Access Exception - maybe a classloader issue?");
100             throw new JspException(iae);
101         }
102 
103         // Continue processing this page
104         return (SKIP_BODY);
105     }
106 
107     /***
108      * @jsp.attribute
109      */
110     public void setClassName(String clazz) {
111         this.clazz = clazz;
112     }
113 
114     public String getClassName() {
115         return this.clazz;
116     }
117 
118     /***
119      * @jsp.attribute
120      */
121     public void setScope(String scope) {
122         this.scope = scope;
123     }
124 
125     public String getScope() {
126         return (this.scope);
127     }
128 
129     /***
130      * @jsp.attribute
131      */
132     public void setVar(String var) {
133         this.var = var;
134     }
135 
136     public String getVar() {
137         return (this.var);
138     }
139 
140     /***
141      * Release all allocated resources.
142      */
143     public void release() {
144         super.release();
145         clazz = null;
146         scope = Constants.class.getName();
147     }
148     
149     // ~========== From Struts' TagUtils class =====================
150 
151     /***
152      * Maps lowercase JSP scope names to their PageContext integer constant
153      * values.
154      */
155     private static final Map scopes = new HashMap();
156 
157     /***
158      * Initialize the scope names map and the encode variable with the 
159      * Java 1.4 method if available.
160      */
161     static {
162         scopes.put("page", new Integer(PageContext.PAGE_SCOPE));
163         scopes.put("request", new Integer(PageContext.REQUEST_SCOPE));
164         scopes.put("session", new Integer(PageContext.SESSION_SCOPE));
165         scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE));
166     }
167     
168     /***
169      * Converts the scope name into its corresponding PageContext constant value.
170      * @param scopeName Can be "page", "request", "session", or "application" in any
171      * case.
172      * @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).
173      * @throws JspException if the scopeName is not a valid name.
174      */
175     public int getScope(String scopeName) throws JspException {
176         Integer scope = (Integer) scopes.get(scopeName.toLowerCase());
177 
178         if (scope == null) {
179             throw new JspException("Scope '" + scopeName + "' not a valid option");
180         }
181 
182         return scope.intValue();
183     }
184 }