|
92 | 92 | import org.eclipse.equinox.p2.repository.artifact.IFileArtifactRepository; |
93 | 93 | import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; |
94 | 94 | import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
| 95 | +import org.eclipse.osgi.util.NLS; |
95 | 96 | import org.eclipse.pde.core.target.ITargetDefinition; |
96 | 97 | import org.eclipse.pde.core.target.ITargetHandle; |
97 | 98 | import org.eclipse.pde.core.target.ITargetLocation; |
@@ -984,44 +985,108 @@ private static <T> T getP2Service(Class<T> key, String absentErrorMessage) throw |
984 | 985 | /** |
985 | 986 | * Return a queryable on the metadata defined in the given repo locations |
986 | 987 | * |
987 | | - * @param repos the repos to lookup |
988 | | - * @param followRepositoryReferences whether to follow repository references |
989 | | - * @param monitor the progress monitor |
| 988 | + * @param repos |
| 989 | + * the repos to lookup |
| 990 | + * @param followRepositoryReferences |
| 991 | + * whether to follow repository references |
| 992 | + * @param monitor |
| 993 | + * the progress monitor |
990 | 994 | * @return the set of metadata repositories found |
991 | | - * @throws CoreException if there is a problem getting the repositories |
| 995 | + * @throws CoreException |
| 996 | + * if there is a problem getting the repositories |
992 | 997 | */ |
993 | 998 | static IQueryable<IInstallableUnit> getQueryableMetadata(Collection<URI> repos, boolean followRepositoryReferences, |
994 | 999 | IProgressMonitor monitor) throws CoreException { |
995 | | - IMetadataRepositoryManager manager = getRepoManager(); |
| 1000 | + return getQueryableMetadata(repos, followRepositoryReferences, false, monitor); |
| 1001 | + } |
| 1002 | + |
| 1003 | + /** |
| 1004 | + * Return a queryable on the metadata defined in the given repo locations |
| 1005 | + * |
| 1006 | + * @param repos |
| 1007 | + * the repos to lookup |
| 1008 | + * @param followRepositoryReferences |
| 1009 | + * whether to follow repository references |
| 1010 | + * @param forceReload |
| 1011 | + * forces reloading of repositories |
| 1012 | + * @param monitor |
| 1013 | + * the progress monitor |
| 1014 | + * @return the set of metadata repositories found |
| 1015 | + * @throws CoreException |
| 1016 | + * if there is a problem getting the repositories |
| 1017 | + */ |
| 1018 | + static IQueryable<IInstallableUnit> getQueryableMetadata(Collection<URI> repos, boolean followRepositoryReferences, |
| 1019 | + boolean forceReload, |
| 1020 | + IProgressMonitor monitor) throws CoreException { |
| 1021 | + IMetadataRepositoryManager metadataRepositoryManager = getRepoManager(); |
| 1022 | + Collection<URI> existing = new LinkedHashSet<>( |
| 1023 | + Arrays.asList(metadataRepositoryManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL))); |
996 | 1024 | if (repos.isEmpty()) { |
997 | | - repos = Arrays.asList(manager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL)); |
| 1025 | + repos = existing; |
998 | 1026 | } |
999 | | - SubMonitor subMonitor = SubMonitor.convert(monitor, repos.size() * 2); |
1000 | | - |
| 1027 | + int work = repos.size() * (forceReload ? 4 : 2); |
| 1028 | + SubMonitor subMonitor = SubMonitor.convert(monitor, work); |
1001 | 1029 | Set<IRepositoryReference> seen = new HashSet<>(); |
1002 | 1030 | List<IMetadataRepository> result = new ArrayList<>(repos.size()); |
1003 | 1031 | List<IMetadataRepository> additional = new ArrayList<>(); |
1004 | 1032 | MultiStatus repoStatus = new MultiStatus(PDECore.PLUGIN_ID, 0, Messages.IUBundleContainer_ProblemsLoadingRepositories); |
1005 | 1033 | for (URI location : repos) { |
1006 | 1034 | try { |
1007 | | - IMetadataRepository repository = manager.loadRepository(location, subMonitor.split(1)); |
| 1035 | + IMetadataRepository repository = metadataRepositoryManager.loadRepository(location, subMonitor.split(1)); |
1008 | 1036 | result.add(repository); |
1009 | 1037 | if (followRepositoryReferences) { |
1010 | | - addReferences(repository, additional, seen, manager, subMonitor.split(1)); |
| 1038 | + addReferences(repository, additional, seen, metadataRepositoryManager, subMonitor.split(1)); |
1011 | 1039 | } |
1012 | 1040 | } catch (ProvisionException e) { |
1013 | 1041 | repoStatus.add(e.getStatus()); |
1014 | 1042 | } |
1015 | 1043 | } |
1016 | | - |
1017 | 1044 | if (result.size() != repos.size()) { |
1018 | 1045 | throw new CoreException(repoStatus); |
1019 | 1046 | } |
1020 | 1047 | result.addAll(additional); |
1021 | | - if (result.size() == 1) { |
1022 | | - return result.get(0); |
| 1048 | + if (result.isEmpty()) { |
| 1049 | + return QueryUtil.compoundQueryable(List.of()); |
| 1050 | + } |
| 1051 | + Collection<IMetadataRepository> unique = new LinkedHashSet<>(result); |
| 1052 | + if (forceReload) { |
| 1053 | + List<IMetadataRepository> refreshed = new ArrayList<>(); |
| 1054 | + subMonitor.setWorkRemaining(2 * unique.size()); |
| 1055 | + IArtifactRepositoryManager artifactRepositoryManager = getArtifactRepositoryManager(); |
| 1056 | + metadataRepositoryManager = getRepoManager(); |
| 1057 | + Collection<URI> existingArtifactRepositories = new LinkedHashSet<>( |
| 1058 | + Arrays.asList(artifactRepositoryManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL))); |
| 1059 | + for (IMetadataRepository metadataRepository : unique) { |
| 1060 | + URI location = metadataRepository.getLocation(); |
| 1061 | + if (existing.contains(location)) { |
| 1062 | + try { |
| 1063 | + refreshed.add(metadataRepositoryManager.refreshRepository(location, subMonitor.split(1))); |
| 1064 | + } catch (ProvisionException e) { |
| 1065 | + ILog.get().warn(NLS.bind(Messages.P2TargetUtils_cant_refresh_metadata, location), e); |
| 1066 | + // if refresh do not work, use the previously loaded |
| 1067 | + // repository |
| 1068 | + refreshed.add(metadataRepository); |
| 1069 | + } |
| 1070 | + if (existingArtifactRepositories.contains(location)) { |
| 1071 | + try { |
| 1072 | + artifactRepositoryManager.refreshRepository(location, subMonitor.split(1)); |
| 1073 | + } catch (ProvisionException e) { |
| 1074 | + // we tried our best... |
| 1075 | + ILog.get().warn(NLS.bind(Messages.P2TargetUtils_cant_refresh_artifacts, location), e); |
| 1076 | + } |
| 1077 | + } |
| 1078 | + } else { |
| 1079 | + // the repo was just loaded as part of this call so it has |
| 1080 | + // to be (almost) fresh |
| 1081 | + refreshed.add(metadataRepository); |
| 1082 | + } |
| 1083 | + } |
| 1084 | + unique = refreshed; |
| 1085 | + } |
| 1086 | + if (unique.size() == 1) { |
| 1087 | + return unique.iterator().next(); |
1023 | 1088 | } |
1024 | | - return QueryUtil.compoundQueryable(new LinkedHashSet<>(result)); |
| 1089 | + return QueryUtil.compoundQueryable(unique); |
1025 | 1090 | } |
1026 | 1091 |
|
1027 | 1092 | private static void addReferences(IMetadataRepository repository, List<IMetadataRepository> result, |
|
0 commit comments