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;
11 import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
12 import net.sourceforge.guacamole.properties.GuacamoleProperties;
15 * A collection which iterates over instances of all listeners defined in
16 * guacamole.properties. For each listener defined in guacamole.properties, a
17 * new instance is created and stored in this collection. The contents of this
18 * collection is stored within the HttpSession, and will be reused if available.
19 * Each listener is instantiated once per session. Listeners are singleton
20 * classes within the session, but not globally.
22 * @author Michael Jumper
24 public class SessionListenerCollection extends AbstractCollection {
27 * The name of the session attribute which will contain the listener
30 private static final String SESSION_ATTRIBUTE = "GUAC_LISTENERS";
33 * The wrapped collection of listeners, possibly retrieved from the
36 private Collection listeners;
39 * Creates a new SessionListenerCollection which stores all listeners
40 * defined in guacamole.properties in the provided session. If listeners
41 * are already stored in the provided session, those listeners are used
44 * @param session The HttpSession to store listeners within.
45 * @throws GuacamoleException If an error occurs while instantiating new
48 public SessionListenerCollection(HttpSession session) throws GuacamoleException {
50 // Pull cached listeners from session
51 listeners = (Collection) session.getAttribute(SESSION_ATTRIBUTE);
53 // If no listeners stored, listeners must be loaded first
54 if (listeners == null) {
56 // Load listeners from guacamole.properties
57 listeners = new ArrayList();
60 // Get all listener classes from properties
61 Collection<Class> listenerClasses =
62 GuacamoleProperties.getProperty(BasicGuacamoleProperties.EVENT_LISTENERS);
64 // Add an instance of each class to the list
65 if (listenerClasses != null) {
66 for (Class listenerClass : listenerClasses) {
68 // Instantiate listener
69 Object listener = listenerClass.getConstructor().newInstance();
71 // Add listener to collection of listeners
72 listeners.add(listener);
78 catch (InstantiationException e) {
79 throw new GuacamoleException("Listener class is abstract.", e);
81 catch (IllegalAccessException e) {
82 throw new GuacamoleException("No access to listener constructor.", e);
84 catch (IllegalArgumentException e) {
85 // This should not happen, given there ARE no arguments
86 throw new GuacamoleException("Illegal arguments to listener constructor.", e);
88 catch (InvocationTargetException e) {
89 throw new GuacamoleException("Error while instantiating listener.", e);
91 catch (NoSuchMethodException e) {
92 throw new GuacamoleException("Listener has no default constructor.", e);
94 catch (SecurityException e) {
95 throw new GuacamoleException("Security restrictions prevent instantiation of listener.", e);
98 // Store listeners for next time
99 session.setAttribute(SESSION_ATTRIBUTE, listeners);
106 public Iterator iterator() {
107 return listeners.iterator();
112 return listeners.size();