0

i was wondering about the spring scope equivalent to window scope ? according to the documentation here http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch04s04.html

i couldn't find any, so if there's a custom scope equivalent, can anyone please tell me, thanks.

EDIT: example of window scope in JSF

http://icefaces-showcase.icesoft.org/showcase.jsf?grp=compatMenu&exp=popup

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • There is no such scope, what do you mean by *window* scope? Can you describe it or show examples in other frameworks? – Tomasz Nurkiewicz Apr 08 '12 at 11:23
  • @Tomasz Nurkiewicz, window scope is used here in icefaces example, it's a custom scope as i understand, is there's an example of such custom scope in spring ? http://icefaces-showcase.icesoft.org/showcase.jsf?grp=compatMenu&exp=popup – Mahmoud Saleh Apr 08 '12 at 11:26
  • I am thinking these to pieces of jargon differ considerably. What are you trying to do or solve? – chrislovecnm Apr 08 '12 at 18:38
  • i want to make my bean like this one in the example http://icefaces-showcase.icesoft.org/showcase.jsf?grp=compatMenu&exp=popup – Mahmoud Saleh Apr 09 '12 at 07:10

1 Answers1

0

One possible approach is having a session scoped bean that holds all the window sessions, and a window custom scope that access to the corresponding bean through that session scoped bean.

The required classes are:

Window Session

public class WindowSession {
    private Map<String, Map<String,Object>> scopeMap = new HashMap<String, Map<String,Object>>();
    private Map<String, Boolean> validSessions = new HashMap<String, Boolean>();

    public WindowSession() {
        super();
    }

    public Map<String, Map<String, Object>> getScopeMap() {
        return scopeMap;
    }

    public void setScopeMap(Map<String, Map<String, Object>> scopeMap) {
        this.scopeMap = scopeMap;
    }

    public Map<String, Boolean> getValidSessions() {
        return validSessions;
    }

    public void setValidSessions(Map<String, Boolean> validSessions) {
        this.validSessions = validSessions;
    }

}

WindowIdContext

@Service
@Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class WindowIdContext {

    public static final String DEFAULT_SESSION_ID = "";

    private String id = DEFAULT_SESSION_ID;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getQueryParamWithSessionId() {
        if (DEFAULT_SESSION_ID.equals(id)) {
            return "";
        }
        return "?" + WindowScope.WINDOW_ID_PARAM + "=" + id;
    }
}

WindowSessionContext

