KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > core > impl > PortletSessionImpl


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
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
18  */

19
20 package org.apache.pluto.core.impl;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.portlet.PortletContext;
26 import javax.portlet.PortletSession;
27 import javax.portlet.PortletSessionUtil;
28
29 import org.apache.pluto.factory.PortletObjectAccess;
30 import org.apache.pluto.om.window.PortletWindow;
31
32 public class PortletSessionImpl implements PortletSession, javax.servlet.http.HttpSession JavaDoc
33 {
34     private final int DEFAULT_SCOPE = PortletSession.PORTLET_SCOPE;
35
36     private javax.servlet.http.HttpSession JavaDoc httpSession;
37     private PortletContext portletContext = null;
38
39     private PortletWindow portletWindow;
40
41     public PortletSessionImpl(PortletWindow portletWindow,
42                               javax.servlet.http.HttpSession JavaDoc httpSession)
43     {
44         this.portletWindow = portletWindow;
45         this.httpSession = httpSession;
46     }
47
48     // javax.portlet.PortletSession and javax.servlet.http.HttpSession implementation -------------
49
public Object JavaDoc getAttribute(String JavaDoc name)
50     {
51         return this.getAttribute(name, DEFAULT_SCOPE);
52     }
53
54     public Enumeration JavaDoc getAttributeNames()
55     {
56         return this.getAttributeNames(DEFAULT_SCOPE);
57     }
58
59     public long getCreationTime() throws java.lang.IllegalStateException JavaDoc
60     {
61         return httpSession.getCreationTime();
62     }
63
64     public String JavaDoc getId() throws java.lang.IllegalStateException JavaDoc
65     {
66         return httpSession.getId();
67     }
68
69     public long getLastAccessedTime() throws java.lang.IllegalStateException JavaDoc
70     {
71         return httpSession.getLastAccessedTime();
72     }
73
74     public int getMaxInactiveInterval()
75     {
76         return httpSession.getMaxInactiveInterval();
77     }
78
79     public void invalidate() throws java.lang.IllegalStateException JavaDoc
80     {
81         httpSession.invalidate();
82     }
83
84     public boolean isNew() throws java.lang.IllegalStateException JavaDoc
85     {
86         return httpSession.isNew();
87     }
88
89     public void removeAttribute(String JavaDoc name)
90     {
91         this.removeAttribute(name, DEFAULT_SCOPE);
92     }
93
94     public void setAttribute(String JavaDoc name, Object JavaDoc value)
95     {
96         this.setAttribute(name, value, DEFAULT_SCOPE);
97     }
98
99     public void setMaxInactiveInterval(int interval)
100     {
101         httpSession.setMaxInactiveInterval(interval);
102     }
103     // --------------------------------------------------------------------------------------------
104

105     // javax.portlet.PortletSession implementation ------------------------------------------------
106
public java.lang.Object JavaDoc getAttribute(String JavaDoc name, int scope) throws java.lang.IllegalStateException JavaDoc
107     {
108         if (name == null)
109         {
110             throw new IllegalArgumentException JavaDoc("name must not be null");
111         }
112         if (scope==PortletSession.APPLICATION_SCOPE)
113         {
114             return httpSession.getAttribute(name);
115         }
116         else
117         {
118             Object JavaDoc attribute = httpSession.getAttribute("javax.portlet.p."+portletWindow.getId()+"?"+name);
119             if (attribute == null)
120             {
121                 // not sure, if this should be done for all attributes or only javax.servlet.
122
attribute = httpSession.getAttribute(name);
123             }
124             return attribute;
125         }
126     }
127
128     public java.util.Enumeration JavaDoc getAttributeNames(int scope)
129     {
130         if (scope==PortletSession.APPLICATION_SCOPE)
131         {
132             return httpSession.getAttributeNames();
133         }
134         else
135         {
136             Enumeration JavaDoc attributes = httpSession.getAttributeNames();
137
138             Vector JavaDoc portletAttributes = new Vector JavaDoc();
139
140             /* Fix that ONLY attributes of PORTLET_SCOPE are returned. */
141             int prefix_length = "javax.portlet.p.".length();
142             String JavaDoc portletWindowId = portletWindow.getId().toString();
143             
144             while (attributes.hasMoreElements())
145             {
146                 String JavaDoc attribute = (String JavaDoc)attributes.nextElement();
147
148                 int attributeScope = PortletSessionUtil.decodeScope(attribute);
149                 
150                 if (attributeScope == PortletSession.PORTLET_SCOPE && attribute.startsWith(portletWindowId, prefix_length))
151                 {
152                     String JavaDoc portletAttribute = PortletSessionUtil.decodeAttributeName(attribute);
153                     
154                     if (portletAttribute!=null)
155                     { // it is in the portlet's namespace
156
portletAttributes.add(portletAttribute);
157                     }
158                 }
159            }
160
161            return portletAttributes.elements();
162         }
163     }
164
165     public void removeAttribute(String JavaDoc name, int scope) throws java.lang.IllegalStateException JavaDoc
166     {
167         if (name == null)
168         {
169             throw new IllegalArgumentException JavaDoc("name must not be null");
170         }
171         if (scope == PortletSession.APPLICATION_SCOPE)
172         {
173             httpSession.removeAttribute(name);
174         }
175         else
176         {
177             httpSession.removeAttribute("javax.portlet.p."+portletWindow.getId()+"?"+name);
178         }
179     }
180
181     public void setAttribute(java.lang.String JavaDoc name, java.lang.Object JavaDoc value, int scope) throws IllegalStateException JavaDoc
182     {
183         if (name == null)
184         {
185             throw new IllegalArgumentException JavaDoc("name must not be null");
186         }
187         if (scope==PortletSession.APPLICATION_SCOPE)
188         {
189             httpSession.setAttribute(name,value);
190         }
191         else
192         {
193             httpSession.setAttribute("javax.portlet.p."+portletWindow.getId()+"?"+name, value);
194         }
195     }
196
197     public PortletContext getPortletContext()
198     {
199         return getInternalPortletContext();
200     }
201     // --------------------------------------------------------------------------------------------
202

203     // javax.servlet.http.HttpSession implementation ----------------------------------------------
204
public javax.servlet.ServletContext JavaDoc getServletContext()
205     {
206         // TBD, open issue. it would be good if we could also implement the ServletContext interface at the PortletContextImpl
207
return httpSession.getServletContext();
208     }
209     
210     /**
211      * @deprecated As of Java(tm) Servlet API 2.1 for security reasons,
212      * with no replacement. This interface will be removed
213      * in a future version of this API.
214      */

215     public javax.servlet.http.HttpSessionContext JavaDoc getSessionContext()
216     {
217         return httpSession.getSessionContext();
218     }
219
220     public Object JavaDoc getValue(String JavaDoc name)
221     {
222         return this.getAttribute(name, DEFAULT_SCOPE);
223     }
224
225     public String JavaDoc[] getValueNames()
226     {
227         // TBD
228
return null;
229     }
230
231     public void putValue(String JavaDoc name, Object JavaDoc value)
232     {
233         this.setAttribute(name, value, DEFAULT_SCOPE);
234     }
235
236     public void removeValue(String JavaDoc name)
237     {
238         this.removeAttribute(name, DEFAULT_SCOPE);
239     }
240     // --------------------------------------------------------------------------------------------
241

242     // internal methods ---------------------------------------------------------------------------
243
private synchronized PortletContext getInternalPortletContext()
244     {
245         if (this.portletContext == null)
246         {
247             this.portletContext = PortletObjectAccess.getPortletContext(
248                                                                        getServletContext(),
249                                                                        portletWindow.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition()
250                                                                        );
251         }
252         return this.portletContext;
253     }
254     // --------------------------------------------------------------------------------------------
255
}
256
Popular Tags