44
55import java .util .*;
66import java .util .concurrent .ConcurrentHashMap ;
7+ import java .util .concurrent .atomic .AtomicInteger ;
78
89public class SessionImpl implements WxSession , InternalSession {
910
@@ -90,11 +91,6 @@ public void invalidate() {
9091 */
9192 protected volatile boolean isValid = false ;
9293
93- /**
94- * Flag indicating whether this session is new or not.
95- */
96- protected boolean isNew = false ;
97-
9894 /**
9995 * We are currently processing a session expiration, so bypass
10096 * certain IllegalStateException tests. NOTE: This value is not
@@ -123,11 +119,6 @@ public void invalidate() {
123119 */
124120 protected volatile long thisAccessedTime = creationTime ;
125121
126- /**
127- * The last accessed time for this Session.
128- */
129- protected volatile long lastAccessedTime = creationTime ;
130-
131122 /**
132123 * The default maximum inactive interval for Sessions created by
133124 * this Manager.
@@ -140,9 +131,15 @@ public void invalidate() {
140131 */
141132 protected transient InternalSessionFacade facade = null ;
142133
134+ /**
135+ * The access count for this session.
136+ */
137+ protected transient AtomicInteger accessCount = null ;
138+
143139
144140 public SessionImpl (InternalSessionManager manager ) {
145141 this .manager = manager ;
142+ this .accessCount = new AtomicInteger ();
146143 }
147144
148145
@@ -176,7 +173,28 @@ public void setValid(boolean isValid) {
176173
177174 @ Override
178175 public boolean isValid () {
179- return isValid ;
176+ if (!this .isValid ) {
177+ return false ;
178+ }
179+
180+ if (this .expiring ) {
181+ return true ;
182+ }
183+
184+ if (accessCount .get () > 0 ) {
185+ return true ;
186+ }
187+
188+ if (maxInactiveInterval > 0 ) {
189+ long timeNow = System .currentTimeMillis ();
190+ int timeIdle ;
191+ timeIdle = (int ) ((timeNow - thisAccessedTime ) / 1000L );
192+ if (timeIdle >= maxInactiveInterval ) {
193+ expire ();
194+ }
195+ }
196+
197+ return this .isValid ;
180198 }
181199
182200 @ Override
@@ -215,6 +233,8 @@ public void expire() {
215233 // Mark this session as "being expired"
216234 expiring = true ;
217235
236+ accessCount .set (0 );
237+
218238 // Remove this session from our manager's active sessions
219239 manager .remove (this , true );
220240
@@ -238,23 +258,23 @@ public void expire() {
238258 public void access () {
239259
240260 this .thisAccessedTime = System .currentTimeMillis ();
261+ accessCount .incrementAndGet ();
241262
242263 }
243264
244265
245266 @ Override
246- public void setNew ( boolean isNew ) {
267+ public void endAccess ( ) {
247268
248- this .isNew = isNew ;
269+ this .thisAccessedTime = System .currentTimeMillis ();
270+ accessCount .decrementAndGet ();
249271
250272 }
251273
252-
253274 @ Override
254275 public void setCreationTime (long time ) {
255276
256277 this .creationTime = time ;
257- this .lastAccessedTime = time ;
258278 this .thisAccessedTime = time ;
259279
260280 }
0 commit comments