1 7 package org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers; 8 9 import java.util.ArrayList ; 10 import java.util.Enumeration ; 11 import java.util.Vector ; 12 13 import javax.servlet.http.HttpSession ; 14 import javax.servlet.http.HttpSessionBindingEvent ; 15 import javax.servlet.http.HttpSessionBindingListener ; 16 17 21 public class SharedSessionWrapper extends HttpSessionWrapper{ 22 23 private static String CREATION_TIME = "javax.portlet.creation-time"; 24 private static String LAST_ACCESS_TIME = "javax.portlet.last-access-time"; 25 private static String MAXIMUM_INACTIVE_INTERVAL = "javax.portlet.max-inactive-interval"; 26 27 private static int defaultMaximalInterval = 900; 29 private String applicationId; 30 31 public SharedSessionWrapper(HttpSession session) { 32 super(session); 33 } 34 35 public void fillSharedSessionWrapper(HttpSession session, 36 String applicationId) { 37 super.setSession(session); 38 this.applicationId = applicationId; 39 } 40 41 public void emptySharedSessionWrapper() { 42 this.applicationId = null; 43 } 44 45 public void init(){ 46 String attCreation = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, CREATION_TIME); 48 String attLast = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, LAST_ACCESS_TIME); 49 if (super.getAttribute(attCreation) == null) { 50 String maxInterval = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, MAXIMUM_INACTIVE_INTERVAL); 51 super.setAttribute(attCreation, new Long (System.currentTimeMillis())); 52 super.setAttribute(attLast, new Long (System.currentTimeMillis())); 53 super.setAttribute(maxInterval, new Integer (defaultMaximalInterval)); 54 } 55 } 59 60 public Object getAttribute(String name) { 61 setLastAccessTime(); 62 String key = SharedSessionUtil.encodePortletSessionAttribute(applicationId, name); 63 Object value = super.getAttribute(key); 64 if(value instanceof HttpSessionBindingListenerProxy){ 65 return((HttpSessionBindingListenerProxy)value).getListener(); 66 } 67 return value; 68 } 69 70 public Enumeration getAttributeNames() { 71 setLastAccessTime(); 72 Enumeration e = super.getAttributeNames(); 73 Vector v = new Vector (); 74 while (e.hasMoreElements()) { 75 String s = (String ) e.nextElement(); 76 if (!SharedSessionUtil.isMetaDataAttribute(applicationId, s) 77 && SharedSessionUtil.isAttribute(applicationId, s)) { 78 s = SharedSessionUtil.decodePortletSessionAttribute(applicationId, s); 79 v.add(s); 80 } 81 } 82 return v.elements(); 83 } 84 85 public void removeAttribute(String name) { 86 String key = SharedSessionUtil.encodePortletSessionAttribute(applicationId, name); 87 if(super.getAttribute(key) instanceof HttpSessionBindingListenerProxy){ 88 HttpSessionBindingListener listener = ((HttpSessionBindingListenerProxy)super.getAttribute(key)).getListener(); 89 listener.valueUnbound(new HttpSessionBindingEvent (this, name)); 90 } 91 super.removeAttribute(key); 92 setLastAccessTime(); 93 } 94 95 public void removeValue(String name) { 96 } 97 98 public void setAttribute(String name, Object value) { 99 if(value instanceof HttpSessionBindingListener ){ 100 HttpSessionBindingListener listener = (HttpSessionBindingListener )value; 101 listener.valueBound(new HttpSessionBindingEvent (this, name)); 102 value = new HttpSessionBindingListenerProxy(listener); 103 } 104 String key = SharedSessionUtil.encodePortletSessionAttribute(applicationId, name); 105 super.setAttribute(key, value); 106 setLastAccessTime(); 107 } 108 109 public long getCreationTime() { 110 setLastAccessTime(); 111 String key = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, 112 CREATION_TIME); 113 Long creationTime = (Long ) super.getAttribute(key); 114 return creationTime.longValue(); 115 } 116 117 public int getMaxInactiveInterval() { 118 setLastAccessTime(); 119 String key = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, 120 MAXIMUM_INACTIVE_INTERVAL); 121 Integer maxInactivInterval = (Integer ) super.getAttribute(key); 122 return maxInactivInterval.intValue(); 123 } 124 125 public void invalidate() { 126 Enumeration e = super.getAttributeNames(); 127 ArrayList list = new ArrayList (15) ; 128 while (e.hasMoreElements()) { 129 String s = (String ) e.nextElement(); 130 if (SharedSessionUtil.isMetaDataAttribute(applicationId, s) 131 || SharedSessionUtil.isAttribute(applicationId, s)) { 132 list.add(s); 133 } 134 } 135 for (int i = 0 ; i < list.size(); i++) { 136 String name = (String ) list.get(i) ; 137 super.removeAttribute(name); 138 } 139 } 140 141 public boolean isNew() { 142 setLastAccessTime(); 143 return false; 144 } 145 146 public void setMaxInactiveInterval(int i) { 147 String key = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, 148 MAXIMUM_INACTIVE_INTERVAL); 149 super.setAttribute(key, new Integer (i)); 150 setLastAccessTime(); 151 } 152 153 private void setLastAccessTime(){ 154 String attLast = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, 155 LAST_ACCESS_TIME); 156 super.setAttribute(attLast, new Long (System.currentTimeMillis())); 157 } 158 159 public long getLastAccessedTime() { 160 String key = SharedSessionUtil.encodePortletSessionMetaDataAttribute(applicationId, 161 LAST_ACCESS_TIME); 162 Long lastAccessTime = (Long ) super.getAttribute(key); 163 if(lastAccessTime == null) 164 return -1; 165 return lastAccessTime.longValue(); 166 } 167 168 } 169 | Popular Tags |