View Javadoc

1   package org.appfuse.webapp.taglib;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.appfuse.Constants;
6   
7   import javax.servlet.jsp.JspException;
8   import javax.servlet.jsp.PageContext;
9   import javax.servlet.jsp.tagext.TagSupport;
10  import java.lang.reflect.AccessibleObject;
11  import java.lang.reflect.Field;
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  
16  /**
17   * <p>This class is designed to put all the public variables in a class to a
18   * specified scope - designed for exposing a Constants class to Tag
19   * Libraries.</p>
20   *
21   * <p>It is designed to be used as follows:
22   * <pre>&lt;tag:constants /&gt;</pre>
23   * </p>
24   *
25   * <p>Optional values are "className" (fully qualified) and "scope".</p>
26   *
27   * <p>
28   * <a href="BaseAction.java.html"><i>View Source</i></a>
29   * </p>
30   *
31   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
32   */
33  public class ConstantsTag extends TagSupport {
34      private static final long serialVersionUID = 3258417209566116146L;
35      private final Log log = LogFactory.getLog(ConstantsTag.class);
36  
37      /**
38       * The class to expose the variables from.
39       */
40      private String clazz = Constants.class.getName();
41  
42      /**
43       * The scope to be put the variable in.
44       */
45      protected String scope;
46  
47      /**
48       * The single variable to expose.
49       */
50      protected String var;
51  
52      /**
53       * Main method that does processing and exposes Constants in specified scope 
54       * @return int
55       * @throws JspException if processing fails
56       */
57      @Override
58      public int doStartTag() throws JspException {
59          // Using reflection, get the available field names in the class
60          Class c = null;
61          int toScope = PageContext.PAGE_SCOPE;
62  
63          if (scope != null) {
64              toScope = getScope(scope);
65          }
66  
67          try {
68              c = Class.forName(clazz);
69          } catch (ClassNotFoundException cnf) {
70              log.error("ClassNotFound - maybe a typo?");
71              throw new JspException(cnf.getMessage());
72          }
73  
74          try {
75              // if var is null, expose all variables
76              if (var == null) {
77                  Field[] fields = c.getDeclaredFields();
78  
79                  AccessibleObject.setAccessible(fields, true);
80  
81                  for (Field field : fields) {
82                      pageContext.setAttribute(field.getName(), field.get(this), toScope);
83                  }
84              } else {
85                  try {
86                      Object value = c.getField(var).get(this);
87                      pageContext.setAttribute(c.getField(var).getName(), value, toScope);
88                  } catch (NoSuchFieldException nsf) {
89                      log.error(nsf.getMessage());
90                      throw new JspException(nsf);
91                  }
92              }
93          } catch (IllegalAccessException iae) {
94              log.error("Illegal Access Exception - maybe a classloader issue?");
95              throw new JspException(iae);
96          }
97  
98          // Continue processing this page
99          return (SKIP_BODY);
100     }
101 
102     public void setClassName(String clazz) {
103         this.clazz = clazz;
104     }
105 
106     public String getClassName() {
107         return this.clazz;
108     }
109 
110     public void setScope(String scope) {
111         this.scope = scope;
112     }
113 
114     public String getScope() {
115         return (this.scope);
116     }
117 
118     public void setVar(String var) {
119         this.var = var;
120     }
121 
122     public String getVar() {
123         return (this.var);
124     }
125 
126     /**
127      * Release all allocated resources.
128      */
129     public void release() {
130         super.release();
131         clazz = null;
132         scope = Constants.class.getName();
133     }
134 
135     /**
136      * Maps lowercase JSP scope names to their PageContext integer constant
137      * values.
138      */
139     private static final Map<String, Integer> SCOPES = new HashMap<String, Integer>();
140 
141     /**
142      * Initialize the scope names map and the encode variable
143      */
144     static {
145         SCOPES.put("page", PageContext.PAGE_SCOPE);
146         SCOPES.put("request", PageContext.REQUEST_SCOPE);
147         SCOPES.put("session", PageContext.SESSION_SCOPE);
148         SCOPES.put("application", PageContext.APPLICATION_SCOPE);
149     }
150     
151     /**
152      * Converts the scope name into its corresponding PageContext constant value.
153      * @param scopeName Can be "page", "request", "session", or "application" in any
154      * case.
155      * @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).
156      * @throws JspException if the scopeName is not a valid name.
157      */
158     public int getScope(String scopeName) throws JspException {
159         Integer scope = (Integer) SCOPES.get(scopeName.toLowerCase());
160 
161         if (scope == null) {
162             throw new JspException("Scope '" + scopeName + "' not a valid option");
163         }
164 
165         return scope;
166     }
167 }