Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e9be12e6782a586096100343606875bcd6f9af29 (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
/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package test.bug375784;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	public void start(BundleContext context) throws Exception {
		final ClassLoader parent = getClass().getClassLoader();
		// create a custom class loader that uses a bundle's class loader as the parent
		ClassLoader testCL = new ClassLoader(parent) {

			@Override
			protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
				if ("test.bug375784.Test".equals(name)) {
					URL classURL = this.getClass().getResource("Test.class");
					if (classURL == null) {
						throw new ClassNotFoundException(name);
					}
					byte[] bytes;
					try {
						bytes = getBytes(classURL);
						return defineClass(name, bytes, 0, bytes.length);
					} catch (IOException e) {
						throw new ClassNotFoundException(name, e);
					}
				}
				return super.loadClass(name, resolve);
			}

		};

		// Load a class that expects to be able to have free access to SaxParserFactory
		Class clazz;
		try {
			clazz = testCL.loadClass("test.bug375784.Test");
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}

		try {
			clazz.newInstance();
			throw new RuntimeException("Should have failed to create object from class: " + clazz);
		} catch (NoClassDefFoundError e) {
			// expected
		}
	}

	public static byte[] getBytes(URL url) throws IOException {
		InputStream in = url.openStream();
		byte[] classbytes;
		int bytesread = 0;
		int readcount;
		try {
			int length = 1024;
			classbytes = new byte[length];
			readloop: while (true) {
				for (; bytesread < length; bytesread += readcount) {
					readcount = in.read(classbytes, bytesread, length - bytesread);
					if (readcount <= 0) /* if we didn't read anything */
						break readloop; /* leave the loop */
				}
				byte[] oldbytes = classbytes;
				length += 1024;
				classbytes = new byte[length];
				System.arraycopy(oldbytes, 0, classbytes, 0, bytesread);
			}
			if (classbytes.length > bytesread) {
				byte[] oldbytes = classbytes;
				classbytes = new byte[bytesread];
				System.arraycopy(oldbytes, 0, classbytes, 0, bytesread);
			}
		} finally {
			try {
				in.close();
			} catch (IOException ee) {
				// nothing to do here
			}
		}
		return classbytes;
	}

	public void stop(BundleContext context) throws Exception {
		// Nothing
	}

}

Back to the top