From 514fd95ec8776b43b366c82feac1d508b4a792e6 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 23 Mar 2012 13:45:06 -0700 Subject: [PATCH] Implemented SessionListenerCollection - a simple auto-populating collection of instances of all available listeners. --- .../net/basic/event/SessionListenerCollection.java | 110 ++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java diff --git a/src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java b/src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java new file mode 100644 index 0000000..6fc7ca3 --- /dev/null +++ b/src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java @@ -0,0 +1,110 @@ +package net.sourceforge.guacamole.net.basic.event; + +import java.lang.reflect.InvocationTargetException; +import java.util.AbstractCollection; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import javax.servlet.http.HttpSession; +import net.sourceforge.guacamole.GuacamoleException; +import net.sourceforge.guacamole.net.basic.GuacamoleClassLoader; + +/** + * A collection which iterates over instances of all listeners defined in + * guacamole.properties. For each listener defined in guacamole.properties, a + * new instance is created and stored in this collection. The contents of this + * collection is stored within the HttpSession, and will be reused if available. + * Each listener is instantiated once per session. Listeners are singleton + * classes within the session, but not globally. + * + * @author Michael Jumper + */ +public class SessionListenerCollection extends AbstractCollection { + + /** + * The name of the session attribute which will contain the listener + * collection. + */ + private static final String SESSION_ATTRIBUTE = "__GUAC_LISTENERS"; + + /** + * The wrapped collection of listeners, possibly retrieved from the + * session. + */ + private Collection listeners; + + /** + * Creates a new SessionListenerCollection which stores all listeners + * defined in guacamole.properties in the provided session. If listeners + * are already stored in the provided session, those listeners are used + * instead. + * + * @param session The HttpSession to store listeners within. + * @throws GuacamoleException If an error occurs while instantiating new + * listeners. + */ + public SessionListenerCollection(HttpSession session) throws GuacamoleException { + + // Pull cached listeners from session + listeners = (Collection) session.getAttribute(SESSION_ATTRIBUTE); + + // If no listeners stored, listeners must be loaded first + if (listeners == null) { + + // Load listeners from guacamole.properties + listeners = new ArrayList(); + try { + + // TODO: Retrieve list of listener classnames from properties + Class listenerClass = GuacamoleClassLoader.getInstance().loadClass( + "net.sourceforge.guacamole.test.GenericLoggingListener" + ); + + // Instantiate listener + Object listener = listenerClass.getConstructor().newInstance(); + + // Add listener to collection of listeners + listeners.add(listener); + + } + catch (ClassNotFoundException e) { + throw new GuacamoleException("Could not find listener class.", e); + } + catch (InstantiationException e) { + throw new GuacamoleException("Listener class is abstract.", e); + } + catch (IllegalAccessException e) { + throw new GuacamoleException("No access to listener constructor.", e); + } + catch (IllegalArgumentException e) { + // This should not happen, given there ARE no arguments + throw new GuacamoleException("Illegal arguments to listener constructor.", e); + } + catch (InvocationTargetException e) { + throw new GuacamoleException("Error while instantiating listener.", e); + } + catch (NoSuchMethodException e) { + throw new GuacamoleException("Listener has no default constructor.", e); + } + catch (SecurityException e) { + throw new GuacamoleException("Security restrictions prevent instantiation of listener.", e); + } + + // Store listeners for next time + session.setAttribute(SESSION_ATTRIBUTE, listeners); + + } + + } + + @Override + public Iterator iterator() { + return listeners.iterator(); + } + + @Override + public int size() { + return listeners.size(); + } + +} -- 1.7.10.4