KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > portlet > PortletSession


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.environment.portlet;
17
18 import org.apache.cocoon.environment.Session;
19
20 import java.util.Enumeration JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22
23 /**
24  * Provides access to the JSR-168 (Portlet) environment session.
25  *
26  * <p>Portlet scope and application scope session attributes are differentiated
27  * using attribute name prefix, {@link PortletEnvironment#SESSION_APPLICATION_SCOPE}.
28  *
29  * @see javax.portlet.PortletSession
30  * @author <a HREF="mailto:alex.rudnev@dc.gov">Alex Rudnev</a>
31  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
32  * @version CVS $Id: PortletSession.java 53987 2004-10-07 15:33:25Z vgritsenko $
33  */

34 public final class PortletSession implements Session {
35
36     private static final String JavaDoc APP_SCOPE = PortletEnvironment.SESSION_APPLICATION_SCOPE;
37     private static final String JavaDoc PORTLET_SCOPE = PortletEnvironment.SESSION_PORTLET_SCOPE;
38
39     javax.portlet.PortletSession session;
40
41     /**
42      * Default session scope. One of
43      * {@link javax.portlet.PortletSession#APPLICATION_SCOPE},
44      * {@link javax.portlet.PortletSession#PORTLET_SCOPE}.
45      */

46     private int scope;
47
48     /**
49      * Construct a new session from an PortletSession
50      */

51     public PortletSession(javax.portlet.PortletSession session, int scope) {
52         this.scope = scope;
53         this.session = session;
54     }
55
56     /**
57      * Returns the time when this session was created, measured
58      * in milliseconds since midnight January 1, 1970 GMT.
59      *
60      * @return a <code>long</code> specifying
61      * when this session was created,
62      * expressed in
63      * milliseconds since 1/1/1970 GMT
64      *
65      * @exception IllegalStateException if this method is called on an
66      * invalidated session
67      */

68     public long getCreationTime() {
69         return this.session.getCreationTime();
70     }
71
72     /**
73      * Returns a string containing the unique identifier assigned
74      * to this session. The identifier is assigned
75      * by the context container and is implementation dependent.
76      *
77      * @return a string specifying the identifier
78      * assigned to this session
79      *
80      * @exception IllegalStateException if this method is called on an
81      * invalidated session
82      */

83     public String JavaDoc getId() {
84         return this.session.getId();
85     }
86
87     /**
88      * Returns the last time the client sent a request associated with
89      * this session, as the number of milliseconds since midnight
90      * January 1, 1970 GMT.
91      *
92      * <p>Actions that your application takes, such as getting or setting
93      * a value associated with the session, do not affect the access
94      * time.
95      *
96      * @return a <code>long</code>
97      * representing the last time
98      * the client sent a request associated
99      * with this session, expressed in
100      * milliseconds since 1/1/1970 GMT
101      *
102      * @exception IllegalStateException if this method is called on an
103      * invalidated session
104      */

105     public long getLastAccessedTime() {
106         return this.session.getLastAccessedTime();
107     }
108
109     /**
110      * Specifies the time, in seconds, between client requests before the
111      * contextcontainer will invalidate this session. A negative time
112      * indicates the session should never timeout.
113      *
114      * @param interval An integer specifying the number
115      * of seconds
116      */

117     public void setMaxInactiveInterval(int interval) {
118         this.session.setMaxInactiveInterval(interval);
119     }
120
121     /**
122      * Returns the maximum time interval, in seconds, that
123      * the context container will keep this session open between
124      * client accesses. After this interval, the context container
125      * will invalidate the session. The maximum time interval can be set
126      * with the <code>setMaxInactiveInterval</code> method.
127      * A negative time indicates the session should never timeout.
128      *
129      *
130      * @return an integer specifying the number of
131      * seconds this session remains open
132      * between client requests
133      *
134      * @see #setMaxInactiveInterval(int)
135      */

136     public int getMaxInactiveInterval() {
137         return this.session.getMaxInactiveInterval();
138     }
139
140     /**
141      * Returns the object bound with the specified name in this session, or
142      * <code>null</code> if no object is bound under the name.
143      *
144      * @param name a string specifying the name of the object
145      *
146      * @return the object with the specified name
147      *
148      * @exception IllegalStateException if this method is called on an
149      * invalidated session
150      */

151     public Object JavaDoc getAttribute(String JavaDoc name) {
152         if (name.startsWith(APP_SCOPE)) {
153             return this.session.getAttribute(name.substring(APP_SCOPE.length()),
154                                              javax.portlet.PortletSession.APPLICATION_SCOPE);
155         } else if (name.startsWith(PORTLET_SCOPE)) {
156             return this.session.getAttribute(name.substring(PORTLET_SCOPE.length()),
157                                              javax.portlet.PortletSession.PORTLET_SCOPE);
158         } else {
159             return this.session.getAttribute(name, this.scope);
160         }
161     }
162
163     /**
164      * Returns an <code>Enumeration</code> of <code>String</code> objects
165      * containing the names of all the objects bound to this session.
166      *
167      * <p>Objects' names in portlet session scope will be prefixed with
168      * {@link PortletEnvironment#SESSION_PORTLET_SCOPE}, and names in
169      * application scope will be prefixed with
170      * {@link PortletEnvironment#SESSION_APPLICATION_SCOPE}.</p>
171      *
172      * @return an <code>Enumeration</code> of
173      * <code>String</code> objects specifying the
174      * names of all the objects bound to
175      * this session
176      *
177      * @exception IllegalStateException if this method is called on an
178      * invalidated session
179      */

180     public Enumeration JavaDoc getAttributeNames() {
181         final Enumeration JavaDoc names1 = this.session.getAttributeNames(javax.portlet.PortletSession.PORTLET_SCOPE);
182         final Enumeration JavaDoc names2 = this.session.getAttributeNames(javax.portlet.PortletSession.APPLICATION_SCOPE);
183
184         return new Enumeration JavaDoc() {
185             public boolean hasMoreElements() {
186                 return names1.hasMoreElements() || names2.hasMoreElements();
187             }
188
189             public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
190                 if (names1.hasMoreElements()) {
191                     return PORTLET_SCOPE + names1.nextElement();
192                 } else if (names2.hasMoreElements()) {
193                     return APP_SCOPE + names2.nextElement();
194                 }
195
196                 throw new NoSuchElementException JavaDoc();
197             }
198         };
199     }
200
201     /**
202      * Binds an object to this session, using the name specified.
203      * If an object of the same name is already bound to the session,
204      * the object is replaced.
205      *
206      *
207      * @param name the name to which the object is bound;
208      * cannot be null
209      *
210      * @param value the object to be bound; cannot be null
211      *
212      * @exception IllegalStateException if this method is called on an
213      * invalidated session
214      */

