Skip to content

Package: ServiceObjectTracker

ServiceObjectTracker

nameinstructionbranchcomplexitylinemethod
ServiceObjectTracker(BundleContext, Class)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
dispose()
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getService()
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * eugen - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.common;
15:
16: import java.util.LinkedHashSet;
17: import java.util.Set;
18:
19: import org.osgi.framework.BundleContext;
20: import org.osgi.framework.ServiceObjects;
21: import org.osgi.framework.ServiceReference;
22:
23: /**
24: * Helper class to track {@link ServiceObjects} and the services retrieved from them.
25: *
26: * @param <T> The type to track
27: * @since 1.18
28: */
29: public class ServiceObjectTracker<T> {
30:         private final ServiceObjects<T> serviceObjects;
31:         private final Set<T> trackedServices = new LinkedHashSet<T>();
32:
33:         /**
34:          * Constructor.
35:          *
36:          * @param bundleContext The {@link BundleContext} to use
37:          * @param clazz The Class of the service to retrieve
38:          */
39:         public ServiceObjectTracker(BundleContext bundleContext, Class<T> clazz) {
40:                 super();
41:                 final ServiceReference<T> serviceRef = bundleContext.getServiceReference(clazz);
42:                 serviceObjects = bundleContext.getServiceObjects(serviceRef);
43:         }
44:
45:         /**
46:          * Retrieves the Service.
47:          *
48:          * @return The Service or null
49:          * @see ServiceObjects#getService()
50:          */
51:         public T getService() {
52:                 final T service = serviceObjects.getService();
53:•                if (service != null) {
54:                         trackedServices.add(service);
55:                 }
56:                 return service;
57:         }
58:
59:         /**
60:          * Disposes this services which leads to ungetting all retrieved services.
61:          *
62:          * @see ServiceObjects#ungetService(Object)
63:          */
64:         public void dispose() {
65:•                for (final T service : trackedServices) {
66:                         serviceObjects.ungetService(service);
67:                 }
68:         }
69: }