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.properties.BasicGuacamoleProperties;
11 import net.sourceforge.guacamole.properties.GuacamoleProperties;
14 * A collection which iterates over instances of all listeners defined in
15 * guacamole.properties. For each listener defined in guacamole.properties, a
16 * new instance is created and stored in this collection. The contents of this
17 * collection is stored within the HttpSession, and will be reused if available.
18 * Each listener is instantiated once per session. Listeners are singleton
19 * classes within the session, but not globally.
21 * @author Michael Jumper
23 public class SessionListenerCollection extends AbstractCollection {
26 * The name of the session attribute which will contain the listener
29 private static final String SESSION_ATTRIBUTE = "GUAC_LISTENERS";
32 * The wrapped collection of listeners, possibly retrieved from the
35 private Collection listeners;
38 * Creates a new SessionListenerCollection which stores all listeners
39 * defined in guacamole.properties in the provided session. If listeners
40 * are already stored in the provided session, those listeners are used
43 * @param session The HttpSession to store listeners within.
44 * @throws GuacamoleException If an error occurs while instantiating new
47 public SessionListenerCollection(HttpSession session) throws GuacamoleException {
49 // Pull cached listeners from session
50 listeners = (Collection) session.getAttribute(SESSION_ATTRIBUTE);
52 // If no listeners stored, listeners must be loaded first
53 if (listeners == null) {
55 // Load listeners from guacamole.properties
56 listeners = new ArrayList();
59 // Get all listener classes from properties
60 Collection<Class> listenerClasses =
61 GuacamoleProperties.getProperty(BasicGuacamoleProperties.EVENT_LISTENERS);
63 // Add an instance of each class to the list
64 if (listenerClasses != null) {
65 for (Class listenerClass : listenerClasses) {
67 // Instantiate listener
68 Object listener = listenerClass.getConstructor().newInstance();
70 // Add listener to collection of listeners
71 listeners.add(listener);
77 catch (InstantiationException e) {
78 throw new GuacamoleException("Listener class is abstract.", e);
80 catch (IllegalAccessException e) {
81 throw new GuacamoleException("No access to listener constructor.", e);
83 catch (IllegalArgumentException e) {
84 // This should not happen, given there ARE no arguments
85 throw new GuacamoleException("Illegal arguments to listener constructor.", e);
87 catch (InvocationTargetException e) {
88 throw new GuacamoleException("Error while instantiating listener.", e);
90 catch (NoSuchMethodException e) {
91 throw new GuacamoleException("Listener has no default constructor.", e);
93 catch (SecurityException e) {
94 throw new GuacamoleException("Security restrictions prevent instantiation of listener.", e);
97 // Store listeners for next time
98 session.setAttribute(SESSION_ATTRIBUTE, listeners);
105 public Iterator iterator() {
106 return listeners.iterator();
111 return listeners.size();