Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-api / src / main / java / org / opendaylight / controller / config / api / osgi / WaitingServiceTracker.java
1 /*
2  * Copyright (c) 2016, 2017 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.config.api.osgi;
9
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;
18
19 /**
20  * Tracker that waits for an OSGi service.
21  *
22  * @author Thomas Pantelis
23  */
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);
27
28     private final ServiceTracker<T, ?> tracker;
29     private final Class<T> serviceInterface;
30
31     private WaitingServiceTracker(final Class<T> serviceInterface, final ServiceTracker<T, ?> tracker) {
32         this.tracker = tracker;
33         this.serviceInterface = serviceInterface;
34     }
35
36     /**
37      * Waits for an OSGi services.
38      *
39      * @param timeoutInMillis
40      *            the timeout in millis
41      * @return the service instance
42      * @throws ServiceNotFoundException
43      *             if it times out or is interrupted
44      */
45     @SuppressWarnings("unchecked")
46     public T waitForService(final long timeoutInMillis) throws ServiceNotFoundException {
47         try {
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));
52             }
53
54             return service;
55         } catch (final InterruptedException e) {
56             throw new ServiceNotFoundException(
57                     String.format("Wait for OSGi service %s was interrrupted", serviceInterface));
58         }
59     }
60
61     /**
62      * Creates an instance.
63      *
64      * @param serviceInterface
65      *            the service interface
66      * @param context
67      *            the BundleContext
68      * @return new WaitingServiceTracker instance
69      */
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);
73         tracker.open();
74         return new WaitingServiceTracker<>(serviceInterface, tracker);
75     }
76
77     /**
78      * Creates an instance.
79      *
80      * @param serviceInterface
81      *            the service interface
82      * @param context
83      *            the BundleContext
84      * @param filter
85      *            the OSGi service filter
86      * @return new WaitingServiceTracker instance
87      */
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);
91         try {
92             ServiceTracker<T, ?> tracker = new ServiceTracker<>(context, context.createFilter(newFilter), null);
93             tracker.open();
94             return new WaitingServiceTracker<>(serviceInterface, tracker);
95         } catch (final InvalidSyntaxException e) {
96             throw new IllegalArgumentException(String.format("Invalid OSGi filter %s", newFilter), e);
97         }
98     }
99
100     @Override
101     public void close() {
102         tracker.close();
103     }
104 }