1 9 package org.jboss.portal.portlet.impl; 10 11 import java.util.Enumeration ; 12 13 import javax.portlet.PortletContext; 14 import javax.portlet.PortletSession; 15 import javax.servlet.http.HttpSession ; 16 17 21 public class PortletSessionImpl implements PortletSession 22 { 23 24 private HttpSession session; 25 private String prefix; 26 private PortletContext context; 27 private boolean valid; 28 29 public PortletSessionImpl(HttpSession session, String 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 getAttribute(String s) 38 { 39 return getAttribute(s, PORTLET_SCOPE); 40 } 41 42 public Object getAttribute(String s, int i) 43 { 44 if (!valid) 45 { 46 throw new IllegalStateException ("Session is not valid anymore"); 47 } 48 if (s == null) 49 { 50 throw new IllegalArgumentException ("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 getAttributeNames() 60 { 61 return getAttributeNames(PORTLET_SCOPE); 62 } 63 64 public Enumeration getAttributeNames(int scope) 65 { 66 if (!valid) 67 { 68 throw new IllegalStateException ("Session is not valid anymore"); 69 } 70 final boolean app = scope == APPLICATION_SCOPE; 71 return new Enumeration () 72 { 73 private Enumeration e; 74 private String 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 nextElement() 85 { 86 String result = next; 87 next = null; 88 next(); 89 return result; 90 } 91 private void next() 92 { 93 while (e.hasMoreElements()) 94 { 95 String attribute = (String )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 ("Session is not valid anymore"); 116 } 117 return session.getCreationTime(); 118 } 119 120 public String 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 ("Session is not valid anymore"); 140 } 141 142 session.invalidate(); 144 145 valid = false; 147 } 148 149 public boolean isNew() 150 { 151 if (!valid) 152 { 153 throw new IllegalStateException ("Session is not valid anymore"); 154 } 155 return false; 156 } 157 158 public void removeAttribute(String s) 159 { 160 removeAttribute(s, PORTLET_SCOPE); 161 } 162 163 public void removeAttribute(String s, int i) 164 { 165 if (!valid) 166 { 167 throw new IllegalStateException ("Session is not valid anymore"); 168 } 169 if (s == null) 170 { 171 throw new IllegalArgumentException ("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 s, Object o) 181 { 182 setAttribute(s, o, PORTLET_SCOPE); 183 } 184 185 public void setAttribute(String s, Object o, int i) 186 { 187 if (!valid) 188 { 189 throw new IllegalStateException ("Session is not valid anymore"); 190 } 191 if (s == null) 192 { 193 throw new IllegalArgumentException ("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 215 HttpSession getSession() 216 { 217 return session; 218 } 219 }; 220 | Popular Tags |