From fe5dad85f2cf541738df580708106a9cb3c1df65 Mon Sep 17 00:00:00 2001 From: "David M. Johnson" Date: Mon, 20 Jan 2025 10:37:04 -0500 Subject: [PATCH] Logging improvements --- .../ui/core/RollerSessionManager.java | 51 ++++++++++++------- .../ui/struts2/util/UIBeanFactory.java | 9 +++- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSessionManager.java b/app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSessionManager.java index bcbc45d6f..29703addc 100644 --- a/app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSessionManager.java +++ b/app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSessionManager.java @@ -28,48 +28,63 @@ import java.util.HashMap; import java.util.Map; - public class RollerSessionManager implements SessionManager { private static final Log log = LogFactory.getLog(RollerSessionManager.class); private static final String CACHE_ID = "roller.session.cache"; private final Cache sessionCache; - public class SessionCacheHandler extends CacheHandlerAdapter { - @Override - public void invalidate(User user) { - if (user != null && user.getUserName() != null) { - sessionCache.remove(user.getUserName()); - } - } - } - public RollerSessionManager() { Map cacheProps = new HashMap<>(); cacheProps.put("id", CACHE_ID); this.sessionCache = CacheManager.constructCache(null, cacheProps); - SessionCacheHandler cacheHandler = new SessionCacheHandler(); - CacheManager.registerHandler(cacheHandler); + CacheManager.registerHandler(new SessionCacheHandler()); } public void register(String userName, RollerSession session) { if (userName != null && session != null) { - this.sessionCache.put(userName, session); - log.debug("Registered session for user: " + userName); + try { + this.sessionCache.put(userName, session); + log.debug("Registered session for user: " + userName); + } catch (Exception e) { + log.error("Failed to register session for user: " + userName, e); + } } } public RollerSession get(String userName) { if (userName != null) { - return (RollerSession) this.sessionCache.get(userName); + try { + return (RollerSession) this.sessionCache.get(userName); + } catch (Exception e) { + log.error("Failed to retrieve session for user: " + userName, e); + } } return null; } public void invalidate(String userName) { if (userName != null) { - this.sessionCache.remove(userName); - log.debug("Invalidated session for user: " + userName); + try { + this.sessionCache.remove(userName); + log.debug("Invalidated session for user: " + userName); + } catch (Exception e) { + log.error("Failed to invalidate session for user: " + userName, e); + } + } + } + + class SessionCacheHandler extends CacheHandlerAdapter { + @Override + public void invalidate(User user) { + if (user != null && user.getUserName() != null) { + try { + sessionCache.remove(user.getUserName()); + log.debug("Cache handler invalidated session for user: " + user.getUserName()); + } catch (Exception e) { + log.error("Cache handler failed to invalidate session for user: " + user.getUserName(), e); + } + } } } -} +} \ No newline at end of file diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIBeanFactory.java b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIBeanFactory.java index efbe6cf7d..2adebbce5 100644 --- a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIBeanFactory.java +++ b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIBeanFactory.java @@ -34,9 +34,14 @@ public static T getBean(Class beanClass) throws ServletException { .getContainer() .getInstance(ObjectFactory.class); return (T) objectFactory.buildBean(beanClass, null); + } catch (NullPointerException e) { + String msg = "Struts context not initialized for bean type: " + beanClass.getName(); + log.error(msg, e); + throw new ServletException(msg, e); } catch (Exception e) { - log.error("Failed to create bean of type " + beanClass.getName(), e); - throw new ServletException("Failed to create bean of type " + beanClass.getName(), e); + String msg = String.format("Failed to create bean of type %s: %s", beanClass.getName(), e.getMessage()); + log.error(msg, e); + throw new ServletException(msg, e); } } } \ No newline at end of file