001 package com.khubla.pragmatach.framework.controller; 002 003 import java.util.Hashtable; 004 import java.util.Set; 005 006 import javax.servlet.http.HttpSession; 007 008 import com.khubla.pragmatach.framework.annotation.Controller; 009 import com.khubla.pragmatach.framework.api.PragmatachException; 010 011 /** 012 * @author tome 013 */ 014 public class SessionScopedControllers { 015 /*** 016 * Session key for session scoped controllers 017 */ 018 private static final String SESSION_CONTROLLERS = "Session-Scoped-Controllers"; 019 020 /** 021 * get controller with clazz 022 */ 023 public static PragmatachController getController(HttpSession httpSession, Class<?> clazz) { 024 final Hashtable<Class<?>, PragmatachController> map = getMap(httpSession); 025 return map.get(clazz); 026 } 027 028 /** 029 * get the map 030 */ 031 @SuppressWarnings("unchecked") 032 public static Hashtable<Class<?>, PragmatachController> getMap(HttpSession httpSession) { 033 Hashtable<Class<?>, PragmatachController> controllers = (Hashtable<Class<?>, PragmatachController>) httpSession.getAttribute(SESSION_CONTROLLERS); 034 if (null == controllers) { 035 controllers = new Hashtable<Class<?>, PragmatachController>(); 036 httpSession.setAttribute(SESSION_CONTROLLERS, controllers); 037 } 038 return controllers; 039 } 040 041 /** 042 * create instances of all session controllers and make sure they're in the map 043 */ 044 public static void populateSessionControllers(HttpSession httpSession) throws PragmatachException { 045 try { 046 final Set<Class<?>> controllers = Controllers.getInstance().getControllers(); 047 if (null != controllers) { 048 for (final Class<?> clazz : controllers) { 049 final Controller controller = clazz.getAnnotation(Controller.class); 050 if (controller.scope() == Controller.Scope.session) { 051 final PragmatachController pragmatachController = Controllers.getInstance(clazz); 052 SessionScopedControllers.setController(httpSession, pragmatachController); 053 } 054 } 055 } 056 } catch (final Exception e) { 057 throw new PragmatachException("Exception in populateSessionControllers", e); 058 } 059 } 060 061 /** 062 * set controller 063 */ 064 public static void setController(HttpSession httpSession, PragmatachController pragmatachController) { 065 final Hashtable<Class<?>, PragmatachController> map = getMap(httpSession); 066 map.put(pragmatachController.getClass(), pragmatachController); 067 } 068 }