*/
package org.opendaylight.controller.blueprint.ext;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.Objects.requireNonNull;
+
import java.util.Collections;
import java.util.function.Consumer;
import org.apache.aries.blueprint.container.AbstractServiceReferenceRecipe;
-import org.apache.aries.blueprint.container.SatisfiableRecipe;
import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
import org.osgi.framework.ServiceReference;
import org.osgi.service.blueprint.container.ComponentDefinitionException;
class StaticServiceReferenceRecipe extends AbstractServiceReferenceRecipe {
private static final Logger LOG = LoggerFactory.getLogger(StaticServiceReferenceRecipe.class);
- private static final SatisfactionListener NOOP_LISTENER = new SatisfactionListener() {
- @Override
- public void notifySatisfaction(SatisfiableRecipe satisfiable) {
-
- }
+ private static final SatisfactionListener NOOP_LISTENER = satisfiable -> {
+ // Intentional NOOP
};
private volatile ServiceReference<?> trackedServiceReference;
private volatile Object trackedService;
private Consumer<Object> serviceSatisfiedCallback;
- StaticServiceReferenceRecipe(String name, ExtendedBlueprintContainer blueprintContainer,
- String interfaceClass) {
+ StaticServiceReferenceRecipe(final String name, final ExtendedBlueprintContainer blueprintContainer,
+ final String interfaceClass) {
super(name, blueprintContainer, new MandatoryServiceReferenceMetadata(name, interfaceClass), null, null,
Collections.emptyList());
}
- void startTracking(Consumer<Object> serviceSatisfiedCallback) {
- this.serviceSatisfiedCallback = serviceSatisfiedCallback;
+ void startTracking(final Consumer<Object> newServiceSatisfiedCallback) {
+ this.serviceSatisfiedCallback = newServiceSatisfiedCallback;
super.start(NOOP_LISTENER);
}
@SuppressWarnings("rawtypes")
@Override
- protected void track(ServiceReference reference) {
+ protected void track(final ServiceReference reference) {
retrack();
}
@SuppressWarnings("rawtypes")
@Override
- protected void untrack(ServiceReference reference) {
+ protected void untrack(final ServiceReference reference) {
LOG.debug("{}: In untrack {}", getName(), reference);
- if(trackedServiceReference == reference) {
- LOG.debug("{}: Current reference has been untracked", getName(), trackedServiceReference);
+ if (trackedServiceReference == reference) {
+ LOG.debug("{}: Current reference {} has been untracked", getName(), trackedServiceReference);
}
}
protected void retrack() {
LOG.debug("{}: In retrack", getName());
- if(trackedServiceReference == null) {
+ if (trackedServiceReference == null) {
trackedServiceReference = getBestServiceReference();
LOG.debug("{}: getBestServiceReference: {}", getName(), trackedServiceReference);
- if(trackedServiceReference != null && serviceSatisfiedCallback != null) {
+ if (trackedServiceReference != null && serviceSatisfiedCallback != null) {
serviceSatisfiedCallback.accept(internalCreate());
}
}
}
@Override
+ // Disables "Either log or rethrow this exception" sonar warning
+ @SuppressWarnings("squid:S1166")
protected void doStop() {
LOG.debug("{}: In doStop", getName());
- if(trackedServiceReference != null && trackedService != null) {
+ if (trackedServiceReference != null && trackedService != null) {
try {
getBundleContextForServiceLookup().ungetService(trackedServiceReference);
- } catch(IllegalStateException e) {
+ } catch (final IllegalStateException e) {
// In case the service no longer exists, ignore.
}
LOG.debug("{}: In internalCreate: trackedServiceReference: {}", getName(), localTrackedServiceReference);
// being paranoid - internalCreate should only get called once
- if(trackedService != null) {
+ if (trackedService != null) {
return trackedService;
}
- Preconditions.checkNotNull(localTrackedServiceReference, "trackedServiceReference is null");
-
- trackedService = getServiceSecurely(localTrackedServiceReference);
+ trackedService = getServiceSecurely(requireNonNull(localTrackedServiceReference,
+ "trackedServiceReference is null"));
LOG.debug("{}: Returning service instance: {}", getName(), trackedService);
- Preconditions.checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
+ return checkNotNull(trackedService, "getService() returned null for %s", localTrackedServiceReference);
+ }
- return trackedService;
+ @Override
+ public boolean isStaticLifecycle() {
+ return true;
}
}