Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8c948141f8ac4b0c6ac90945f29b393492491ad (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
/*******************************************************************************
 * Copyright (c) 2004, 2015 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.core.internal.runtime;

import java.io.*;
import java.net.URL;
import org.eclipse.core.internal.boot.PlatformURLConnection;
import org.eclipse.core.internal.boot.PlatformURLHandler;
import org.eclipse.core.runtime.IPath;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;

public class PlatformURLMetaConnection extends PlatformURLConnection {
	private Bundle target = null;
	private static boolean isRegistered = false;
	public static final String META = "meta"; //$NON-NLS-1$

	/*
	 * Constructor for the class.
	 */
	public PlatformURLMetaConnection(URL url) {
		super(url);
	}

	@Override
	protected URL resolve() throws IOException {
		String spec = url.getFile().trim();
		if (spec.startsWith("/")) //$NON-NLS-1$
			spec = spec.substring(1);
		if (!spec.startsWith(META))
			throw new IOException(NLS.bind(CommonMessages.url_badVariant, url.toString()));
		int ix = spec.indexOf("/", META.length() + 1); //$NON-NLS-1$
		String ref = ix == -1 ? spec.substring(META.length() + 1) : spec.substring(META.length() + 1, ix);
		String id = getId(ref);
		Activator activator = Activator.getDefault();
		if (activator == null)
			throw new IOException(CommonMessages.activator_not_available);
		target = activator.getBundle(id);
		if (target == null)
			throw new IOException(NLS.bind(CommonMessages.url_resolvePlugin, url.toString()));
		IPath path = MetaDataKeeper.getMetaArea().getStateLocation(target);
		if (ix != -1 || (ix + 1) <= spec.length())
			path = path.append(spec.substring(ix + 1));
		return path.toFile().toURL();
	}

	public static void startup() {
		// register connection type for platform:/meta handling
		if (isRegistered)
			return;
		PlatformURLHandler.register(META, PlatformURLMetaConnection.class);
		isRegistered = true;
	}

	@Override
	public OutputStream getOutputStream() throws IOException {
		//This is not optimal but connection is a private instance variable in super.
		URL resolved = getResolvedURL();
		if (resolved != null) {
			String fileString = resolved.getFile();
			if (fileString != null) {
				File file = new File(fileString);
				String parent = file.getParent();
				if (parent != null)
					new File(parent).mkdirs();
				return new FileOutputStream(file);
			}
		}
		return null;
	}
}

Back to the top