1 /******************************************************************************* 2 * Copyright (c) 2011, 2018 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.equinox.p2.internal.repository.tools; 15 16 import java.util.List; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository; 19 import org.eclipse.equinox.p2.core.ProvisionException; 20 import org.eclipse.equinox.p2.metadata.IArtifactKey; 21 import org.eclipse.equinox.p2.query.IQueryResult; 22 import org.eclipse.equinox.p2.repository.artifact.*; 23 import org.eclipse.equinox.p2.repository.tools.comparator.ArtifactComparatorFactory; 24 import org.eclipse.equinox.p2.repository.tools.comparator.IArtifactComparator; 25 import org.eclipse.osgi.util.NLS; 26 27 public class ArtifactRepositoryValidator { 28 29 private IArtifactComparator comparator; 30 ArtifactRepositoryValidator(String comparatorId)31 public ArtifactRepositoryValidator(String comparatorId) throws ProvisionException { 32 try { 33 comparator = ArtifactComparatorFactory.getArtifactComparator(comparatorId); 34 } catch (IllegalArgumentException e) { 35 throw new ProvisionException(NLS.bind(Messages.invalidComparatorId, comparatorId), e); 36 } 37 } 38 validateRepository(IArtifactRepository repository)39 public IStatus validateRepository(IArtifactRepository repository) { 40 if (repository instanceof CompositeArtifactRepository) 41 return validateComposite((CompositeArtifactRepository) repository); 42 43 IQueryResult<IArtifactKey> queryResult = repository.query(ArtifactKeyQuery.ALL_KEYS, new NullProgressMonitor()); 44 for (IArtifactKey iArtifactKey : queryResult) { 45 IArtifactDescriptor[] descriptors = repository.getArtifactDescriptors(iArtifactKey); 46 for (int i = 0; i < descriptors.length - 2; i++) { 47 IStatus compareResult = comparator.compare(repository, descriptors[i], repository, descriptors[i + 1]); 48 if (!compareResult.isOK()) { 49 return compareResult; 50 } 51 } 52 } 53 return Status.OK_STATUS; 54 } 55 validateComposite(CompositeArtifactRepository repository)56 public IStatus validateComposite(CompositeArtifactRepository repository) { 57 List<IArtifactRepository> repos = repository.getLoadedChildren(); 58 IQueryResult<IArtifactKey> queryResult = repository.query(ArtifactKeyQuery.ALL_KEYS, new NullProgressMonitor()); 59 for (IArtifactKey key : queryResult) { 60 IArtifactRepository firstRepo = null; 61 for (IArtifactRepository child : repos) { 62 if (child.contains(key)) { 63 if (firstRepo == null) { 64 firstRepo = child; 65 continue; 66 } 67 68 IArtifactDescriptor[] d1 = firstRepo.getArtifactDescriptors(key); 69 IArtifactDescriptor[] d2 = child.getArtifactDescriptors(key); 70 // If we assume each repo is internally consistant, we only need to compare one 71 // descriptor from each repo 72 IStatus compareResult = comparator.compare(firstRepo, d1[0], child, d2[0]); 73 if (!compareResult.isOK()) { 74 // LogHelper.log(compareResult); 75 return compareResult; 76 } 77 } 78 } 79 } 80 return Status.OK_STATUS; 81 } 82 validateComposite(CompositeArtifactRepository composite, IArtifactRepository repository)83 public IStatus validateComposite(CompositeArtifactRepository composite, IArtifactRepository repository) { 84 IQueryResult<IArtifactKey> queryResult = repository.query(ArtifactKeyQuery.ALL_KEYS, new NullProgressMonitor()); 85 for (IArtifactKey key : queryResult) { 86 if (composite.contains(key)) { 87 IArtifactDescriptor[] d1 = composite.getArtifactDescriptors(key); 88 IArtifactDescriptor[] d2 = repository.getArtifactDescriptors(key); 89 // If we assume each repo is internally consistant, we only need to compare one 90 // descriptor from each repo 91 IStatus compareResult = comparator.compare(composite, d1[0], repository, d2[0]); 92 if (!compareResult.isOK()) 93 return compareResult; 94 } 95 } 96 return Status.OK_STATUS; 97 } 98 } 99