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.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  	// private ServiceRegistration registration;
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  		// this.registration = (registration == null ? new
79  		// MockServiceRegistration() :
80  		// registration);
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  		// add mandatory properties
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 	 * (non-Javadoc)
105 	 * 
106 	 * @see org.osgi.framework.ServiceReference#getBundle()
107 	 */
108 	public Bundle getBundle() {
109 		return bundle;
110 	}
111 
112 	/*
113 	 * (non-Javadoc)
114 	 * 
115 	 * @see org.osgi.framework.ServiceReference#getProperty(java.lang.String)
116 	 */
117 	public Object getProperty(String key) {
118 		return properties.get(key);
119 	}
120 
121 	/*
122 	 * (non-Javadoc)
123 	 * 
124 	 * @see org.osgi.framework.ServiceReference#getPropertyKeys()
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 	 * (non-Javadoc)
139 	 * 
140 	 * @see org.osgi.framework.ServiceReference#getUsingBundles()
141 	 */
142 	public Bundle[] getUsingBundles() {
143 		return new Bundle[] {};
144 	}
145 
146 	/*
147 	 * (non-Javadoc)
148 	 * 
149 	 * @see org.osgi.framework.ServiceReference#isAssignableTo(org.osgi.framework.Bundle,
150 	 * java.lang.String)
151 	 */
152 	public boolean isAssignableTo(Bundle bundle, String className) {
153 		return false;
154 	}
155 
156 	public void setProperties(Dictionary properties) {
157 		/*
158 		 * Enumeration keys = props.keys(); while (keys.hasMoreElements())
159 		 * this.properties.remove(keys.nextElement());
160 		 * 
161 		 * Enumeration enm = props.keys(); while (enm.hasMoreElements()) {
162 		 * Object key = enm.nextElement(); this.properties.put(key,
163 		 * props.get(key)); }
164 		 */
165 
166 		if (properties != null) {
167 			// copy mandatory properties
168 			properties.put(Constants.SERVICE_ID, this.properties.get(Constants.SERVICE_ID));
169 			properties.put(Constants.OBJECTCLASS, this.properties.get(Constants.OBJECTCLASS));
170 			// optional property
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 	 * (non-Javadoc)
200 	 * @see java.lang.Object#toString()
201 	 */
202 	public String toString() {
203 		return "mock service reference [owning bundle id=" + bundle.hashCode() + "|props : " + properties + "]";
204 	}
205 
206 }