From 264bc9efde67899b50774fb4e06cbced6cc4aa41 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Tue, 19 Nov 2019 11:51:51 +0530 Subject: [PATCH] switched to beanutil to get beans outside spring handled classes --- .../java/org/ros/chatto/logged/BeanUtil.java | 28 ++++++++++++++ .../org/ros/chatto/logged/LoggedUser.java | 38 ++++++------------- 2 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 chatto/src/main/java/org/ros/chatto/logged/BeanUtil.java diff --git a/chatto/src/main/java/org/ros/chatto/logged/BeanUtil.java b/chatto/src/main/java/org/ros/chatto/logged/BeanUtil.java new file mode 100644 index 0000000..5e6b260 --- /dev/null +++ b/chatto/src/main/java/org/ros/chatto/logged/BeanUtil.java @@ -0,0 +1,28 @@ +package org.ros.chatto.logged; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Service; + +@Service + +public class BeanUtil implements ApplicationContextAware { + + private static ApplicationContext context; + + @Override + + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + + context = applicationContext; + + } + + public static T getBean(Class beanClass) { + + return context.getBean(beanClass); + + } + +} \ No newline at end of file diff --git a/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java b/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java index dfef103..ea2dfd5 100644 --- a/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java +++ b/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java @@ -27,17 +27,19 @@ public class LoggedUser implements HttpSessionBindingListener { private String username; - public LoggedUser(String username) - { + public LoggedUser(String username) { this.username = username; } + public LoggedUser() { } @Override public void valueBound(HttpSessionBindingEvent event) { - UserService userService = getUserService(event); - UserSessionRepository userSessionRepository = getUserSessionRepository(event); +// UserService userService = getUserService(event); + UserService userService = BeanUtil.getBean(UserService.class); +// UserSessionRepository userSessionRepository = getUserSessionRepository(event); + UserSessionRepository userSessionRepository = BeanUtil.getBean(UserSessionRepository.class); LoggedUser user = (LoggedUser) event.getValue(); Instant instant = Instant.now(); @@ -59,14 +61,11 @@ public class LoggedUser implements HttpSessionBindingListener { @Override public void valueUnbound(HttpSessionBindingEvent event) { LoggedUser user = (LoggedUser) event.getValue(); - - UserService userService = getUserService(event); - UserSessionRepository userSessionRepository = getUserSessionRepository(event); + + UserService userService = BeanUtil.getBean(UserService.class);; + UserSessionRepository userSessionRepository = BeanUtil.getBean(UserSessionRepository.class);; Instant instant = Instant.now(); - - - ChatUser chatUser = userService.findByUserName(user.getUsername()); UserSession userSession = userSessionRepository.findByUserName(user.getUsername()); @@ -75,11 +74,10 @@ public class LoggedUser implements HttpSessionBindingListener { // userSession = new UserSession(); throw new UsernameNotFoundException("User session not found"); } - + int numSessions = userSession.getNumSessions(); - - if(--numSessions == 0) - { + + if (--numSessions == 0) { userSession.setOnline(false); } @@ -88,16 +86,4 @@ public class LoggedUser implements HttpSessionBindingListener { userSession.setNumSessions(numSessions); userSessionRepository.save(userSession); } - - private UserService getUserService(HttpSessionEvent se) { - WebApplicationContext context = WebApplicationContextUtils - .getWebApplicationContext(se.getSession().getServletContext()); - return (UserService) context.getBean(UserService.class); - } - - private UserSessionRepository getUserSessionRepository(HttpSessionEvent se) { - WebApplicationContext context = WebApplicationContextUtils - .getWebApplicationContext(se.getSession().getServletContext()); - return (UserSessionRepository) context.getBean(UserSessionRepository.class); - } } \ No newline at end of file