Read event listeners from properties.
authorMichael Jumper <zhangmaike@users.sourceforge.net>
Sat, 24 Mar 2012 00:34:24 +0000 (17:34 -0700)
committerMichael Jumper <zhangmaike@users.sourceforge.net>
Sat, 24 Mar 2012 00:34:24 +0000 (17:34 -0700)
src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java
src/main/java/net/sourceforge/guacamole/net/basic/properties/BasicGuacamoleProperties.java
src/main/java/net/sourceforge/guacamole/net/basic/properties/EventListenersProperty.java [new file with mode: 0644]

index 8f31473..62b0891 100644 (file)
@@ -8,6 +8,8 @@ import java.util.Iterator;
 import javax.servlet.http.HttpSession;
 import net.sourceforge.guacamole.GuacamoleException;
 import net.sourceforge.guacamole.net.basic.GuacamoleClassLoader;
+import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
+import net.sourceforge.guacamole.properties.GuacamoleProperties;
 
 /**
  * A collection which iterates over instances of all listeners defined in
@@ -55,20 +57,23 @@ public class SessionListenerCollection extends AbstractCollection {
             listeners = new ArrayList();
             try {
 
-                // TODO: Retrieve list of listener classnames from properties
-                Class listenerClass = GuacamoleClassLoader.getInstance().loadClass(
-                        "net.sourceforge.guacamole.test.GenericLoggingListener"
-                );
+                // Get all listener classes from properties
+                Collection<Class> listenerClasses =
+                        GuacamoleProperties.getProperty(BasicGuacamoleProperties.EVENT_LISTENERS);
 
-                // Instantiate listener
-                Object listener = listenerClass.getConstructor().newInstance();
+                // Add an instance of each class to the list
+                if (listenerClasses != null) {
+                    for (Class listenerClass : listenerClasses) {
 
-                // Add listener to collection of listeners
-                listeners.add(listener);
+                        // Instantiate listener
+                        Object listener = listenerClass.getConstructor().newInstance();
+
+                        // Add listener to collection of listeners
+                        listeners.add(listener);
+
+                    }
+                }
 
-            }
-            catch (ClassNotFoundException e) {
-                throw new GuacamoleException("Could not find listener class.", e);
             }
             catch (InstantiationException e) {
                 throw new GuacamoleException("Listener class is abstract.", e);
index 4110b46..17f0b39 100644 (file)
@@ -54,4 +54,14 @@ public class BasicGuacamoleProperties {
 
     };
 
+    /**
+     * The comma-separated list of all classes to use as event listeners.
+     */
+    public static final EventListenersProperty EVENT_LISTENERS = new EventListenersProperty() {
+
+        @Override
+        public String getName() { return "event-listeners"; }
+
+    };
+
 }
diff --git a/src/main/java/net/sourceforge/guacamole/net/basic/properties/EventListenersProperty.java b/src/main/java/net/sourceforge/guacamole/net/basic/properties/EventListenersProperty.java
new file mode 100644 (file)
index 0000000..9efa602
--- /dev/null
@@ -0,0 +1,68 @@
+package net.sourceforge.guacamole.net.basic.properties;
+
+/*
+ *  Guacamole - Clientless Remote Desktop
+ *  Copyright (C) 2010  Michael Jumper
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import java.util.ArrayList;
+import java.util.Collection;
+import net.sourceforge.guacamole.GuacamoleException;
+import net.sourceforge.guacamole.net.basic.GuacamoleClassLoader;
+import net.sourceforge.guacamole.properties.GuacamoleProperty;
+
+/**
+ * A GuacamoleProperty whose value is a comma-separated list of class names,
+ * where each class will be used as a listener for events.
+ * 
+ * @author Michael Jumper
+ */
+public abstract class EventListenersProperty implements GuacamoleProperty<Collection<Class>> {
+
+    @Override
+    public Collection<Class> parseValue(String classNameList) throws GuacamoleException {
+
+        // If no property provided, return null.
+        if (classNameList == null)
+            return null;
+
+        // Parse list
+        String[] classNames = classNameList.split(",[\\s]*");
+        
+        // Fill list of classes
+        Collection<Class> listeners = new ArrayList<Class>();
+        try {
+
+            // Load all classes in list
+            for (String className : classNames) {
+                Class clazz = GuacamoleClassLoader.getInstance().loadClass(className);
+                listeners.add(clazz);
+            }
+
+        }
+        catch (ClassNotFoundException e) {
+            throw new GuacamoleException("Listener class not found.", e);
+        }
+        catch (SecurityException e) {
+            throw new GuacamoleException("Security settings prevent loading of listener class.", e);
+        }
+
+        return listeners;
+
+    }
+
+}
+