X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fcore%2Fapi%2FAbstractConsumer.java;h=46e9cdcd718917c4f26f33647eb1bf197a477d3f;hp=5e13aad49918ccf6c4e79a08f5411abd31066c10;hb=e2607370f5ac443a5a2f1f00f693f82a0b57161d;hpb=6b64494fd8e4654a298312afb4b8e6e827b75d5d diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/sal/core/api/AbstractConsumer.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/sal/core/api/AbstractConsumer.java index 5e13aad499..46e9cdcd71 100644 --- a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/sal/core/api/AbstractConsumer.java +++ b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/sal/core/api/AbstractConsumer.java @@ -1,40 +1,76 @@ +/* + * 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.controller.sal.core.api; import java.util.Collection; import java.util.Collections; - import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; -public abstract class AbstractConsumer implements Consumer, BundleActivator { - - Broker broker; - ServiceReference brokerRef; - @Override - public final void start(BundleContext context) throws Exception { - brokerRef = context.getServiceReference(Broker.class); - broker = context.getService(brokerRef); +public abstract class AbstractConsumer implements Consumer, BundleActivator,ServiceTrackerCustomizer { - this.startImpl(context); + private BundleContext context; + private ServiceTracker tracker; + private Broker broker; - broker.registerConsumer(this,context); + @Override + public final void start(final BundleContext bundleContext) throws Exception { + this.context = bundleContext; + this.startImpl(bundleContext); + tracker = new ServiceTracker<>(bundleContext, Broker.class, this); + tracker.open(); } - public abstract void startImpl(BundleContext context); - @Override - public final void stop(BundleContext context) throws Exception { + public final void stop(final BundleContext bundleContext) throws Exception { + stopImpl(bundleContext); broker = null; - if(brokerRef != null) { - context.ungetService(brokerRef); + + if (tracker != null) { + tracker.close(); } } - + protected void startImpl(final BundleContext bundleContext) { + // NOOP + } + + protected void stopImpl(final BundleContext bundleContext) { + // NOOP + } + @Override public Collection getConsumerFunctionality() { return Collections.emptySet(); } + + @Override + public Broker addingService(final ServiceReference reference) { + if (broker == null && context != null) { + broker = context.getService(reference); + broker.registerConsumer(this, context); + return broker; + } + + return null; + } + + @Override + public void modifiedService(final ServiceReference reference, final Broker service) { + // NOOP + } + + @Override + public void removedService(final ServiceReference reference, final Broker service) { + stopImpl(context); + } }