| 1 package org.appfuse.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 org.appfuse.Constants; 15 16 17 38 public class ConstantsTag extends TagSupport { 39 private static final long serialVersionUID = 3258417209566116146L; 40 private final Log log = LogFactory.getLog(ConstantsTag.class); 41 42 45 public String clazz = Constants.class.getName(); 46 47 50 protected String scope = null; 51 52 55 protected String var = null; 56 57 public int doStartTag() throws JspException { 58 Class 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 cnf) { 69 log.error("ClassNotFound - maybe a typo?"); 70 throw new JspException (cnf.getMessage()); 71 } 72 73 try { 74 if (var == null) { 76 Field [] fields = c.getDeclaredFields(); 77 78 AccessibleObject.setAccessible(fields, true); 79 80 for (int i = 0; i < fields.length; i++) { 81 86 pageContext.setAttribute(fields[i].getName(), 87 fields[i].get(this), toScope); 88 } 89 } else { 90 try { 91 Object value = c.getField(var).get(this); 92 pageContext.setAttribute(c.getField(var).getName(), value, 93 toScope); 94 } catch (NoSuchFieldException nsf) { 95 log.error(nsf.getMessage()); 96 throw new JspException (nsf); 97 } 98 } 99 } catch (IllegalAccessException iae) { 100 log.error("Illegal Access Exception - maybe a classloader issue?"); 101 throw new JspException (iae); 102 } 103 104 return (SKIP_BODY); 106 } 107 108 111 public void setClassName(String clazz) { 112 this.clazz = clazz; 113 } 114 115 public String getClassName() { 116 return this.clazz; 117 } 118 119 122 public void setScope(String scope) { 123 this.scope = scope; 124 } 125 126 public String getScope() { 127 return (this.scope); 128 } 129 130 133 public void setVar(String var) { 134 this.var = var; 135 } 136 137 public String getVar() { 138 return (this.var); 139 } 140 141 144 public void release() { 145 super.release(); 146 clazz = null; 147 scope = Constants.class.getName(); 148 } 149 150 152 156 private static final Map scopes = new HashMap (); 157 158 162 static { 163 scopes.put("page", new Integer (PageContext.PAGE_SCOPE)); 164 scopes.put("request", new Integer (PageContext.REQUEST_SCOPE)); 165 scopes.put("session", new Integer (PageContext.SESSION_SCOPE)); 166 scopes.put("application", new Integer (PageContext.APPLICATION_SCOPE)); 167 } 168 169 176 public int getScope(String scopeName) throws JspException { 177 Integer scope = (Integer ) scopes.get(scopeName.toLowerCase()); 178 179 if (scope == null) { 180 throw new JspException ("Scope '" + scopeName + "' not a valid option"); 181 } 182 183 return scope.intValue(); 184 } 185 } 186 | Popular Tags |