This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit ee7c03b2f1105cb5aadbf70a7a9b04bbfe3e8710
Author: Alex Heneveld <[email protected]>
AuthorDate: Tue Mar 30 22:27:01 2021 +0100

    prevent warning which can appear on session lookup during initialization
---
 .../rest/util/MultiSessionAttributeAdapter.java    | 26 +++++++++++++++-------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/util/MultiSessionAttributeAdapter.java
 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/util/MultiSessionAttributeAdapter.java
index 8fc8a32..aa6b635 100644
--- 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/util/MultiSessionAttributeAdapter.java
+++ 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/util/MultiSessionAttributeAdapter.java
@@ -204,7 +204,7 @@ public class MultiSessionAttributeAdapter {
             List<String> invalidatedSessions = new ArrayList<>();
             if (handlers!=null) {
                 for (Handler h: handlers) {
-                    Session session = 
((SessionHandler)h).getSession(preferredSession.getId());
+                    Session session = getSessionSafely(h, 
preferredSession.getId());
                     if (session!=null) {
                         invalidatedSessions.add(session.getId());
                         session.invalidate();
@@ -225,7 +225,7 @@ public class MultiSessionAttributeAdapter {
                     String extendedId= localSession.getId();
                     SessionIdManager idManager = 
preferredHandler.getSessionIdManager();
                     String id = idManager.getId(extendedId);
-                    preferredSession = preferredHandler.getSession(id);
+                    preferredSession = getSessionSafely(preferredHandler, id);
                     if (preferredSession != null && 
!((Session)preferredSession).getExtendedId().equals(extendedId))
                         ((Session)preferredSession).setIdChanged(true);
                 }
@@ -272,7 +272,7 @@ public class MultiSessionAttributeAdapter {
                 // does the server have a globally preferred handler
                 SessionHandler preferredServerGlobalSessionHandler = 
getServerGlobalPreferredHandler(server);
                 if (preferredServerGlobalSessionHandler!=null) {
-                    sessionAtServerGlobalPreferredHandler = 
preferredServerGlobalSessionHandler.getSession(localSession.getId());
+                    sessionAtServerGlobalPreferredHandler = 
getSessionSafely(preferredServerGlobalSessionHandler, localSession.getId());
                     if (sessionAtServerGlobalPreferredHandler!=null && 
Boolean.TRUE.equals( 
sessionAtServerGlobalPreferredHandler.getAttribute(KEY_IS_PREFERRED)) ) {
                         return preferredServerGlobalSessionHandler;
                     }
@@ -359,7 +359,7 @@ public class MultiSessionAttributeAdapter {
             if (handlers != null) {
                 for (Handler h: handlers) {
                     SessionHandler sh = (SessionHandler)h;
-                    Session sessionHere = sh.getSession(localSessionId);
+                    Session sessionHere = getSessionSafely(sh, localSessionId);
                     if (sessionHere!=null) {
                         if 
(Boolean.TRUE.equals(sessionHere.getAttribute(KEY_IS_PREFERRED))) {
                             if (preferredHandler!=null) {
@@ -550,7 +550,7 @@ public class MultiSessionAttributeAdapter {
             Handler[] hh = getSessionHandlers();
             if (hh!=null) {
                 for (Handler h: hh) {
-                    Session ss = 
((SessionHandler)h).getSession(localSession.getId());
+                    Session ss = getSessionSafely(h, localSession.getId());
                     if (ss!=null) {
                         ss.setAttribute(name, value);
                     }
@@ -574,7 +574,7 @@ public class MultiSessionAttributeAdapter {
             Handler[] hh = getSessionHandlers();
             if (hh!=null) {
                 for (Handler h: hh) {
-                    Session ss = 
((SessionHandler)h).getSession(localSession.getId());
+                    Session ss = getSessionSafely(h, localSession.getId());
                     if (ss!=null) {
                         ss.removeAttribute(name);
                     }
@@ -638,12 +638,22 @@ public class MultiSessionAttributeAdapter {
         Handler[] hh = getSessionHandlers();
         if (hh!=null) {
             for (Handler h: hh) {
-                Session ss = ((SessionHandler)h).getSession(getId());
-                if (ss!=null) {
+                Session ss = getSessionSafely(h, getId());
+                if (ss != null) {
                     ss.setMaxInactiveInterval(maxInativeInterval);
                 }
             }
         }
         return this;
     }
+
+    private static Session getSessionSafely(Handler h, String id) {
+        if (((SessionHandler)h).getSessionCache()==null) {
+            // suppress the log warning that the call to getSession can 
trigger, if racing during startup
+            log.debug("Skipping session reset expiration for "+id+" on "+h+" 
because session cache not initialized (yet)");
+            return null;
+        } else {
+            return ((SessionHandler) h).getSession(id);
+        }
+    }
 }

Reply via email to