1 16 package com.blandware.atleap.webapp.taglib.core; 17 18 import com.blandware.atleap.common.Constants; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.struts.taglib.TagUtils; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.JspTagException ; 25 import javax.servlet.jsp.PageContext ; 26 import javax.servlet.jsp.tagext.SimpleTagSupport ; 27 import java.lang.reflect.AccessibleObject ; 28 import java.lang.reflect.Field ; 29 import java.lang.reflect.Modifier ; 30 31 32 73 public class ConstantsTag extends SimpleTagSupport { 74 protected transient final Log log = LogFactory.getLog(ConstantsTag.class); 75 76 79 public String className = Constants.class.getName(); 80 81 84 protected String scope = null; 85 86 89 protected String var = null; 90 91 96 public void doTag() throws JspException { 97 98 PageContext pageContext = (PageContext ) getJspContext(); 99 100 Class c = null; 102 Object obj = null; 103 int toScope = PageContext.PAGE_SCOPE; 104 105 if ( scope != null ) { 106 toScope = TagUtils.getInstance().getScope(scope); 108 } 109 110 try { 111 c = Class.forName(className); 112 } catch ( ClassNotFoundException cnf ) { 113 if ( log.isErrorEnabled() ) { 114 log.error("Class " + className + " not found - maybe a typo?"); 115 } 116 throw new JspTagException (cnf.getMessage()); 117 } 118 119 try { 120 if ( var == null ) { 122 123 125 Field [] fields = c.getFields(); 126 127 AccessibleObject.setAccessible(fields, true); 128 129 for ( int i = 0; i < fields.length; i++ ) { 130 if ( Modifier.isStatic(fields[i].getModifiers()) ) { 131 if ( log.isDebugEnabled() ) { 132 log.debug("putting '" + fields[i].getName() + "=" + 133 fields[i].get(obj) + "' into " + scope + 134 " scope"); 135 } 136 pageContext.setAttribute(fields[i].getName(), 137 String.valueOf(fields[i].get(obj)), toScope); 138 } 139 } 140 141 } else { 142 try { 143 Field field = c.getField(var); 144 if ( Modifier.isStatic(field.getModifiers()) ) { 145 String value = String.valueOf(field.get(null)); 146 pageContext.setAttribute(field.getName(), value, 147 toScope); 148 } else { 149 String errorMessage = "Field '" + var + "' is not static and will not be exported"; 150 if ( log.isDebugEnabled() ) { 151 log.debug(errorMessage); 152 } 153 throw new JspTagException (errorMessage); 154 } 155 } catch ( NoSuchFieldException nsf ) { 156 log.error(nsf.getMessage()); 157 throw new JspTagException (nsf); 158 } 159 } 160 } catch ( IllegalAccessException iae ) { 161 log.error("Illegal Access Exception - maybe a classloader issue?"); 162 throw new JspTagException (iae); 163 } 164 165 if ( log.isDebugEnabled() ) { 166 log.debug("Continue processing this page"); 167 } 168 169 } 170 171 177 public void setClassName(String clazz) { 178 this.className = clazz; 179 } 180 181 186 public String getClassName() { 187 return this.className; 188 } 189 190 196 public void setScope(String scope) { 197 this.scope = scope; 198 } 199 200 205 public String getScope() { 206 return (this.scope); 207 } 208 209 215 public void setVar(String var) { 216 this.var = var; 217 } 218 219 224 public String getVar() { 225 return (this.var); 226 } 227 228 } 229 | Popular Tags |