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 8eb595df03856decab3259ddfaa13a9fc96fd670..b0024153d37603a072cd002f9158051ddcf19ac9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,49 +7,46 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
-import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
+import akka.actor.ActorRef;
+import java.util.EventListener;
+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.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-final class DelayedListenerRegistration implements
-    ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> {
+abstract class DelayedListenerRegistration<L extends EventListener, M extends ListenerRegistrationMessage>
+        implements ListenerRegistration<L> {
+    private final M registrationMessage;
+    private final ActorRef registrationActor;
 
-    private volatile boolean closed;
+    @GuardedBy("this")
+    private boolean closed;
 
-    private final RegisterChangeListener registerChangeListener;
-
-    private volatile ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
-                                                         NormalizedNode<?, ?>>> delegate;
-
-    DelayedListenerRegistration(final RegisterChangeListener registerChangeListener) {
-        this.registerChangeListener = registerChangeListener;
-    }
-
-    void setDelegate( final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
-                                        NormalizedNode<?, ?>>> registration) {
-        this.delegate = registration;
+    protected DelayedListenerRegistration(M registrationMessage, ActorRef registrationActor) {
+        this.registrationMessage = registrationMessage;
+        this.registrationActor = registrationActor;
     }
 
-    boolean isClosed() {
-        return closed;
+    M getRegistrationMessage() {
+        return registrationMessage;
     }
 
-    RegisterChangeListener getRegisterChangeListener() {
-        return registerChangeListener;
+    synchronized void createDelegate(final AbstractDataListenerSupport<L, M, ?> support) {
+        if (!closed) {
+            support.doRegistration(registrationMessage, registrationActor);
+        }
     }
 
     @Override
-    public AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> getInstance() {
-        return delegate != null ? delegate.getInstance() : null;
+    public L 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 void close() {
+    public synchronized void close() {
         closed = true;
-        if(delegate != null) {
-            delegate.close();
-        }
     }
-}
\ No newline at end of file
+}