1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.osgi.mock;
17
18 import java.util.Dictionary;
19 import java.util.Enumeration;
20 import java.util.Hashtable;
21
22 import org.osgi.framework.Bundle;
23 import org.osgi.framework.Constants;
24 import org.osgi.framework.ServiceReference;
25 import org.osgi.framework.ServiceRegistration;
26
27 /**
28 * ServiceReference mock.
29 *
30 * <p/> This mock tries to adhere to the OSGi spec as much as possible by
31 * providing the mandatory serviceId properties such as
32 * {@link Constants#SERVICE_ID}, {@link Constants#OBJECTCLASS} and
33 * {@link Constants#SERVICE_RANKING}.
34 *
35 * @author Costin Leau
36 *
37 */
38 public class MockServiceReference implements ServiceReference {
39
40 private Bundle bundle;
41
42 private static long GLOBAL_SERVICE_ID = System.currentTimeMillis();
43
44 private long serviceId;
45
46
47 private Dictionary properties;
48
49 private String[] objectClass = new String[] { Object.class.getName() };
50
51 public MockServiceReference() {
52 this(null, null, null);
53 }
54
55 public MockServiceReference(Bundle bundle) {
56 this(bundle, null, null);
57 }
58
59 public MockServiceReference(String[] classes) {
60 this(null, null, null, classes);
61
62 }
63
64 public MockServiceReference(Bundle bundle, String[] classes) {
65 this(bundle, null, null, classes);
66 }
67
68 public MockServiceReference(ServiceRegistration registration) {
69 this(null, null, registration);
70 }
71
72 public MockServiceReference(Bundle bundle, Dictionary properties, ServiceRegistration registration) {
73 this(bundle, properties, registration, null);
74 }
75
76 public MockServiceReference(Bundle bundle, Dictionary properties, ServiceRegistration registration, String[] classes) {
77 this.bundle = (bundle == null ? new MockBundle() : bundle);
78
79
80
81 this.properties = (properties == null ? new Hashtable() : properties);
82 if (classes != null && classes.length > 0)
83 this.objectClass = classes;
84 addMandatoryProperties(this.properties);
85 }
86
87 private void addMandatoryProperties(Dictionary dict) {
88
89 Object id = dict.get(Constants.SERVICE_ID);
90 if (id == null || !(id instanceof Long))
91 dict.put(Constants.SERVICE_ID, new Long(GLOBAL_SERVICE_ID++));
92
93 if (dict.get(Constants.OBJECTCLASS) == null)
94 dict.put(Constants.OBJECTCLASS, objectClass);
95
96 Object ranking = dict.get(Constants.SERVICE_RANKING);
97 if (ranking == null || !(ranking instanceof Integer))
98 dict.put(Constants.SERVICE_RANKING, new Integer(0));
99
100 serviceId = ((Long) dict.get(Constants.SERVICE_ID)).longValue();
101 }
102
103
104
105
106
107
108 public Bundle getBundle() {
109 return bundle;
110 }
111
112
113
114
115
116
117 public Object getProperty(String key) {
118 return properties.get(key);
119 }
120
121
122
123
124
125
126 public String[] getPropertyKeys() {
127 String[] keys = new String[this.properties.size()];
128 Enumeration ks = this.properties.keys();
129
130 for (int i = 0; i < keys.length && ks.hasMoreElements(); i++) {
131 keys[i] = (String) ks.nextElement();
132 }
133
134 return keys;
135 }
136
137
138
139
140
141
142 public Bundle[] getUsingBundles() {
143 return new Bundle[] {};
144 }
145
146
147
148
149
150
151
152 public boolean isAssignableTo(Bundle bundle, String className) {
153 return false;
154 }
155
156 public void setProperties(Dictionary properties) {
157
158
159
160
161
162
163
164
165
166 if (properties != null) {
167
168 properties.put(Constants.SERVICE_ID, this.properties.get(Constants.SERVICE_ID));
169 properties.put(Constants.OBJECTCLASS, this.properties.get(Constants.OBJECTCLASS));
170
171 if (properties.get(Constants.SERVICE_RANKING) == null)
172 properties.put(Constants.SERVICE_RANKING, this.properties.get(Constants.SERVICE_RANKING));
173
174 this.properties = properties;
175 }
176 }
177
178 /**
179 * Two mock service references are equal if they contain the same service
180 * id.
181 */
182 public boolean equals(Object obj) {
183 if (this == obj)
184 return true;
185 if (obj instanceof MockServiceReference) {
186 return this.hashCode() == ((MockServiceReference) obj).hashCode();
187 }
188 return false;
189 }
190
191 /**
192 * Return a hash code based on the class and service id.
193 */
194 public int hashCode() {
195 return MockServiceReference.class.hashCode() * 13 + (int) serviceId;
196 }
197
198
199
200
201
202 public String toString() {
203 return "mock service reference [owning bundle id=" + bundle.hashCode() + "|props : " + properties + "]";
204 }
205
206 }