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 static com.google.common.base.Preconditions.checkNotNull;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Collections;
14 import java.util.function.Consumer;
15 import org.apache.aries.blueprint.container.AbstractServiceReferenceRecipe;
16 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
17 import org.osgi.framework.ServiceReference;
18 import org.osgi.service.blueprint.container.ComponentDefinitionException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * A Blueprint bean recipe for a static OSGi service reference, meaning it obtains the service instance once
24 * and doesn't react to service removal. In addition the returned object is the actual service instance and
27 * @author Thomas Pantelis
29 class StaticServiceReferenceRecipe extends AbstractServiceReferenceRecipe {
30 private static final Logger LOG = LoggerFactory.getLogger(StaticServiceReferenceRecipe.class);
32 private static final SatisfactionListener NOOP_LISTENER = satisfiable -> {
36 private volatile ServiceReference<?> trackedServiceReference;
37 private volatile Object trackedService;
38 private Consumer<Object> serviceSatisfiedCallback;
40 StaticServiceReferenceRecipe(final String name, final ExtendedBlueprintContainer blueprintContainer,
41 final String interfaceClass) {
42 super(name, blueprintContainer, new MandatoryServiceReferenceMetadata(name, interfaceClass), null, null,
43 Collections.emptyList());
46 void startTracking(final Consumer<Object> newServiceSatisfiedCallback) {
47 this.serviceSatisfiedCallback = newServiceSatisfiedCallback;
48 super.start(NOOP_LISTENER);
51 @SuppressWarnings("rawtypes")
53 protected void track(final ServiceReference reference) {
57 @SuppressWarnings("rawtypes")
59 protected void untrack(final ServiceReference reference) {
60 LOG.debug("{}: In untrack {}", getName(), reference);
62 if (trackedServiceReference == reference) {
63 LOG.debug("{}: Current reference {} has been untracked", getName(), trackedServiceReference);
68 protected void retrack() {
69 LOG.debug("{}: In retrack", getName());
71 if (trackedServiceReference == null) {
72 trackedServiceReference = getBestServiceReference();
74 LOG.debug("{}: getBestServiceReference: {}", getName(), trackedServiceReference);
76 if (trackedServiceReference != null && serviceSatisfiedCallback != null) {
77 serviceSatisfiedCallback.accept(internalCreate());
83 // Disables "Either log or rethrow this exception" sonar warning
84 @SuppressWarnings("squid:S1166")
85 protected void doStop() {
86 LOG.debug("{}: In doStop", getName());
88 if (trackedServiceReference != null && trackedService != null) {
90 getBundleContextForServiceLookup().ungetService(trackedServiceReference);
91 } catch (final 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 trackedService = getServiceSecurely(requireNonNull(localTrackedServiceReference,
112 "trackedServiceReference is null"));
114 LOG.debug("{}: Returning service instance: {}", getName(), trackedService);
116 return checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
120 public boolean isStaticLifecycle() {