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.services.ExtendedBlueprintContainer;
15 import org.osgi.framework.ServiceReference;
16 import org.osgi.service.blueprint.container.ComponentDefinitionException;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * A Blueprint bean recipe for a static OSGi service reference, meaning it obtains the service instance once
22 * and doesn't react to service removal. In addition the returned object is the actual service instance and
25 * @author Thomas Pantelis
27 class StaticServiceReferenceRecipe extends AbstractServiceReferenceRecipe {
28 private static final Logger LOG = LoggerFactory.getLogger(StaticServiceReferenceRecipe.class);
30 private static final SatisfactionListener NOOP_LISTENER = satisfiable -> {
34 private volatile ServiceReference<?> trackedServiceReference;
35 private volatile Object trackedService;
36 private Consumer<Object> serviceSatisfiedCallback;
38 StaticServiceReferenceRecipe(final String name, final ExtendedBlueprintContainer blueprintContainer,
39 final String interfaceClass) {
40 super(name, blueprintContainer, new MandatoryServiceReferenceMetadata(name, interfaceClass), null, null,
41 Collections.emptyList());
44 void startTracking(final Consumer<Object> newServiceSatisfiedCallback) {
45 this.serviceSatisfiedCallback = newServiceSatisfiedCallback;
46 super.start(NOOP_LISTENER);
49 @SuppressWarnings("rawtypes")
51 protected void track(final ServiceReference reference) {
55 @SuppressWarnings("rawtypes")
57 protected void untrack(final ServiceReference reference) {
58 LOG.debug("{}: In untrack {}", getName(), reference);
60 if (trackedServiceReference == reference) {
61 LOG.debug("{}: Current reference {} has been untracked", getName(), trackedServiceReference);
66 protected void retrack() {
67 LOG.debug("{}: In retrack", getName());
69 if (trackedServiceReference == null) {
70 trackedServiceReference = getBestServiceReference();
72 LOG.debug("{}: getBestServiceReference: {}", getName(), trackedServiceReference);
74 if (trackedServiceReference != null && serviceSatisfiedCallback != null) {
75 serviceSatisfiedCallback.accept(internalCreate());
81 // Disables "Either log or rethrow this exception" sonar warning
82 @SuppressWarnings("squid:S1166")
83 protected void doStop() {
84 LOG.debug("{}: In doStop", getName());
86 if (trackedServiceReference != null && trackedService != null) {
88 getBundleContextForServiceLookup().ungetService(trackedServiceReference);
89 } catch (final IllegalStateException e) {
90 // In case the service no longer exists, ignore.
93 trackedServiceReference = null;
94 trackedService = null;
99 protected Object internalCreate() throws ComponentDefinitionException {
100 ServiceReference<?> localTrackedServiceReference = trackedServiceReference;
102 LOG.debug("{}: In internalCreate: trackedServiceReference: {}", getName(), localTrackedServiceReference);
104 // being paranoid - internalCreate should only get called once
105 if (trackedService != null) {
106 return trackedService;
109 Preconditions.checkNotNull(localTrackedServiceReference, "trackedServiceReference is null");
111 trackedService = getServiceSecurely(localTrackedServiceReference);
113 LOG.debug("{}: Returning service instance: {}", getName(), trackedService);
115 Preconditions.checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
117 return trackedService;
121 public boolean isStaticLifecycle() {