KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > portlet > impl > PortletSessionImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.portlet.impl;
10
11 import java.util.Enumeration JavaDoc;
12
13 import javax.portlet.PortletContext;
14 import javax.portlet.PortletSession;
15 import javax.servlet.http.HttpSession JavaDoc;
16
17 /**
18  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
19  * @version $Revision: 1.3 $
20  */

21 public class PortletSessionImpl implements PortletSession
22 {
23
24    private HttpSession JavaDoc session;
25    private String JavaDoc prefix;
26    private PortletContext context;
27    private boolean valid;
28
29    public PortletSessionImpl(HttpSession JavaDoc session, String JavaDoc id, PortletContext context)
30    {
31       this.session = session;
32       this.prefix = "javax.portlet.p." + id + "?";
33       this.context = context;
34       this.valid = true;
35    }
36
37    public Object JavaDoc getAttribute(String JavaDoc s)
38    {
39       return getAttribute(s, PORTLET_SCOPE);
40    }
41
42    public Object JavaDoc getAttribute(String JavaDoc s, int i)
43    {
44       if (!valid)
45       {
46          throw new IllegalStateException JavaDoc("Session is not valid anymore");
47       }
48       if (s == null)
49       {
50          throw new IllegalArgumentException JavaDoc("Name must not be null");
51       }
52       if (i == PORTLET_SCOPE)
53       {
54          s = prefix + s;
55       }
56       return session.getAttribute(s);
57    }
58
59    public Enumeration JavaDoc getAttributeNames()
60    {
61       return getAttributeNames(PORTLET_SCOPE);
62    }
63
64    public Enumeration JavaDoc getAttributeNames(int scope)
65    {
66       if (!valid)
67       {
68          throw new IllegalStateException JavaDoc("Session is not valid anymore");
69       }
70       final boolean app = scope == APPLICATION_SCOPE;
71       return new Enumeration JavaDoc()
72       {
73          private Enumeration JavaDoc e;
74          private String JavaDoc next;
75          {
76             e = session.getAttributeNames();
77             next = null;
78             next();
79          }
80          public boolean hasMoreElements()
81          {
82             return next != null;
83          }
84          public Object JavaDoc nextElement()
85          {
86             String JavaDoc result = next;
87             next = null;
88             next();
89             return result;
90          }
91          private void next()
92          {
93             while (e.hasMoreElements())
94             {
95                String JavaDoc attribute = (String JavaDoc)e.nextElement();
96                if (app && !attribute.startsWith("javax.portlet."))
97                {
98                   next = attribute;
99                   break;
100                }
101                else if (!app && attribute.startsWith(prefix))
102                {
103                   next = attribute.substring(prefix.length());
104                   break;
105                }
106             }
107          }
108       };
109    }
110
111    public long getCreationTime()
112    {
113       if (!valid)
114       {
115          throw new IllegalStateException JavaDoc("Session is not valid anymore");
116       }
117       return session.getCreationTime();
118    }
119
120    public String JavaDoc getId()
121    {
122       return session.getId();
123    }
124
125    public long getLastAccessedTime()
126    {
127       return session.getLastAccessedTime();
128    }
129
130    public int getMaxInactiveInterval()
131    {
132       return session.getMaxInactiveInterval();
133    }
134
135    public void invalidate()
136    {
137       if (!valid)
138       {
139          throw new IllegalStateException JavaDoc("Session is not valid anymore");
140       }
141
142       // Invalidate the underlying HTTP session
143
session.invalidate();
144
145       // Mark the session as invalid
146
valid = false;
147    }
148
149    public boolean isNew()
150    {
151       if (!valid)
152       {
153          throw new IllegalStateException JavaDoc("Session is not valid anymore");
154       }
155       return false;
156    }
157
158    public void removeAttribute(String JavaDoc s)
159    {
160       removeAttribute(s, PORTLET_SCOPE);
161    }
162
163    public void removeAttribute(String JavaDoc s, int i)
164    {
165       if (!valid)
166       {
167          throw new IllegalStateException JavaDoc("Session is not valid anymore");
168       }
169       if (s == null)
170       {
171          throw new IllegalArgumentException JavaDoc("Name must not be null");
172       }
173       if (i == PORTLET_SCOPE)
174       {
175          s = prefix + s;
176       }
177       session.removeAttribute(s);
178    }
179
180    public void setAttribute(String JavaDoc s, Object JavaDoc o)
181    {
182       setAttribute(s, o, PORTLET_SCOPE);
183    }
184
185    public void setAttribute(String JavaDoc s, Object JavaDoc o, int i)
186    {
187       if (!valid)
188       {
189          throw new IllegalStateException JavaDoc("Session is not valid anymore");
190       }
191       if (s == null)
192       {
193          throw new IllegalArgumentException JavaDoc("Name must not be null");
194       }
195       if (i == PORTLET_SCOPE)
196       {
197          s = prefix + s;
198       }
199       session.setAttribute(s, o);
200    }
201
202    public void setMaxInactiveInterval(int i)
203    {
204       session.setMaxInactiveInterval(i);
205    }
206
207    public PortletContext getPortletContext()
208    {
209       return context;
210    }
211
212    /**
213     * Return the underlying session.
214     */

215    HttpSession JavaDoc getSession()
216    {
217       return session;
218    }
219 };
220
Popular Tags