Fix findbugs warnings
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / StaticServiceReferenceRecipe.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.blueprint.ext;
9
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;
19
20 /**
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
23  * not a proxy.
24  *
25  * @author Thomas Pantelis
26  */
27 class StaticServiceReferenceRecipe extends AbstractServiceReferenceRecipe {
28     private static final Logger LOG = LoggerFactory.getLogger(StaticServiceReferenceRecipe.class);
29
30     private static final SatisfactionListener NOOP_LISTENER = satisfiable -> {
31         // Intentional NOOP
32     };
33
34     private volatile ServiceReference<?> trackedServiceReference;
35     private volatile Object trackedService;
36     private Consumer<Object> serviceSatisfiedCallback;
37
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());
42     }
43
44     void startTracking(final Consumer<Object> newServiceSatisfiedCallback) {
45         this.serviceSatisfiedCallback = newServiceSatisfiedCallback;
46         super.start(NOOP_LISTENER);
47     }
48
49     @SuppressWarnings("rawtypes")
50     @Override
51     protected void track(final ServiceReference reference) {
52         retrack();
53     }
54
55     @SuppressWarnings("rawtypes")
56     @Override
57     protected void untrack(final ServiceReference reference) {
58         LOG.debug("{}: In untrack {}", getName(), reference);
59
60         if (trackedServiceReference == reference) {
61             LOG.debug("{}: Current reference {} has been untracked", getName(), trackedServiceReference);
62         }
63     }
64
65     @Override
66     protected void retrack() {
67         LOG.debug("{}: In retrack", getName());
68
69         if (trackedServiceReference == null) {
70             trackedServiceReference = getBestServiceReference();
71
72             LOG.debug("{}: getBestServiceReference: {}", getName(), trackedServiceReference);
73
74             if (trackedServiceReference != null && serviceSatisfiedCallback != null) {
75                 serviceSatisfiedCallback.accept(internalCreate());
76             }
77         }
78     }
79
80     @Override
81     // Disables "Either log or rethrow this exception" sonar warning
82     @SuppressWarnings("squid:S1166")
83     protected void doStop() {
84         LOG.debug("{}: In doStop", getName());
85
86         if (trackedServiceReference != null && trackedService != null) {
87             try {
88                 getBundleContextForServiceLookup().ungetService(trackedServiceReference);
89             } catch (final IllegalStateException e) {
90                 // In case the service no longer exists, ignore.
91             }
92
93             trackedServiceReference = null;
94             trackedService = null;
95         }
96     }
97
98     @Override
99     protected Object internalCreate() throws ComponentDefinitionException {
100         ServiceReference<?> localTrackedServiceReference = trackedServiceReference;
101
102         LOG.debug("{}: In internalCreate: trackedServiceReference: {}", getName(), localTrackedServiceReference);
103
104         // being paranoid - internalCreate should only get called once
105         if (trackedService != null) {
106             return trackedService;
107         }
108
109         Preconditions.checkNotNull(localTrackedServiceReference, "trackedServiceReference is null");
110
111         trackedService = getServiceSecurely(localTrackedServiceReference);
112
113         LOG.debug("{}: Returning service instance: {}", getName(), trackedService);
114
115         Preconditions.checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
116
117         return trackedService;
118     }
119 }