Skip to main content
summaryrefslogtreecommitdiffstats
blob: 00d35d6bef19d2132e664f52c40b5dd783ceb270 (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
/*******************************************************************************
 * Copyright (c) 2015 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.util.HashMap;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;

/**
 * @since 3.7
 */
public class ClassResolverObjectInputStream extends ObjectInputStream {

	public static final HashMap<String, Class<?>> primClasses = new HashMap<String, Class<?>>(8, 1.0F);

	static {
		primClasses.put("boolean", boolean.class); //$NON-NLS-1$
		primClasses.put("byte", byte.class); //$NON-NLS-1$
		primClasses.put("char", char.class); //$NON-NLS-1$
		primClasses.put("short", short.class); //$NON-NLS-1$
		primClasses.put("int", int.class); //$NON-NLS-1$
		primClasses.put("long", long.class); //$NON-NLS-1$
		primClasses.put("float", float.class); //$NON-NLS-1$
		primClasses.put("double", double.class); //$NON-NLS-1$
		primClasses.put("void", void.class); //$NON-NLS-1$
	}

	public static ObjectInputStream create(BundleContext ctxt, InputStream ins, String filter) throws IOException, SecurityException {
		if (ctxt == null)
			return new ObjectInputStream(ins);
		try {
			return new ClassResolverObjectInputStream(ctxt, ins, filter);
		} catch (InvalidSyntaxException e) {
			throw new IOException("Could not create ClassResolverObjectInputStream because of InvalidSyntaxException in filter=" + filter); //$NON-NLS-1$
		}
	}

	public static ObjectInputStream create(BundleContext ctxt, InputStream ins) throws IOException {
		return create(ctxt, ins, "(" + IClassResolver.BUNDLE_PROP_NAME + "=" + ctxt.getBundle().getSymbolicName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	private final BundleContext bundleContext;
	private ServiceTracker<IClassResolver, IClassResolver> classResolverST;

	private Filter createClassResolverFilter(String classResolverFilterString) throws InvalidSyntaxException {
		String objectClassFilterString = "(" + Constants.OBJECTCLASS + "=" + IClassResolver.class.getName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		if (classResolverFilterString != null)
			objectClassFilterString = "(&" + objectClassFilterString + classResolverFilterString + ")"; //$NON-NLS-1$ //$NON-NLS-2$
		return this.bundleContext.createFilter(objectClassFilterString);
	}

	protected ClassResolverObjectInputStream(BundleContext ctxt, String classResolverFilter) throws IOException, SecurityException, InvalidSyntaxException {
		super();
		this.bundleContext = ctxt;
		this.classResolverST = new ServiceTracker<IClassResolver, IClassResolver>(this.bundleContext, createClassResolverFilter(classResolverFilter), null);
	}

	protected ClassResolverObjectInputStream(BundleContext ctxt) throws IOException, SecurityException, InvalidSyntaxException {
		this(ctxt, (String) null);
	}

	public ClassResolverObjectInputStream(BundleContext ctxt, InputStream ins, String classResolverFilter) throws IOException, SecurityException, InvalidSyntaxException {
		super(ins);
		this.bundleContext = ctxt;
		this.classResolverST = new ServiceTracker<IClassResolver, IClassResolver>(this.bundleContext, createClassResolverFilter(classResolverFilter), null);
	}

	public ClassResolverObjectInputStream(BundleContext ctxt, InputStream ins) throws IOException, SecurityException, InvalidSyntaxException {
		this(ctxt, ins, null);
	}

	protected BundleContext getContext() {
		return this.bundleContext;
	}

	private IClassResolver getClassResolver() {
		IClassResolver result = null;
		if (this.classResolverST != null) {
			this.classResolverST.open();
			result = this.classResolverST.getService();
			this.classResolverST.close();
		}
		return result;
	}

	@SuppressWarnings("unused")
	@Override
	protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
		IClassResolver classResolver = null;
		if (this.classResolverST != null) {
			this.classResolverST.open();
			classResolver = this.classResolverST.getService();
			this.classResolverST.close();
		}
		if (classResolver != null)
			return classResolver.resolveClass(desc);
		throw new ClassNotFoundException("Cannot deserialize class=" + desc + " because no IClassResolver service available"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	public static Class<?> resolvePrimitiveClass(ObjectStreamClass desc, ClassNotFoundException cnfe) throws ClassNotFoundException {
		Class<?> cl = primClasses.get(desc.getName());
		if (cl != null)
			return cl;
		if (cnfe != null)
			throw cnfe;
		throw new ClassNotFoundException("Could not find class=" + desc); //$NON-NLS-1$
	}

}

Back to the top