Remove trailing whitespace.
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / event / SessionListenerCollection.java
1 package net.sourceforge.guacamole.net.basic.event;
2
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;
12
13 /**
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.
20  *
21  * @author Michael Jumper
22  */
23 public class SessionListenerCollection extends AbstractCollection {
24
25     /**
26      * The name of the session attribute which will contain the listener
27      * collection.
28      */
29     private static final String SESSION_ATTRIBUTE = "GUAC_LISTENERS";
30
31     /**
32      * The wrapped collection of listeners, possibly retrieved from the
33      * session.
34      */
35     private Collection listeners;
36
37     /**
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
41      * instead.
42      *
43      * @param session The HttpSession to store listeners within.
44      * @throws GuacamoleException If an error occurs while instantiating new
45      *                            listeners.
46      */
47     public SessionListenerCollection(HttpSession session) throws GuacamoleException {
48
49         // Pull cached listeners from session
50         listeners = (Collection) session.getAttribute(SESSION_ATTRIBUTE);
51
52         // If no listeners stored, listeners must be loaded first
53         if (listeners == null) {
54
55             // Load listeners from guacamole.properties
56             listeners = new ArrayList();
57             try {
58
59                 // Get all listener classes from properties
60                 Collection<Class> listenerClasses =
61                         GuacamoleProperties.getProperty(BasicGuacamoleProperties.EVENT_LISTENERS);
62
63                 // Add an instance of each class to the list
64                 if (listenerClasses != null) {
65                     for (Class listenerClass : listenerClasses) {
66
67                         // Instantiate listener
68                         Object listener = listenerClass.getConstructor().newInstance();
69
70                         // Add listener to collection of listeners
71                         listeners.add(listener);
72
73                     }
74                 }
75
76             }
77             catch (InstantiationException e) {
78                 throw new GuacamoleException("Listener class is abstract.", e);
79             }
80             catch (IllegalAccessException e) {
81                 throw new GuacamoleException("No access to listener constructor.", e);
82             }
83             catch (IllegalArgumentException e) {
84                 // This should not happen, given there ARE no arguments
85                 throw new GuacamoleException("Illegal arguments to listener constructor.", e);
86             }
87             catch (InvocationTargetException e) {
88                 throw new GuacamoleException("Error while instantiating listener.", e);
89             }
90             catch (NoSuchMethodException e) {
91                 throw new GuacamoleException("Listener has no default constructor.", e);
92             }
93             catch (SecurityException e) {
94                 throw new GuacamoleException("Security restrictions prevent instantiation of listener.", e);
95             }
96
97             // Store listeners for next time
98             session.setAttribute(SESSION_ATTRIBUTE, listeners);
99
100         }
101
102     }
103
104     @Override
105     public Iterator iterator() {
106         return listeners.iterator();
107     }
108
109     @Override
110     public int size() {
111         return listeners.size();
112     }
113
114 }