Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4ff52e2d892fb67aa7e0af6a5d07c03c8f771030 (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
/*******************************************************************************
 * Copyright (c) 2007, 2010 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
 *     Cloudsmith Inc. - IMemberProvider access.
 *******************************************************************************/
package org.eclipse.equinox.p2.repository.artifact.spi;

import java.util.Arrays;
import java.util.Map;
import org.eclipse.equinox.internal.p2.core.helpers.OrderedProperties;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.expression.IMemberProvider;
import org.eclipse.equinox.p2.repository.artifact.*;

/**
 * This represents information about a given artifact stored on a particular byte server.
 * @since 2.0
 */
public class ArtifactDescriptor implements IArtifactDescriptor, IMemberProvider {
	public static final String MEMBER_ARTIFACT_KEY = "artifactKey"; //$NON-NLS-1$
	public static final String MEMBER_PROCESSING_STEPS = "processingSteps"; //$NON-NLS-1$
	public static final String MEMBER_PROPERTIES = "properties"; //$NON-NLS-1$
	public static final String MEMBER_REPOSITORY = "repository"; //$NON-NLS-1$

	private static final IProcessingStepDescriptor[] EMPTY_STEPS = new ProcessingStepDescriptor[0];

	protected IArtifactKey key; // The key associated with this artifact

	// The list of post processing steps that must be applied one the artifact once it 
	// has been downloaded (e.g, unpack, then md5 checksum, then...)
	protected IProcessingStepDescriptor[] processingSteps = EMPTY_STEPS;

	protected Map<String, String> properties = new OrderedProperties();

	private transient IArtifactRepository repository;

	/**
	 * Creates a new artifact descriptor with the same key, properties, repository,
	 * and processing steps as the provided base descriptor.
	 * 
	 * @param base the descriptor to use as a template for this new descriptor
	 */
	public ArtifactDescriptor(IArtifactDescriptor base) {
		super();
		key = base.getArtifactKey();
		setProcessingSteps(base.getProcessingSteps());
		properties.putAll(base.getProperties());
		repository = base.getRepository();
	}

	/**
	 * Returns a new artifact descriptor that uses the provided artifact key
	 * 
	 * @param key The artifact key corresponding to this descriptor
	 */
	public ArtifactDescriptor(IArtifactKey key) {
		super();
		this.key = key;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor#getArtifactKey()
	 */
	@Override
	public IArtifactKey getArtifactKey() {
		return key;
	}

	@Override
	public String getProperty(String propertyKey) {
		return properties.get(propertyKey);
	}

	public void setProperty(String key, String value) {
		if (value == null)
			properties.remove(key);
		else
			properties.put(key, value);
	}

	public void addProperties(Map<String, String> additionalProperties) {
		properties.putAll(additionalProperties);
	}

	/**
	 * Returns a read-only collection of the properties of the artifact descriptor.
	 * @return the properties of this artifact descriptor.
	 */
	@Override
	public Map<String, String> getProperties() {
		return OrderedProperties.unmodifiableProperties(properties);
	}

	@Override
	public IProcessingStepDescriptor[] getProcessingSteps() {
		return processingSteps;
	}

	public void setProcessingSteps(IProcessingStepDescriptor[] value) {
		processingSteps = value == null || value.length == 0 ? EMPTY_STEPS : value;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;

		// Other implementations of IArtifactDescriptor must not be considered equal
		if (!(obj.getClass().equals(getClass())))
			return false;

		ArtifactDescriptor other = (ArtifactDescriptor) obj;
		if (key == null) {
			if (other.getArtifactKey() != null)
				return false;
		} else if (!key.equals(other.getArtifactKey()))
			return false;

		if (!Arrays.equals(processingSteps, other.getProcessingSteps()))
			return false;

		String format = getProperty(FORMAT);
		String otherFormat = other.getProperty(FORMAT);
		if (format != null ? !format.equals(otherFormat) : otherFormat != null)
			return false;

		return true;
	}

	@Override
	public int hashCode() {
		String format = getProperty(FORMAT);

		final int prime = 31;
		int result = 1;
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		result = prime * result + Arrays.asList(processingSteps).hashCode();
		result = prime * result + (format != null ? format.hashCode() : 0);
		return result;
	}

	@Override
	public IArtifactRepository getRepository() {
		return repository;
	}

	public void setRepository(IArtifactRepository value) {
		repository = value;
	}

	@Override
	public String toString() {
		String format = getProperty(IArtifactDescriptor.FORMAT);
		if (format == null)
			return "canonical: " + key.toString(); //$NON-NLS-1$
		return format + ": " + key.toString(); //$NON-NLS-1$
	}

	@Override
	public Object getMember(String memberName) {
		if (memberName == MEMBER_ARTIFACT_KEY)
			return key;

		if (memberName == MEMBER_PROPERTIES)
			return properties;

		if (memberName == MEMBER_PROCESSING_STEPS)
			return processingSteps;

		if (memberName == MEMBER_REPOSITORY)
			return repository;

		throw new IllegalArgumentException("No such member: " + memberName); //$NON-NLS-1$
	}

}

Back to the top