Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7380098cbc9dca22ef30625ab0d135d4e720af11 (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
/*******************************************************************************
 * Copyright (c) 2011 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.equinox.p2.internal.repository.tools;

import org.eclipse.equinox.p2.repository.tools.comparator.ArtifactComparatorFactory;
import org.eclipse.equinox.p2.repository.tools.comparator.IArtifactComparator;

import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.repository.artifact.*;

public class ArtifactRepositoryValidator {

	private IArtifactComparator comparator;

	public ArtifactRepositoryValidator(String comparatorId) throws ProvisionException {
		try {
			comparator = ArtifactComparatorFactory.getArtifactComparator(comparatorId);
		} catch (IllegalArgumentException e) {
			throw new ProvisionException(Messages.invalidComparatorId);
		}
	}

	public IStatus validateRepository(IArtifactRepository repository) {
		if (repository instanceof CompositeArtifactRepository)
			return validateComposite((CompositeArtifactRepository) repository);

		IQueryResult<IArtifactKey> queryResult = repository.query(ArtifactKeyQuery.ALL_KEYS, new NullProgressMonitor());
		for (Iterator<IArtifactKey> iterator = queryResult.iterator(); iterator.hasNext();) {
			IArtifactDescriptor[] descriptors = repository.getArtifactDescriptors(iterator.next());
			for (int i = 0; i < descriptors.length - 2; i++) {
				IStatus compareResult = comparator.compare(repository, descriptors[i], repository, descriptors[i + 1]);
				if (!compareResult.isOK()) {
					return compareResult;
				}
			}
		}
		return Status.OK_STATUS;
	}

	public IStatus validateComposite(CompositeArtifactRepository repository) {
		List<IArtifactRepository> repos = repository.getLoadedChildren();
		IQueryResult<IArtifactKey> queryResult = repository.query(ArtifactKeyQuery.ALL_KEYS, new NullProgressMonitor());
		for (Iterator<IArtifactKey> iterator = queryResult.iterator(); iterator.hasNext();) {
			IArtifactKey key = iterator.next();
			IArtifactRepository firstRepo = null;
			for (IArtifactRepository child : repos) {
				if (child.contains(key)) {
					if (firstRepo == null) {
						firstRepo = child;
						continue;
					}

					IArtifactDescriptor[] d1 = firstRepo.getArtifactDescriptors(key);
					IArtifactDescriptor[] d2 = child.getArtifactDescriptors(key);
					//If we assume each repo is internally consistant, we only need to compare one descriptor from each repo
					IStatus compareResult = comparator.compare(firstRepo, d1[0], child, d2[0]);
					if (!compareResult.isOK()) {
						//LogHelper.log(compareResult);
						return compareResult;
					}
				}
			}
		}
		return Status.OK_STATUS;
	}

	public IStatus validateComposite(CompositeArtifactRepository composite, IArtifactRepository repository) {
		IQueryResult<IArtifactKey> queryResult = repository.query(ArtifactKeyQuery.ALL_KEYS, new NullProgressMonitor());
		for (Iterator<IArtifactKey> iterator = queryResult.iterator(); iterator.hasNext();) {
			IArtifactKey key = iterator.next();
			if (composite.contains(key)) {
				IArtifactDescriptor[] d1 = composite.getArtifactDescriptors(key);
				IArtifactDescriptor[] d2 = repository.getArtifactDescriptors(key);
				//If we assume each repo is internally consistant, we only need to compare one descriptor from each repo
				IStatus compareResult = comparator.compare(composite, d1[0], repository, d2[0]);
				if (!compareResult.isOK())
					return compareResult;
			}
		}
		return Status.OK_STATUS;
	}
}

Back to the top