368980565525c600bad52cd378302a3262400a25
[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
32     };
33
34     private volatile ServiceReference<?> trackedServiceReference;
35     private volatile Object trackedService;
36     private Consumer<Object> serviceSatisfiedCallback;
37
38     StaticServiceReferenceRecipe(String name, ExtendedBlueprintContainer blueprintContainer,
39             String interfaceClass) {
40         super(name, blueprintContainer, new MandatoryServiceReferenceMetadata(name, interfaceClass), null, null,
41                 Collections.emptyList());
42     }
43
44     void startTracking(Consumer<Object> newServiceSatisfiedCallback) {
45         this.serviceSatisfiedCallback = newServiceSatisfiedCallback;
46         super.start(NOOP_LISTENER);
47     }
48
49     @SuppressWarnings("rawtypes")
50     @Override
51     protected void track(ServiceReference reference) {
52         retrack();
53     }
54
55     @SuppressWarnings("rawtypes")
56     @Override
57     protected void untrack(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     protected void doStop() {
82         LOG.debug("{}: In doStop", getName());
83
84         if (trackedServiceReference != null && trackedService != null) {
85             try {
86                 getBundleContextForServiceLookup().ungetService(trackedServiceReference);
87             } catch (IllegalStateException e) {
88                 // In case the service no longer exists, ignore.
89             }
90
91             trackedServiceReference = null;
92             trackedService = null;
93         }
94     }
95
96     @Override
97     protected Object internalCreate() throws ComponentDefinitionException {
98         ServiceReference<?> localTrackedServiceReference = trackedServiceReference;
99
100         LOG.debug("{}: In internalCreate: trackedServiceReference: {}", getName(), localTrackedServiceReference);
101
102         // being paranoid - internalCreate should only get called once
103         if (trackedService != null) {
104             return trackedService;
105         }
106
107         Preconditions.checkNotNull(localTrackedServiceReference, "trackedServiceReference is null");
108
109         trackedService = getServiceSecurely(localTrackedServiceReference);
110
111         LOG.debug("{}: Returning service instance: {}", getName(), trackedService);
112
113         Preconditions.checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
114
115         return trackedService;
116     }
117 }