01: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
02:
03: package jodd.bean.loader;
04:
05: import java.util.Enumeration;
06:
07: import javax.servlet.http.HttpSession;
08:
09: import jodd.bean.BeanUtil;
10:
11: /**
12: * Populates java bean from HttpSession objects. It allows to be instanced with a
13: * 'prefix' that will be added in front of all attributes.
14: */
15: public class SessionBeanLoader implements BeanLoader {
16:
17: public SessionBeanLoader() {
18: }
19:
20: String prefix;
21:
22: public SessionBeanLoader(String prefix) {
23: this .prefix = prefix;
24: }
25:
26: public static void loadBean(Object bean, Object session,
27: String prefix) {
28: if (session instanceof HttpSession) {
29:
30: Enumeration attribNames = ((HttpSession) session)
31: .getAttributeNames();
32: while (attribNames.hasMoreElements()) {
33: String attribName = (String) attribNames.nextElement();
34: Object value = ((HttpSession) session)
35: .getAttribute(attribName);
36: if (value == null) {
37: continue;
38: }
39: if (prefix != null) {
40: attribName = prefix
41: + Character.toUpperCase(attribName
42: .charAt(0))
43: + attribName.substring(1);
44: }
45: BeanUtil.setPropertyForcedSilent(bean, attribName,
46: value);
47: }
48: }
49: }
50:
51: public void load(Object bean, Object session) {
52: loadBean(bean, session, prefix);
53: }
54:
55: }
|