KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > PortletSessionImp


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
6 /**
7  * Created by The eXo Platform SARL
8  * Author : Mestrallet Benjamin
9  * benjmestrallet@users.sourceforge.net
10  * Date: Jul 26, 2003
11  * Time: 4:30:52 PM
12  */

13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp;
14
15
16 import java.util.Enumeration JavaDoc;
17 import java.util.Vector JavaDoc;
18
19 import javax.portlet.PortletContext;
20 import javax.portlet.PortletSession;
21 import javax.portlet.PortletSessionUtil;
22 import javax.servlet.http.HttpSession JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.exoplatform.commons.map.AbstractMap;
26 import org.exoplatform.container.PortalContainer;
27 import org.exoplatform.services.log.LogService;
28 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.utils.PortletSessionImpUtil;
29
30 public class PortletSessionImp extends AbstractMap implements PortletSession {
31
32     private HttpSession JavaDoc session;
33     private PortletContext context;
34     private String JavaDoc windowId;
35   private String JavaDoc applicationId ;
36   private boolean invalidated;
37   private Log log;
38
39   public PortletSessionImp() {
40     this.log = ((LogService)PortalContainer.getInstance().getComponentInstanceOfType(LogService.class)).
41         getLog("org.exoplatform.services.portletcontainer");
42   }
43
44     public void fillPortletSession(HttpSession JavaDoc session,
45                                                                  PortletContext context,
46                                                                  String JavaDoc windowId) {
47         this.session = session;
48         this.context = context;
49         this.windowId = windowId;
50     this.applicationId = context.getPortletContextName() ;
51     invalidated = false ;
52     }
53
54     public void emptyPortletSession() {
55         this.windowId = "";
56     }
57
58     public Object JavaDoc getAttribute(String JavaDoc name) {
59         return getAttribute(name, PortletSession.PORTLET_SCOPE);
60     }
61
62     public Object JavaDoc getAttribute(String JavaDoc name, int i) {
63     if(invalidated)
64       throw new IllegalStateException JavaDoc("session invalidated");
65     if (name == null) {
66       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null") ;
67     }
68         if (PortletSession.APPLICATION_SCOPE == i) {
69             return session.getAttribute(name);
70         } else if (PortletSession.PORTLET_SCOPE == i) {
71       String JavaDoc key = PortletSessionImpUtil.encodePortletSessionAttribute(windowId, name, PortletSession.PORTLET_SCOPE) ;
72             return session.getAttribute(key);
73         }
74         return null;
75     }
76
77     public void removeAttribute(String JavaDoc name) {
78         removeAttribute(name, PortletSession.PORTLET_SCOPE);
79     }
80
81     public void removeAttribute(String JavaDoc name, int i) {
82     if(invalidated)
83       throw new IllegalStateException JavaDoc("session invalidated");
84     if (name == null) {
85       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null") ;
86     }
87         if (PortletSession.APPLICATION_SCOPE == i) {
88             session.removeAttribute(name);
89         } else if (PortletSession.PORTLET_SCOPE == i) {
90       String JavaDoc key = PortletSessionImpUtil.encodePortletSessionAttribute(windowId, name, PortletSession.PORTLET_SCOPE) ;
91             session.removeAttribute(key);
92         }
93     }
94
95     final public void setAttribute(String JavaDoc name, Object JavaDoc o) {
96     setAttribute(name, o, PortletSession.PORTLET_SCOPE);
97     }
98
99     public void setAttribute(String JavaDoc name, Object JavaDoc o, int i) {
100     if(invalidated)
101       throw new IllegalStateException JavaDoc("session invalidated");
102     if (name == null) {
103       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null") ;
104     }
105         if (PortletSession.APPLICATION_SCOPE == i) {
106       if (o == null) session.removeAttribute(name);
107       else session.setAttribute(name, o);
108         } else if (PortletSession.PORTLET_SCOPE == i) {
109       String JavaDoc key = PortletSessionImpUtil.encodePortletSessionAttribute(windowId, name, PortletSession.PORTLET_SCOPE) ;
110       if (o == null) session.removeAttribute(key);
111       else session.setAttribute(key, o);
112         }
113     }
114
115   public Enumeration JavaDoc getAttributeNames() {
116     return getAttributeNames(PortletSession.PORTLET_SCOPE);
117   }
118
119   public Enumeration JavaDoc getAttributeNames(int i) {
120     if(invalidated)
121       throw new IllegalStateException JavaDoc("session invalidated");
122     Enumeration JavaDoc e = session.getAttributeNames();
123     Vector JavaDoc v = new Vector JavaDoc();
124     while (e.hasMoreElements()) {
125       String JavaDoc s = (String JavaDoc) e.nextElement();
126       if(i == PortletSession.PORTLET_SCOPE){
127         if (PortletSessionUtil.decodeScope(s) == PortletSession.PORTLET_SCOPE)
128           v.add(PortletSessionUtil.decodeAttributeName(s));
129       } else {
130         if (PortletSessionUtil.decodeScope(s) == PortletSession.APPLICATION_SCOPE)
131           v.add(s);
132       }
133     }
134     return v.elements();
135   }
136   
137
138   public long getCreationTime() {
139     if(invalidated)
140       throw new IllegalStateException JavaDoc("session invalidated");
141     return session.getCreationTime();
142   }
143
144   public String JavaDoc getId() {
145     if(invalidated)
146       throw new IllegalStateException JavaDoc("session invalidated");
147     return session.getId() + applicationId;
148   }
149
150   public long getLastAccessedTime() {
151     if(invalidated)
152       throw new IllegalStateException JavaDoc("session invalidated");
153     return session.getLastAccessedTime();
154   }
155
156   public int getMaxInactiveInterval() {
157     if(invalidated)
158       throw new IllegalStateException JavaDoc("session invalidated");
159     return session.getMaxInactiveInterval();
160   }
161
162   public void invalidate() {
163     if(invalidated)
164       throw new IllegalStateException JavaDoc("session invalidated");
165     session.invalidate();
166     invalidated = true;
167   }
168   
169   public boolean isSessionValid() {
170     try{
171       long lastAccessTime = session.getLastAccessedTime();
172       //tomcat 5
173
if(lastAccessTime == 0)
174         return true;
175       //tomcat 4
176
if(lastAccessTime == -1)
177         return false;
178       int maxInterval = session.getMaxInactiveInterval();
179       if(maxInterval < 0)
180         return true;
181       if((System.currentTimeMillis() - lastAccessTime) > (maxInterval * 1000)){
182         session.invalidate();
183         return false;
184       }
185       return true;
186     } catch(IllegalStateException JavaDoc e){
187       log.error("IllegalStateException in PortletSessionImp for isSessionValid()", e);
188       return false;
189     }
190   }
191   
192   public boolean isNew() {
193     if(invalidated)
194       throw new IllegalStateException JavaDoc("session invalidated");
195     return session.isNew();
196   }
197  
198   public void setMaxInactiveInterval(int i) {
199     if(invalidated)
200       throw new IllegalStateException JavaDoc("session invalidated");
201     session.setMaxInactiveInterval(i);
202   }
203
204     public PortletContext getPortletContext() {
205         return context;
206     }
207
208   public HttpSession JavaDoc getSession() {
209     return session;
210   }
211
212   public void setSession(HttpSession JavaDoc session) {
213     this.session = session;
214   }
215
216 }
217
Popular Tags