Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c29b6ac01ca0c7f9077a2ef415e5a0126287b1d5 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*******************************************************************************
 * Copyright (c) 2018 Composent, Inc. and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Composent, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.core.util;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;
import org.osgi.service.log.LogService;

/**
 * @since 3.9
 */
public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObjectStreamConstants {

	protected final ObjectInputStream in;
	protected final Bundle b;
	protected LogService logger;

	public OSGIObjectInputStream(Bundle b, InputStream in, LogService logger) throws IOException {
		super();
		this.b = b;
		this.in = new ObjectInputStream(in);
		this.logger = logger;
	}

	public OSGIObjectInputStream(Bundle b, InputStream in) throws IOException {
		this(b, in, null);
	}

	public void setLogService(LogService log) {
		this.logger = log;
	}

	protected void trace(String message) {
		LogService ls = this.logger;
		if (ls != null) {
			ls.log(LogService.LOG_DEBUG, message);
		}
	}

	protected Class loadClass(String classname) throws ClassNotFoundException {
		Bundle bundle = b;
		return (bundle == null) ? Class.forName(classname) : bundle.loadClass(classname);
	}

	protected Class<?> getClassForType(String type) throws ClassNotFoundException {
		if (type.equals(byte.class.getName()))
			return byte.class;
		else if (type.equals(long.class.getName()))
			return long.class;
		else if (type.equals(int.class.getName()))
			return int.class;
		else if (type.equals(short.class.getName()))
			return short.class;
		else if (type.equals(char.class.getName()))
			return char.class;
		else if (type.equals(boolean.class.getName()))
			return boolean.class;
		else if (type.equals(float.class.getName()))
			return float.class;
		else if (type.equals(double.class.getName()))
			return double.class;
		else
			return loadClass(type);
	}

	protected final Object readObjectOverride() throws IOException, ClassNotFoundException {
		final byte type = in.readByte();
		switch (type) {
			case C_NULL : // null
				trace("null"); //$NON-NLS-1$
				return null;
			case C_SER : // Serializable
				trace("readSerializedObject"); //$NON-NLS-1$
				return readSerializedObject();
			case C_VER : // Version
				trace("readVersion"); //$NON-NLS-1$
				return Version.parseVersion(in.readUTF());
			case C_ARRAY : // Object[]
				// read array length
				trace("readArray"); //$NON-NLS-1$
				int ol = in.readInt();
				// read component type and create array for that component type
				Class<?> clazz = getClassForType(in.readUTF());
				Object oresult = Array.newInstance(clazz, ol);
				for (int i = 0; i < ol; i++)
					Array.set(oresult, i, readObjectOverride());
				return oresult;
			case C_DTO : // DTO
				trace("readDTO"); //$NON-NLS-1$
				return readDTO();
			case C_DICT : // Dictionary
				trace("readDictionary"); //$NON-NLS-1$
				Class<?> dictClazz = loadClass(in.readUTF());
				Dictionary dict = null;
				Constructor cons;
				try {
					cons = dictClazz.getDeclaredConstructor((Class[]) null);
					cons.setAccessible(true);
					dict = (Dictionary) cons.newInstance((Object[]) null);
				} catch (Exception e) {
					throw new IOException("Could not create dictionary instance of clazz=" + dictClazz.getName()); //$NON-NLS-1$
				}
				int dsize = in.readInt();
				for (int i = 0; i < dsize; i++) {
					Object key = readObjectOverride();
					Object val = readObjectOverride();
					dict.put(key, val);
				}
				return dict;
			case C_MAP : // Map
				// read map length
				int ms = in.readInt();
				trace("readMap=" + ms); //$NON-NLS-1$
				Map mr = new HashMap();
				for (int i = 0; i < ms; i++) {
					Object key = readObjectOverride();
					Object val = readObjectOverride();
					mr.put(key, val);
				}
				return mr;
			case C_LIST : // List
				int lsize = in.readInt();
				trace("readList=" + lsize); //$NON-NLS-1$
				List l = new ArrayList(lsize);
				for (int i = 0; i < lsize; i++)
					l.add(readObjectOverride());
				return l;
			case C_SET : // Set
				int ssize = in.readInt();
				trace("readSet=" + ssize); //$NON-NLS-1$
				Set s = new HashSet(ssize);
				for (int i = 0; i < ssize; i++)
					s.add(readObjectOverride());
				return s;
			case C_COLL : // Collection
				int csize = in.readInt();
				trace("readCol=" + csize); //$NON-NLS-1$
				Collection c = new ArrayList(csize);
				for (int i = 0; i < csize; i++)
					c.add(readObjectOverride());
				return c;
			case C_ITER : // Iterable
				int isize = in.readInt();
				trace("readIter=" + isize); //$NON-NLS-1$
				List itr = new ArrayList(isize);
				for (int i = 0; i < isize; i++)
					itr.add(readObjectOverride());
				return itr;
			case C_EXTER : // Externalizable
				return readExternalizable();
			case C_STRING : // String
				trace("readString"); //$NON-NLS-1$
				return in.readUTF();
			case C_LONG :
			case C_OLONG :
				trace("readLong"); //$NON-NLS-1$
				return in.readLong();
			case C_INT :
			case C_OINT :
				trace("readInt"); //$NON-NLS-1$
				return in.readInt();
			case C_SHORT :
			case C_OSHORT :
				trace("readShort"); //$NON-NLS-1$
				return in.readShort();
			case C_BOOL :
			case C_OBOOL :
				trace("readBool"); //$NON-NLS-1$
				return in.readBoolean();
			case C_BYTE :
			case C_OBYTE :
				trace("readByte"); //$NON-NLS-1$
				return in.readByte();
			case C_CHAR :
			case C_OCHAR :
				trace("readChar"); //$NON-NLS-1$
				return in.readChar();
			case C_DOUBLE :
			case C_ODOUBLE :
				trace("readDouble"); //$NON-NLS-1$
				return in.readDouble();
			case C_FLOAT :
			case C_OFLOAT :
				trace("readFloat"); //$NON-NLS-1$
				return in.readFloat();
			case C_ENUM :
				trace("readEnum"); //$NON-NLS-1$
				return in.readObject();
			case C_OBJECT :
				return readNonSerializedObject();
			default :
				throw new IOException("Cannot deserialize object with type=" + type); //$NON-NLS-1$
		}
	}

	private Object readDTO() throws IOException, ClassNotFoundException {
		Class<?> clazz = loadClass(in.readUTF());
		Object result = null;
		try {
			result = clazz.newInstance();
			for (Field f : clazz.getFields()) {
				final int mod = f.getModifiers();
				// If it's static or transient then ignore
				if (Modifier.isStatic(mod) || Modifier.isTransient(mod))
					continue;
				// Else read and set value of field
				f.set(result, readObjectOverride());
			}
		} catch (Exception e) {
			throw new IOException("Cannot deserialize DTO because of exception: " + e.getMessage()); //$NON-NLS-1$
		}
		return result;
	}

	private Object createInstance(ObjectStreamClass osc) throws IOException {
		try {
			Method m = osc.getClass().getDeclaredMethod("newInstance", (Class<?>[]) null); //$NON-NLS-1$
			m.setAccessible(true);
			return m.invoke(osc, (Object[]) null);
		} catch (Exception e) {
			throw new IOException("Exception creating newInstance of class=" + osc.getName()); //$NON-NLS-1$
		}
	}

	protected Object readExternalizable() throws ClassNotFoundException, IOException {
		final String clazzName = in.readUTF();
		trace("readExternalizable " + clazzName); //$NON-NLS-1$
		Class<?> clazz = loadClass(clazzName);
		Object inst = createInstance(clazz);
		Externalizable ex = (Externalizable) inst;
		ex.readExternal(this);
		return inst;
	}

	protected Object readFields(Class<?> clazz, Object inst) throws IOException {
		try {
			int fieldCount = in.readInt();
			while (fieldCount > -1) {
				for (int i = 0; i < fieldCount; i++) {
					final String fieldName = in.readUTF();
					final Field field = clazz.getDeclaredField(fieldName);
					final int mod = field.getModifiers();
					if (!Modifier.isPublic(mod))
						field.setAccessible(true);

					//
					final Object value = readObjectOverride();
					field.set(inst, value);
				}
				clazz = clazz.getSuperclass();
				fieldCount = in.readInt();
			}
			return inst;
		} catch (final Exception e) {
			throw new IOException("Error while deserializing class=" + clazz.getName() + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	protected Object createInstance(Class<?> clazz) throws IOException {
		try {
			return clazz.newInstance();
		} catch (Exception e) {
			throw new IOException("Could create new instance of class=" + clazz.getName() + ".  Class must have public no-arg constructor"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	protected Object readNonSerializedObject() throws IOException, ClassNotFoundException {
		// read object stream class
		String className = in.readUTF();
		trace("readNonSerializedObject " + className); //$NON-NLS-1$
		Class<?> clazz = loadClass(className);
		// create instance
		Object instance = createInstance(clazz);
		return readFields(clazz, instance);
	}

	protected Object readSerializedObject() throws IOException, ClassNotFoundException {
		// read object stream class
		ObjectStreamClass osc = (ObjectStreamClass) in.readObject();
		trace("readSerializedObject " + osc.getName()); //$NON-NLS-1$
		Class<?> clazz = osc.forClass();
		// create instance
		final Object instance = createInstance(osc);
		return readFields(clazz, instance);
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#read()
	 */
	public final int read() throws IOException {
		return in.read();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#read(byte[], int, int)
	 */
	public final int read(final byte[] buf, final int off, final int len) throws IOException {
		return in.read(buf, off, len);
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#available()
	 */
	public final int available() throws IOException {
		return in.available();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#close()
	 */
	public final void close() throws IOException {
		in.close();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readBoolean()
	 */
	public final boolean readBoolean() throws IOException {
		return in.readBoolean();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readByte()
	 */
	public final byte readByte() throws IOException {
		return in.readByte();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readUnsignedByte()
	 */
	public final int readUnsignedByte() throws IOException {
		return in.readUnsignedByte();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readChar()
	 */
	public final char readChar() throws IOException {
		return in.readChar();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readShort()
	 */
	public final short readShort() throws IOException {
		return in.readShort();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readUnsignedShort()
	 */
	public final int readUnsignedShort() throws IOException {
		return in.readUnsignedShort();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readInt()
	 */
	public final int readInt() throws IOException {
		return in.readInt();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readLong()
	 */
	public final long readLong() throws IOException {
		return in.readLong();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readFloat()
	 */
	public final float readFloat() throws IOException {
		return in.readFloat();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readDouble()
	 */
	public final double readDouble() throws IOException {
		return in.readDouble();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readFully(byte[])
	 */
	public final void readFully(final byte[] buf) throws IOException {
		in.readFully(buf);
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readFully(byte[], int, int)
	 */
	public final void readFully(final byte[] buf, final int off, final int len) throws IOException {
		in.readFully(buf, off, len);
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#skipBytes(int)
	 */
	public final int skipBytes(final int len) throws IOException {
		return in.skipBytes(len);
	}

	/**
	 * @return String
	 * @throws IOException 
	 * @deprecated
	 */
	public final String readLine() throws IOException {
		return in.readLine();
	}

	/**
	 * 
	 * @see java.io.ObjectInputStream#readUTF()
	 */
	public final String readUTF() throws IOException {
		return in.readUTF();
	}

}

Back to the top