Skip to content

Commit

Permalink
Logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopdave committed Jan 20, 2025
1 parent b5b4d28 commit fe5dad8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ public static <T> T getBean(Class<T> 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);
}
}
}

0 comments on commit fe5dad8

Please sign in to comment.