ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject [08/45] git commit: AMBARI-7004 - Views: Need ability to remove view event listener.
Date Thu, 28 Aug 2014 17:49:43 GMT
AMBARI-7004 - Views: Need ability to remove view event listener.


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/8ca145bb
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/8ca145bb
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/8ca145bb

Branch: refs/heads/branch-alerts-dev
Commit: 8ca145bbda6134f0541c9f14ff7d99a0d3ac081e
Parents: af1db65
Author: tbeerbower <tbeerbower@hortonworks.com>
Authored: Mon Aug 25 14:38:55 2014 -0400
Committer: tbeerbower <tbeerbower@hortonworks.com>
Committed: Wed Aug 27 12:45:33 2014 -0400

----------------------------------------------------------------------
 .../ambari/server/view/ViewContextImpl.java     |  9 ++++
 .../apache/ambari/server/view/ViewRegistry.java | 30 ++++++++++--
 .../ambari/server/view/ViewRegistryTest.java    | 50 ++++++++++++++++++++
 .../org/apache/ambari/view/ViewController.java  | 19 ++++++++
 4 files changed, 103 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/8ca145bb/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
index d36ad6e..8915134 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
@@ -322,6 +322,15 @@ public class ViewContextImpl implements ViewContext, ViewController {
     viewRegistry.registerListener(listener, viewName, viewVersion);
   }
 
+  @Override
+  public void unregisterListener(Listener listener, String viewName) {
+    viewRegistry.unregisterListener(listener, viewName, null);
+  }
+
+  @Override
+  public void unregisterListener(Listener listener, String viewName, String viewVersion)
{
+    viewRegistry.unregisterListener(listener, viewName, viewVersion);
+  }
 
   // ----- helper methods ----------------------------------------------------
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/8ca145bb/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
index 6e5f0a2..36c9081 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
@@ -37,6 +37,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
@@ -46,6 +47,7 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
 
+import com.google.common.collect.Sets;
 import org.apache.ambari.server.api.resources.ResourceInstanceFactoryImpl;
 import org.apache.ambari.server.api.resources.SubResourceDefinition;
 import org.apache.ambari.server.api.resources.ViewExternalSubResourceDefinition;
@@ -135,8 +137,8 @@ public class ViewRegistry {
   /**
    * Mapping of view names to registered listeners.
    */
-  private final Map<String, List<Listener>> listeners =
-      new HashMap<String, List<Listener>>();
+  private final Map<String, Set<Listener>> listeners =
+      new ConcurrentHashMap<String, Set<Listener>>();
 
   /**
    * Helper class.
@@ -633,10 +635,10 @@ public class ViewRegistry {
 
     String name = viewVersion == null ? viewName : ViewEntity.getViewName(viewName, viewVersion);
 
-    List<Listener> listeners = this.listeners.get(name);
+    Set<Listener> listeners = this.listeners.get(name);
 
     if (listeners == null) {
-      listeners = new LinkedList<Listener>();
+      listeners = Sets.newSetFromMap(new ConcurrentHashMap<Listener, Boolean>());
       this.listeners.put(name, listeners);
     }
 
@@ -644,6 +646,24 @@ public class ViewRegistry {
   }
 
   /**
+   * Un-register the given listener from the view identified by the given name and version.
+   *
+   * @param listener     the listener
+   * @param viewName     the view name
+   * @param viewVersion  the view version; null indicates all versions
+   */
+  public synchronized void unregisterListener(Listener listener, String viewName, String
viewVersion) {
+
+    String name = viewVersion == null ? viewName : ViewEntity.getViewName(viewName, viewVersion);
+
+    Set<Listener> listeners = this.listeners.get(name);
+
+    if (listeners != null) {
+      listeners.remove(listener);
+    }
+  }
+
+  /**
    * Determine whether or not the access specified by the given permission
    * is permitted for the given user on the view instance identified by
    * the given resource.
@@ -1212,7 +1232,7 @@ public class ViewRegistry {
 
   // notify the view identified by the given view name of the given event
   private void fireEvent(Event event, String viewName) {
-    List<Listener> listeners = this.listeners.get(viewName);
+    Set<Listener> listeners = this.listeners.get(viewName);
 
     if (listeners != null) {
       for (Listener listener : listeners) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/8ca145bb/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
index 55dca84..dea9b3b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
@@ -412,6 +412,56 @@ public class ViewRegistryTest {
 
     listener.clear();
 
+    // fire an event for a different view
+    event = EventImplTest.getEvent("MyEvent", Collections.<String, String>emptyMap(),
view_xml2);
+
+    registry.fireEvent(event);
+
+    Assert.assertNull(listener.getLastEvent());
+
+    // un-register the listener
+    registry.unregisterListener(listener, "MY_VIEW", "1.0.0");
+
+    event = EventImplTest.getEvent("MyEvent", Collections.<String, String>emptyMap(),
view_xml1);
+
+    registry.fireEvent(event);
+
+    Assert.assertNull(listener.getLastEvent());
+  }
+
+  @Test
+  public void testListener_allVersions() throws Exception {
+    ViewRegistry registry = getRegistry();
+
+    TestListener listener = new TestListener();
+    registry.registerListener(listener, "MY_VIEW", null); // all versions of MY_VIEW
+
+    EventImpl event = EventImplTest.getEvent("MyEvent", Collections.<String, String>emptyMap(),
view_xml1);
+
+    registry.fireEvent(event);
+
+    Assert.assertEquals(event, listener.getLastEvent());
+
+    listener.clear();
+
+    // fire an event for a different view
+    event = EventImplTest.getEvent("MyEvent", Collections.<String, String>emptyMap(),
view_xml2);
+
+    registry.fireEvent(event);
+
+    Assert.assertEquals(event, listener.getLastEvent());
+
+    listener.clear();
+
+    // un-register the listener
+    registry.unregisterListener(listener, "MY_VIEW", null); // all versions of MY_VIEW
+
+    event = EventImplTest.getEvent("MyEvent", Collections.<String, String>emptyMap(),
view_xml1);
+
+    registry.fireEvent(event);
+
+    Assert.assertNull(listener.getLastEvent());
+
     event = EventImplTest.getEvent("MyEvent", Collections.<String, String>emptyMap(),
view_xml2);
 
     registry.fireEvent(event);

http://git-wip-us.apache.org/repos/asf/ambari/blob/8ca145bb/ambari-views/src/main/java/org/apache/ambari/view/ViewController.java
----------------------------------------------------------------------
diff --git a/ambari-views/src/main/java/org/apache/ambari/view/ViewController.java b/ambari-views/src/main/java/org/apache/ambari/view/ViewController.java
index d182de0..741ec2e 100644
--- a/ambari-views/src/main/java/org/apache/ambari/view/ViewController.java
+++ b/ambari-views/src/main/java/org/apache/ambari/view/ViewController.java
@@ -53,4 +53,23 @@ public interface ViewController {
    * @param viewVersion  the view version
    */
   public void registerListener(Listener listener, String viewName, String viewVersion);
+
+  /**
+   * Un-register the listener that is registered for the view identified by the
+   * given name.
+   *
+   * @param listener  the listener
+   * @param viewName  the view to listen to
+   */
+  public void unregisterListener(Listener listener, String viewName);
+
+  /**
+   * Un-register the listener that is registered for the view identified by the
+   * given name and version.
+   *
+   * @param listener     the listener
+   * @param viewName     the view to listen to
+   * @param viewVersion  the view version
+   */
+  public void unregisterListener(Listener listener, String viewName, String viewVersion);
 }


Mime
View raw message