1 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 ; 55 import java.util.Enumeration ; 56 import java.util.NoSuchElementException ; 57 import java.util.logging.Logger ; 58 59 62 public class HttpPortletSession implements PortletSession { 63 static protected final Logger log 64 = Logger.getLogger(HttpPortletSession.class.getName()); 65 66 public final String PORTLET_SCOPE_PREFIX = "javax.portlet.p."; 67 public final String PORTLET_SCOPE_RESERVED_PREFIX = "javax.portlet.p.javax.portlet."; 68 69 private PortletContext _portletContext; 70 private HttpSession _httpSession; 71 72 public HttpPortletSession() 73 { 74 } 75 76 public void start( PortletContext portletContext, 77 HttpSession httpSession ) 78 { 79 _portletContext = portletContext; 80 _httpSession = httpSession; 81 82 if (_httpSession != null) { 83 for (Enumeration en = _httpSession.getAttributeNames(); en.hasMoreElements(); ) { 84 String name = (String ) en.nextElement(); 85 Object value = _httpSession.getAttribute(name); 86 } 87 } 88 89 } 90 91 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 getHttpSession() 107 { 108 return _httpSession; 109 } 110 111 private String scopedName(String name, int scope) 112 { 113 if (name == null) 114 throw new IllegalArgumentException ("name is null"); 115 116 if (false) throw new IllegalStateException ("session is invalid"); 118 119 if (scope == APPLICATION_SCOPE) { 120 } 121 else if (scope == PORTLET_SCOPE) { 122 StringBuffer sb = new StringBuffer (PORTLET_SCOPE_PREFIX); 123 sb.append(name); 124 name = sb.toString(); 125 } 126 else { 127 throw new IllegalArgumentException ("invalid scope `" + scope + "'"); 128 } 129 130 return name; 131 } 132 133 public Object getAttribute(String name) 134 { 135 return getAttribute(name,PORTLET_SCOPE); 136 } 137 138 public Object getAttribute(String name,int scope) 139 { 140 return _httpSession.getAttribute(scopedName(name,scope)); 141 } 142 143 public Enumeration getAttributeNames() 144 { 145 return getAttributeNames(PORTLET_SCOPE); 146 } 147 148 public Enumeration getAttributeNames(int scope) 149 { 150 if (scope == APPLICATION_SCOPE) { 151 return _httpSession.getAttributeNames(); 152 } 153 else if (scope == PORTLET_SCOPE) { 154 return new Enumeration () { 155 private Enumeration _e = _httpSession.getAttributeNames(); 156 private Object _next; 157 158 public boolean hasMoreElements() 159 { 160 nextIfNeeded(); 161 return _next != null ? true : false; 162 } 163 164 public Object nextElement() 165 { 166 nextIfNeeded(); 167 if (_next == null) 168 throw new NoSuchElementException (); 169 String result = (String ) _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 e = (String ) _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 ("invalid scope `" + scope + "'"); 193 } 194 } 195 196 public long getCreationTime() 197 { 198 return _httpSession.getCreationTime(); 199 } 200 201 public String 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 name) 227 { 228 removeAttribute(name,PORTLET_SCOPE); 229 } 230 231 public void removeAttribute(String name, int scope) 232 { 233 _httpSession.removeAttribute(scopedName(name,scope)); 234 } 235 236 public void setAttribute(String name, Object value) 237 { 238 setAttribute(name,value,PORTLET_SCOPE); 239 } 240 241 public void setAttribute(String name, Object 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 |