KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > HttpPortletSession


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49
50 package com.caucho.portal.generic;
51
52 import javax.portlet.PortletContext;
53 import javax.portlet.PortletSession;
54 import javax.servlet.http.HttpSession JavaDoc;
55 import java.util.Enumeration JavaDoc;
56 import java.util.NoSuchElementException JavaDoc;
57 import java.util.logging.Logger JavaDoc;
58
59 /**
60  * An adapter to a {@link javax.servlet.HttpSession}.
61  */

62 public class HttpPortletSession implements PortletSession {
63   static protected final Logger JavaDoc log
64     = Logger.getLogger(HttpPortletSession.class.getName());
65
66   public final String JavaDoc PORTLET_SCOPE_PREFIX = "javax.portlet.p.";
67   public final String JavaDoc PORTLET_SCOPE_RESERVED_PREFIX = "javax.portlet.p.javax.portlet.";
68
69   private PortletContext _portletContext;
70   private HttpSession JavaDoc _httpSession;
71   
72   public HttpPortletSession()
73   {
74   }
75
76   public void start( PortletContext portletContext,
77                      HttpSession JavaDoc httpSession )
78   {
79     _portletContext = portletContext;
80     _httpSession = httpSession;
81
82     if (_httpSession != null) {
83       for (Enumeration JavaDoc en = _httpSession.getAttributeNames(); en.hasMoreElements(); ) {
84         String JavaDoc name = (String JavaDoc) en.nextElement();
85         Object JavaDoc value = _httpSession.getAttribute(name);
86       }
87     }
88
89   }
90
91   /**
92    * Finish with the HttpSession object. After calling this method,
93    * this object can be placed into a pool and reused for subsequent requests.
94    */

95   public void finish()
96   {
97     _httpSession = null;
98     _portletContext = null;
99   }
100
101   public PortletContext getPortletContext()
102   {
103     return _portletContext;
104   }
105
106   public HttpSession JavaDoc getHttpSession()
107   {
108     return _httpSession;
109   }
110
111   private String JavaDoc scopedName(String JavaDoc name, int scope)
112   {
113     if (name == null)
114       throw new IllegalArgumentException JavaDoc("name is null");
115
116     if (false) // XXX: isInvalid() might be handled by HttpSession
117
throw new IllegalStateException JavaDoc("session is invalid");
118
119     if (scope == APPLICATION_SCOPE) {
120     }
121     else if (scope == PORTLET_SCOPE) {
122       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(PORTLET_SCOPE_PREFIX);
123       sb.append(name);
124       name = sb.toString();
125     }
126     else {
127       throw new IllegalArgumentException JavaDoc("invalid scope `" + scope + "'");
128     }
129
130     return name;
131   }
132
133   public Object JavaDoc getAttribute(String JavaDoc name)
134   {
135     return getAttribute(name,PORTLET_SCOPE);
136   }
137
138   public Object JavaDoc getAttribute(String JavaDoc name,int scope)
139   {
140     return _httpSession.getAttribute(scopedName(name,scope));
141   }
142
143   public Enumeration JavaDoc getAttributeNames()
144   {
145     return getAttributeNames(PORTLET_SCOPE);
146   }
147
148   public Enumeration JavaDoc getAttributeNames(int scope)
149   {
150     if (scope == APPLICATION_SCOPE) {
151       return _httpSession.getAttributeNames();
152     }
153     else if (scope == PORTLET_SCOPE) {
154       return new Enumeration JavaDoc() {
155         private Enumeration JavaDoc _e = _httpSession.getAttributeNames();
156         private Object JavaDoc _next;
157
158         public boolean hasMoreElements()
159         {
160           nextIfNeeded();
161           return _next != null ? true : false;
162         }
163
164         public Object JavaDoc nextElement()
165         {
166           nextIfNeeded();
167           if (_next == null)
168             throw new NoSuchElementException JavaDoc();
169           String JavaDoc result = (String JavaDoc) _next;
170           result = result.substring(PORTLET_SCOPE_PREFIX.length());
171           _next = null;
172           return result;
173         }
174
175         private void nextIfNeeded()
176         {
177           if (_next == null) {
178             while (_e.hasMoreElements()) {
179               String JavaDoc e = (String JavaDoc) _e.nextElement();
180               if (e.startsWith(PORTLET_SCOPE_PREFIX)) {
181                 if (!e.startsWith(PORTLET_SCOPE_RESERVED_PREFIX)) {
182                   _next = e;
183                   break;
184                 }
185               }
186             }
187           }
188         }
189       };
190     }
191     else {
192       throw new IllegalArgumentException JavaDoc("invalid scope `" + scope + "'");
193     }
194   }
195
196   public long getCreationTime()
197   {
198     return _httpSession.getCreationTime();
199   }
200
201   public String JavaDoc getId()
202   {
203     return _httpSession.getId();
204   }
205
206   public long getLastAccessedTime()
207   {
208     return _httpSession.getLastAccessedTime();
209   }
210
211   public int getMaxInactiveInterval()
212   {
213     return _httpSession.getMaxInactiveInterval();
214   }
215
216   public void invalidate()
217   {
218     _httpSession.invalidate();
219   }
220
221   public boolean isNew()
222   {
223     return _httpSession.isNew();
224   }
225
226   public void removeAttribute(String JavaDoc name)
227   {
228     removeAttribute(name,PORTLET_SCOPE);
229   }
230
231   public void removeAttribute(String JavaDoc name, int scope)
232   {
233     _httpSession.removeAttribute(scopedName(name,scope));
234   }
235
236   public void setAttribute(String JavaDoc name, Object JavaDoc value)
237   {
238     setAttribute(name,value,PORTLET_SCOPE);
239   }
240
241   public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope)
242   {
243     _httpSession.setAttribute(scopedName(name,scope),value);
244   }
245
246   public void setMaxInactiveInterval(int interval)
247   {
248     _httpSession.setMaxInactiveInterval(interval);
249   }
250
251 }
252
253
Popular Tags