Package: ServiceReferenceTracker

ServiceReferenceTracker

nameinstructionbranchcomplexitylinemethod
ServiceReferenceTracker(BundleContext)
M: 6 C: 11
65%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
track(ServiceReference)
M: 6 C: 23
79%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
ungetAll()
M: 7 C: 36
84%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * This file is part of the Virgo Web Server.
3: *
4: * Copyright (c) 2010 VMware Inc.
5: * All rights reserved. This program and the accompanying materials
6: * are made available under the terms of the Eclipse Public License v1.0
7: * which accompanies this distribution, and is available at
8: * http://www.eclipse.org/legal/epl-v10.html
9: *
10: * Contributors:
11: * SpringSource, a division of VMware - initial API and implementation and/or initial documentation
12: *******************************************************************************/
13:
14: package org.eclipse.virgo.nano.core.internal;
15:
16: import java.util.HashSet;
17: import java.util.Set;
18:
19: import org.osgi.framework.BundleContext;
20: import org.osgi.framework.ServiceReference;
21:
22:
23: /**
24: * Utility class that tracks a set of {@link ServiceReference}s for a bundle and allows them to be safely
25: * ungotten.
26: * <p/>
27: * <code>ServiceReferences</code> are tracked in a thread-safe manner, and are ungotten without
28: * holding any locks.
29: *
30: * @see ServiceReference
31: */
32: class ServiceReferenceTracker {
33:
34: private final BundleContext context;
35:
36: private final Object monitor = new Object();
37:
38: private Set<ServiceReference<?>> references; // protected by monitor.
39:
40:
41: ServiceReferenceTracker(BundleContext context) {
42: this.context = context;
43: }
44:
45: /**
46: * Tracks the supplied {@link ServiceReference}. This <code>ServiceReference</code> will be
47: * {@link BundleContext#ungetService ungotten} during {@link #ungetAll()}.
48: *
49: * @param reference the <code>ServiceReference</code> to track.
50: * @return the reference itself
51: */
52: public ServiceReference<?> track(ServiceReference<?> reference) {
53: synchronized (this.monitor) {
54:• if (this.references == null) {
55: this.references = new HashSet<ServiceReference<?>>();
56: }
57: this.references.add(reference);
58: }
59: return reference;
60: }
61:
62: /**
63: * Safely unregisters all the tracked <code>ServiceRegistrations</code>.
64: */
65: public void ungetAll() {
66: Set<ServiceReference<?>> toUnget = null;
67: synchronized (this.monitor) {
68: toUnget = this.references;
69: this.references = null;
70: }
71:• if (toUnget != null) {
72:• for (ServiceReference<?> serviceReference : toUnget) {
73: try {
74: this.context.ungetService(serviceReference);
75: } catch (IllegalStateException e) {
76: }
77: }
78: }
79: }
80: }