Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8406fd2dca3a3526542b9f6ae543adb5eb04ade0 (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
/*******************************************************************************
 * Copyright (c) 2005, 2017 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 org.eclipse.equinox.launcher;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.*;
import java.util.*;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipFile;

/**
 * The launcher to start eclipse using webstart. To use this launcher, the client 
 * must accept to give all security permissions.
 * <p>
 * <b>Note:</b> This class should not be referenced programmatically by
 * other Java code. This class exists only for the purpose of launching Eclipse
 * using Java webstart. To launch Eclipse programmatically, use 
 * org.eclipse.core.runtime.adaptor.EclipseStarter. The fields and methods
 * on this class are not API.
 * 
 * @noextend This class is not intended to be subclassed by clients.
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
//The bundles are discovered by finding all the jars on the classpath. Then they are added with their full path to the osgi.bundles list.
public class WebStartMain extends Main {
	private static final String PROP_WEBSTART_AUTOMATIC_INSTALLATION = "eclipse.webstart.automaticInstallation"; //$NON-NLS-1$
	private static final String DEFAULT_OSGI_BUNDLES = "org.eclipse.equinox.common@2:start, org.eclipse.core.runtime@start"; //$NON-NLS-1$
	private static final String PROP_OSGI_BUNDLES = "osgi.bundles"; //$NON-NLS-1$
	private static final String PROP_CHECK_CONFIG = "osgi.checkConfiguration"; //$NON-NLS-1$

	private Map<String, List<BundleInfo>> allBundles = null; // Map of all the bundles found on the classpath. Id -> ArrayList of BundleInfo
	private List<BundleInfo> bundleList = null; //The list of bundles found on the osgi.bundle list 

	protected class BundleInfo {
		String bsn;
		String version;
		String startData;
		String location;
	}

	/**
	 * @noreference This method is not intended to be referenced by clients.
	 */
	public static void main(String[] args) {
		System.setSecurityManager(null); //TODO Hack so that when the classloader loading the fwk is created we don't have funny permissions. This should be revisited. 
		int result = new WebStartMain().run(args);
		if (!Boolean.getBoolean(PROP_NOSHUTDOWN))
			System.exit(result);
	}

	private void setDefaultBundles() {
		if (System.getProperty(PROP_OSGI_BUNDLES) != null)
			return;
		System.getProperties().put(PROP_OSGI_BUNDLES, DEFAULT_OSGI_BUNDLES);
	}

	@Override
	protected void basicRun(String[] args) throws Exception {
		setDefaultBundles();
		initializeBundleListStructure();
		discoverBundles();
		//Set the fwk location since the regular lookup would not find it
		String fwkURL = searchFor(framework, null);
		if (fwkURL == null) {
			//MESSAGE CAN"T FIND THE FWK
		}
		allBundles.remove(framework);
		System.getProperties().put(PROP_FRAMEWORK, fwkURL);
		super.basicRun(args);
	}

	@Override
	protected void beforeFwkInvocation() {
		// set the check config option so we pick up modified bundle jars (bug 152825)
		if (System.getProperty(PROP_CHECK_CONFIG) == null)
			System.getProperties().put(PROP_CHECK_CONFIG, "true"); //$NON-NLS-1$
		buildOSGiBundleList();
		cleanup();
	}

	/*
	 * Null out all the fields containing data 
	 */
	private void cleanup() {
		allBundles = null;
		bundleList = null;
	}

	/*
	 * Find the target bundle among all the bundles that are on the classpath.
	 * The start parameter is not used in this context
	 */
	@Override
	protected String searchFor(final String target, String start) {
		List<BundleInfo> matches = allBundles.get(target);
		if (matches == null)
			return null;
		int numberOfMatches = matches.size();
		if (numberOfMatches == 1) {
			return matches.get(0).location;
		}
		if (numberOfMatches == 0)
			return null;

		String[] versions = new String[numberOfMatches];
		int highest = 0;
		for (int i = 0; i < versions.length; i++) {
			versions[i] = matches.get(i).version;
		}
		highest = findMax(null, versions);
		return matches.get(highest).location;
	}

	private BundleInfo findBundle(final String target, String version, boolean removeMatch) {
		List<BundleInfo> matches = allBundles.get(target);
		int numberOfMatches = matches != null ? matches.size() : 0;
		if (numberOfMatches == 1) {
			//TODO Need to check the version
			return removeMatch ? matches.remove(0) : matches.get(0);
		}
		if (numberOfMatches == 0)
			return null;

		if (version != null) {
			for (Iterator<BundleInfo> iterator = matches.iterator(); iterator.hasNext();) {
				BundleInfo bi = iterator.next();
				if (bi.version.equals(version)) {
					if (removeMatch)
						iterator.remove();
					return bi;
				}
			}
			//TODO Need to log the fact that we could not find the version mentioned
			return null;
		}
		String[] versions = new String[numberOfMatches];
		int highest = 0;
		for (int i = 0; i < versions.length; i++) {
			versions[i] = matches.get(i).version;
		}
		highest = findMax(null, versions);
		return removeMatch ? matches.remove(highest) : matches.get(highest);
	}

	/* 
	 * Get all the bundles available on the webstart classpath
	 */
	private void discoverBundles() {
		allBundles = new HashMap<>();
		try {
			Enumeration<URL> resources = WebStartMain.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
			while (resources.hasMoreElements()) {
				BundleInfo found = getBundleInfo(resources.nextElement());
				if (found == null)
					continue;
				List<BundleInfo> matching = allBundles.get(found.bsn);
				if (matching == null) {
					matching = new ArrayList<>(1);
					allBundles.put(found.bsn, matching);
				}
				matching.add(found);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String extractInnerURL(URL url) {
		try {
			URLConnection connection = null;
			try {
				connection = url.openConnection();
				if (connection instanceof JarURLConnection) {
					JarFile jarFile = ((JarURLConnection) connection).getJarFile();
					String name = jarFile.getName();
					// Some VMs may not return a jar name as a security precaution
					if (name == null || name.length() == 0)
						name = getJarNameByReflection(jarFile);

					if (name != null && name.length() > 0)
						return "file:" + name; //$NON-NLS-1$
				}
			} finally {
				if (connection != null)
					connection.getInputStream().close();
			}
		} catch (IOException e) {
			//Ignore and return the external form
		}
		return url.toExternalForm();
	}

	/*
	 *  Get a value of the ZipFile.name field using reflection.
	 *  For this to succeed, we need the "suppressAccessChecks" permission.
	 */
	private String getJarNameByReflection(JarFile jarFile) {
		if (jarFile == null)
			return null;

		Field nameField = null;
		try {
			nameField = ZipFile.class.getDeclaredField("name"); //$NON-NLS-1$
		} catch (NoSuchFieldException e1) {
			try {
				nameField = ZipFile.class.getDeclaredField("fileName"); //$NON-NLS-1$
			} catch (NoSuchFieldException e) {
				//ignore
			}
		}

		if (nameField == null || Modifier.isStatic(nameField.getModifiers()) || nameField.getType() != String.class)
			return null;

		try {
			nameField.setAccessible(true);
			return (String) nameField.get(jarFile);
		} catch (SecurityException e) {
			// Don't have permissions, ignore
		} catch (IllegalArgumentException e) {
			// Shouldn't happen
		} catch (IllegalAccessException e) {
			// Shouldn't happen
		}

		return null;
	}

	/*
	 * Construct bundle info objects from items found on the osgi.bundles list
	 */
	private void initializeBundleListStructure() {
		final char STARTLEVEL_SEPARATOR = '@';

		//In webstart the bundles list can only contain bundle names with or without a version.
		String prop = System.getProperty(PROP_OSGI_BUNDLES);
		if (prop == null || prop.trim().equals("")) { //$NON-NLS-1$
			bundleList = new ArrayList<>(0);
			return;
		}

		bundleList = new ArrayList<>(10);
		StringTokenizer tokens = new StringTokenizer(prop, ","); //$NON-NLS-1$
		while (tokens.hasMoreTokens()) {
			String token = tokens.nextToken().trim();
			String bundleId = token;
			if (token.equals("")) //$NON-NLS-1$
				continue;
			int startLevelSeparator;
			BundleInfo toAdd = new BundleInfo();
			toAdd.bsn = bundleId;
			if ((startLevelSeparator = token.lastIndexOf(STARTLEVEL_SEPARATOR)) != -1) {
				toAdd.bsn = token.substring(0, startLevelSeparator);
				toAdd.startData = token.substring(startLevelSeparator);
				//Note that here we don't try to parse the start attribute since this info is then used to recompose the value for osgi.bundles
			}
			bundleList.add(toAdd);
		}
	}

	private BundleInfo getBundleInfo(URL manifestURL) {
		final String BUNDLE_SYMBOLICNAME = "Bundle-SymbolicName"; //$NON-NLS-1$
		final String BUNDLE_VERSION = "Bundle-Version"; //$NON-NLS-1$
		final String DEFAULT_VERSION = "0.0.0"; //$NON-NLS-1$

		Manifest mf;
		try {
			mf = new Manifest(manifestURL.openStream());
			String symbolicNameString = mf.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME);
			if (symbolicNameString == null)
				return null;

			BundleInfo result = new BundleInfo();
			String version = mf.getMainAttributes().getValue(BUNDLE_VERSION);
			result.version = (version != null) ? version : DEFAULT_VERSION;
			result.location = extractInnerURL(manifestURL);
			int pos = symbolicNameString.lastIndexOf(';');
			if (pos != -1) {
				result.bsn = symbolicNameString.substring(0, pos);
				return result;
			}
			result.bsn = symbolicNameString;
			return result;
		} catch (IOException e) {
			if (debug)
				e.printStackTrace();
		}
		return null;
	}

	//Build the osgi bundle list. The allbundles data structure is changed during the process. 
	private void buildOSGiBundleList() {
		StringBuffer finalBundleList = new StringBuffer(allBundles.size() * 30);
		//First go through all the bundles of the bundle
		for (Iterator<BundleInfo> iterator = bundleList.iterator(); iterator.hasNext();) {
			BundleInfo searched = iterator.next();
			BundleInfo found = findBundle(searched.bsn, searched.version, true);
			if (found != null)
				finalBundleList.append(REFERENCE_SCHEME).append(found.location).append(searched.startData).append(',');
		}

		if (!Boolean.FALSE.toString().equalsIgnoreCase(System.getProperties().getProperty(PROP_WEBSTART_AUTOMATIC_INSTALLATION))) {
			for (Iterator<List<BundleInfo>> iterator = allBundles.values().iterator(); iterator.hasNext();) {
				List<BundleInfo> toAdd = iterator.next();
				for (Iterator<BundleInfo> iterator2 = toAdd.iterator(); iterator2.hasNext();) {
					BundleInfo bi = iterator2.next();
					finalBundleList.append(REFERENCE_SCHEME).append(bi.location).append(',');
				}
			}
		}
		System.getProperties().put(PROP_OSGI_BUNDLES, finalBundleList.toString());
	}
}

Back to the top