KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > helpers > SharedSessionWrapper


1 /*
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  *
5  * Created on 1 d�c. 2003
6  */

7 package org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 import javax.servlet.http.HttpSession JavaDoc;
14 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
15 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
16
17 /**
18  * @author Mestrallet Benjamin
19  * benjmestrallet@users.sourceforge.net
20  */

21 public class SharedSessionWrapper extends HttpSessionWrapper{
22   
23   private static String JavaDoc CREATION_TIME = "javax.portlet.creation-time";
24   private static String JavaDoc LAST_ACCESS_TIME = "javax.portlet.last-access-time";
25   private static String JavaDoc MAXIMUM_INACTIVE_INTERVAL = "javax.portlet.max-inactive-interval";
26   
27   private static int defaultMaximalInterval = 900;//in seconds
28

29   private String JavaDoc applicationId;
30
31   public SharedSessionWrapper(HttpSession JavaDoc session) {
32     super(session);
33   }
34   
35   public void fillSharedSessionWrapper(HttpSession JavaDoc session,
36                                        String JavaDoc applicationId) {
37     super.setSession(session);
38     this.applicationId = applicationId;
39   }
40
41   public void emptySharedSessionWrapper() {
42     this.applicationId = null;
43   }
44   
45   public void init(){
46     //look if the associated session has already been created
47
String JavaDoc attCreation = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, CREATION_TIME);
48     String JavaDoc attLast = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, LAST_ACCESS_TIME);
49     if (super.getAttribute(attCreation) == null) {
50       String JavaDoc maxInterval = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, MAXIMUM_INACTIVE_INTERVAL);
51       super.setAttribute(attCreation, new Long JavaDoc(System.currentTimeMillis()));
52       super.setAttribute(attLast, new Long JavaDoc(System.currentTimeMillis()));
53       super.setAttribute(maxInterval, new Integer JavaDoc(defaultMaximalInterval));
54     }
55 // } else {//update some info about access times
56
// super.setAttribute(attLast, new Long(System.currentTimeMillis()));
57
// }
58
}
59     
60   public Object JavaDoc getAttribute(String JavaDoc name) {
61     setLastAccessTime();
62     String JavaDoc key = SharedSessionUtil.encodePortletSessionAttribute(applicationId, name);
63     Object JavaDoc value = super.getAttribute(key);
64     if(value instanceof HttpSessionBindingListenerProxy){
65       return((HttpSessionBindingListenerProxy)value).getListener();
66     }
67     return value;
68   }
69
70   public Enumeration JavaDoc getAttributeNames() {
71     setLastAccessTime();
72     Enumeration JavaDoc e = super.getAttributeNames();
73     Vector JavaDoc v = new Vector JavaDoc();
74     while (e.hasMoreElements()) {
75       String JavaDoc s = (String JavaDoc) e.nextElement();
76       if (!SharedSessionUtil.isMetaDataAttribute(applicationId, s)
77           && SharedSessionUtil.isAttribute(applicationId, s)) {
78         s = SharedSessionUtil.decodePortletSessionAttribute(applicationId, s);
79         v.add(s);
80       }
81     }
82     return v.elements();
83   }
84
85   public void removeAttribute(String JavaDoc name) {
86     String JavaDoc key = SharedSessionUtil.encodePortletSessionAttribute(applicationId, name);
87     if(super.getAttribute(key) instanceof HttpSessionBindingListenerProxy){
88       HttpSessionBindingListener JavaDoc listener = ((HttpSessionBindingListenerProxy)super.getAttribute(key)).getListener();
89       listener.valueUnbound(new HttpSessionBindingEvent JavaDoc(this, name));
90     }
91     super.removeAttribute(key);
92     setLastAccessTime();
93   }
94
95   public void removeValue(String JavaDoc name) {
96   }
97
98   public void setAttribute(String JavaDoc name, Object JavaDoc value) {
99     if(value instanceof HttpSessionBindingListener JavaDoc){
100       HttpSessionBindingListener JavaDoc listener = (HttpSessionBindingListener JavaDoc)value;
101       listener.valueBound(new HttpSessionBindingEvent JavaDoc(this, name));
102       value = new HttpSessionBindingListenerProxy(listener);
103     }
104     String JavaDoc key = SharedSessionUtil.encodePortletSessionAttribute(applicationId, name);
105     super.setAttribute(key, value);
106     setLastAccessTime();
107   }
108
109   public long getCreationTime() {
110     setLastAccessTime();
111     String JavaDoc key = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId,
112                                                                          CREATION_TIME);
113     Long JavaDoc creationTime = (Long JavaDoc) super.getAttribute(key);
114     return creationTime.longValue();
115   }
116   
117   public int getMaxInactiveInterval() {
118     setLastAccessTime();
119     String JavaDoc key = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId,
120                                                                          MAXIMUM_INACTIVE_INTERVAL);
121     Integer JavaDoc maxInactivInterval = (Integer JavaDoc) super.getAttribute(key);
122     return maxInactivInterval.intValue();
123   }
124   
125   public void invalidate() {
126     Enumeration JavaDoc e = super.getAttributeNames();
127     ArrayList JavaDoc list = new ArrayList JavaDoc(15) ;
128     while (e.hasMoreElements()) {
129       String JavaDoc s = (String JavaDoc) e.nextElement();
130       if (SharedSessionUtil.isMetaDataAttribute(applicationId, s)
131           || SharedSessionUtil.isAttribute(applicationId, s)) {
132         list.add(s);
133       }
134     }
135     for (int i = 0 ; i < list.size(); i++) {
136       String JavaDoc name = (String JavaDoc) list.get(i) ;
137       super.removeAttribute(name);
138     }
139   }
140   
141   public boolean isNew() {
142     setLastAccessTime();
143     return false;
144   }
145   
146   public void setMaxInactiveInterval(int i) {
147     String JavaDoc key = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId,
148                                                                          MAXIMUM_INACTIVE_INTERVAL);
149     super.setAttribute(key, new Integer JavaDoc(i));
150     setLastAccessTime();
151   }
152   
153   private void setLastAccessTime(){
154     String JavaDoc attLast = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId,
155                                                                              LAST_ACCESS_TIME);
156     super.setAttribute(attLast, new Long JavaDoc(System.currentTimeMillis()));
157   }
158   
159   public long getLastAccessedTime() {
160     String JavaDoc key = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId,
161                                                                          LAST_ACCESS_TIME);
162     Long JavaDoc lastAccessTime = (Long JavaDoc) super.getAttribute(key);
163     if(lastAccessTime == null)
164       return -1;
165     return lastAccessTime.longValue();
166   }
167
168 }
169
Popular Tags