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.io.IOException;
19 import java.io.InputStream;
20 import java.net.URL;
21 import java.util.Dictionary;
22 import java.util.Enumeration;
23 import java.util.Hashtable;
24 import java.util.NoSuchElementException;
25
26 import org.osgi.framework.Bundle;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.BundleException;
29 import org.osgi.framework.Constants;
30 import org.osgi.framework.ServiceReference;
31
32 /**
33 * Bundle Mock.
34 *
35 * <p/> The mock will the thread current classloader for loading
36 * classes/resources.
37 *
38 * @author Costin Leau
39 *
40 */
41 public class MockBundle implements Bundle {
42
43 private String location;
44
45 private Dictionary headers;
46
47 private static int GENERAL_BUNDLE_ID = 0;
48
49 private long bundleId = (GENERAL_BUNDLE_ID++);
50
51
52 private BundleContext bundleContext;
53
54 private ClassLoader loader = getClass().getClassLoader();
55
56 private Dictionary defaultHeaders = new Hashtable(0);
57
58 private final String SYMBOLIC_NAME = "Mock-Bundle_" + System.currentTimeMillis();
59
60 private final String symName;
61
62 private static class EmptyEnumeration implements Enumeration {
63 public boolean hasMoreElements() {
64 return false;
65 }
66
67 public Object nextElement() {
68 throw new NoSuchElementException();
69 }
70 }
71
72 public MockBundle() {
73 this(null, null, null);
74 }
75
76 public MockBundle(Dictionary headers) {
77 this(null, headers, null);
78 }
79
80 public MockBundle(BundleContext context) {
81 this(null, null, context);
82 }
83
84 public MockBundle(String symName) {
85 this(symName, null, null);
86 }
87
88 public MockBundle(String symName, Dictionary headers, BundleContext context) {
89 this.symName = ((symName != null && symName.length() > 0) ? symName : SYMBOLIC_NAME);
90 defaultHeaders.put("Bundle-SymbolicName", this.symName);
91
92 this.location = "<default location>";
93 this.headers = (headers == null ? defaultHeaders : headers);
94 this.bundleContext = (context == null ? new MockBundleContext(this) : context);
95 }
96
97 /**
98 * Delegates to the classloader. Identical to classLoader.getResources(path +
99 * filePattern);
100 *
101 * @see org.osgi.framework.Bundle#findEntries(java.lang.String,
102 * java.lang.String, boolean)
103 */
104 public Enumeration findEntries(String path, String filePattern, boolean recurse) {
105 Enumeration enm = null;
106
107 try {
108 enm = loader.getResources(path + "/" + filePattern);
109 }
110 catch (IOException ex) {
111
112 System.err.println("returning an empty enumeration as cannot load resource; exception " + ex);
113 }
114 return (enm == null ? new EmptyEnumeration() : enm);
115 }
116
117
118
119
120
121
122 public long getBundleId() {
123 return this.bundleId;
124 }
125
126 public void setBundleId(long bundleId) {
127 this.bundleId = bundleId;
128 }
129
130
131
132
133
134
135 public URL getEntry(String name) {
136 return loader.getResource(name);
137 }
138
139
140
141
142
143
144 public Enumeration getEntryPaths(String path) {
145 return new EmptyEnumeration();
146 }
147
148
149
150
151
152
153 public Dictionary getHeaders() {
154 return headers;
155 }
156
157
158
159
160
161
162 public Dictionary getHeaders(String locale) {
163 return getHeaders();
164 }
165
166
167
168
169
170
171 public long getLastModified() {
172 return 0;
173 }
174
175
176
177
178
179
180 public String getLocation() {
181 return location;
182 }
183
184
185
186
187
188
189 public ServiceReference[] getRegisteredServices() {
190 return new ServiceReference[] {};
191 }
192
193
194
195
196
197
198 public URL getResource(String name) {
199 return loader.getResource(name);
200 }
201
202
203
204
205
206
207 public Enumeration getResources(String name) throws IOException {
208 return loader.getResources(name);
209 }
210
211
212
213
214
215
216 public ServiceReference[] getServicesInUse() {
217 return new ServiceReference[] {};
218 }
219
220
221
222
223
224
225 public int getState() {
226 return Bundle.ACTIVE;
227 }
228
229
230
231
232
233
234 public String getSymbolicName() {
235 String name = (String) headers.get(Constants.BUNDLE_SYMBOLICNAME);
236 return (name == null ? SYMBOLIC_NAME : name);
237 }
238
239
240
241
242
243
244 public boolean hasPermission(Object permission) {
245 return true;
246 }
247
248
249
250
251
252
253 public Class loadClass(String name) throws ClassNotFoundException {
254 return loader.loadClass(name);
255 }
256
257
258
259
260
261
262 public void start() throws BundleException {
263 }
264
265
266
267
268
269
270 public void stop() throws BundleException {
271 }
272
273
274
275
276
277
278 public void uninstall() throws BundleException {
279 }
280
281
282
283
284
285
286 public void update() throws BundleException {
287 }
288
289
290
291
292
293
294 public void update(InputStream in) throws BundleException {
295 }
296
297
298
299
300
301 public BundleContext getContext() {
302 return this.bundleContext;
303 }
304
305 public BundleContext getBundleContext() {
306 return getContext();
307 }
308
309 public String toString() {
310 return symName;
311 }
312
313 public void setLocation(String location) {
314 this.location = location;
315 }
316
317 }