/**
* Wrapper class to give listener a close method.
*/
- public class BGPListenerRegistration implements ListenerRegistration<BGPSessionListener> {
-
- private final BGPSessionListener listener;
-
+ public class BGPListenerRegistration extends ListenerRegistration<BGPSessionListener> {
private final BGPSession session;
public BGPListenerRegistration(final BGPSessionListener l, final BGPSession session) {
- this.listener = l;
+ super(l);
this.session = session;
}
@Override
- public void close() {
+ public void removeRegistration() {
this.session.close();
}
-
- @Override
- public BGPSessionListener getListener() {
- return this.listener;
- }
}
private final BGPDispatcher dispatcher;
import java.util.List;
import java.util.Set;
-import javax.annotation.concurrent.GuardedBy;
-
import org.opendaylight.protocol.bgp.parser.BGPSession;
import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
import org.opendaylight.protocol.bgp.parser.BGPTableType;
* This class has @Subscribe annotated methods which receive events from {@link EventBus} . Events are produced by
* {@link BGPMock}, and each instance notifies exactly one {@link BGPSessionListener}.
*/
-class EventBusRegistration implements ListenerRegistration<BGPSessionListener> {
+class EventBusRegistration extends ListenerRegistration<BGPSessionListener> {
private final EventBus eventBus;
- private final BGPSessionListener listener;
- @GuardedBy("this")
- private boolean closed = false;
public static EventBusRegistration createAndRegister(final EventBus eventBus, final BGPSessionListener listener,
final List<Notification> allPreviousMessages) {
}
private EventBusRegistration(final EventBus eventBus, final BGPSessionListener listener, final List<Notification> allPreviousMessages) {
+ super(listener);
this.eventBus = eventBus;
- this.listener = listener;
for (final Notification message : allPreviousMessages) {
sendMessage(listener, message);
}
}
@Override
- public synchronized void close() {
- if (this.closed) {
- return;
- }
+ public synchronized void removeRegistration() {
this.eventBus.unregister(this);
- this.closed = true;
}
private static void sendMessage(final BGPSessionListener listener, final Notification message) {
listener.onMessage(null, message);
}
}
-
- @Override
- public BGPSessionListener getListener() {
- return this.listener;
- }
}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.concepts;
+
+import javax.annotation.concurrent.GuardedBy;
+import javax.annotation.concurrent.ThreadSafe;
+
+/**
+ * Utility registration handle. It is a convenience for register-style method
+ * which can return an AutoCloseable realized by a subclass of this class.
+ * Invoking the close() method triggers unregistration of the state the method
+ * installed.
+ */
+@ThreadSafe
+public abstract class AbstractRegistration implements AutoCloseable {
+ @GuardedBy("this")
+ private boolean closed = false;
+
+ /**
+ * Remove the state referenced by this registration. This method is
+ * guaranteed to be called at most once. The referenced state must be
+ * retained until this method is invoked.
+ */
+ protected abstract void removeRegistration();
+
+ @Override
+ public final synchronized void close() {
+ if (!closed) {
+ closed = true;
+ removeRegistration();
+ }
+ }
+}
*/
package org.opendaylight.protocol.concepts;
-import java.io.Closeable;
import java.util.EventListener;
+import javax.annotation.concurrent.ThreadSafe;
+
+import com.google.common.base.Preconditions;
+
/**
* An interface representing a listener registration. Objects offering
* the ability to register listener should return an implementation of this
*
* @param <T> template reference to associated EventListener implementation
*/
-public interface ListenerRegistration<T extends EventListener> extends Closeable {
+@ThreadSafe
+public abstract class ListenerRegistration<T extends EventListener> extends AbstractRegistration {
+ protected final T listener;
+
+ protected ListenerRegistration(final T listener) {
+ this.listener = Preconditions.checkNotNull(listener);
+ }
+
/**
* Access the listener object associated with this registration.
*
* @return Associated listener.
*/
- public T getListener();
-
- @Override
- public void close();
+ public final T getListener() {
+ return listener;
+ }
}