Search in sources :

Example 1 with TimeoutException

use of cucumber.api.osgi.TimeoutException in project cucumber-jvm by cucumber.

the class OsgiObjectFactory method getService.

private Object getService(Class<?> type, long timeout, String filter) {
    final String serviceName = type.getName() + " " + filter;
    try {
        final long tryUntil = System.currentTimeMillis() + timeout;
        Object service = null;
        search: while (service == null) {
            final Collection<?> serviceReferences = bundleContext.getServiceReferences(type, filter.isEmpty() ? null : filter);
            for (Object serviceReference : serviceReferences) {
                service = bundleContext.getService((ServiceReference<?>) serviceReference);
                break search;
            }
            if (tryUntil < System.currentTimeMillis())
                break;
            Thread.sleep(20);
        }
        if (service == null)
            throw new ServiceNotFoundException(serviceName);
        return service;
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException(e);
    } catch (InterruptedException e) {
        throw new TimeoutException(serviceName);
    }
}
Also used : ServiceNotFoundException(cucumber.api.osgi.ServiceNotFoundException) Collection(java.util.Collection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TimeoutException(cucumber.api.osgi.TimeoutException)

Aggregations

ServiceNotFoundException (cucumber.api.osgi.ServiceNotFoundException)1 TimeoutException (cucumber.api.osgi.TimeoutException)1 Collection (java.util.Collection)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1