BUG-8665: fix memory leak around RangeSets
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DelayedListenerRegistration.java
index 8f18cb74dc79c6cd7cb6c08b03640b9df173dcd8..b0024153d37603a072cd002f9158051ddcf19ac9 100644 (file)
@@ -7,53 +7,46 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
-import com.google.common.base.Optional;
+import akka.actor.ActorRef;
 import java.util.EventListener;
-import java.util.Map.Entry;
 import javax.annotation.concurrent.GuardedBy;
+import org.opendaylight.controller.cluster.datastore.messages.ListenerRegistrationMessage;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 
-abstract class DelayedListenerRegistration<L extends EventListener, R> implements ListenerRegistration<L> {
-    private final R registrationMessage;
-    private volatile ListenerRegistration<L> delegate;
+abstract class DelayedListenerRegistration<L extends EventListener, M extends ListenerRegistrationMessage>
+        implements ListenerRegistration<L> {
+    private final M registrationMessage;
+    private final ActorRef registrationActor;
 
     @GuardedBy("this")
     private boolean closed;
 
-    protected DelayedListenerRegistration(R registrationMessage) {
+    protected DelayedListenerRegistration(M registrationMessage, ActorRef registrationActor) {
         this.registrationMessage = registrationMessage;
+        this.registrationActor = registrationActor;
     }
 
-    R getRegistrationMessage() {
+    M getRegistrationMessage() {
         return registrationMessage;
     }
 
-    ListenerRegistration<L> getDelegate() {
-        return delegate;
-    }
-
-    synchronized <LR extends ListenerRegistration<L>> void createDelegate(
-            final LeaderLocalDelegateFactory<R, LR, Optional<DataTreeCandidate>> factory) {
+    synchronized void createDelegate(final AbstractDataListenerSupport<L, M, ?> support) {
         if (!closed) {
-            final Entry<LR, Optional<DataTreeCandidate>> res = factory.createDelegate(registrationMessage);
-            this.delegate = res.getKey();
+            support.doRegistration(registrationMessage, registrationActor);
         }
     }
 
     @Override
     public L getInstance() {
-        final ListenerRegistration<L> d = delegate;
-        return d == null ? null : (L)d.getInstance();
+        // We could return null if the delegate is not set yet. In reality though, we do not and should not ever call
+        // this method on DelayedListenerRegistration instances but, since we have to provide an implementation to
+        // satisfy the interface, we throw UnsupportedOperationException to avoid possibly returning null.
+        throw new UnsupportedOperationException(
+                "getInstance should not be called on this instance since it could be null");
     }
 
     @Override
     public synchronized void close() {
-        if (!closed) {
-            closed = true;
-            if (delegate != null) {
-                delegate.close();
-            }
-        }
+        closed = true;
     }
 }