Remove forwarded-aware event broker

Replacing the Gerrit's event broken with another one through a libModule
is not an option.

Catch the forwarded events instead down in the broker publisher and
avoid to generate the output message.

Change-Id: If310ba65709897c058921354e76d2cf15de72efe
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/broker/BrokerPublisher.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/broker/BrokerPublisher.java
index cce9cc5..eff8824 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/broker/BrokerPublisher.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/broker/BrokerPublisher.java
@@ -24,6 +24,7 @@
 import com.googlesource.gerrit.plugins.multisite.InstanceId;
 import com.googlesource.gerrit.plugins.multisite.MessageLogger;
 import com.googlesource.gerrit.plugins.multisite.MessageLogger.Direction;
+import com.googlesource.gerrit.plugins.multisite.forwarder.Context;
 import com.googlesource.gerrit.plugins.multisite.forwarder.events.EventFamily;
 import com.googlesource.gerrit.plugins.multisite.kafka.consumer.SourceAwareEventWrapper;
 import java.util.UUID;
@@ -63,6 +64,10 @@
   }
 
   public boolean publishEvent(EventFamily eventType, Event event) {
+    if (Context.isForwardedEvent()) {
+      return true;
+    }
+
     SourceAwareEventWrapper brokerEvent = toBrokerEvent(event);
     msgLog.log(Direction.PUBLISH, brokerEvent);
     return session.publishEvent(eventType, getPayload(brokerEvent));
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedAwareEventBroker.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedAwareEventBroker.java
deleted file mode 100644
index 30b0b33..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedAwareEventBroker.java
+++ /dev/null
@@ -1,54 +0,0 @@
-// Copyright (C) 2016 The Android Open Source Project
-//
-// 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 com.googlesource.gerrit.plugins.multisite.forwarder;
-
-import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gerrit.server.events.Event;
-import com.google.gerrit.server.events.EventBroker;
-import com.google.gerrit.server.events.EventListener;
-import com.google.gerrit.server.events.UserScopedEventListener;
-import com.google.gerrit.server.notedb.ChangeNotes.Factory;
-import com.google.gerrit.server.permissions.PermissionBackend;
-import com.google.gerrit.server.plugincontext.PluginSetContext;
-import com.google.gerrit.server.project.ProjectCache;
-import com.google.inject.Inject;
-import com.google.inject.Provider;
-
-class ForwardedAwareEventBroker extends EventBroker {
-
-  @Inject
-  ForwardedAwareEventBroker(
-      PluginSetContext<UserScopedEventListener> listeners,
-      PluginSetContext<EventListener> unrestrictedListeners,
-      PermissionBackend permissionBackend,
-      ProjectCache projectCache,
-      Factory notesFactory,
-      Provider<ReviewDb> dbProvider) {
-    super(
-        listeners,
-        unrestrictedListeners,
-        permissionBackend,
-        projectCache,
-        notesFactory,
-        dbProvider);
-  }
-
-  @Override
-  protected void fireEventForUnrestrictedListeners(Event event) {
-    if (!Context.isForwardedEvent()) {
-      super.fireEventForUnrestrictedListeners(event);
-    }
-  }
-}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwarderModule.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwarderModule.java
index 6df5f51..ca17004 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwarderModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwarderModule.java
@@ -14,16 +14,13 @@
 
 package com.googlesource.gerrit.plugins.multisite.forwarder;
 
-import com.google.gerrit.extensions.registration.DynamicItem;
 import com.google.gerrit.extensions.registration.DynamicSet;
-import com.google.gerrit.server.events.EventDispatcher;
 import com.google.inject.AbstractModule;
 
 public class ForwarderModule extends AbstractModule {
 
   @Override
   protected void configure() {
-    DynamicItem.bind(binder(), EventDispatcher.class).to(ForwardedAwareEventBroker.class);
     DynamicSet.setOf(binder(), CacheEvictionForwarder.class);
     DynamicSet.setOf(binder(), IndexEventForwarder.class);
     DynamicSet.setOf(binder(), ProjectListUpdateForwarder.class);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedAwareEventBrokerTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedAwareEventBrokerTest.java
deleted file mode 100644
index c070e76..0000000
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedAwareEventBrokerTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
-// Copyright (C) 2016 The Android Open Source Project
-//
-// 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 com.googlesource.gerrit.plugins.multisite.forwarder;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-
-import com.google.gerrit.extensions.registration.DynamicSet;
-import com.google.gerrit.server.events.Event;
-import com.google.gerrit.server.events.EventListener;
-import com.google.gerrit.server.plugincontext.PluginContext.PluginMetrics;
-import com.google.gerrit.server.plugincontext.PluginSetContext;
-import org.junit.Before;
-import org.junit.Test;
-
-public class ForwardedAwareEventBrokerTest {
-
-  private EventListener listenerMock;
-  private ForwardedAwareEventBroker broker;
-  private Event event = new Event(null) {};
-
-  @Before
-  public void setUp() {
-    PluginMetrics mockMetrics = mock(PluginMetrics.class);
-    listenerMock = mock(EventListener.class);
-    DynamicSet<EventListener> set = DynamicSet.emptySet();
-    set.add("multi-site", listenerMock);
-    PluginSetContext<EventListener> listeners = new PluginSetContext<>(set, mockMetrics);
-    broker = new ForwardedAwareEventBroker(null, listeners, null, null, null, null);
-  }
-
-  @Test
-  public void shouldDispatchEvent() {
-    broker.fireEventForUnrestrictedListeners(event);
-    verify(listenerMock).onEvent(event);
-  }
-
-  @Test
-  public void shouldNotDispatchForwardedEvents() {
-    Context.setForwardedEvent(true);
-    try {
-      broker.fireEventForUnrestrictedListeners(event);
-    } finally {
-      Context.unsetForwardedEvent();
-    }
-    verifyZeroInteractions(listenerMock);
-  }
-}