Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e5e43281e08bc566b4ad58411c169e0f36dba7f0 (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
/*******************************************************************************
 * Copyright (c) 2011, 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.team.core.importing.provisional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.team.core.ProjectSetCapability;
import org.eclipse.team.core.ProjectSetSerializationContext;
import org.eclipse.team.core.RepositoryProviderType;
import org.eclipse.team.core.ScmUrlImportDescription;
import org.eclipse.team.internal.core.TeamPlugin;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;

/**
 * Abstract implementation of {@link IBundleImporterDelegate}. It is recommended
 * to subclass this class rather than implementing IBundleImporterDelegate
 * directly when providing importer delegates.
 * <p>
 * <strong>EXPERIMENTAL</strong>. This interface has been added as part of a
 * work in progress. There is no guarantee that this API will work or that it
 * will remain the same. Please do not use this API without consulting with the
 * Team team.
 * </p>
 *
 * @since 3.6
 */
public abstract class BundleImporterDelegate implements IBundleImporterDelegate {

	private static final String ATTR_PROJECT = "project"; //$NON-NLS-1$

	public static final String ECLIPSE_SOURCE_REFERENCES = "Eclipse-SourceReferences"; //$NON-NLS-1$

	protected abstract Set getSupportedValues();

	protected abstract RepositoryProviderType getProviderType();

	@Override
	public ScmUrlImportDescription[] validateImport(Map[] manifests) {
		ScmUrlImportDescription[] results = new ScmUrlImportDescription[manifests.length];
		if (getProviderType() != null) {
			for (int i = 0; i < manifests.length; i++) {
				Map manifest = manifests[i];
				String value = (String) manifest.get(ECLIPSE_SOURCE_REFERENCES);
				if (value != null && value.length() > 8) {
					String prefix = value.substring(0, 8);
					if (getSupportedValues().contains(prefix)) {
						try {
							ManifestElement[] elements = ManifestElement.parseHeader(ECLIPSE_SOURCE_REFERENCES, value);
							for (ManifestElement element : elements) {
								String url = element.toString();
								String project = element.getAttribute(ATTR_PROJECT);
								if (project == null) {
									String bsn = (String) manifests[i].get(Constants.BUNDLE_SYMBOLICNAME);
									if (bsn != null) {
										ManifestElement[] bsnElement = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, bsn);
										project = bsnElement[0].getValue();
									}
								}
								results[i] = new ScmUrlImportDescription(url, project);
							}
						} catch (BundleException e) {
							TeamPlugin.log(IStatus.ERROR, "An exception occured while parsing a manifest header", e);//$NON-NLS-1$
						}
					}
				}
			}
		}
		return results;
	}

	@Override
	public IProject[] performImport(ScmUrlImportDescription[] descriptions, IProgressMonitor monitor) throws CoreException {
		List<String> references = new ArrayList<>();
		ProjectSetCapability psfCapability = getProviderType().getProjectSetCapability();
		IProject[] result = null;
		if (psfCapability != null) {
			// collect and validate all header values
			for (ScmUrlImportDescription description : descriptions) {
				references.add(psfCapability.asReference(description.getUri(), description.getProject()));
			}
			// create projects
			if (!references.isEmpty()) {
				SubMonitor subMonitor = SubMonitor.convert(monitor, references.size());
				result = psfCapability.addToWorkspace(references.toArray(new String[references.size()]), new ProjectSetSerializationContext(), subMonitor);
				subMonitor.done();
			}
		}
		return result;
	}
}

Back to the top