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