<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AMF48 &#187; Unknown</title>
	<atom:link href="http://amf48.develop.jp/archives/category/unknown/feed" rel="self" type="application/rss+xml" />
	<link>http://amf48.develop.jp</link>
	<description>AMF Encode/Decode Library for Java</description>
	<lastBuildDate>Sun, 04 Mar 2012 09:47:28 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.8.41</generator>
	<item>
		<title>How to get session list.</title>
		<link>http://amf48.develop.jp/en/archives/399</link>
		<comments>http://amf48.develop.jp/en/archives/399#comments</comments>
		<pubDate>Sun, 04 Mar 2012 09:47:28 +0000</pubDate>
		<dc:creator><![CDATA[wacky]]></dc:creator>
				<category><![CDATA[Unknown]]></category>

		<guid isPermaLink="false">http://amf48.develop.jp/?p=399</guid>
		<description><![CDATA[I need to get all active sessions from servlet. But I believe this API is obsolated&#8230; That is why, I tried to implement using existing API. 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 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I need to get all active sessions from servlet.<br />
But I believe this API is obsolated&#8230;</p>
<p>That is why, I tried to implement using existing API.</p>
<pre class="brush:java">
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);
    	}
	}
}
</pre>
<p>If anybody find a problem, please let me know&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://amf48.develop.jp/en/archives/399/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