215     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
216         if (name.startsWith(APP_SCOPE)) {
217             this.session.setAttribute(name.substring(APP_SCOPE.length()),
218                                       value,
219                                       javax.portlet.PortletSession.APPLICATION_SCOPE);
220         } else if (name.startsWith(PORTLET_SCOPE)) {
221             this.session.setAttribute(name.substring(PORTLET_SCOPE.length()),
222                                       value,
223                                       javax.portlet.PortletSession.PORTLET_SCOPE);
224         } else {
225             this.session.setAttribute(name, value, this.scope);
226         }
227     }
228
229     /**
230      * Removes the object bound with the specified name from
231      * this session. If the session does not have an object
232      * bound with the specified name, this method does nothing.
233      *
234      *
235      * @param name the name of the object to
236      * remove from this session
237      *
238      * @exception IllegalStateException if this method is called on an
239      * invalidated session
240      */

241     public void removeAttribute(String JavaDoc name) {
242         if (name.startsWith(APP_SCOPE)) {
243             this.session.removeAttribute(name.substring(APP_SCOPE.length()),
244                                          javax.portlet.PortletSession.APPLICATION_SCOPE);
245         } else if (name.startsWith(PORTLET_SCOPE)) {
246             this.session.removeAttribute(name.substring(PORTLET_SCOPE.length()),
247                                          javax.portlet.PortletSession.PORTLET_SCOPE);
248         } else {
249             this.session.removeAttribute(name, this.scope);
250         }
251     }
252
253     /**
254      * Invalidates this session to it.
255      *
256      * @exception IllegalStateException if this method is called on an
257      * already invalidated session
258      */

259     public void invalidate() {
260         this.session.invalidate();
261     }
262
263     /**
264      * Returns <code>true</code> if the client does not yet know about the
265      * session or if the client chooses not to join the session. For
266      * example, if the server used only cookie-based sessions, and
267      * the client had disabled the use of cookies, then a session would
268      * be new on each request.
269      *
270      * @return <code>true</code> if the
271      * server has created a session,
272      * but the client has not yet joined
273      *
274      * @exception IllegalStateException if this method is called on an
275      * already invalidated session
276      */

277     public boolean isNew() {
278         return this.session.isNew();
279     }
280 }
281
Popular Tags