Package: ServiceUtils

ServiceUtils

nameinstructionbranchcomplexitylinemethod
ServiceUtils()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getInitialisedWaitLimit(BundleContext)
M: 31 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getPotentiallyDelayedService(BundleContext, Class)
M: 44 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getWaitLimitSeconds()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasText(String)
M: 16 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
readBundleStartupWaitLimit(BundleContext)
M: 39 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
readFrameworkProperty(String, BundleContext)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
sleepABitMore()
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2012 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.kernel.osgi.framework;
13:
14: import java.util.concurrent.TimeUnit;
15: import java.util.concurrent.TimeoutException;
16:
17: import org.osgi.framework.BundleContext;
18: import org.slf4j.Logger;
19: import org.slf4j.LoggerFactory;
20:
21: /**
22: * {@link ServiceUtils} is a collection of OSGi service utilities for use by the kernel.
23: * <p />
24: *
25: * <strong>Concurrent Semantics</strong><br />
26: * Thread safe
27: */
28: public class ServiceUtils {
29:
30: public static final String PROPERTY_KERNEL_STARTUP_WAIT_LIMIT = "org.eclipse.virgo.kernel.startup.wait.limit";
31:
32: private static final Logger LOGGER = LoggerFactory.getLogger(ServiceUtils.class);
33:
34: private static final int DEFAULT_STARTUP_WAIT_LIMIT = 180; // 3 minutes
35:
36: private static volatile int maxSecondsWaitForService = DEFAULT_STARTUP_WAIT_LIMIT;
37:
38: private static volatile long maxMillisWaitForService = 0;
39:
40: private static final Object monitor = new Object();
41:
42: /**
43: * Wait for a service of the given class but throw a TimeoutException if this takes longer than the number of
44: * seconds configured in the framework property org.eclipse.virgo.kernel.startup.wait.limit.
45: */
46: public static <T> T getPotentiallyDelayedService(BundleContext context, Class<T> serviceClass) throws TimeoutException, InterruptedException {
47: T service = null;
48: OsgiServiceHolder<T> serviceHolder;
49: long millisWaited = 0;
50:• while (service == null && millisWaited <= getInitialisedWaitLimit(context)) {
51: try {
52: serviceHolder = OsgiFrameworkUtils.getService(context, serviceClass);
53:• if (serviceHolder != null) {
54: service = serviceHolder.getService();
55: } else {
56: millisWaited += sleepABitMore();
57: }
58: } catch (IllegalStateException e) {
59: }
60: }
61:• if (service == null) {
62: throw new TimeoutException(serviceClass.getName());
63: }
64: return service;
65: }
66:
67: private static long getInitialisedWaitLimit(BundleContext context) {
68:• if (maxMillisWaitForService == 0) {
69: synchronized (monitor) {
70:• if (maxMillisWaitForService == 0) {
71: maxSecondsWaitForService = readBundleStartupWaitLimit(context);
72: maxMillisWaitForService = TimeUnit.SECONDS.toMillis(maxSecondsWaitForService);
73: }
74: }
75: }
76: return maxMillisWaitForService;
77: }
78:
79: /**
80: * Returns the service wait limit in seconds. This method will only return the correct value after
81: * getInitialisedWaitLimit has been called, for instance after TimeoutException has been thrown from
82: * getPotentiallyDelayedService.
83: *
84: * @return the service wait limit in seconds or 0 if this has not been initialised
85: */
86: public static long getWaitLimitSeconds() {
87: return maxSecondsWaitForService;
88: }
89:
90: private static int readBundleStartupWaitLimit(BundleContext context) {
91: String waitLimitProperty = readFrameworkProperty(PROPERTY_KERNEL_STARTUP_WAIT_LIMIT, context);
92:• if (!hasText(waitLimitProperty)) {
93: return DEFAULT_STARTUP_WAIT_LIMIT;
94: }
95:
96: try {
97: return Integer.parseInt(waitLimitProperty);
98: } catch (NumberFormatException e) {
99: LOGGER.warn("Could not parse property {} with value '{}'. Using default limit {} seconds", new Object[] {
100: PROPERTY_KERNEL_STARTUP_WAIT_LIMIT, waitLimitProperty, DEFAULT_STARTUP_WAIT_LIMIT });
101: return DEFAULT_STARTUP_WAIT_LIMIT;
102: }
103: }
104:
105: private static String readFrameworkProperty(String propertyKey, BundleContext context) {
106: return context.getProperty(propertyKey);
107: }
108:
109: private static boolean hasText(String string) {
110:• return (string != null && !string.trim().isEmpty());
111: }
112:
113: private static long sleepABitMore() throws InterruptedException {
114: long before = System.currentTimeMillis();
115: Thread.sleep(100);
116: return System.currentTimeMillis() - before;
117: }
118:
119: }