1 5 6 13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp; 14 15 16 import java.util.Enumeration ; 17 import java.util.Vector ; 18 19 import javax.portlet.PortletContext; 20 import javax.portlet.PortletSession; 21 import javax.portlet.PortletSessionUtil; 22 import javax.servlet.http.HttpSession ; 23 24 import org.apache.commons.logging.Log; 25 import org.exoplatform.commons.map.AbstractMap; 26 import org.exoplatform.container.PortalContainer; 27 import org.exoplatform.services.log.LogService; 28 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.utils.PortletSessionImpUtil; 29 30 public class PortletSessionImp extends AbstractMap implements PortletSession { 31 32 private HttpSession session; 33 private PortletContext context; 34 private String windowId; 35 private String applicationId ; 36 private boolean invalidated; 37 private Log log; 38 39 public PortletSessionImp() { 40 this.log = ((LogService)PortalContainer.getInstance().getComponentInstanceOfType(LogService.class)). 41 getLog("org.exoplatform.services.portletcontainer"); 42 } 43 44 public void fillPortletSession(HttpSession session, 45 PortletContext context, 46 String windowId) { 47 this.session = session; 48 this.context = context; 49 this.windowId = windowId; 50 this.applicationId = context.getPortletContextName() ; 51 invalidated = false ; 52 } 53 54 public void emptyPortletSession() { 55 this.windowId = ""; 56 } 57 58 public Object getAttribute(String name) { 59 return getAttribute(name, PortletSession.PORTLET_SCOPE); 60 } 61 62 public Object getAttribute(String name, int i) { 63 if(invalidated) 64 throw new IllegalStateException ("session invalidated"); 65 if (name == null) { 66 throw new IllegalArgumentException ("The attribute name cannot be null") ; 67 } 68 if (PortletSession.APPLICATION_SCOPE == i) { 69 return session.getAttribute(name); 70 } else if (PortletSession.PORTLET_SCOPE == i) { 71 String key = PortletSessionImpUtil.encodePortletSessionAttribute(windowId, name, PortletSession.PORTLET_SCOPE) ; 72 return session.getAttribute(key); 73 } 74 return null; 75 } 76 77 public void removeAttribute(String name) { 78 removeAttribute(name, PortletSession.PORTLET_SCOPE); 79 } 80 81 public void removeAttribute(String name, int i) { 82 if(invalidated) 83 throw new IllegalStateException ("session invalidated"); 84 if (name == null) { 85 throw new IllegalArgumentException ("The attribute name cannot be null") ; 86 } 87 if (PortletSession.APPLICATION_SCOPE == i) { 88 session.removeAttribute(name); 89 } else if (PortletSession.PORTLET_SCOPE == i) { 90 String key = PortletSessionImpUtil.encodePortletSessionAttribute(windowId, name, PortletSession.PORTLET_SCOPE) ; 91 session.removeAttribute(key); 92 } 93 } 94 95 final public void setAttribute(String name, Object o) { 96 setAttribute(name, o, PortletSession.PORTLET_SCOPE); 97 } 98 99 public void setAttribute(String name, Object o, int i) { 100 if(invalidated) 101 throw new IllegalStateException ("session invalidated"); 102 if (name == null) { 103 throw new IllegalArgumentException ("The attribute name cannot be null") ; 104 } 105 if (PortletSession.APPLICATION_SCOPE == i) { 106 if (o == null) session.removeAttribute(name); 107 else session.setAttribute(name, o); 108 } else if (PortletSession.PORTLET_SCOPE == i) { 109 String key = PortletSessionImpUtil.encodePortletSessionAttribute(windowId, name, PortletSession.PORTLET_SCOPE) ; 110 if (o == null) session.removeAttribute(key); 111 else session.setAttribute(key, o); 112 } 113 } 114 115 public Enumeration getAttributeNames() { 116 return getAttributeNames(PortletSession.PORTLET_SCOPE); 117 } 118 119 public Enumeration getAttributeNames(int i) { 120 if(invalidated) 121 throw new IllegalStateException ("session invalidated"); 122 Enumeration e = session.getAttributeNames(); 123 Vector v = new Vector (); 124 while (e.hasMoreElements()) { 125 String s = (String ) e.nextElement(); 126 if(i == PortletSession.PORTLET_SCOPE){ 127 if (PortletSessionUtil.decodeScope(s) == PortletSession.PORTLET_SCOPE) 128 v.add(PortletSessionUtil.decodeAttributeName(s)); 129 } else { 130 if (PortletSessionUtil.decodeScope(s) == PortletSession.APPLICATION_SCOPE) 131 v.add(s); 132 } 133 } 134 return v.elements(); 135 } 136 137 138 public long getCreationTime() { 139 if(invalidated) 140 throw new IllegalStateException ("session invalidated"); 141 return session.getCreationTime(); 142 } 143 144 public String getId() { 145 if(invalidated) 146 throw new IllegalStateException ("session invalidated"); 147 return session.getId() + applicationId; 148 } 149 150 public long getLastAccessedTime() { 151 if(invalidated) 152 throw new IllegalStateException ("session invalidated"); 153 return session.getLastAccessedTime(); 154 } 155 156 public int getMaxInactiveInterval() { 157 if(invalidated) 158 throw new IllegalStateException ("session invalidated"); 159 return session.getMaxInactiveInterval(); 160 } 161 162 public void invalidate() { 163 if(invalidated) 164 throw new IllegalStateException ("session invalidated"); 165 session.invalidate(); 166 invalidated = true; 167 } 168 169 public boolean isSessionValid() { 170 try{ 171 long lastAccessTime = session.getLastAccessedTime(); 172 if(lastAccessTime == 0) 174 return true; 175 if(lastAccessTime == -1) 177 return false; 178 int maxInterval = session.getMaxInactiveInterval(); 179 if(maxInterval < 0) 180 return true; 181 if((System.currentTimeMillis() - lastAccessTime) > (maxInterval * 1000)){ 182 session.invalidate(); 183 return false; 184 } 185 return true; 186 } catch(IllegalStateException e){ 187 log.error("IllegalStateException in PortletSessionImp for isSessionValid()", e); 188 return false; 189 } 190 } 191 192 public boolean isNew() { 193 if(invalidated) 194 throw new IllegalStateException ("session invalidated"); 195 return session.isNew(); 196 } 197 198 public void setMaxInactiveInterval(int i) { 199 if(invalidated) 200 throw new IllegalStateException ("session invalidated"); 201 session.setMaxInactiveInterval(i); 202 } 203 204 public PortletContext getPortletContext() { 205 return context; 206 } 207 208 public HttpSession getSession() { 209 return session; 210 } 211 212 public void setSession(HttpSession session) { 213 this.session = session; 214 } 215 216 } 217 | Popular Tags |