View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  	// required for introspection by util classes (should be removed)
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 			// catch to allow nice behavior
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 	 * (non-Javadoc)
119 	 * 
120 	 * @see org.osgi.framework.Bundle#getBundleId()
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 	 * (non-Javadoc)
132 	 * 
133 	 * @see org.osgi.framework.Bundle#getEntry(java.lang.String)
134 	 */
135 	public URL getEntry(String name) {
136 		return loader.getResource(name);
137 	}
138 
139 	/*
140 	 * (non-Javadoc)
141 	 * 
142 	 * @see org.osgi.framework.Bundle#getEntryPaths(java.lang.String)
143 	 */
144 	public Enumeration getEntryPaths(String path) {
145 		return new EmptyEnumeration();
146 	}
147 
148 	/*
149 	 * (non-Javadoc)
150 	 * 
151 	 * @see org.osgi.framework.Bundle#getHeaders()
152 	 */
153 	public Dictionary getHeaders() {
154 		return headers;
155 	}
156 
157 	/*
158 	 * (non-Javadoc)
159 	 * 
160 	 * @see org.osgi.framework.Bundle#getHeaders(java.lang.String)
161 	 */
162 	public Dictionary getHeaders(String locale) {
163 		return getHeaders();
164 	}
165 
166 	/*
167 	 * (non-Javadoc)
168 	 * 
169 	 * @see org.osgi.framework.Bundle#getLastModified()
170 	 */
171 	public long getLastModified() {
172 		return 0;
173 	}
174 
175 	/*
176 	 * (non-Javadoc)
177 	 * 
178 	 * @see org.osgi.framework.Bundle#getLocation()
179 	 */
180 	public String getLocation() {
181 		return location;
182 	}
183 
184 	/*
185 	 * (non-Javadoc)
186 	 * 
187 	 * @see org.osgi.framework.Bundle#getRegisteredServices()
188 	 */
189 	public ServiceReference[] getRegisteredServices() {
190 		return new ServiceReference[] {};
191 	}
192 
193 	/*
194 	 * (non-Javadoc)
195 	 * 
196 	 * @see org.osgi.framework.Bundle#getResource(java.lang.String)
197 	 */
198 	public URL getResource(String name) {
199 		return loader.getResource(name);
200 	}
201 
202 	/*
203 	 * (non-Javadoc)
204 	 * 
205 	 * @see org.osgi.framework.Bundle#getResources(java.lang.String)
206 	 */
207 	public Enumeration getResources(String name) throws IOException {
208 		return loader.getResources(name);
209 	}
210 
211 	/*
212 	 * (non-Javadoc)
213 	 * 
214 	 * @see org.osgi.framework.Bundle#getServicesInUse()
215 	 */
216 	public ServiceReference[] getServicesInUse() {
217 		return new ServiceReference[] {};
218 	}
219 
220 	/*
221 	 * (non-Javadoc)
222 	 * 
223 	 * @see org.osgi.framework.Bundle#getState()
224 	 */
225 	public int getState() {
226 		return Bundle.ACTIVE;
227 	}
228 
229 	/*
230 	 * (non-Javadoc)
231 	 * 
232 	 * @see org.osgi.framework.Bundle#getSymbolicName()
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 	 * (non-Javadoc)
241 	 * 
242 	 * @see org.osgi.framework.Bundle#hasPermission(java.lang.Object)
243 	 */
244 	public boolean hasPermission(Object permission) {
245 		return true;
246 	}
247 
248 	/*
249 	 * (non-Javadoc)
250 	 * 
251 	 * @see org.osgi.framework.Bundle#loadClass(java.lang.String)
252 	 */
253 	public Class loadClass(String name) throws ClassNotFoundException {
254 		return loader.loadClass(name);
255 	}
256 
257 	/*
258 	 * (non-Javadoc)
259 	 * 
260 	 * @see org.osgi.framework.Bundle#start()
261 	 */
262 	public void start() throws BundleException {
263 	}
264 
265 	/*
266 	 * (non-Javadoc)
267 	 * 
268 	 * @see org.osgi.framework.Bundle#stop()
269 	 */
270 	public void stop() throws BundleException {
271 	}
272 
273 	/*
274 	 * (non-Javadoc)
275 	 * 
276 	 * @see org.osgi.framework.Bundle#uninstall()
277 	 */
278 	public void uninstall() throws BundleException {
279 	}
280 
281 	/*
282 	 * (non-Javadoc)
283 	 * 
284 	 * @see org.osgi.framework.Bundle#update()
285 	 */
286 	public void update() throws BundleException {
287 	}
288 
289 	/*
290 	 * (non-Javadoc)
291 	 * 
292 	 * @see org.osgi.framework.Bundle#update(java.io.InputStream)
293 	 */
294 	public void update(InputStream in) throws BundleException {
295 	}
296 
297 	// chiefly here so that compilers/find-bugs don't complain about the
298 	// "unused" bundleContext field.
299 	// also enables OsgiResoureUtils.getBundleContext to find the context via
300 	// reflection
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 }