From 761c570cad87432d0be557c749c30d289fd528b7 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Thu, 15 Dec 2016 16:13:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E7=81=AD=E4=B8=8D=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=88=E7=94=B1sonatype=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E5=87=BA=E6=9D=A5=E7=9A=84=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/session/StandardSession.java | 110 +++++++++++------- 1 file changed, 69 insertions(+), 41 deletions(-) diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java index dc2a0105..f9d61707 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java @@ -11,12 +11,12 @@ public class StandardSession implements WxSession, InternalSession { /** * The string manager for this package. */ - protected static final StringManager sm = - StringManager.getManager(Constants.Package); + protected static final StringManager sm = StringManager.getManager(Constants.Package); /** * Type array. */ - protected static final String EMPTY_ARRAY[] = new String[0]; + private static final String[] EMPTY_ARRAY = new String[0]; + // ------------------------------ WxSession protected Map attributes = new ConcurrentHashMap<>(); /** @@ -71,20 +71,23 @@ public class StandardSession implements WxSession, InternalSession { @Override public Object getAttribute(String name) { - if (!isValidInternal()) + if (!isValidInternal()) { throw new IllegalStateException (sm.getString("sessionImpl.getAttribute.ise")); + } - if (name == null) return null; + if (name == null) { + return null; + } - return (this.attributes.get(name)); + return this.attributes.get(name); } @Override public Enumeration getAttributeNames() { - if (!isValidInternal()) - throw new IllegalStateException - (sm.getString("sessionImpl.getAttributeNames.ise")); + if (!isValidInternal()) { + throw new IllegalStateException(sm.getString("sessionImpl.getAttributeNames.ise")); + } Set names = new HashSet<>(); names.addAll(this.attributes.keySet()); @@ -94,9 +97,9 @@ public class StandardSession implements WxSession, InternalSession { @Override public void setAttribute(String name, Object value) { // Name cannot be null - if (name == null) - throw new IllegalArgumentException - (sm.getString("sessionImpl.setAttribute.namenull")); + if (name == null) { + throw new IllegalArgumentException(sm.getString("sessionImpl.setAttribute.namenull")); + } // Null value is the same as removeAttribute() if (value == null) { @@ -105,9 +108,9 @@ public class StandardSession implements WxSession, InternalSession { } // Validate our current state - if (!isValidInternal()) - throw new IllegalStateException(sm.getString( - "sessionImpl.setAttribute.ise", getIdInternal())); + if (!isValidInternal()) { + throw new IllegalStateException(sm.getString("sessionImpl.setAttribute.ise", getIdInternal())); + } this.attributes.put(name, value); @@ -121,8 +124,7 @@ public class StandardSession implements WxSession, InternalSession { @Override public void invalidate() { if (!isValidInternal()) - throw new IllegalStateException - (sm.getString("sessionImpl.invalidate.ise")); + throw new IllegalStateException(sm.getString("sessionImpl.invalidate.ise")); // Cause this session to expire expire(); @@ -131,12 +133,11 @@ public class StandardSession implements WxSession, InternalSession { @Override public WxSession getSession() { - if (this.facade == null) { this.facade = new StandardSessionFacade(this); } - return (this.facade); + return this.facade; } /** @@ -185,12 +186,14 @@ public class StandardSession implements WxSession, InternalSession { @Override public String getIdInternal() { - return (this.id); + return this.id; } protected void removeAttributeInternal(String name) { // Avoid NPE - if (name == null) return; + if (name == null) { + return; + } // Remove this attribute from our collection this.attributes.remove(name); @@ -202,19 +205,22 @@ public class StandardSession implements WxSession, InternalSession { // Check to see if session has already been invalidated. // Do not check expiring at this point as expire should not return until // isValid is false - if (!this.isValid) + if (!this.isValid) { return; + } synchronized (this) { // Check again, now we are inside the sync so this code only runs once // Double check locking - isValid needs to be volatile // The check of expiring is to ensure that an infinite loop is not // entered as per bug 56339 - if (this.expiring || !this.isValid) + if (this.expiring || !this.isValid) { return; + } - if (this.manager == null) + if (this.manager == null) { return; + } // Mark this session as "being expired" this.expiring = true; @@ -230,9 +236,9 @@ public class StandardSession implements WxSession, InternalSession { this.expiring = false; // Unbind any objects associated with this session - String keys[] = keys(); - for (int i = 0; i < keys.length; i++) { - removeAttributeInternal(keys[i]); + String[] keys = keys(); + for (String key : keys) { + removeAttributeInternal(key); } } @@ -273,13 +279,15 @@ public class StandardSession implements WxSession, InternalSession { @Override public void setId(String id) { - if ((this.id != null) && (this.manager != null)) + if ((this.id != null) && (this.manager != null)) { this.manager.remove(this); + } this.id = id; - if (this.manager != null) + if (this.manager != null) { this.manager.add(this); + } } /** @@ -295,21 +303,41 @@ public class StandardSession implements WxSession, InternalSession { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof StandardSession)) return false; + if (this == o) { + return true; + } + if (!(o instanceof StandardSession)) { + return false; + } StandardSession session = (StandardSession) o; - if (this.creationTime != session.creationTime) return false; - if (this.expiring != session.expiring) return false; - if (this.isValid != session.isValid) return false; - if (this.maxInactiveInterval != session.maxInactiveInterval) return false; - if (this.thisAccessedTime != session.thisAccessedTime) return false; - if (this.accessCount.get() != session.accessCount.get()) return false; - if (!this.attributes.equals(session.attributes)) return false; - if (!this.facade.equals(session.facade)) return false; - if (!this.id.equals(session.id)) return false; - return this.manager.equals(session.manager); + if (this.creationTime != session.creationTime) { + return false; + } + if (this.expiring != session.expiring) { + return false; + } + if (this.isValid != session.isValid) { + return false; + } + if (this.maxInactiveInterval != session.maxInactiveInterval) { + return false; + } + if (this.thisAccessedTime != session.thisAccessedTime) { + return false; + } + if (this.accessCount.get() != session.accessCount.get()) { + return false; + } + if (!this.attributes.equals(session.attributes)) { + return false; + } + if (!this.facade.equals(session.facade)) { + return false; + } + + return this.id.equals(session.id) && this.manager.equals(session.manager); }