Package: ServiceUtils

ServiceUtils

nameinstructionbranchcomplexitylinemethod
ServiceUtils()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
arrayContainsEntry(Object[], Object)
M: 32 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getService(BundleContext, Class, String, String)
M: 117 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 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.shell.internal;
13:
14: import org.osgi.framework.BundleContext;
15: import org.osgi.framework.InvalidSyntaxException;
16: import org.osgi.framework.ServiceReference;
17: import org.slf4j.Logger;
18: import org.slf4j.LoggerFactory;
19:
20:
21: /**
22: * Utility methods for working with services in the OSGi service registry.
23: * <p />
24: *
25: * <strong>Concurrent Semantics</strong><br />
26: *
27: * Thread-safe.
28: *
29: */
30: public final class ServiceUtils {
31:
32: private static final Logger LOGGER = LoggerFactory.getLogger(ServiceUtils.class);
33:
34: public static <T> T getService(BundleContext bundleContext, Class<T> clazz, String requiredProperty, String requiredPropertyValue) {
35: T result = null;
36: try {
37: ServiceReference<?>[] serviceReferences = bundleContext.getServiceReferences((String)null, String.format("(%s=*)", requiredProperty));
38:• if (serviceReferences != null) {
39:• for (ServiceReference<?> serviceReference : serviceReferences) {
40: Object offeredPropertyValue = serviceReference.getProperty(requiredProperty);
41:• if (offeredPropertyValue instanceof String) { // String value
42: String offeredProperty = (String) offeredPropertyValue;
43:• if (offeredProperty != null && requiredPropertyValue.equals(offeredProperty)) {
44: Object potentialResult = bundleContext.getService(serviceReference);
45:• if (clazz.isInstance(potentialResult)) {
46: result = clazz.cast(bundleContext.getService(serviceReference));
47: break;
48: }
49: }
50:• } else if (offeredPropertyValue instanceof String[]) { // String[] value
51: String[] offeredProperties = (String[]) offeredPropertyValue;
52:• if (offeredProperties != null && arrayContainsEntry(offeredProperties, requiredPropertyValue)) {
53: Object potentialResult = bundleContext.getService(serviceReference);
54:• if (clazz.isInstance(potentialResult)) {
55: result = clazz.cast(bundleContext.getService(serviceReference));
56: break;
57: }
58: }
59: } else {
60: LOGGER.warn(String.format(
61: "Matching service found from bundle %d but with a bad type for the '%s' property, String or String[] expected.",
62: serviceReference.getBundle().getBundleId(), requiredProperty));
63: }
64: }
65: }
66: } catch (InvalidSyntaxException e) {
67: throw new RuntimeException("Unexpected InvalidSyntaxException", e);
68: }
69:
70: return result;
71: }
72:
73: private static <T> boolean arrayContainsEntry(T[] array, T entry) {
74:• if (entry == null || array == null) {
75: return false;
76: }
77:• for (T arrayEntry : array) {
78:• if (arrayEntry != null && arrayEntry.equals(entry)) {
79: return true;
80: }
81: }
82: return false;
83: }
84: }