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