Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e541cf90971cdc07c25191dcaa5e2345c584bb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* Copyright (c) 2006-2009 Jan S. Rellermeyer
 * Systems Group,
 * Institute for Pervasive Computing, ETH Zurich.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    - Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    - Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    - Neither the name of ETH Zurich nor the names of its contributors may be
 *      used to endorse or promote products derived from this software without
 *      specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ch.ethz.iks.r_osgi.messages;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import ch.ethz.iks.util.StringUtils;

/**
 * <p>
 * DeliverServiceMessage is used to bring an OSGi service to a remote machine.
 * The service interface is transferred and if specified, also an abstract class
 * that can contain code that behaves like a smart proxy and moves some methods
 * to the client machine.
 * </p>
 * <p>
 * Currently, R-OSGi does not check for class dependencies so if an interface
 * method uses custom objects as parameter and it can not be assumed that the
 * client already has these custom objects, they have to be defined as class
 * injections. However, all inner classes of injections and possible inner
 * classes of the abstract class provided as smart proxy object are
 * automatically added to the class injections.
 * </p>
 * 
 * @author Jan S. Rellermeyer, ETH Zurich.
 * @since 0.1
 */
public final class DeliverServiceMessage extends RemoteOSGiMessage {

	/**
	 * the service ID.
	 */
	private String serviceID;

	/**
	 * The class name of the interface that describes the service.
	 */
	private String[] serviceInterfaceNames;

	/**
	 * Optionally, the class name of a smart proxy class.
	 */
	private String smartProxyName;

	/**
	 * the injections.
	 */
	private Map injections;

	/**
	 * the imports.
	 */
	private String imports;

	/**
	 * the exports.
	 */
	private String exports;

	/**
	 * Optional imports
	 */
	private String optionalImports = "";

	/**
	 * Create a new DeliverServiceMessage.
	 * 
	 */
	public DeliverServiceMessage() {
		super(DELIVER_SERVICE);
	}

	/**
	 * Create a new DeliverServiceMessage from a network packet.
	 * 
	 * <pre>
	 *         0                   1                   2                   3
	 *         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
	 *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 *        |       R-OSGi header (function = Service = 2)                  |
	 *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 *        |   length of &lt;ServiceID&gt;     |    &lt;ServiceID&gt; String       \
	 *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 *        |          imports                                              \ 
	 *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 *        |          exports                                              \ 
	 *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 *        |          interface name                                       \ 
	 *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 *        | length of &lt;ProxyClassName&gt;    |    &lt;ProxyClassName&gt; String    \
	 *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 *        |    number of injection blocks   |   class inj blocks          \
	 *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	 * </pre>
	 * 
	 * @param input
	 *            a <code>DataInput</code> that provides the body of a R-OSGi
	 *            network packet.
	 * @throws IOException
	 *             in case of parse errors.
	 */
	DeliverServiceMessage(final ObjectInputStream input) throws IOException {
		super(DELIVER_SERVICE);
		// the serviceID
		serviceID = input.readUTF();
		// imports
		imports = input.readUTF();
		// exports
		exports = input.readUTF();
		// interface names
		serviceInterfaceNames = readStringArray(input);
		// smart proxy name, if defined.
		final String p = input.readUTF();
		smartProxyName = "".equals(p) ? null : p; //$NON-NLS-1$
		// process all class injections
		final short blocks = input.readShort();
		injections = new HashMap(blocks);
		for (short i = 0; i < blocks; i++) {
			injections.put(input.readUTF(), readBytes(input));
		}
		
		// generate option imports from injections
		final Set set = new HashSet();
		
		// no need to add imports twice
		final String[] imp = (imports != null ? StringUtils.stringToArray(imports, ",") : new String[0]);
		for (int i = 0; i < imp.length; i++) {
			String string = imp[i].trim();
			set.add(string); 
		}
		
		for (final Iterator itr = injections.keySet().iterator(); itr.hasNext();) {
			final String className = (String) itr.next();
			final int lastIndexOf = className.lastIndexOf("/");
			final String pkgName = className.substring(0, lastIndexOf).replace('/', '.');
			if(!set.contains(pkgName)) {
				set.add(pkgName);
				optionalImports += (optionalImports.length() > 0 ? ", " : "") 
								+ pkgName + ";resolution:=optional";
			}
		}

	}

