2 * Copyright (c) 2016, 2017 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.api.osgi;
10 import java.util.concurrent.TimeUnit;
11 import javax.annotation.Nonnull;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.framework.Constants;
14 import org.osgi.framework.InvalidSyntaxException;
15 import org.osgi.util.tracker.ServiceTracker;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Tracker that waits for an OSGi service.
22 * @author Thomas Pantelis
24 public final class WaitingServiceTracker<T> implements AutoCloseable {
25 private static final Logger LOG = LoggerFactory.getLogger(WaitingServiceTracker.class);
26 public static final long FIVE_MINUTES = TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES);
28 private final ServiceTracker<T, ?> tracker;
29 private final Class<T> serviceInterface;
31 private WaitingServiceTracker(final Class<T> serviceInterface, final ServiceTracker<T, ?> tracker) {
32 this.tracker = tracker;
33 this.serviceInterface = serviceInterface;
37 * Waits for an OSGi services.
39 * @param timeoutInMillis
40 * the timeout in millis
41 * @return the service instance
42 * @throws ServiceNotFoundException
43 * if it times out or is interrupted
45 @SuppressWarnings("unchecked")
46 public T waitForService(final long timeoutInMillis) throws ServiceNotFoundException {
48 T service = (T) tracker.waitForService(timeoutInMillis);
49 if (service == null) {
50 throw new ServiceNotFoundException(
51 String.format("OSGi Service %s was not found after %d ms", serviceInterface, timeoutInMillis));
55 } catch (final InterruptedException e) {
56 throw new ServiceNotFoundException(
57 String.format("Wait for OSGi service %s was interrrupted", serviceInterface));
62 * Creates an instance.
64 * @param serviceInterface
65 * the service interface
68 * @return new WaitingServiceTracker instance
70 public static <T> WaitingServiceTracker<T> create(@Nonnull final Class<T> serviceInterface,
71 @Nonnull final BundleContext context) {
72 ServiceTracker<T, ?> tracker = new ServiceTracker<>(context, serviceInterface, null);
74 return new WaitingServiceTracker<>(serviceInterface, tracker);
78 * Creates an instance.
80 * @param serviceInterface
81 * the service interface
85 * the OSGi service filter
86 * @return new WaitingServiceTracker instance
88 public static <T> WaitingServiceTracker<T> create(@Nonnull final Class<T> serviceInterface,
89 @Nonnull final BundleContext context, @Nonnull final String filter) {
90 String newFilter = String.format("(&(%s=%s)%s)", Constants.OBJECTCLASS, serviceInterface.getName(), filter);
92 ServiceTracker<T, ?> tracker = new ServiceTracker<>(context, context.createFilter(newFilter), null);
94 return new WaitingServiceTracker<>(serviceInterface, tracker);
95 } catch (final InvalidSyntaxException e) {
96 throw new IllegalArgumentException(String.format("Invalid OSGi filter %s", newFilter), e);
101 public void close() {