From 903ba96a0c700374009e0f5f8199ba4d3cbd5c47 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 23 Mar 2012 17:34:24 -0700 Subject: [PATCH] Read event listeners from properties. --- .../net/basic/event/SessionListenerCollection.java | 27 ++++---- .../basic/properties/BasicGuacamoleProperties.java | 10 +++ .../basic/properties/EventListenersProperty.java | 68 ++++++++++++++++++++ 3 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/sourceforge/guacamole/net/basic/properties/EventListenersProperty.java diff --git a/src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java b/src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java index 8f31473..62b0891 100644 --- a/src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java +++ b/src/main/java/net/sourceforge/guacamole/net/basic/event/SessionListenerCollection.java @@ -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 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); diff --git a/src/main/java/net/sourceforge/guacamole/net/basic/properties/BasicGuacamoleProperties.java b/src/main/java/net/sourceforge/guacamole/net/basic/properties/BasicGuacamoleProperties.java index 4110b46..17f0b39 100644 --- a/src/main/java/net/sourceforge/guacamole/net/basic/properties/BasicGuacamoleProperties.java +++ b/src/main/java/net/sourceforge/guacamole/net/basic/properties/BasicGuacamoleProperties.java @@ -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 index 0000000..9efa602 --- /dev/null +++ b/src/main/java/net/sourceforge/guacamole/net/basic/properties/EventListenersProperty.java @@ -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 . + */ + +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> { + + @Override + public Collection 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 listeners = new ArrayList(); + 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; + + } + +} + -- 1.7.10.4