	/**
	 * write the message body to a stream.
	 * 
	 * @param out
	 *            the output stream.
	 * @throws IOException
	 *             in case of parse errors.
	 */
	public void writeBody(final ObjectOutputStream out) throws IOException {
		out.writeUTF(serviceID);
		out.writeUTF(imports);
		out.writeUTF(exports);
		writeStringArray(out, serviceInterfaceNames);
		out.writeUTF(smartProxyName == null ? "" : smartProxyName); //$NON-NLS-1$
		final short blocks = (short) injections.size();
		out.writeShort(blocks);
		final String[] injectionNames = (String[]) injections.keySet().toArray(
				new String[blocks]);
		for (short i = 0; i < blocks; i++) {
			out.writeUTF(injectionNames[i]);
			writeBytes(out, (byte[]) injections.get(injectionNames[i]));
		}
	}

	/**
	 * get the service ID.
	 * 
	 * @return the service ID.
	 */
	public String getServiceID() {
		return serviceID;
	}

	/**
	 * set the service ID.
	 * 
	 * @param serviceID
	 *            the service ID.
	 */
	public void setServiceID(final String serviceID) {
		this.serviceID = serviceID;
	}

	/**
	 * set the injections.
	 * 
	 * @param injections
	 *            the injections.
	 */
	public void setInjections(final Map injections) {
		this.injections = injections;
	}

	/**
	 * get the interface name of the delivered service.
	 * 
	 * @return the class name of the interface.
	 */
	public String[] getInterfaceNames() {
		return serviceInterfaceNames;
	}

	/**
	 * set the interface names.
	 * 
	 * @param interfaceNames
	 *            the interface class names.
	 */
	public void setInterfaceNames(final String[] interfaceNames) {
		serviceInterfaceNames = interfaceNames;
	}

	/**
	 * convenience method to get the bytes of the interface class.
	 * 
	 * @return the interface class.
	 */
	public byte[] getInterfaceClass() {
		return (byte[]) injections.get(serviceInterfaceNames[0].replace('.',
				'/')
				+ ".class"); //$NON-NLS-1$
	}

	/**
	 * get the smart proxy class name.
	 * 
	 * @return the class name of the smart proxy or null of undefined.
	 */
	public String getSmartProxyName() {
		return smartProxyName;
	}

	/**
	 * set the smart proxy name.
	 * 
	 * @param smartProxyName
	 *            the smart proxy name.
	 */
	public void setSmartProxyName(final String smartProxyName) {
		this.smartProxyName = smartProxyName;
	}

	/**
	 * convenience method to get the bytes of the smart proxy class.
	 * 
	 * @return the class or null if undefined.
	 */
	public byte[] getProxyClass() {
		if (smartProxyName == null) {
			return null;
		}
		return (byte[]) injections.get(smartProxyName.replace('.', '/')
				+ ".class"); //$NON-NLS-1$
	}

	/**
	 * get the list of class injection.
	 * 
	 * @return a <code>List</code> of class names.
	 */
	public Map getInjections() {
		return injections;
	}

	/**
	 * get the imports.
	 * 
	 * @return the imports.
	 */
	public String getImports() {
		return imports;
	}

	/**
	 * set the imports.
	 * 
	 * @param imports
	 *            the imports.
	 */
	public void setImports(final String imports) {
		this.imports = imports;
	}

	/**
	 * get the exports.
	 * 
	 * @return the exports.
	 */
	public String getExports() {
		return exports;
	}

	/**
	 * set the exports.
	 * 
	 * @param exports
	 *            the exports.
	 */
	public void setExports(final String exports) {
		this.exports = exports;
	}

	/**
	 * String representation for debug outputs.
	 * 
	 * @return a string representation.
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		final StringBuffer buffer = new StringBuffer();
		buffer.append("[DELIVER_SERVICE] - XID: "); //$NON-NLS-1$
		buffer.append(xid);
		buffer.append(", serviceID: "); //$NON-NLS-1$
		buffer.append(serviceID);
		buffer.append(", serviceInterfaceName: "); //$NON-NLS-1$
		buffer.append(Arrays.asList(serviceInterfaceNames));
		if (smartProxyName != null) {
			buffer.append(" smartProxy: "); //$NON-NLS-1$
			buffer.append(smartProxyName);
		}
		if (injections.size() > 0) {
			buffer.append(", classInjections "); //$NON-NLS-1$
			buffer.append(injections.keySet());
		}
		return buffer.toString();
	}

	public String getOptionalImports() {
		return optionalImports;
	}

}

Back to the top