@Service
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class WindowSessionContext {

    private static SecureRandom random = new SecureRandom();

    private WindowSession windowSession = new WindowSession();

    @Autowired
    private WindowIdContext windowIdContext;

    public WindowSessionContext() {
        windowSession.getValidSessions().put(WindowIdContext.DEFAULT_SESSION_ID, false);
    }

    public Object getObject(String name, ObjectFactory<?> objectFactory) {
        String sessionId = windowIdContext.getId();
        synchronized(this) {
            if (!windowSession.getValidSessions().containsKey(sessionId)) {
                windowSession.getValidSessions().put(sessionId, false);
            }
            Map<String,Object> sessionMap = windowSession.getScopeMap().get(sessionId);
            if (sessionMap == null) {
                sessionMap = new HashMap<String,Object>();
                windowSession.getScopeMap().put(sessionId, sessionMap);
            }
            Object object = sessionMap.get(name);
            if (object == null) {
                object = objectFactory.getObject();
                sessionMap.put(name, object);
            }
            return object;
        }
    }

    public Object removeObject(String name) {
        String sessionId = windowIdContext.getId();
        synchronized(this) {
            Map<String,Object> sessionMap = windowSession.getScopeMap().get(sessionId);
            if (sessionMap == null) {
                return null;
            }
            Object object = sessionMap.remove(name);
            return object;
        }
    }

    public String addSession() {
        synchronized(this) {
            String sessionId;
            do {
                sessionId = new BigInteger(130, random).toString(32);
            } while (windowSession.getValidSessions().containsKey(sessionId));
            windowSession.getValidSessions().put(sessionId, false);
            return sessionId;
        }
    }

    public void removeSession() {
        String sessionId = windowIdContext.getId();
        synchronized(this) {
            windowSession.getScopeMap().remove(sessionId);
            windowSession.getValidSessions().remove(sessionId);
        }
    }

    public boolean isSessionValid(String sessionId) {
        Boolean inUse = windowSession.getValidSessions().get(sessionId);
        return inUse != null;
    }

    public boolean isSessionInUse(String sessionId) {
        Boolean inUse = windowSession.getValidSessions().get(sessionId);
        return inUse == true;
    }

    public synchronized boolean invalidateSession(String sessionId) {
        Boolean valid = windowSession.getValidSessions().get(sessionId);
        if (valid == null) {
            return false;
        }
        if (sessionId.equals(WindowIdContext.DEFAULT_SESSION_ID)) {
            windowSession.getValidSessions().put(sessionId, false);
        } else {
            windowSession.getValidSessions().remove(sessionId);
        }
        windowSession.getScopeMap().remove(sessionId);
        for (Entry<String,Boolean> validSession : windowSession.getValidSessions().entrySet()) {
            if (validSession.getValue() == true) {
                return false;
            }
        }
        return true;
    }

    public synchronized void setSessionInUse(String sessionId) {
        windowSession.getValidSessions().put(sessionId, true);
    }

    public int getCount() {
        return windowSession.getValidSessions().size();
    }
}

WindowScope

public class WindowScope implements Scope {

    private final int scope;
    private static final int WINDOW_SCOPE = 40;
    public static final String NAME = "window";
    public static final String WINDOW_ID_PARAM = "windowid";

    private ServletContext servletContext = null;

    public WindowScope(ServletContext sc) {
        servletContext = sc;
        this.scope = WINDOW_SCOPE;
    }

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        WindowSessionContext windowSessionContext = applicationContext.getBean(WindowSessionContext.class);
        return windowSessionContext.getObject(name, objectFactory);
    }

    @Override
    public Object remove(String name) {
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        WindowSessionContext windowSessionContext = applicationContext.getBean(WindowSessionContext.class);
        return windowSessionContext.removeObject(name);
    }

    @Override
    public String getConversationId() {
        return RequestContextHolder.currentRequestAttributes().getSessionId();
    }

    @Override
    public void registerDestructionCallback(String arg0, Runnable arg1) {
    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }

    protected int getScope() {
        return this.scope;
    }

}

WindowScopeFilter

@Component("windowScopeFilter")
public class WindowScopeFilter implements Filter {

    @Autowired
    private WindowSessionContext windowSessionContext;

    @Autowired
    private WindowIdContext windowIdContext;

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        String sessionId = httpRequest.getParameter(WindowScope.WINDOW_ID_PARAM);
        if (sessionId != null) {
            if (windowSessionContext.isSessionValid(sessionId)) {
                windowIdContext.setId(sessionId);
            } else {
                httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            }
        } else {
            windowSessionContext.setSessionInUse(WindowIdContext.DEFAULT_SESSION_ID);
        }
        chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {
    }

}

WindowScopeListener

@Component
public class WindowScopeListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        WebApplicationContext applicationContext = (WebApplicationContext)event.getApplicationContext();
        Scope windowScope = new WindowScope(applicationContext.getServletContext());
        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory)applicationContext.getAutowireCapableBeanFactory();
        beanFactory.registerScope("window", windowScope);
    }

}

Source with complete source code: http://notwithoutmycode.com/post/window/scope/in/spring

Juan Sánchez
  • 980
  • 2
  • 10
  • 26
  • I am trying to use this solution, but am unable to remove the bean. How do I specify when to remove the bean? Also I tried to access the source code but was not able to is there a way to access the source code? – rdev Aug 23 '16 at 02:05