Fix modernization issues
[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 static com.google.common.base.Preconditions.checkNotNull;
11 import static java.util.Objects.requireNonNull;
12
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;
21
22 /**
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
25  * not a proxy.
26  *
27  * @author Thomas Pantelis
28  */
29 class StaticServiceReferenceRecipe extends AbstractServiceReferenceRecipe {
30     private static final Logger LOG = LoggerFactory.getLogger(StaticServiceReferenceRecipe.class);
31
32     private static final SatisfactionListener NOOP_LISTENER = satisfiable -> {
33         // Intentional NOOP
34     };
35
36     private volatile ServiceReference<?> trackedServiceReference;
37     private volatile Object trackedService;
38     private Consumer<Object> serviceSatisfiedCallback;
39
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());
44     }
45
46     void startTracking(final Consumer<Object> newServiceSatisfiedCallback) {
47         this.serviceSatisfiedCallback = newServiceSatisfiedCallback;
48         super.start(NOOP_LISTENER);
49     }
50
51     @SuppressWarnings("rawtypes")
52     @Override
53     protected void track(final ServiceReference reference) {
54         retrack();
55     }
56
57     @SuppressWarnings("rawtypes")
58     @Override
59     protected void untrack(final ServiceReference reference) {
60         LOG.debug("{}: In untrack {}", getName(), reference);
61
62         if (trackedServiceReference == reference) {
63             LOG.debug("{}: Current reference {} has been untracked", getName(), trackedServiceReference);
64         }
65     }
66
67     @Override
68     protected void retrack() {
69         LOG.debug("{}: In retrack", getName());
70
71         if (trackedServiceReference == null) {
72             trackedServiceReference = getBestServiceReference();
73
74             LOG.debug("{}: getBestServiceReference: {}", getName(), trackedServiceReference);
75
76             if (trackedServiceReference != null && serviceSatisfiedCallback != null) {
77                 serviceSatisfiedCallback.accept(internalCreate());
78             }
79         }
80     }
81
82     @Override
83     // Disables "Either log or rethrow this exception" sonar warning
84     @SuppressWarnings("squid:S1166")
85     protected void doStop() {
86         LOG.debug("{}: In doStop", getName());
87
88         if (trackedServiceReference != null && trackedService != null) {
89             try {
90                 getBundleContextForServiceLookup().ungetService(trackedServiceReference);
91             } catch (final IllegalStateException e) {
92                 // In case the service no longer exists, ignore.
93             }
94
95             trackedServiceReference = null;
96             trackedService = null;
97         }
98     }
99
100     @Override
101     protected Object internalCreate() throws ComponentDefinitionException {
102         ServiceReference<?> localTrackedServiceReference = trackedServiceReference;
103
104         LOG.debug("{}: In internalCreate: trackedServiceReference: {}", getName(), localTrackedServiceReference);
105
106         // being paranoid - internalCreate should only get called once
107         if (trackedService != null) {
108             return trackedService;
109         }
110
111         trackedService = getServiceSecurely(requireNonNull(localTrackedServiceReference,
112             "trackedServiceReference is null"));
113
114         LOG.debug("{}: Returning service instance: {}", getName(), trackedService);
115
116         return checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
117     }
118
119     @Override
120     public boolean isStaticLifecycle() {
121         return true;
122     }
123 }