Commit 54f0501d authored by Matt Tucker's avatar Matt Tucker Committed by matt

Added support for property events (JM-232).


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1192 b35dd754-fafc-0310-a699-88a17e54d16e
parent f03d3f64
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2004 Jive Software.
*
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.messenger.event;
import org.jivesoftware.util.Log;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Dispatches property events. Each event has a {@link EventType type}
* and optional parameters, as follows:<p>
*
* <table border="1">
* <tr><th>Event Type</th><th>Extra Params</th></tr>
* <tr><td>{@link EventType#property_set property_set}</td><td>A param named <tt>value</tt> that
* has the value of the property set.</td></tr>
* <tr><td>{@link EventType#property_deleted property_deleted}</td><td><i>None</i></td></tr>
* <tr><td>{@link EventType#xml_property_set xml_property_set}</td><td>A param named <tt>value</tt> that
* has the value of the property set.</td></tr>
* <tr><td>{@link EventType#xml_property_deleted xml_property_deleted}</td><td><i>None</i></td></tr>
* </table>
*
* @author Matt Tucker
*/
public class PropertyEventDispatcher {
private static List<PropertyEventListener> listeners =
new CopyOnWriteArrayList<PropertyEventListener>();
private PropertyEventDispatcher() {
// Not instantiable.
}
/**
* Registers a listener to receive events.
*
* @param listener the listener.
*/
public static void addListener(PropertyEventListener listener) {
if (listener == null) {
throw new NullPointerException();
}
listeners.add(listener);
}
/**
* Unregisters a listener to receive events.
*
* @param listener the listener.
*/
public static void removeListener(PropertyEventListener listener) {
listeners.remove(listener);
}
/**
* Dispatches an event to all listeners.
*
* @param property the property.
* @param eventType the event type.
* @param params event parameters.
*/
public static void dispatchEvent(String property, EventType eventType, Map params) {
for (PropertyEventListener listener : listeners) {
try {
switch (eventType) {
case property_set: {
listener.propertySet(property, params);
break;
}
case property_deleted: {
listener.propertyDeleted(property, params);
break;
}
case xml_property_set: {
listener.xmlPropertySet(property, params);
break;
}
case xml_property_deleted: {
listener.xmlPropertyDeleted(property, params);
break;
}
default:
break;
}
}
catch (Exception e) {
Log.error(e);
}
}
}
/**
* Represents valid event types.
*/
public enum EventType {
/**
* A property was set.
*/
property_set,
/**
* A property was deleted.
*/
property_deleted,
/**
* An XML property was set.
*/
xml_property_set,
/**
* An XML property was deleted.
*/
xml_property_deleted;
}
}
\ No newline at end of file
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2004 Jive Software.
*
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.messenger.event;
import java.util.Map;
/**
* Interface to listen for property events. Use the
* {@link PropertyEventDispatcher#addListener(PropertyEventListener)}
* method to register for events.
*
* @author Matt Tucker
*/
public interface PropertyEventListener {
/**
* A property was set.
*
* @param property the property.
* @param params event parameters.
*/
public void propertySet(String property, Map params);
/**
* A property was deleted.
*
* @param property the deleted.
* @param params event parameters.
*/
public void propertyDeleted(String property, Map params);
/**
* An XML property was set.
*
* @param property the property.
* @param params event parameters.
*/
public void xmlPropertySet(String property, Map params);
/**
* An XML property was deleted.
*
* @param property the property.
* @param params event parameters.
*/
public void xmlPropertyDeleted(String property, Map params);
}
\ No newline at end of file
...@@ -16,6 +16,7 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -16,6 +16,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.sql.*; import java.sql.*;
import org.jivesoftware.database.DbConnectionManager; import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.messenger.event.PropertyEventDispatcher;
/** /**
* Retrieves and stores Jive properties. Properties are stored in the database. * Retrieves and stores Jive properties. Properties are stored in the database.
...@@ -162,6 +163,11 @@ public class JiveProperties implements Map { ...@@ -162,6 +163,11 @@ public class JiveProperties implements Map {
} }
} }
deleteProperty((String)key); deleteProperty((String)key);
// Generate event.
PropertyEventDispatcher.dispatchEvent((String)key,
PropertyEventDispatcher.EventType.property_deleted, Collections.emptyMap());
return value; return value;
} }
...@@ -185,6 +191,13 @@ public class JiveProperties implements Map { ...@@ -185,6 +191,13 @@ public class JiveProperties implements Map {
else { else {
insertProperty((String)key, (String)value); insertProperty((String)key, (String)value);
} }
// Generate event.
Map params = new HashMap();
params.put("value", value);
PropertyEventDispatcher.dispatchEvent((String)key,
PropertyEventDispatcher.EventType.property_set, params);
return properties.put((String)key, (String)value); return properties.put((String)key, (String)value);
} }
......
...@@ -16,6 +16,7 @@ import java.util.*; ...@@ -16,6 +16,7 @@ import java.util.*;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.io.*; import org.dom4j.io.*;
import org.jivesoftware.messenger.event.PropertyEventDispatcher;
/** /**
* Provides the the ability to use simple XML property files. Each property is * Provides the the ability to use simple XML property files. Each property is
...@@ -314,6 +315,12 @@ public class XMLProperties { ...@@ -314,6 +315,12 @@ public class XMLProperties {
element.addElement(childName).setText(value); element.addElement(childName).setText(value);
} }
saveProperties(); saveProperties();
// Generate event.
Map params = new HashMap();
params.put("value", values);
PropertyEventDispatcher.dispatchEvent((String)name,
PropertyEventDispatcher.EventType.xml_property_set, params);
} }
/** /**
...@@ -375,8 +382,14 @@ public class XMLProperties { ...@@ -375,8 +382,14 @@ public class XMLProperties {
} }
// Set the value of the property in this node. // Set the value of the property in this node.
element.setText(value); element.setText(value);
// write the XML properties to disk // Write the XML properties to disk
saveProperties(); saveProperties();
// Generate event.
Map params = new HashMap();
params.put("value", value);
PropertyEventDispatcher.dispatchEvent((String)name,
PropertyEventDispatcher.EventType.xml_property_set, params);
} }
/** /**
...@@ -402,6 +415,10 @@ public class XMLProperties { ...@@ -402,6 +415,10 @@ public class XMLProperties {
element.remove(element.element(propName[propName.length - 1])); element.remove(element.element(propName[propName.length - 1]));
// .. then write to disk. // .. then write to disk.
saveProperties(); saveProperties();
// Generate event.
PropertyEventDispatcher.dispatchEvent((String)name,
PropertyEventDispatcher.EventType.xml_property_deleted, Collections.emptyMap());
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment