Skip to main content
summaryrefslogtreecommitdiffstats
blob: 33158051d85dbd841fbe77a61336ee18fc0f81f6 (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.embeddedequinox;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Map;
import org.osgi.framework.BundleContext;

/*
 * This class assumes the bundle's classloader only has access to org.osgi.framework
 * from the hosting framework.  Any more access could result in ClassVerifier errors.
 */
public class EmbeddedEquinox {
	private final Map frameworkProperties;
	private final String[] frameworkArgs;
	private Class eclipseStarterClazz;
	private BundleContext context;
	private URL[] frameworkClassPath;

	public EmbeddedEquinox(Map frameworkProperties, String[] frameworkArgs, URL[] frameworkClassPath) {
		this.frameworkProperties = frameworkProperties;
		this.frameworkArgs = frameworkArgs;
		this.frameworkClassPath = frameworkClassPath;
	}

	public BundleContext startFramework() {
		System.setProperty("osgi.framework.useSystemProperties", "false");
		try {
			URLClassLoader frameworkLoader = new FrameworkClassLoader(frameworkClassPath, this.getClass().getClassLoader());
			eclipseStarterClazz = frameworkLoader.loadClass("org.eclipse.core.runtime.adaptor.EclipseStarter");

			Method setInitialProperties = eclipseStarterClazz.getMethod("setInitialProperties", new Class[] {Map.class}); //$NON-NLS-1$
			setInitialProperties.invoke(null, new Object[] {frameworkProperties});

			Method runMethod = eclipseStarterClazz.getMethod("startup", new Class[] {String[].class, Runnable.class}); //$NON-NLS-1$
			context = (BundleContext) runMethod.invoke(null, new Object[] {frameworkArgs, null});
		} catch (Throwable t) {
			if (t instanceof RuntimeException)
				throw (RuntimeException) t;
			throw new RuntimeException(t);
		}
		return context;
	}

	public void shutdown() {
		try {
			Method shutdownMethod = eclipseStarterClazz.getMethod("shutdown", (Class[]) null); //$NON-NLS-1$
			shutdownMethod.invoke((Object[]) null, (Object[]) null);

		} catch (Throwable t) {
			if (t instanceof RuntimeException)
				throw (RuntimeException) t;
			throw new RuntimeException(t);
		}
	}

	public class FrameworkClassLoader extends URLClassLoader {
		ClassLoader embeddedBundleLoader;

		public FrameworkClassLoader(URL[] urls, ClassLoader parent) {
			super(urls, null);
			this.embeddedBundleLoader = parent;
		}

		protected Class findClass(String name) throws ClassNotFoundException {
			if (name.startsWith("org.osgi.framework."))
				// TODO this should call findClass; but then have to use reflection!!
				return embeddedBundleLoader.loadClass(name);
			return super.findClass(name);
		}

		public URL findResource(String name) {
			if (name.startsWith("org/osgi/framework/"))
				// TODO this should call findResource; but then have to use reflection!!
				return embeddedBundleLoader.getResource(name);
			return super.findResource(name);
		}

		public Enumeration findResources(String name) throws IOException {
			if (name.startsWith("org/osgi/framework/"))
				// TODO this should call findResources; but then have to use reflection!!
				return embeddedBundleLoader.getResources(name);
			return super.findResources(name);
		}

	}
}

Back to the top