Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a8f5d16bfc86b3cb1da1e8ae5dbad9892c1c9f5b (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
/*******************************************************************************
 * Copyright (c) 2008, 2017 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.pde.internal.build.publisher;

import java.util.*;
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils.IPathComputer;
import org.eclipse.equinox.internal.p2.publisher.FileSetDescriptor;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.publisher.AbstractAdvice;
import org.eclipse.equinox.p2.publisher.actions.IFeatureRootAdvice;

public class FeatureRootAdvice extends AbstractAdvice implements IFeatureRootAdvice {
	private static final int IDX_COMPUTER = 0;
	private static final int IDX_DESCRIPTOR = 1;

	// String config -> Object[] { GatheringComputer, Map: permission -> Set, String }
	private final Map<String, Object[]> advice = new HashMap<>();
	private String featureId;
	private Version featureVersion;

	@Override
	public boolean isApplicable(String configSpec, boolean includeDefault, String id, Version version) {
		if (featureId != null && !featureId.equals(id))
			return false;
		if (featureVersion != null && !featureVersion.equals(version))
			return false;

		if (configSpec != null && !advice.containsKey(configSpec))
			return false;

		return true;
	}

	/**
	 * Return the configs for which we have advice
	 * @return String[]
	 */
	public String[] getConfigs() {
		return advice.keySet().toArray(new String[advice.size()]);
	}

	/**
	 * Return the GatheringComputer containing the set of rootfiles to include for the given config
	 * Returns null if we have no advice for the given config.
	 * @param config
	 * @return GatheringComputer
	 */
	@Override
	public IPathComputer getRootFileComputer(String config) {
		if (advice.containsKey(config))
			return (GatheringComputer) advice.get(config)[IDX_COMPUTER];
		return null;
	}

	public void addRootfiles(String config, GatheringComputer computer) {
		Object[] configAdvice = getConfigAdvice(config);

		if (configAdvice[IDX_COMPUTER] == null)
			configAdvice[IDX_COMPUTER] = computer;
		else {
			GatheringComputer existing = (GatheringComputer) configAdvice[IDX_COMPUTER];
			existing.addAll(computer);
		}
		FileSetDescriptor descriptor = getDescriptor(config);
		descriptor.addFiles(computer.getFiles());
	}

	public void addPermissions(String config, String permissions, String[] files) {
		FileSetDescriptor descriptor = getDescriptor(config);
		for (int i = 0; i < files.length; i++) {
			descriptor.addPermissions(new String[] {permissions, files[i]});
		}
	}

	public void addLinks(String config, String links) {
		FileSetDescriptor descriptor = getDescriptor(config);
		descriptor.setLinks(links);
	}

	private Object[] getConfigAdvice(String config) {
		Object[] configAdvice = advice.get(config);
		if (configAdvice == null) {
			configAdvice = new Object[3];
			advice.put(config, configAdvice);
		}
		return configAdvice;
	}

	@Override
	public FileSetDescriptor getDescriptor(String config) {
		Object[] configAdvice = getConfigAdvice(config);
		FileSetDescriptor descriptor = null;

		if (configAdvice[IDX_DESCRIPTOR] != null)
			descriptor = (FileSetDescriptor) configAdvice[IDX_DESCRIPTOR];
		else {
			String key = "root"; //$NON-NLS-1$
			if (config.length() > 0)
				key += "." + config; //$NON-NLS-1$
			descriptor = new FileSetDescriptor(key, config);
			configAdvice[IDX_DESCRIPTOR] = descriptor;
		}
		return descriptor;
	}

	public void setFeatureId(String featureId) {
		this.featureId = featureId;
	}

	public void setFeatureVersion(Version featureVersion) {
		this.featureVersion = featureVersion;
	}

	@Override
	public String[] getConfigurations() {
		Set<String> keys = advice.keySet();
		return keys.toArray(new String[keys.size()]);
	}

}

Back to the top