Use NodeListener support class.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 13 Nov 2008 23:12:50 +0000 (00:12 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 13 Nov 2008 23:12:50 +0000 (00:12 +0100)
src/net/pterodactylus/jsite/core/NodeManager.java

index 40e4190..02f03b3 100644 (file)
@@ -66,8 +66,8 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
        /** Object used for synchronization. */
        private final Object syncObject = new Object();
 
-       /** Node listeners. */
-       private List<NodeListener> nodeListeners = Collections.synchronizedList(new ArrayList<NodeListener>());
+       /** Node listener support. */
+       private final NodeListenerSupport nodeListenerSupport = new NodeListenerSupport();
 
        /** All nodes. */
        private List<Node> nodes = Collections.synchronizedList(new ArrayList<Node>());
@@ -105,7 +105,7 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
         *            The listener to add
         */
        public void addNodeListener(NodeListener nodeListener) {
-               nodeListeners.add(nodeListener);
+               nodeListenerSupport.addListener(nodeListener);
        }
 
        /**
@@ -115,77 +115,7 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
         *            The listener to remove
         */
        public void removeNodeListener(NodeListener nodeListener) {
-               nodeListeners.remove(nodeListener);
-       }
-
-       /**
-        * Notifies all listeners that a node was added.
-        *
-        * @param node
-        *            The node that was added.
-        */
-       private void fireNodeAdded(Node node) {
-               logger.log(Level.FINEST, "firing nodeAdded event with [node=" + node + "]");
-               for (NodeListener nodeListener : nodeListeners) {
-                       nodeListener.nodeAdded(node);
-               }
-       }
-
-       /**
-        * Notifies all listeners that a node was removed.
-        *
-        * @param node
-        *            The node that was removed
-        */
-       private void fireNodeRemoved(Node node) {
-               logger.log(Level.FINEST, "firing nodeRemoved event with [node=" + node + "]");
-               for (NodeListener nodeListener : nodeListeners) {
-                       nodeListener.nodeRemoved(node);
-               }
-       }
-
-       /**
-        * Notifies all listeners that the given node was connected.
-        *
-        * @param node
-        *            The node that is now connected
-        */
-       private void fireNodeConnected(Node node) {
-               logger.log(Level.FINEST, "firing nodeConnected event with [node=" + node + "]");
-               for (NodeListener nodeListener : nodeListeners) {
-                       nodeListener.nodeConnected(node);
-               }
-       }
-
-       /**
-        * Notifies all listeners that a connection to a node has failed.
-        *
-        * @param node
-        *            The node that could not be connected
-        * @param cause
-        *            The cause of the failure
-        */
-       private void fireNodeConnectionFailed(Node node, Throwable cause) {
-               logger.log(Level.FINEST, "firing nodeConnectionFailed event with [node=" + node + ",cause=" + cause + "]");
-               for (NodeListener nodeListener : nodeListeners) {
-                       nodeListener.nodeConnectionFailed(node, cause);
-               }
-       }
-
-       /**
-        * Notifies all listeners that the given node was disconnected.
-        *
-        * @param node
-        *            The node that is now disconnected
-        * @param throwable
-        *            The exception that caused the disconnect, or <code>null</code>
-        *            if there was no exception
-        */
-       private void fireNodeDisconnected(Node node, Throwable throwable) {
-               logger.log(Level.FINEST, "firing nodeDisconnected event with [node=" + node + ",throwable=" + throwable + "]");
-               for (NodeListener nodeListener : nodeListeners) {
-                       nodeListener.nodeDisconnected(node, throwable);
-               }
+               nodeListenerSupport.removeListener(nodeListener);
        }
 
        //
@@ -347,7 +277,7 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
                clientNodes.put(highLevelClient, node);
                nodeClients.put(node, highLevelClient);
                highLevelClient.addHighLevelClientListener(this);
-               fireNodeAdded(node);
+               nodeListenerSupport.fireNodeAdded(node);
                return true;
        }
 
@@ -370,7 +300,7 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
                        nodes.remove(node);
                        idNodes.remove(node.getId());
                        node.removePropertyChangeListener(this);
-                       fireNodeRemoved(node);
+                       nodeListenerSupport.fireNodeRemoved(node);
                }
        }
 
@@ -391,9 +321,9 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
                try {
                        highLevelClient.connect(node.getHostname(), node.getPort());
                } catch (UnknownHostException uhe1) {
-                       fireNodeConnectionFailed(node, uhe1);
+                       nodeListenerSupport.fireNodeConnectionFailed(node, uhe1);
                } catch (IOException ioe1) {
-                       fireNodeConnectionFailed(node, ioe1);
+                       nodeListenerSupport.fireNodeConnectionFailed(node, ioe1);
                }
        }
 
@@ -505,7 +435,7 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
                        logger.log(Level.WARNING, "got event for unknown client");
                        return;
                }
-               fireNodeConnected(node);
+               nodeListenerSupport.fireNodeConnected(node);
        }
 
        /**
@@ -519,7 +449,7 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
                                logger.log(Level.WARNING, "got event for unknown client");
                                return;
                        }
-                       fireNodeDisconnected(node, throwable);
+                       nodeListenerSupport.fireNodeDisconnected(node, throwable);
                }
        }
 
@@ -546,9 +476,9 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
                                        try {
                                                highLevelClient.connect(node.getHostname(), node.getPort());
                                        } catch (UnknownHostException uhe1) {
-                                               fireNodeConnectionFailed(node, uhe1);
+                                               nodeListenerSupport.fireNodeConnectionFailed(node, uhe1);
                                        } catch (IOException ioe1) {
-                                               fireNodeConnectionFailed(node, ioe1);
+                                               nodeListenerSupport.fireNodeConnectionFailed(node, ioe1);
                                        }
                                }
                        }