0466aab6c20fe0649d572c763b540e4e9cd32099
[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.container.SatisfiableRecipe;
15 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
16 import org.osgi.framework.ServiceReference;
17 import org.osgi.service.blueprint.container.ComponentDefinitionException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * A Blueprint bean recipe for a static OSGi service reference, meaning it obtains the service instance once
23  * and doesn't react to service removal. In addition the returned object is the actual service instance and
24  * not a proxy.
25  *
26  * @author Thomas Pantelis
27  */
28 class StaticServiceReferenceRecipe extends AbstractServiceReferenceRecipe {
29     private static final Logger LOG = LoggerFactory.getLogger(StaticServiceReferenceRecipe.class);
30
31     private static final SatisfactionListener NOOP_LISTENER = new SatisfactionListener() {
32         @Override
33         public void notifySatisfaction(SatisfiableRecipe satisfiable) {
34
35         }
36     };
37
38     private volatile ServiceReference<?> trackedServiceReference;
39     private volatile Object trackedService;
40     private Consumer<Object> serviceSatisfiedCallback;
41
42     StaticServiceReferenceRecipe(String name, ExtendedBlueprintContainer blueprintContainer,
43             String interfaceClass) {
44         super(name, blueprintContainer, new MandatoryServiceReferenceMetadata(name, interfaceClass), null, null,
45                 Collections.emptyList());
46     }
47
48     void startTracking(Consumer<Object> serviceSatisfiedCallback) {
49         this.serviceSatisfiedCallback = serviceSatisfiedCallback;
50         super.start(NOOP_LISTENER);
51     }
52
53     @SuppressWarnings("rawtypes")
54     @Override
55     protected void track(ServiceReference reference) {
56         retrack();
57     }
58
59     @SuppressWarnings("rawtypes")
60     @Override
61     protected void untrack(ServiceReference reference) {
62         LOG.debug("{}: In untrack {}", getName(), reference);
63
64         if(trackedServiceReference == reference) {
65             LOG.debug("{}: Current reference has been untracked", getName(), trackedServiceReference);
66         }
67     }
68
69     @Override
70     protected void retrack() {
71         LOG.debug("{}: In retrack", getName());
72
73         if(trackedServiceReference == null) {
74             trackedServiceReference = getBestServiceReference();
75
76             LOG.debug("{}: getBestServiceReference: {}", getName(), trackedServiceReference);
77
78             if(trackedServiceReference != null && serviceSatisfiedCallback != null) {
79                 serviceSatisfiedCallback.accept(internalCreate());
80             }
81         }
82     }
83
84     @Override
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(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         Preconditions.checkNotNull(localTrackedServiceReference, "trackedServiceReference is null");
112
113         trackedService = getServiceSecurely(localTrackedServiceReference);
114
115         LOG.debug("{}: Returning service instance: {}", getName(), trackedService);
116
117         Preconditions.checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
118
119         return trackedService;
120     }
121 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.