1 3 package jodd.bean.loader; 4 5 import java.util.Enumeration ; 6 7 import javax.servlet.ServletContext ; 8 9 import jodd.bean.BeanUtil; 10 import jodd.bean.BeanException; 11 12 16 public class ServletContextBeanLoader implements BeanLoader { 17 18 public ServletContextBeanLoader() { 19 } 20 21 String prefix = null; 22 23 public ServletContextBeanLoader(String prefix) { 24 this.prefix = prefix; 25 } 26 27 public static void loadBean(Object bean, Object context, String prefix) { 28 if (context instanceof ServletContext ) { 29 30 Enumeration attribNames = ((ServletContext )context).getAttributeNames(); 31 while (attribNames.hasMoreElements()) { 32 String attribName = (String ) attribNames.nextElement(); 33 Object value = ((ServletContext )context).getAttribute(attribName); 34 if (value == null) { 35 continue; 36 } 37 if (prefix != null) { 38 attribName = prefix + Character.toUpperCase(attribName.charAt(0)) + attribName.substring(1); 39 } 40 try { 41 BeanUtil.setPropertyForcedSilent(bean, attribName, value); 42 } catch (BeanException bex) { 43 } 45 } 46 } 47 } 48 49 public void load(Object bean, Object context) { 50 loadBean(bean, context, prefix); 51 } 52 53 } 54 | Popular Tags |