1 package net.sourceforge.guacamole.net.basic.event;
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.AbstractCollection;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Iterator;
8 import javax.servlet.http.HttpSession;
9 import net.sourceforge.guacamole.GuacamoleException;
10 import net.sourceforge.guacamole.net.basic.GuacamoleClassLoader;
13 * A collection which iterates over instances of all listeners defined in
14 * guacamole.properties. For each listener defined in guacamole.properties, a
15 * new instance is created and stored in this collection. The contents of this
16 * collection is stored within the HttpSession, and will be reused if available.
17 * Each listener is instantiated once per session. Listeners are singleton
18 * classes within the session, but not globally.
20 * @author Michael Jumper
22 public class SessionListenerCollection extends AbstractCollection {
25 * The name of the session attribute which will contain the listener
28 private static final String SESSION_ATTRIBUTE = "GUAC_LISTENERS";
31 * The wrapped collection of listeners, possibly retrieved from the
34 private Collection listeners;
37 * Creates a new SessionListenerCollection which stores all listeners
38 * defined in guacamole.properties in the provided session. If listeners
39 * are already stored in the provided session, those listeners are used
42 * @param session The HttpSession to store listeners within.
43 * @throws GuacamoleException If an error occurs while instantiating new
46 public SessionListenerCollection(HttpSession session) throws GuacamoleException {
48 // Pull cached listeners from session
49 listeners = (Collection) session.getAttribute(SESSION_ATTRIBUTE);
51 // If no listeners stored, listeners must be loaded first
52 if (listeners == null) {
54 // Load listeners from guacamole.properties
55 listeners = new ArrayList();
58 // TODO: Retrieve list of listener classnames from properties
59 Class listenerClass = GuacamoleClassLoader.getInstance().loadClass(
60 "net.sourceforge.guacamole.test.GenericLoggingListener"
63 // Instantiate listener
64 Object listener = listenerClass.getConstructor().newInstance();
66 // Add listener to collection of listeners
67 listeners.add(listener);
70 catch (ClassNotFoundException e) {
71 throw new GuacamoleException("Could not find listener class.", e);
73 catch (InstantiationException e) {
74 throw new GuacamoleException("Listener class is abstract.", e);
76 catch (IllegalAccessException e) {
77 throw new GuacamoleException("No access to listener constructor.", e);
79 catch (IllegalArgumentException e) {
80 // This should not happen, given there ARE no arguments
81 throw new GuacamoleException("Illegal arguments to listener constructor.", e);
83 catch (InvocationTargetException e) {
84 throw new GuacamoleException("Error while instantiating listener.", e);
86 catch (NoSuchMethodException e) {
87 throw new GuacamoleException("Listener has no default constructor.", e);
89 catch (SecurityException e) {
90 throw new GuacamoleException("Security restrictions prevent instantiation of listener.", e);
93 // Store listeners for next time
94 session.setAttribute(SESSION_ATTRIBUTE, listeners);
101 public Iterator iterator() {
102 return listeners.iterator();
107 return listeners.size();