Package: StubServiceRegistration

StubServiceRegistration

nameinstructionbranchcomplexitylinemethod
StubServiceRegistration(StubBundleContext, String[])
M: 45 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getBundleContext()
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%
getProperties()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getReference()
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getUnregistered()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
populateDefaultProperties(Dictionary)
M: 29 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
setProperties(Dictionary)
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
setServiceReference(StubServiceReference)
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
toString()
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
unregister()
M: 41 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 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.test.stubs.framework;
13:
14: import static org.eclipse.virgo.test.stubs.internal.Assert.assertNotNull;
15: import static org.eclipse.virgo.test.stubs.internal.Duplicator.shallowCopy;
16:
17: import java.util.Arrays;
18: import java.util.Dictionary;
19: import java.util.Hashtable;
20:
21: import org.osgi.framework.BundleContext;
22: import org.osgi.framework.Constants;
23: import org.osgi.framework.ServiceReference;
24: import org.osgi.framework.ServiceRegistration;
25:
26: /**
27: * A stub testing implementation of {@link ServiceRegistration} as defined in section 6.1.24 of the OSGi Service Platform Core
28: * Specification.
29: * <p />
30: *
31: * <strong>Concurrent Semantics</strong><br />
32: *
33: * Threadsafe
34: *
35: * @param <S> ServiceRegistration type
36: *
37: */
38: public final class StubServiceRegistration<S> implements ServiceRegistration<S> {
39:
40: private volatile Dictionary<String, Object> properties = new Hashtable<String, Object>();
41:
42: private final Object propertiesMonitor = new Object();
43:
44: private volatile boolean unregistered = false;
45:
46: private final Object unregisteredMonitor = new Object();
47:
48: private final StubBundleContext bundleContext;
49:
50: private final String[] objectClasses;
51:
52: private volatile StubServiceReference<S> serviceReference;
53:
54: private final Object serviceReferenceMonitor = new Object();
55:
56: /**
57: * Creates a new {@link StubServiceRegistration} and sets its initial state
58: *
59: * @param bundleContext The bundle context that created this {@link ServiceRegistration}
60: * @param objectClasses The classes that this service is registered under
61: */
62: public StubServiceRegistration(StubBundleContext bundleContext, String... objectClasses) {
63: assertNotNull(bundleContext, "bundleContext");
64:
65: this.bundleContext = bundleContext;
66: this.objectClasses = objectClasses;
67: this.serviceReference = new StubServiceReference<S>(this);
68: populateDefaultProperties(this.properties);
69: }
70:
71: /**
72: * {@inheritDoc}
73: */
74: public ServiceReference<S> getReference() {
75: synchronized (this.serviceReferenceMonitor) {
76: return this.serviceReference;
77: }
78: }
79:
80: /**
81: * Sets the service reference to return for all subsequent calls to {@link #getReference()}.
82: *
83: * @param serviceReference The service reference to return
84: *
85: * @return <code>this</code> instance of the {@link StubServiceRegistration}
86: */
87: public StubServiceRegistration<S> setServiceReference(StubServiceReference<S> serviceReference) {
88: assertNotNull(serviceReference, "serviceReference");
89: synchronized (this.serviceReferenceMonitor) {
90: this.serviceReference = serviceReference;
91:
92: synchronized (this.propertiesMonitor) {
93: populateDefaultProperties(this.properties);
94: }
95:
96: return this;
97: }
98: }
99:
100: /**
101: * {@inheritDoc}
102: */
103: @SuppressWarnings("unchecked")
104: public void setProperties(Dictionary<String, ?> properties) {
105:• if (properties == null) {
106: return;
107: }
108:
109: synchronized (this.propertiesMonitor) {
110: this.properties = (Dictionary<String, Object>) properties;
111: populateDefaultProperties(this.properties);
112: }
113: }
114:
115: /**
116: * Gets the properties that were last set with a call to {@link #setProperties(Dictionary)}.
117: *
118: * @return The properties last passed in with a call to {@link #setProperties(Dictionary)} or <code>null</code> if
119: * {@link #setProperties(Dictionary)} has not been called
120: */
121: public Dictionary<String, Object> getProperties() {
122: synchronized (this.propertiesMonitor) {
123: return shallowCopy(this.properties);
124: }
125: }
126:
127: /**
128: * {@inheritDoc}
129: */
130: public void unregister() {
131: synchronized (this.unregisteredMonitor) {
132: this.bundleContext.removeRegisteredService(this);
133: this.unregistered = true;
134:
135: synchronized (this.serviceReferenceMonitor) {
136: this.serviceReference.setBundle(null);
137: }
138: }
139: }
140:
141: /**
142: * Gets whether this {@link ServiceRegistration} has been unregistered with a call to {@link #unregister()}.
143: *
144: * @return Whether or not this {@link StubServiceRegistration} has been unregistered
145: */
146: public boolean getUnregistered() {
147: synchronized (this.unregisteredMonitor) {
148: return this.unregistered;
149: }
150: }
151:
152: /**
153: * Gets the {@link BundleContext} that created this registration
154: *
155: * @return The {@link BundleContext} that created this registration
156: */
157: public StubBundleContext getBundleContext() {
158: return this.bundleContext;
159: }
160:
161: /**
162: * {@inheritDoc}
163: */
164: @Override
165: public String toString() {
166: return String.format("object classes: %s, unregistered: %b, properties: %s", Arrays.toString(this.objectClasses), this.unregistered,
167: this.properties);
168: }
169:
170: private void populateDefaultProperties(Dictionary<String, Object> properties) {
171: synchronized (this.serviceReferenceMonitor) {
172: properties.put(Constants.SERVICE_ID, this.serviceReference.getServiceId());
173: properties.put(Constants.SERVICE_RANKING, this.serviceReference.getServiceRanking());
174: properties.put(Constants.OBJECTCLASS, this.objectClasses);
175: }
176: }
177: }