2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.blueprint.ext;
10 import com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.function.Consumer;
13 import org.apache.aries.blueprint.container.AbstractServiceReferenceRecipe;
14 import org.apache.aries.blueprint.container.SatisfiableRecipe;
15 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
16 import org.osgi.framework.ServiceReference;
17 import org.osgi.service.blueprint.container.ComponentDefinitionException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * A Blueprint bean recipe for a static OSGi service reference, meaning it obtains the service instance once
23 * and doesn't react to service removal. In addition the returned object is the actual service instance and
26 * @author Thomas Pantelis
28 class StaticServiceReferenceRecipe extends AbstractServiceReferenceRecipe {
29 private static final Logger LOG = LoggerFactory.getLogger(StaticServiceReferenceRecipe.class);
31 private static final SatisfactionListener NOOP_LISTENER = new SatisfactionListener() {
33 public void notifySatisfaction(SatisfiableRecipe satisfiable) {
38 private volatile ServiceReference<?> trackedServiceReference;
39 private volatile Object trackedService;
40 private Consumer<Object> serviceSatisfiedCallback;
42 StaticServiceReferenceRecipe(String name, ExtendedBlueprintContainer blueprintContainer,
43 String interfaceClass) {
44 super(name, blueprintContainer, new MandatoryServiceReferenceMetadata(name, interfaceClass), null, null,
45 Collections.emptyList());
48 void startTracking(Consumer<Object> serviceSatisfiedCallback) {
49 this.serviceSatisfiedCallback = serviceSatisfiedCallback;
50 super.start(NOOP_LISTENER);
53 @SuppressWarnings("rawtypes")
55 protected void track(ServiceReference reference) {
59 @SuppressWarnings("rawtypes")
61 protected void untrack(ServiceReference reference) {
62 LOG.debug("{}: In untrack {}", getName(), reference);
64 if(trackedServiceReference == reference) {
65 LOG.debug("{}: Current reference has been untracked", getName(), trackedServiceReference);
70 protected void retrack() {
71 LOG.debug("{}: In retrack", getName());
73 if(trackedServiceReference == null) {
74 trackedServiceReference = getBestServiceReference();
76 LOG.debug("{}: getBestServiceReference: {}", getName(), trackedServiceReference);
78 if(trackedServiceReference != null && serviceSatisfiedCallback != null) {
79 serviceSatisfiedCallback.accept(internalCreate());
85 protected void doStop() {
86 LOG.debug("{}: In doStop", getName());
88 if(trackedServiceReference != null && trackedService != null) {
90 getBundleContextForServiceLookup().ungetService(trackedServiceReference);
91 } catch(IllegalStateException e) {
92 // In case the service no longer exists, ignore.
95 trackedServiceReference = null;
96 trackedService = null;
101 protected Object internalCreate() throws ComponentDefinitionException {
102 ServiceReference<?> localTrackedServiceReference = trackedServiceReference;
104 LOG.debug("{}: In internalCreate: trackedServiceReference: {}", getName(), localTrackedServiceReference);
106 // being paranoid - internalCreate should only get called once
107 if(trackedService != null) {
108 return trackedService;
111 Preconditions.checkNotNull(localTrackedServiceReference, "trackedServiceReference is null");
113 trackedService = getServiceSecurely(localTrackedServiceReference);
115 LOG.debug("{}: Returning service instance: {}", getName(), trackedService);
117 Preconditions.checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
119 return trackedService;