1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.fridaymvc.models;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.servlet.http.HttpSession;
23
24 import net.sf.fridaymvc.RunData;
25
26 /***
27 * @author <a href="mailto:arto.pastinen@ofw.fi">Arto Pastinen</a>
28 * @version $Id: AbstractModel.java,v 1.2 2004/11/23 20:35:58 artsi Exp $
29 */
30
31 public abstract class AbstractModel implements Model {
32 protected Map statelessAttributeMap;
33
34 public abstract void service() throws Exception;
35
36 /***
37 * @return Returns the statelessAttributeMap.
38 */
39 public Map getStatelessAttributeMap() {
40 if(this.statelessAttributeMap == null) {
41 synchronized(this) {
42 if(this.statelessAttributeMap == null) setStatelessAttributeMap(new HashMap(1));
43 }
44 }
45 return this.statelessAttributeMap;
46 }
47
48 /***
49 * @param statelessAttributeMap The statelessAttributeMap to set.
50 */
51 public void setStatelessAttributeMap(Map statelessAttributeMap) {
52 this.statelessAttributeMap = statelessAttributeMap;
53 }
54
55
56
57
58 public Object setStatelessAttribute(Object key, Object value) {
59 return getStatelessAttributeMap().put(key, value);
60 }
61
62
63
64
65 public Map getStatefulAttributeMap() {
66 HttpSession httpSession = RunData.getInstance().getHttpServletRequest().getSession();
67 Map models = (Map) httpSession.getAttribute("models");
68 boolean isDirty = false;
69 if(models == null) {
70 isDirty = true;
71 models = new HashMap(1);
72 }
73
74 Map modelAttributes = (Map) models.get(RunData.getInstance().getCurrentViewName());
75 if(modelAttributes == null) {
76 isDirty = true;
77 modelAttributes = new HashMap(1);
78 models.put(RunData.getInstance().getCurrentViewName(), modelAttributes);
79 }
80
81 if(isDirty) httpSession.setAttribute("models", models);
82 return modelAttributes;
83 }
84
85
86
87
88 public void setStatefulAttribute(Object key, Object value) {
89 HttpSession httpSession = RunData.getInstance().getHttpServletRequest().getSession();
90 Map models = (Map) httpSession.getAttribute("models");
91 if(models == null) {
92 models = new HashMap(1);
93 }
94
95 Map modelAttributes = (Map) models.get(RunData.getInstance().getCurrentViewName());
96 if(modelAttributes == null) {
97 modelAttributes = new HashMap(1);
98 models.put(RunData.getInstance().getCurrentViewName(), modelAttributes);
99 }
100
101 modelAttributes.put(key, value);
102
103 httpSession.setAttribute("models", models);
104 }
105 }