サーブレットから、漏れの無いセッションリストを取得する必要が出て来たのですが
たしかセキュリティへの配慮から、このAPIは提供されていない認識です。
そこで、自分で既存のAPIを使って作り出すしかないのかなと思い、実装してみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | package jp.develop.tool.servlet; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionActivationListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * Session list providing Listener. */ public class SessionListListener implements HttpSessionListener, HttpSessionActivationListener, Serializable { private static final boolean DEBUG = true ; private static final long serialVersionUID = 7490899888824580726L; private static final String SESSION_LISTENER_KEY = "_sessionListListener" ; private static Object lock = new Object(); private static List<httpsession> sessions = new ArrayList<httpsession>(); private static List<httpsession> sessionsForOutput = Collections.unmodifiableList(sessions); /** * Get all sessions. * @return All sessions list. */ public static List<httpsession> getSessions() { return sessionsForOutput; } /** * Default Constructor. */ public SessionListListener() { log(String.format( "Session List Listener is initialized." )); } /** * @see HttpSessionListener#sessionCreated(HttpSessionEvent) */ public void sessionCreated(HttpSessionEvent event) { synchronized (lock) { HttpSession session = event.getSession(); session.setAttribute(SESSION_LISTENER_KEY, this ); sessions.add(session); log(String.format( "Session Added: %s" , session.getId())); } } /** * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent) */ public void sessionDestroyed(HttpSessionEvent event) { synchronized (lock) { HttpSession session = event.getSession(); session.removeAttribute(SESSION_LISTENER_KEY); sessions.remove(session); // XXX May not good implementation for performance. log(String.format( "Session Removed: %s" , session.getId())); } } /** * @see HttpSessionActivationListener#sessionDidActivate(HttpSessionEvent) */ public void sessionDidActivate(HttpSessionEvent event) { synchronized (lock) { HttpSession session = event.getSession(); sessions.add(session); log(String.format( "Session Activate: %s" , session.getId())); } } /** * @see HttpSessionActivationListener#sessionWillPassivate(HttpSessionEvent) */ public void sessionWillPassivate(HttpSessionEvent event) { synchronized (lock) { HttpSession session = event.getSession(); sessions.remove(session); // XXX May not good implementation for performance. log(String.format( "Session Passivate: %s" , session.getId())); } } private static void log(String message) { if (DEBUG) { System.out.println(message); } } } </httpsession></httpsession></httpsession></httpsession> |
問題を見つけた方、是非、教えてください~
最近のコメント