View Javadoc

1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      /* (non-Javadoc)
56       * @see net.sf.fridaymvc.models.Model#setStatelessAttribute(java.lang.Object, java.lang.Object)
57       */
58      public Object setStatelessAttribute(Object key, Object value) {
59          return getStatelessAttributeMap().put(key, value);
60      }
61      
62      /* (non-Javadoc)
63       * @see net.sf.fridaymvc.models.Model#getStatefulAttributeMap()
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      /* (non-Javadoc)
86       * @see net.sf.fridaymvc.models.Model#setStatefulAttribute(java.lang.Object, java.lang.Object)
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 }