From 8796f60c1d0bd9452659a885993310d3a17563e9 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 2 Sep 2023 11:51:55 +0200 Subject: [PATCH] Use new new IRepositoryManager methods to unify manager usages This also unifies the behavior if a repository manager of specific type cannot be found in ProvisioningHelper. --- .../CompositeArtifactRepository.java | 26 +-- .../p2/console/ProvCommandProvider.java | 13 +- .../p2/console/ProvisioningHelper.java | 177 ++++++++---------- .../p2/directorywatcher/Messages.java | 6 +- .../directorywatcher/RepositoryListener.java | 81 ++++---- .../p2/directorywatcher/messages.properties | 6 +- .../p2/engine/ProvisioningContext.java | 31 ++- .../CompositeMetadataRepository.java | 26 +-- .../equinox/p2/publisher/Publisher.java | 38 ++-- .../p2/reconciler/dropins/Activator.java | 22 ++- .../repository/tools/AbstractApplication.java | 151 +++++++-------- .../tools/CompositeRepositoryApplication.java | 97 ++++------ .../repository/tools/MirrorApplication.java | 37 +--- .../repository/helpers/RepositoryHelper.java | 46 ++++- .../p2/ui/admin/ArtifactRepositoriesView.java | 5 +- .../p2/ui/admin/MetadataRepositoriesView.java | 5 +- .../ui/admin/MetadataRepositoryTracker.java | 91 --------- ...cker.java => SingleRepositoryTracker.java} | 48 +++-- .../dialogs/AddArtifactRepositoryDialog.java | 4 +- .../dialogs/AddMetadataRepositoryDialog.java | 4 +- 20 files changed, 360 insertions(+), 554 deletions(-) delete mode 100644 bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/MetadataRepositoryTracker.java rename bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/{ArtifactRepositoryTracker.java => SingleRepositoryTracker.java} (55%) diff --git a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/CompositeArtifactRepository.java b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/CompositeArtifactRepository.java index c2ec67e7ad..084c35e6c5 100644 --- a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/CompositeArtifactRepository.java +++ b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/CompositeArtifactRepository.java @@ -16,7 +16,6 @@ import java.io.*; import java.net.URI; -import java.net.URISyntaxException; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; @@ -24,6 +23,7 @@ import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; import org.eclipse.equinox.internal.p2.persistence.CompositeRepositoryIO; import org.eclipse.equinox.internal.p2.persistence.CompositeRepositoryState; +import org.eclipse.equinox.internal.p2.repository.helpers.RepositoryHelper; import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.metadata.IArtifactKey; @@ -58,28 +58,8 @@ public class CompositeArtifactRepository extends AbstractArtifactRepository impl * @return the repository or null if unable to create one */ public static CompositeArtifactRepository createMemoryComposite(IProvisioningAgent agent) { - if (agent == null) - return null; - IArtifactRepositoryManager manager = agent.getService(IArtifactRepositoryManager.class); - if (manager == null) - return null; - try { - //create a unique URI - long time = System.currentTimeMillis(); - URI repositoryURI = new URI("memory:" + String.valueOf(time)); //$NON-NLS-1$ - while (manager.contains(repositoryURI)) - repositoryURI = new URI("memory:" + String.valueOf(++time)); //$NON-NLS-1$ - - CompositeArtifactRepository result = (CompositeArtifactRepository) manager.createRepository(repositoryURI, repositoryURI.toString(), IArtifactRepositoryManager.TYPE_COMPOSITE_REPOSITORY, null); - manager.removeRepository(repositoryURI); - return result; - } catch (ProvisionException e) { - LogHelper.log(e); - // just return null - } catch (URISyntaxException e) { - // just return null - } - return null; + return (CompositeArtifactRepository) RepositoryHelper.createMemoryComposite(agent, + IArtifactRepositoryManager.class, IArtifactRepositoryManager.TYPE_COMPOSITE_REPOSITORY); } private IArtifactRepositoryManager getManager() { diff --git a/bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvCommandProvider.java b/bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvCommandProvider.java index d7fd978ca6..8cd900cba8 100644 --- a/bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvCommandProvider.java +++ b/bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvCommandProvider.java @@ -305,11 +305,9 @@ public void _provlr(CommandInterpreter interpreter) { String id = processArgument(interpreter.nextArgument()); String version = processArgument(interpreter.nextArgument()); if (urlString == null) { - URI[] repositories = ProvisioningHelper.getMetadataRepositories(agent); - if (repositories != null) - for (URI repository : repositories) { - interpreter.println(repository); - } + for (URI repository : ProvisioningHelper.getMetadataRepositories(agent)) { + interpreter.println(repository); + } return; } URI repoLocation = toURI(interpreter, urlString); @@ -358,10 +356,7 @@ public void _provlg(CommandInterpreter interpreter) { public void _provlar(CommandInterpreter interpreter) { String urlString = processArgument(interpreter.nextArgument()); if (urlString == null) { - URI[] repositories = ProvisioningHelper.getArtifactRepositories(agent); - if (repositories == null) - return; - for (URI repository : repositories) { + for (URI repository : ProvisioningHelper.getArtifactRepositories(agent)) { interpreter.println(repository); } return; diff --git a/bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvisioningHelper.java b/bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvisioningHelper.java index 4074db2fec..47634470ef 100644 --- a/bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvisioningHelper.java +++ b/bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvisioningHelper.java @@ -16,97 +16,46 @@ package org.eclipse.equinox.internal.p2.console; import java.net.URI; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; import org.eclipse.equinox.internal.provisional.p2.director.PlanExecutionHelper; import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.engine.*; -import org.eclipse.equinox.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.p2.metadata.Version; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.planner.IPlanner; import org.eclipse.equinox.p2.planner.IProfileChangeRequest; import org.eclipse.equinox.p2.query.*; +import org.eclipse.equinox.p2.repository.IRepository; import org.eclipse.equinox.p2.repository.IRepositoryManager; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; -import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; import org.eclipse.osgi.service.environment.EnvironmentInfo; public class ProvisioningHelper { - static IMetadataRepository addMetadataRepository(IProvisioningAgent agent, URI location) { - IMetadataRepositoryManager manager = agent.getService(IMetadataRepositoryManager.class); - boolean createRepo = "file".equals(location.getScheme()); //$NON-NLS-1$ - - if (manager == null) - throw new IllegalStateException("No metadata repository manager found"); //$NON-NLS-1$ - try { - return manager.loadRepository(location, null); - } catch (ProvisionException e) { - if (!createRepo) - return null; - } - - // for convenience create and add a repository here - String repositoryName = location + " - metadata"; //$NON-NLS-1$ - try { - return manager.createRepository(location, repositoryName, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null); - } catch (ProvisionException e) { - return null; - } + static IRepository addMetadataRepository(IProvisioningAgent agent, URI location) { + return addRepository(IMetadataRepositoryManager.class, agent, location, "metadata", //$NON-NLS-1$ + IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY); } - static IMetadataRepository getMetadataRepository(IProvisioningAgent agent, URI location) { - IMetadataRepositoryManager manager = agent.getService(IMetadataRepositoryManager.class); - if (manager == null) - throw new IllegalStateException("No metadata repository manager found"); //$NON-NLS-1$ - try { - return manager.loadRepository(location, null); - } catch (ProvisionException e) { - return null; - } + static IRepository getMetadataRepository(IProvisioningAgent agent, URI location) { + return getRepository(IMetadataRepositoryManager.class, agent, location); } static void removeMetadataRepository(IProvisioningAgent agent, URI location) { - IMetadataRepositoryManager manager = agent.getService(IMetadataRepositoryManager.class); - if (manager == null) - throw new IllegalStateException("No metadata repository manager found"); //$NON-NLS-1$ - manager.removeRepository(location); + removeRepository(IMetadataRepositoryManager.class, agent, location); } - static IArtifactRepository addArtifactRepository(IProvisioningAgent agent, URI location) { - IArtifactRepositoryManager manager = agent.getService(IArtifactRepositoryManager.class); - boolean createRepo = "file".equals(location.getScheme()); //$NON-NLS-1$ - - if (manager == null) - throw new IllegalStateException("No metadata repository manager found"); //$NON-NLS-1$ - - try { - return manager.loadRepository(location, null); - } catch (ProvisionException e) { - //fall through and create a new repository - if (!createRepo) - return null; - } - // could not load a repo at that location so create one as a convenience - String repositoryName = location + " - artifacts"; //$NON-NLS-1$ - try { - return manager.createRepository(location, repositoryName, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null); - } catch (ProvisionException e) { - return null; - } + static IRepository addArtifactRepository(IProvisioningAgent agent, URI location) { + return addRepository(IArtifactRepositoryManager.class, agent, location, "artifacts", //$NON-NLS-1$ + IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY); } static void removeArtifactRepository(IProvisioningAgent agent, URI location) { - IArtifactRepositoryManager manager = agent.getService(IArtifactRepositoryManager.class); - if (manager == null) - // TODO log here - return; - manager.removeRepository(location); + removeRepository(IArtifactRepositoryManager.class, agent, location); } static IProfile addProfile(IProvisioningAgent agent, String profileId, Map properties) throws ProvisionException { @@ -172,15 +121,8 @@ static IQueryResult getInstallableUnits(IProvisioningAgent age return Collector.emptyCollector(); } - static URI[] getMetadataRepositories(IProvisioningAgent agent) { - IMetadataRepositoryManager manager = agent.getService(IMetadataRepositoryManager.class); - if (manager == null) - // TODO log here - return null; - URI[] repos = manager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL); - if (repos.length > 0) - return repos; - return null; + static List getMetadataRepositories(IProvisioningAgent agent) { + return getRepositories(IArtifactRepositoryManager.class, agent); } /** @@ -194,11 +136,8 @@ static IStatus install(IProvisioningAgent agent, String unitId, String version, StringBuilder error = new StringBuilder(); error.append("Installable unit not found: " + unitId + ' ' + version + '\n'); //$NON-NLS-1$ error.append("Repositories searched:\n");//$NON-NLS-1$ - URI[] repos = getMetadataRepositories(agent); - if (repos != null) { - for (URI repo : repos) - error.append(repo + "\n");//$NON-NLS-1$ - } + for (URI repo : getMetadataRepositories(agent)) + error.append(repo + "\n");//$NON-NLS-1$ throw new ProvisionException(error.toString()); } @@ -216,25 +155,12 @@ static IStatus install(IProvisioningAgent agent, String unitId, String version, return PlanExecutionHelper.executePlan(result, engine, context, progress); } - static URI[] getArtifactRepositories(IProvisioningAgent agent) { - IArtifactRepositoryManager manager = agent.getService(IArtifactRepositoryManager.class); - if (manager == null) - throw new IllegalStateException("No metadata repository manager found"); //$NON-NLS-1$ - URI[] repos = manager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL); - if (repos.length > 0) - return repos; - return null; + static List getArtifactRepositories(IProvisioningAgent agent) { + return getRepositories(IArtifactRepositoryManager.class, agent); } static IArtifactRepository getArtifactRepository(IProvisioningAgent agent, URI repoURL) { - IArtifactRepositoryManager manager = agent.getService(IArtifactRepositoryManager.class); - try { - if (manager != null) - return manager.loadRepository(repoURL, null); - } catch (ProvisionException e) { - //for console, just ignore repositories that can't be read - } - return null; + return (IArtifactRepository) getRepository(IArtifactRepositoryManager.class, agent, repoURL); } static long[] getProfileTimestamps(IProvisioningAgent agent, String profileId) { @@ -273,8 +199,8 @@ static IStatus revertToPreviousState(IProvisioningAgent agent, IProfile profile, } if (targetProfile == null) throw new ProvisionException("target profile with timestamp=" + revertToPreviousState + " not found"); //$NON-NLS-1$//$NON-NLS-2$ - URI[] artifactRepos = getArtifactRepositories(agent); - URI[] metadataRepos = getMetadataRepositories(agent); + URI[] artifactRepos = getArtifactRepositories(agent).toArray(URI[]::new); + URI[] metadataRepos = getMetadataRepositories(agent).toArray(URI[]::new); IProvisioningPlan plan = planner.getDiffPlan(profile, targetProfile, new NullProgressMonitor()); ProvisioningContext context = new ProvisioningContext(agent); context.setMetadataRepositories(metadataRepos); @@ -293,10 +219,8 @@ static IStatus uninstall(IProvisioningAgent agent, String unitId, String version StringBuilder error = new StringBuilder(); error.append("Installable unit not found: " + unitId + ' ' + version + '\n'); //$NON-NLS-1$ error.append("Repositories searched:\n"); //$NON-NLS-1$ - URI[] repos = getMetadataRepositories(agent); - if (repos != null) { - for (URI repo : repos) - error.append(repo + "\n"); //$NON-NLS-1$ + for (URI repo : getMetadataRepositories(agent)) { + error.append(repo + "\n"); //$NON-NLS-1$ } throw new ProvisionException(error.toString()); } @@ -315,4 +239,57 @@ static IStatus uninstall(IProvisioningAgent agent, String unitId, String version return PlanExecutionHelper.executePlan(result, engine, context, progress); } + private static IRepository addRepository(Class> repositoryManager, + IProvisioningAgent agent, URI location, String nameSuffix, String repoType) { + IRepositoryManager manager = getRepositoryManager(agent, repositoryManager); + try { + return manager.loadRepository(location, null); + } catch (ProvisionException e) { + // fall through and create a new repository + boolean createRepo = "file".equals(location.getScheme()); //$NON-NLS-1$ + if (!createRepo) { + return null; + } + } + // could not load a repo at that location so create one as a convenience + String repositoryName = location + " - " + nameSuffix; //$NON-NLS-1$ + try { + return manager.createRepository(location, repositoryName, repoType, null); + } catch (ProvisionException e) { + return null; + } + } + + private static void removeRepository(Class> repositoryManager, + IProvisioningAgent agent, URI location) { + IRepositoryManager manager = getRepositoryManager(agent, repositoryManager); + manager.removeRepository(location); + } + + private static IRepository getRepository(Class> repositoryManager, + IProvisioningAgent agent, URI location) { + IRepositoryManager manager = getRepositoryManager(agent, repositoryManager); + try { + return manager.loadRepository(location, null); + } catch (ProvisionException e) { + // for console, just ignore repositories that can't be read + return null; + } + } + + private static List getRepositories(Class> repositoryManager, + IProvisioningAgent agent) { + IRepositoryManager manager = getRepositoryManager(agent, repositoryManager); + URI[] repos = manager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL); + return Arrays.asList(repos); + } + + private static IRepositoryManager getRepositoryManager(IProvisioningAgent agent, + Class> repositoryManager) { + IRepositoryManager manager = agent.getService(repositoryManager); + if (manager == null) { + throw new IllegalStateException("No repository manager found for type " + repositoryManager); //$NON-NLS-1$ + } + return manager; + } } diff --git a/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/Messages.java b/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/Messages.java index 99539fb9d9..e5c3f2b6b6 100644 --- a/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/Messages.java +++ b/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/Messages.java @@ -17,13 +17,11 @@ public class Messages extends NLS { private static final String BUNDLE_NAME = "org.eclipse.equinox.internal.provisional.p2.directorywatcher.messages"; //$NON-NLS-1$ - public static String artifact_repo_manager_not_registered; public static String error_main_loop; public static String error_processing; - public static String failed_create_artifact_repo; - public static String failed_create_metadata_repo; + public static String failed_create_repo; public static String filename_missing; - public static String metadata_repo_manager_not_registered; + public static String repo_manager_not_registered; public static String null_folder; public static String thread_not_started; public static String thread_started; diff --git a/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/RepositoryListener.java b/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/RepositoryListener.java index c369fb8b04..7c7e0d4ce2 100644 --- a/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/RepositoryListener.java +++ b/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/RepositoryListener.java @@ -28,6 +28,8 @@ import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction; import org.eclipse.equinox.p2.publisher.eclipse.FeaturesAction; import org.eclipse.equinox.p2.query.*; +import org.eclipse.equinox.p2.repository.IRepository; +import org.eclipse.equinox.p2.repository.IRepositoryManager; import org.eclipse.equinox.p2.repository.artifact.*; import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; @@ -79,42 +81,17 @@ private void initializePublisher() { info.setArtifactOptions(IPublisherInfo.A_INDEX | IPublisherInfo.A_NO_MD5); } - protected CachingArtifactRepository initializeArtifactRepository(String name, URI repositoryLocation, Map properties) { - IArtifactRepositoryManager manager = getArtifactRepositoryManager(); - if (manager == null) - throw new IllegalStateException(Messages.artifact_repo_manager_not_registered); - - try { - IArtifactRepository result = manager.loadRepository(repositoryLocation, null); - return result == null ? null : new CachingArtifactRepository(result); - } catch (ProvisionException e) { - //fall through and create a new repository - } - try { - IArtifactRepository result = manager.createRepository(repositoryLocation, name, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties); - return result == null ? null : new CachingArtifactRepository(result); - } catch (ProvisionException e) { - LogHelper.log(e); - throw new IllegalStateException(NLS.bind(Messages.failed_create_artifact_repo, repositoryLocation)); - } + protected CachingArtifactRepository initializeArtifactRepository(String name, URI repositoryLocation, + Map properties) { + IArtifactRepository repository = (IArtifactRepository) initializeRepository(IArtifactRepositoryManager.class, + name, repositoryLocation, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties); + return repository == null ? null : new CachingArtifactRepository(repository); } - protected IMetadataRepository initializeMetadataRepository(String name, URI repositoryLocation, Map properties) { - IMetadataRepositoryManager manager = getMetadataRepositoryManager(); - if (manager == null) - throw new IllegalStateException(Messages.metadata_repo_manager_not_registered); - - try { - return manager.loadRepository(repositoryLocation, null); - } catch (ProvisionException e) { - //fall through and create new repository - } - try { - return manager.createRepository(repositoryLocation, name, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties); - } catch (ProvisionException e) { - LogHelper.log(e); - throw new IllegalStateException(NLS.bind(Messages.failed_create_metadata_repo, repositoryLocation)); - } + protected IMetadataRepository initializeMetadataRepository(String name, URI repositoryLocation, + Map properties) { + return (IMetadataRepository) initializeRepository(IMetadataRepositoryManager.class, name, repositoryLocation, + IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties); } @Override @@ -136,7 +113,8 @@ public boolean removed(File file) { private boolean process(File file, boolean isAddition) { boolean isDirectory = file.isDirectory(); // is it a feature ? - if (isDirectory && file.getParentFile() != null && file.getParentFile().getName().equals("features") && new File(file, "feature.xml").exists()) //$NON-NLS-1$ //$NON-NLS-2$) + if (isDirectory && file.getParentFile() != null && file.getParentFile().getName().equals("features") //$NON-NLS-1$ + && new File(file, "feature.xml").exists()) //$NON-NLS-1$ ) return processFeature(file, isAddition); // could it be a bundle ? if (isDirectory || file.getName().endsWith(".jar")) //$NON-NLS-1$ @@ -150,7 +128,7 @@ private boolean processBundle(File file, boolean isDirectory, boolean isAddition return false; advice.setProperties(file, file.lastModified(), file.toURI()); - return publish(new BundlesAction(new BundleDescription[] {bundleDescription}), isAddition); + return publish(new BundlesAction(new BundleDescription[] { bundleDescription }), isAddition); // TODO see bug 222370 // we only want to return the bundle IU so must exclude all fragment IUs // not sure if this is still relevant but we should investigate. @@ -159,7 +137,7 @@ private boolean processBundle(File file, boolean isDirectory, boolean isAddition private boolean processFeature(File file, boolean isAddition) { String link = metadataRepository.getProperties().get(Site.PROP_LINK_FILE); advice.setProperties(file, file.lastModified(), file.toURI(), link); - return publish(new FeaturesAction(new File[] {file}), isAddition); + return publish(new FeaturesAction(new File[] { file }), isAddition); } private boolean publish(IPublisherAction action, boolean isAddition) { @@ -238,7 +216,8 @@ private void synchronizeArtifactRepository(final Collection removedFiles) if (artifactRepository == null) return; if (!removedFiles.isEmpty()) { - IArtifactDescriptor[] descriptors = artifactRepository.descriptorQueryable().query(ArtifactDescriptorQuery.ALL_DESCRIPTORS, null).toArray(IArtifactDescriptor.class); + IArtifactDescriptor[] descriptors = artifactRepository.descriptorQueryable() + .query(ArtifactDescriptorQuery.ALL_DESCRIPTORS, null).toArray(IArtifactDescriptor.class); for (IArtifactDescriptor d : descriptors) { SimpleArtifactDescriptor descriptor = (SimpleArtifactDescriptor) d; String filename = descriptor.getRepositoryProperty(FILE_NAME); @@ -305,14 +284,28 @@ public IArtifactRepository getArtifactRepository() { return artifactRepository; } - private static IMetadataRepositoryManager getMetadataRepositoryManager() { - BundleContext bundleContext = FrameworkUtil.getBundle(RepositoryListener.class).getBundleContext(); - return ServiceHelper.getService(bundleContext, IProvisioningAgent.class).getService(IMetadataRepositoryManager.class); - } + private static IRepository initializeRepository(Class> repositoryManager, + String name, URI repositoryLocation, String type, Map properties) { - private static IArtifactRepositoryManager getArtifactRepositoryManager() { BundleContext bundleContext = FrameworkUtil.getBundle(RepositoryListener.class).getBundleContext(); - return ServiceHelper.getService(bundleContext, IProvisioningAgent.class).getService(IArtifactRepositoryManager.class); + IProvisioningAgent agent = ServiceHelper.getService(bundleContext, IProvisioningAgent.class); + IRepositoryManager manager = agent.getService(repositoryManager); + if (manager == null) { + throw new IllegalStateException( + NLS.bind(Messages.repo_manager_not_registered, repositoryManager.getSimpleName())); + } + try { + return manager.loadRepository(repositoryLocation, null); + } catch (ProvisionException e) { + // fall through and create a new repository + } + try { + return manager.createRepository(repositoryLocation, name, type, properties); + } catch (ProvisionException e) { + LogHelper.log(e); + throw new IllegalStateException( + NLS.bind(Messages.failed_create_repo, repositoryManager.getSimpleName(), repositoryLocation)); + } } private static URI getDefaultRepositoryLocation(Object object, String repositoryName) { diff --git a/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/messages.properties b/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/messages.properties index 698be83bc0..86de4fad7f 100644 --- a/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/messages.properties +++ b/bundles/org.eclipse.equinox.p2.directorywatcher/src/org/eclipse/equinox/internal/provisional/p2/directorywatcher/messages.properties @@ -11,13 +11,11 @@ # Contributors: # IBM Corporation - initial API and implementation ############################################################################### -artifact_repo_manager_not_registered=ArtifactRepositoryManager not registered. error_main_loop=Error in watcher thread main loop. error_processing=Error Processing: {0} -failed_create_artifact_repo=Could not create artifact repository for: {0} -failed_create_metadata_repo=Could not create metadata repository for: {0} +failed_create_repo=Could not create repository of type {0} for: {1} filename_missing=The {0} {1} is missing the filename property. -metadata_repo_manager_not_registered=MetadataRepositoryManager not registered. +repo_manager_not_registered=No RepositoryManager registered for type: {0} null_folder=Folder must not be null thread_not_started=Watcher thread not Started thread_started=Watcher thread already Started diff --git a/bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/p2/engine/ProvisioningContext.java b/bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/p2/engine/ProvisioningContext.java index 247cf06ca0..011749fc44 100644 --- a/bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/p2/engine/ProvisioningContext.java +++ b/bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/p2/engine/ProvisioningContext.java @@ -224,9 +224,7 @@ private Set getLoadedMetadataRepositories(IProgressMonitor for (URI repositorie : repositories) { loadMetadataRepository(repoManager, repositorie, repos, shouldFollowReferences(), sub.split(1)); } - Set set = new HashSet<>(); - set.addAll(repos.values()); - return set; + return new HashSet<>(repos.values()); } private void loadMetadataRepository(IMetadataRepositoryManager manager, URI location, @@ -346,16 +344,12 @@ private URI getSecureLocation(URI uri, Transport transport) { } } - private interface Manager { - T loadRepository(URI location, IProgressMonitor monitor) throws ProvisionException; - } - private Collection getAllLoadedMetadataRepositories(IProgressMonitor monitor) { if (allLoadedMetadataRepositories == null) { SubMonitor subMonitor = SubMonitor.convert(monitor, 2); var repoManager = agent.getService(IMetadataRepositoryManager.class); getLoadedMetadataRepositories(subMonitor.split(1)); - allLoadedMetadataRepositories = getAllLoadedRepositories(repoManager::loadRepository, + allLoadedMetadataRepositories = getAllLoadedRepositories(repoManager, loadedMetadataRepositories, failedMetadataRepositories, subMonitor.split(1)); } return allLoadedMetadataRepositories.values(); @@ -366,13 +360,14 @@ private Collection getAllLoadedArtifactRepositories(IProgre SubMonitor subMonitor = SubMonitor.convert(monitor, 2); var repoManager = agent.getService(IArtifactRepositoryManager.class); getLoadedArtifactRepositories(subMonitor.split(1)); - allLoadedArtifactRepositories = getAllLoadedRepositories(repoManager::loadRepository, + allLoadedArtifactRepositories = getAllLoadedRepositories(repoManager, loadedArtifactRepositories, failedArtifactRepositories, subMonitor.split(1)); } return allLoadedArtifactRepositories.values(); } - private Map getAllLoadedRepositories(Manager manager, Map loadedRepositories, + private > Map getAllLoadedRepositories(IRepositoryManager manager, + Map loadedRepositories, Set failedRepositories, IProgressMonitor monitor) { SubMonitor subMonitor = SubMonitor.convert(monitor, loadedRepositories.size()); var allLoadedRepositories = new HashMap<>(loadedRepositories); @@ -382,8 +377,8 @@ private Map getAllLoadedRepositories(Manager manager, Map return allLoadedRepositories; } - private void loadComposites(Manager manager, T repository, Map repos, Set failedRepositories, - IProgressMonitor monitor) { + private > void loadComposites(IRepositoryManager manager, R repository, + Map repos, Set failedRepositories, IProgressMonitor monitor) { if (repository instanceof ICompositeRepository composite) { List children = composite.getChildren(); SubMonitor subMonitor = SubMonitor.convert(monitor, children.size()); @@ -393,18 +388,18 @@ private void loadComposites(Manager manager, T repository, Map re } } - private void loadRepository(Manager manager, URI location, Map repos, Set failedRepositories, + private > void loadRepository(IRepositoryManager manager, URI location, + Map repos, Set failedRepositories, IProgressMonitor monitor) { SubMonitor subMonitor = SubMonitor.convert(monitor, 2); - if (repos.containsKey(location) || failedMetadataRepositories.contains(location)) { + if (failedMetadataRepositories.contains(location)) { return; } - - var repository = repos.get(location); - if (repository == null) { + if (!repos.containsKey(location)) { try { - repository = manager.loadRepository(location, subMonitor.split(1)); + @SuppressWarnings("unchecked") + R repository = (R) manager.loadRepository(location, subMonitor.split(1)); repos.put(location, repository); loadComposites(manager, repository, repos, failedRepositories, subMonitor.split(1)); } catch (ProvisionException e) { diff --git a/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/CompositeMetadataRepository.java b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/CompositeMetadataRepository.java index d9a2514f29..4c584af1f2 100644 --- a/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/CompositeMetadataRepository.java +++ b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/CompositeMetadataRepository.java @@ -16,7 +16,6 @@ import java.io.*; import java.net.URI; -import java.net.URISyntaxException; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; @@ -24,6 +23,7 @@ import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; import org.eclipse.equinox.internal.p2.persistence.CompositeRepositoryIO; import org.eclipse.equinox.internal.p2.persistence.CompositeRepositoryState; +import org.eclipse.equinox.internal.p2.repository.helpers.RepositoryHelper; import org.eclipse.equinox.p2.core.*; import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.metadata.index.IIndex; @@ -63,28 +63,8 @@ public class CompositeMetadataRepository extends AbstractMetadataRepository impl * @return the repository or null if unable to create one */ public static CompositeMetadataRepository createMemoryComposite(IProvisioningAgent agent) { - if (agent == null) - return null; - IMetadataRepositoryManager repoManager = agent.getService(IMetadataRepositoryManager.class); - if (repoManager == null) - return null; - try { - //create a unique opaque URI - long time = System.currentTimeMillis(); - URI repositoryURI = new URI("memory:" + String.valueOf(time)); //$NON-NLS-1$ - while (repoManager.contains(repositoryURI)) - repositoryURI = new URI("memory:" + String.valueOf(++time)); //$NON-NLS-1$ - - CompositeMetadataRepository result = (CompositeMetadataRepository) repoManager.createRepository(repositoryURI, repositoryURI.toString(), IMetadataRepositoryManager.TYPE_COMPOSITE_REPOSITORY, null); - repoManager.removeRepository(repositoryURI); - return result; - } catch (ProvisionException e) { - // just return null - LogHelper.log(e); - } catch (URISyntaxException e) { - // just return null - } - return null; + return (CompositeMetadataRepository) RepositoryHelper.createMemoryComposite(agent, + IMetadataRepositoryManager.class, IMetadataRepositoryManager.TYPE_COMPOSITE_REPOSITORY); } private IMetadataRepositoryManager getManager() { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/Publisher.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/Publisher.java index 8f79df8bb9..f0835d11bb 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/Publisher.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/Publisher.java @@ -17,21 +17,13 @@ import java.net.URI; import java.util.Collection; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.MultiStatus; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.core.helpers.Tracing; import org.eclipse.equinox.internal.p2.publisher.Activator; import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.p2.repository.IRepository; -import org.eclipse.equinox.p2.repository.IRepositoryManager; -import org.eclipse.equinox.p2.repository.IRunnableWithProgress; +import org.eclipse.equinox.p2.repository.*; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; @@ -103,17 +95,9 @@ public static IMetadataRepository createMetadataRepository(IProvisioningAgent ag */ public static IMetadataRepository loadMetadataRepository(IProvisioningAgent agent, URI location, boolean modifiable, boolean removeFromManager) throws ProvisionException { - IMetadataRepositoryManager manager = getService(agent, IMetadataRepositoryManager.SERVICE_NAME); - boolean existing = manager.contains(location); - - IMetadataRepository result = manager.loadRepository(location, - modifiable ? IRepositoryManager.REPOSITORY_HINT_MODIFIABLE : 0, null); - if (!existing && removeFromManager) - manager.removeRepository(location); - return result; + return loadRepository(agent, IMetadataRepositoryManager.SERVICE_NAME, location, modifiable, removeFromManager); } - /** * Returns an artifact repository that corresponds to the given settings. If a * repository at the given location already exists, it is updated with the @@ -169,13 +153,19 @@ public static IArtifactRepository createArtifactRepository(IProvisioningAgent ag */ public static IArtifactRepository loadArtifactRepository(IProvisioningAgent agent, URI location, boolean modifiable, boolean removeFromManager) throws ProvisionException { - IArtifactRepositoryManager manager = getService(agent, IArtifactRepositoryManager.SERVICE_NAME); - boolean existing = manager.contains(location); + return loadRepository(agent, IArtifactRepositoryManager.SERVICE_NAME, location, modifiable, removeFromManager); + } - IArtifactRepository result = manager.loadRepository(location, - modifiable ? IRepositoryManager.REPOSITORY_HINT_MODIFIABLE : 0, null); - if (!existing && removeFromManager) + private static > R loadRepository(IProvisioningAgent agent, String serviceName, + URI location, boolean modifiable, boolean removeFromManager) throws ProvisionException { + IRepositoryManager manager = getService(agent, serviceName); + boolean existing = manager.contains(location); + int flags = modifiable ? IRepositoryManager.REPOSITORY_HINT_MODIFIABLE : 0; + @SuppressWarnings("unchecked") + R result = (R) manager.loadRepository(location, flags, null); + if (!existing && removeFromManager) { manager.removeRepository(location); + } return result; } diff --git a/bundles/org.eclipse.equinox.p2.reconciler.dropins/src/org/eclipse/equinox/internal/p2/reconciler/dropins/Activator.java b/bundles/org.eclipse.equinox.p2.reconciler.dropins/src/org/eclipse/equinox/internal/p2/reconciler/dropins/Activator.java index ed2e411ae2..30fed3557a 100644 --- a/bundles/org.eclipse.equinox.p2.reconciler.dropins/src/org/eclipse/equinox/internal/p2/reconciler/dropins/Activator.java +++ b/bundles/org.eclipse.equinox.p2.reconciler.dropins/src/org/eclipse/equinox/internal/p2/reconciler/dropins/Activator.java @@ -32,6 +32,7 @@ import org.eclipse.equinox.p2.engine.IProfile; import org.eclipse.equinox.p2.engine.IProfileRegistry; import org.eclipse.equinox.p2.repository.IRepository; +import org.eclipse.equinox.p2.repository.IRepositoryManager; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; @@ -98,12 +99,7 @@ private static IProvisioningAgent getAgent() { * @throws ProvisionException */ public static IMetadataRepository loadMetadataRepository(URI location, IProgressMonitor monitor) throws ProvisionException { - IMetadataRepositoryManager manager = getAgent().getService(IMetadataRepositoryManager.class); - if (manager == null) - throw new IllegalStateException("MetadataRepositoryManager not registered."); //$NON-NLS-1$ - IMetadataRepository repository = manager.loadRepository(location, monitor); - manager.setRepositoryProperty(location, IRepository.PROP_SYSTEM, String.valueOf(true)); - return repository; + return (IMetadataRepository) loadRepository(IMetadataRepositoryManager.class, location, monitor); } /** @@ -141,10 +137,16 @@ public static IArtifactRepository createExtensionLocationArtifactRepository(URI * @throws ProvisionException */ public static IArtifactRepository loadArtifactRepository(URI location, IProgressMonitor monitor) throws ProvisionException { - IArtifactRepositoryManager manager = getAgent().getService(IArtifactRepositoryManager.class); - if (manager == null) - throw new IllegalStateException("ArtifactRepositoryManager not registered."); //$NON-NLS-1$ - IArtifactRepository repository = manager.loadRepository(location, monitor); + return (IArtifactRepository) loadRepository(IArtifactRepositoryManager.class, location, monitor); + } + + private static IRepository loadRepository(Class> repositoryManager, + URI location, IProgressMonitor monitor) throws ProvisionException { + IRepositoryManager manager = getAgent().getService(repositoryManager); + if (manager == null) { + throw new IllegalStateException(repositoryManager.getSimpleName() + " not registered."); //$NON-NLS-1$ + } + IRepository repository = manager.loadRepository(location, monitor); manager.setRepositoryProperty(location, IRepository.PROP_SYSTEM, String.valueOf(true)); return repository; } diff --git a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/AbstractApplication.java b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/AbstractApplication.java index fe82a23e67..ea2ffdc338 100644 --- a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/AbstractApplication.java +++ b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/AbstractApplication.java @@ -16,6 +16,7 @@ import java.net.MalformedURLException; import java.net.URI; import java.util.*; +import java.util.function.Predicate; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository; @@ -97,11 +98,13 @@ public void setSourceIUs(List ius) { protected void finalizeRepositories() { if (removeAddedRepositories) { IArtifactRepositoryManager artifactRepositoryManager = getArtifactRepositoryManager(); - for (URI uri : artifactReposToRemove) + for (URI uri : artifactReposToRemove) { artifactRepositoryManager.removeRepository(uri); + } IMetadataRepositoryManager metadataRepositoryManager = getMetadataRepositoryManager(); - for (URI uri : metadataReposToRemove) + for (URI uri : metadataReposToRemove) { metadataRepositoryManager.removeRepository(uri); + } } metadataReposToRemove = null; artifactReposToRemove = null; @@ -127,14 +130,15 @@ public void initializeRepos(IProgressMonitor progress) throws ProvisionException try { curLocation = repo.getRepoLocation(); if (repo.isBoth()) { - addRepository(artifactRepositoryManager, curLocation, 0, progress); - addRepository(metadataRepositoryManager, curLocation, 0, progress); - } else if (repo.isArtifact()) - addRepository(artifactRepositoryManager, curLocation, 0, progress); - else if (repo.isMetadata()) - addRepository(metadataRepositoryManager, curLocation, 0, progress); - else + addRepository(artifactRepositoryManager, curLocation, 0, artifactReposToRemove, progress); + addRepository(metadataRepositoryManager, curLocation, 0, metadataReposToRemove, progress); + } else if (repo.isArtifact()) { + addRepository(artifactRepositoryManager, curLocation, 0, artifactReposToRemove, progress); + } else if (repo.isMetadata()) { + addRepository(metadataRepositoryManager, curLocation, 0, metadataReposToRemove, progress); + } else { throw new ProvisionException(NLS.bind(Messages.unknown_repository_type, repo.getRepoLocation())); + } } catch (ProvisionException e) { if (e.getCause() instanceof MalformedURLException) { throw new ProvisionException(NLS.bind(Messages.exception_invalidSource, curLocation), e); @@ -149,43 +153,33 @@ else if (repo.isMetadata()) // Helper to add a repository. It takes care of adding the repos to the deletion // list and loading it - protected IMetadataRepository addRepository(IMetadataRepositoryManager manager, URI location, int flags, - IProgressMonitor monitor) throws ProvisionException { - if (!manager.contains(location)) - metadataReposToRemove.add(location); - return manager.loadRepository(location, flags, monitor); - } - - // Helper to add a repository. It takes care of adding the repos to the deletion - // list and loading it - protected IArtifactRepository addRepository(IArtifactRepositoryManager manager, URI location, int flags, - IProgressMonitor monitor) throws ProvisionException { - if (!manager.contains(location)) - artifactReposToRemove.add(location); + protected IRepository addRepository(IRepositoryManager manager, URI location, int flags, + List repoList, IProgressMonitor monitor) throws ProvisionException { + if (!manager.contains(location)) { + repoList.add(location); + } return manager.loadRepository(location, flags, monitor); } private void processDestinationRepos(IArtifactRepositoryManager artifactRepositoryManager, IMetadataRepositoryManager metadataRepositoryManager) throws ProvisionException { - RepositoryDescriptor artifactRepoDescriptor = null; - RepositoryDescriptor metadataRepoDescriptor = null; - Iterator iter = destinationRepos.iterator(); - while (iter.hasNext() && (artifactRepoDescriptor == null || metadataRepoDescriptor == null)) { - RepositoryDescriptor repo = iter.next(); - if (repo.isArtifact() && artifactRepoDescriptor == null) - artifactRepoDescriptor = repo; - if (repo.isMetadata() && metadataRepoDescriptor == null) - metadataRepoDescriptor = repo; + destinationArtifactRepository = (IArtifactRepository) initializeDestinationRepo(artifactRepositoryManager, + RepositoryDescriptor::isArtifact); + destinationMetadataRepository = (IMetadataRepository) initializeDestinationRepo(metadataRepositoryManager, + RepositoryDescriptor::isMetadata); + if (destinationMetadataRepository == null && destinationArtifactRepository == null) { + throw new ProvisionException(Messages.AbstractApplication_no_valid_destinations); } + } - if (artifactRepoDescriptor != null) - destinationArtifactRepository = initializeDestination(artifactRepoDescriptor, artifactRepositoryManager); - if (metadataRepoDescriptor != null) - destinationMetadataRepository = initializeDestination(metadataRepoDescriptor, metadataRepositoryManager); - - if (destinationMetadataRepository == null && destinationArtifactRepository == null) - throw new ProvisionException(Messages.AbstractApplication_no_valid_destinations); + private IRepository initializeDestinationRepo(IRepositoryManager repositoryManager, + Predicate typeFilter) throws ProvisionException { + Optional descriptor = destinationRepos.stream().filter(typeFilter).findFirst(); + if (descriptor.isPresent()) { + return initializeDestination(descriptor.get(), repositoryManager); + } + return null; } public IMetadataRepository getDestinationMetadataRepository() { @@ -196,55 +190,34 @@ public IArtifactRepository getDestinationArtifactRepository() { return destinationArtifactRepository; } - protected IMetadataRepository initializeDestination(RepositoryDescriptor toInit, IMetadataRepositoryManager mgr) + protected IRepository initializeDestination(RepositoryDescriptor toInit, IRepositoryManager mgr) throws ProvisionException { - try { - IMetadataRepository repository = addRepository(mgr, toInit.getRepoLocation(), - IRepositoryManager.REPOSITORY_HINT_MODIFIABLE, null); - if (initDestinationRepository(repository, toInit)) - return repository; - } catch (ProvisionException e) { - // fall through and create a new repository below - } - IMetadataRepository source = null; - try { - if (toInit.getFormat() != null) - source = mgr.loadRepository(toInit.getFormat(), 0, null); - } catch (ProvisionException e) { - // Ignore. + String repositoryType; + List repoList; + if (mgr instanceof IMetadataRepositoryManager) { + repositoryType = IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY; + repoList = metadataReposToRemove; + } else if (mgr instanceof IArtifactRepositoryManager) { + repositoryType = IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY; + repoList = artifactReposToRemove; + } else { + throw new AssertionError("Unsupported repository type: " + mgr.getClass()); //$NON-NLS-1$ } - // This code assumes source has been successfully loaded before this point - // No existing repository; create a new repository at destinationLocation but - // with source's attributes. try { - IMetadataRepository result = mgr.createRepository(toInit.getRepoLocation(), - toInit.getName() != null ? toInit.getName() - : (source != null ? source.getName() : toInit.getRepoLocation().toString()), - IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, source != null ? source.getProperties() : null); - if (toInit.isCompressed() && !result.getProperties().containsKey(IRepository.PROP_COMPRESSED)) - result.setProperty(IRepository.PROP_COMPRESSED, "true"); //$NON-NLS-1$ - return (IMetadataRepository) RepositoryHelper.validDestinationRepository(result); - } catch (UnsupportedOperationException e) { - throw new ProvisionException(NLS.bind(Messages.exception_invalidDestination, toInit.getRepoLocation()), - e.getCause()); - } - } - - protected IArtifactRepository initializeDestination(RepositoryDescriptor toInit, IArtifactRepositoryManager mgr) - throws ProvisionException { - try { - IArtifactRepository repository = addRepository(mgr, toInit.getRepoLocation(), - IRepositoryManager.REPOSITORY_HINT_MODIFIABLE, null); - if (initDestinationRepository(repository, toInit)) + IRepository repository = addRepository(mgr, toInit.getRepoLocation(), + IRepositoryManager.REPOSITORY_HINT_MODIFIABLE, repoList, null); + if (initDestinationRepository(repository, toInit)) { return repository; + } } catch (ProvisionException e) { // fall through and create a new repository below } - IArtifactRepository source = null; + IRepository source = null; try { - if (toInit.getFormat() != null) + if (toInit.getFormat() != null) { source = mgr.loadRepository(toInit.getFormat(), 0, null); + } } catch (ProvisionException e) { // Ignore. } @@ -253,13 +226,13 @@ protected IArtifactRepository initializeDestination(RepositoryDescriptor toInit, // with source's attributes. // TODO for now create a Simple repo by default. try { - IArtifactRepository result = mgr.createRepository(toInit.getRepoLocation(), + IRepository result = mgr.createRepository(toInit.getRepoLocation(), toInit.getName() != null ? toInit.getName() : (source != null ? source.getName() : toInit.getRepoLocation().toString()), - IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, source != null ? source.getProperties() : null); + repositoryType, source != null ? source.getProperties() : null); if (toInit.isCompressed() && !result.getProperties().containsKey(IRepository.PROP_COMPRESSED)) result.setProperty(IRepository.PROP_COMPRESSED, "true"); //$NON-NLS-1$ - return (IArtifactRepository) RepositoryHelper.validDestinationRepository(result); + return RepositoryHelper.validDestinationRepository(result); } catch (UnsupportedOperationException e) { throw new ProvisionException(NLS.bind(Messages.exception_invalidDestination, toInit.getRepoLocation()), e.getCause()); @@ -268,14 +241,16 @@ protected IArtifactRepository initializeDestination(RepositoryDescriptor toInit, protected boolean initDestinationRepository(IRepository repository, RepositoryDescriptor descriptor) { if (repository != null && repository.isModifiable()) { - if (descriptor.getName() != null) + if (descriptor.getName() != null) { repository.setProperty(IRepository.PROP_NAME, descriptor.getName()); - if (repository instanceof ICompositeRepository && !descriptor.isAppend()) + } + if (repository instanceof ICompositeRepository && !descriptor.isAppend()) { ((ICompositeRepository) repository).removeAllChildren(); - else if (repository instanceof IMetadataRepository && !descriptor.isAppend()) - ((IMetadataRepository) repository).removeAll(); - else if (repository instanceof IArtifactRepository && !descriptor.isAppend()) - ((IArtifactRepository) repository).removeAll(); + } else if (repository instanceof IMetadataRepository metadataRepository && !descriptor.isAppend()) { + metadataRepository.removeAll(); + } else if (repository instanceof IArtifactRepository artifactRepository && !descriptor.isAppend()) { + artifactRepository.removeAll(); + } return true; } return false; @@ -308,11 +283,11 @@ public IArtifactRepository getCompositeArtifactRepository() { } public boolean hasArtifactSources() { - return ((ICompositeRepository) getCompositeArtifactRepository()).getChildren().size() > 0; + return !((ICompositeRepository) getCompositeArtifactRepository()).getChildren().isEmpty(); } public boolean hasMetadataSources() { - return ((ICompositeRepository) getCompositeMetadataRepository()).getChildren().size() > 0; + return !((ICompositeRepository) getCompositeMetadataRepository()).getChildren().isEmpty(); } public abstract IStatus run(IProgressMonitor monitor) throws ProvisionException; diff --git a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/CompositeRepositoryApplication.java b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/CompositeRepositoryApplication.java index 3c71954668..0fc5eeae47 100644 --- a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/CompositeRepositoryApplication.java +++ b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/CompositeRepositoryApplication.java @@ -14,8 +14,7 @@ package org.eclipse.equinox.p2.internal.repository.tools; import java.net.MalformedURLException; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository; import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository; @@ -23,11 +22,8 @@ import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.p2.repository.ICompositeRepository; -import org.eclipse.equinox.p2.repository.IRepository; -import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.*; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; -import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; import org.eclipse.osgi.util.NLS; @@ -38,7 +34,7 @@ public class CompositeRepositoryApplication extends AbstractApplication { private boolean removeAllChildren = false; private boolean failOnExists = false; private String comparatorID = null; - + public CompositeRepositoryApplication() { super(); } @@ -106,75 +102,57 @@ public void setFailOnExists(boolean value) { } @Override - protected IArtifactRepository initializeDestination(RepositoryDescriptor toInit, IArtifactRepositoryManager mgr) throws ProvisionException { - // remove the repo first. - mgr.removeRepository(toInit.getRepoLocation()); - - // first try and load to see if one already exists at that location. - try { - IArtifactRepository repository = mgr.loadRepository(toInit.getRepoLocation(), null); - if (validRepositoryLocation(repository) && initDestinationRepository(repository, toInit)) - return repository; - throw new ProvisionException(new Status(IStatus.INFO, Activator.ID, NLS.bind(Messages.CompositeRepository_composite_repository_exists, toInit.getRepoLocation()))); - } catch (ProvisionException e) { - // re-throw the exception if we got anything other than "repo not found" - if (e.getStatus().getCode() != ProvisionException.REPOSITORY_NOT_FOUND) { - if (e.getCause() instanceof MalformedURLException) - throw new ProvisionException(NLS.bind(Messages.exception_invalidDestination, toInit.getRepoLocation()), e.getCause()); - throw e; - } - } - - IArtifactRepository source = null; - try { - if (toInit.getFormat() != null) - source = mgr.loadRepository(toInit.getFormat(), 0, null); - } catch (ProvisionException e) { - //Ignore. + protected IRepository initializeDestination(RepositoryDescriptor toInit, IRepositoryManager mgr) + throws ProvisionException { + + String repositoryType; + String defaultName; + if (mgr instanceof IArtifactRepositoryManager) { + repositoryType = IArtifactRepositoryManager.TYPE_COMPOSITE_REPOSITORY; + defaultName = Messages.CompositeRepository_default_artifactRepo_name; + } else if (mgr instanceof IMetadataRepositoryManager) { + repositoryType = IMetadataRepositoryManager.TYPE_COMPOSITE_REPOSITORY; + defaultName = Messages.CompositeRepository_default_metadataRepo_name; + } else { + throw new AssertionError("Unsupported repository type: " + mgr.getClass()); //$NON-NLS-1$ } - //This code assumes source has been successfully loaded before this point - try { - //No existing repository; create a new repository at destinationLocation but with source's attributes. - IArtifactRepository repo = mgr.createRepository(toInit.getRepoLocation(), toInit.getName() != null ? toInit.getName() : (source != null ? source.getName() : Messages.CompositeRepository_default_artifactRepo_name), IArtifactRepositoryManager.TYPE_COMPOSITE_REPOSITORY, source != null ? source.getProperties() : null); - initRepository(repo, toInit); - return repo; - } catch (IllegalStateException e) { - mgr.removeRepository(toInit.getRepoLocation()); - throw e; - } - } - - @Override - protected IMetadataRepository initializeDestination(RepositoryDescriptor toInit, IMetadataRepositoryManager mgr) throws ProvisionException { // remove the repo first. mgr.removeRepository(toInit.getRepoLocation()); // first try and load to see if one already exists at that location. try { - IMetadataRepository repository = mgr.loadRepository(toInit.getRepoLocation(), null); - if (!validRepositoryLocation(repository) && initDestinationRepository(repository, toInit)) - throw new ProvisionException(new Status(IStatus.INFO, Activator.ID, NLS.bind(Messages.CompositeRepository_composite_repository_exists, toInit.getRepoLocation()))); + IRepository repository = mgr.loadRepository(toInit.getRepoLocation(), null); + validRepositoryLocation(repository); + if (!initDestinationRepository(repository, toInit)) { + throw new ProvisionException(Status.info( + NLS.bind(Messages.CompositeRepository_composite_repository_exists, toInit.getRepoLocation()))); + } return repository; } catch (ProvisionException e) { // re-throw the exception if we got anything other than "repo not found" if (e.getStatus().getCode() != ProvisionException.REPOSITORY_NOT_FOUND) { if (e.getCause() instanceof MalformedURLException) - throw new ProvisionException(NLS.bind(Messages.exception_invalidDestination, toInit.getRepoLocation()), e.getCause()); + throw new ProvisionException( + NLS.bind(Messages.exception_invalidDestination, toInit.getRepoLocation()), e.getCause()); throw e; } } - IMetadataRepository source = null; + IRepository source = null; try { - if (toInit.getFormat() != null) + if (toInit.getFormat() != null) { source = mgr.loadRepository(toInit.getFormat(), 0, null); + } } catch (ProvisionException e) { - //Ignore + // Ignore } - //This code assumes source has been successfully loaded before this point + // This code assumes source has been successfully loaded before this point try { - //No existing repository; create a new repository at destinationLocation but with source's attributes. - IMetadataRepository repo = mgr.createRepository(toInit.getRepoLocation(), toInit.getName() != null ? toInit.getName() : (source != null ? source.getName() : Messages.CompositeRepository_default_metadataRepo_name), IMetadataRepositoryManager.TYPE_COMPOSITE_REPOSITORY, source != null ? source.getProperties() : null); + // No existing repository; create a new repository at destinationLocation but + // with source's attributes. + String name = Optional.ofNullable(toInit.getName()).orElse(source != null ? source.getName() : defaultName); + IRepository repo = mgr.createRepository(toInit.getRepoLocation(), name, repositoryType, + source != null ? source.getProperties() : null); initRepository(repo, toInit); return repo; } catch (IllegalStateException e) { @@ -186,17 +164,16 @@ protected IMetadataRepository initializeDestination(RepositoryDescriptor toInit, /* * Determine if the repository is valid for this operation */ - private boolean validRepositoryLocation(IRepository repository) throws ProvisionException { + private void validRepositoryLocation(IRepository repository) throws ProvisionException { if (repository instanceof ICompositeRepository) { // if we have an already existing repository at that location, then throw an error // if the user told us to - if (failOnExists) + if (failOnExists) { throw new ProvisionException(NLS.bind(Messages.CompositeRepository_composite_repository_exists, repository.getLocation())); + } RepositoryHelper.validDestinationRepository(repository); - return true; } // we have a non-composite repo at this location. that is ok because we can co-exist. - return true; } /* diff --git a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/MirrorApplication.java b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/MirrorApplication.java index 738ebe1a9c..72ccc01c06 100644 --- a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/MirrorApplication.java +++ b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/MirrorApplication.java @@ -16,19 +16,8 @@ import java.io.File; import java.net.URI; import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.StringTokenizer; - -import org.eclipse.core.runtime.IConfigurationElement; -import org.eclipse.core.runtime.IExecutableExtension; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.URIUtil; +import java.util.*; +import org.eclipse.core.runtime.*; import org.eclipse.equinox.app.IApplication; import org.eclipse.equinox.app.IApplicationContext; import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; @@ -38,24 +27,13 @@ import org.eclipse.equinox.internal.p2.repository.helpers.RepositoryHelper; import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.core.ProvisionException; -import org.eclipse.equinox.p2.engine.IProfile; -import org.eclipse.equinox.p2.engine.IProfileRegistry; -import org.eclipse.equinox.p2.engine.IProvisioningPlan; +import org.eclipse.equinox.p2.engine.*; import org.eclipse.equinox.p2.internal.repository.comparator.ArtifactChecksumComparator; -import org.eclipse.equinox.p2.internal.repository.mirroring.FileMirrorLog; -import org.eclipse.equinox.p2.internal.repository.mirroring.IArtifactMirrorLog; -import org.eclipse.equinox.p2.internal.repository.mirroring.Mirroring; -import org.eclipse.equinox.p2.internal.repository.mirroring.XMLMirrorLog; -import org.eclipse.equinox.p2.metadata.IArtifactKey; -import org.eclipse.equinox.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.p2.metadata.VersionRange; +import org.eclipse.equinox.p2.internal.repository.mirroring.*; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.planner.IPlanner; import org.eclipse.equinox.p2.planner.IProfileChangeRequest; -import org.eclipse.equinox.p2.query.CompoundQueryable; -import org.eclipse.equinox.p2.query.IQuery; -import org.eclipse.equinox.p2.query.IQueryResult; -import org.eclipse.equinox.p2.query.IQueryable; -import org.eclipse.equinox.p2.query.QueryUtil; +import org.eclipse.equinox.p2.query.*; import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; @@ -292,7 +270,8 @@ private IArtifactRepository initializeBaseline() { if (baseline == null) return null; try { - return addRepository(getArtifactRepositoryManager(), baseline, 0, null); + return (IArtifactRepository) addRepository(getArtifactRepositoryManager(), baseline, 0, + artifactReposToRemove, null); } catch (ProvisionException e) { if (mirrorLog != null && e.getStatus() != null) mirrorLog.log(e.getStatus()); diff --git a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/RepositoryHelper.java b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/RepositoryHelper.java index 9b71118eb8..92945f953c 100644 --- a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/RepositoryHelper.java +++ b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/RepositoryHelper.java @@ -17,18 +17,23 @@ import java.io.File; import java.net.*; import java.util.Objects; +import java.util.stream.LongStream; import org.eclipse.core.runtime.*; +import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; import org.eclipse.equinox.internal.p2.repository.Activator; +import org.eclipse.equinox.p2.core.IProvisioningAgent; +import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.repository.IRepository; +import org.eclipse.equinox.p2.repository.IRepositoryManager; import org.eclipse.osgi.util.NLS; public class RepositoryHelper { protected static final String FILE_SCHEME = "file"; //$NON-NLS-1$ /** - * If the provided URI can be interpreted as representing a local address (no schema, or one letter schema) - * but is missing the file schema, a new URI is created which represents the local file. - * + * If the provided URI can be interpreted as representing a local address (no schema, or one letter schema) + * but is missing the file schema, a new URI is created which represents the local file. + * * @param location the URI to convert * @return the converted URI, or the original */ @@ -98,4 +103,39 @@ public static IStatus checkRepositoryLocationSyntax(URI location) { } return Status.OK_STATUS; } + + /** + * Create a Composite repository in memory. + * + * @return the repository or null if unable to create one + */ + public static IRepository createMemoryComposite(IProvisioningAgent agent, + Class> repoManagerClass, String repositoryType) { + if (agent == null) { + return null; + } + IRepositoryManager manager = agent.getService(repoManagerClass); + if (manager == null) { + return null; + } + try { + // create a unique URI + URI repositoryURI = LongStream.iterate(System.currentTimeMillis(), t -> t + 1) + .mapToObj(t -> URI.create("memory:" + t)) //$NON-NLS-1$ + .dropWhile(manager::contains).findFirst().orElseThrow(); + IRepository result = manager.createRepository(repositoryURI, repositoryURI.toString(), repositoryType, + null); + manager.removeRepository(repositoryURI); + return result; + } catch (ProvisionException e) { + LogHelper.log(e); + // just return null + } catch (IllegalArgumentException e) { + if (!(e.getCause() instanceof URISyntaxException)) { + throw e; // not thrown by the URI creation above + } // else just return null + } + return null; + } + } \ No newline at end of file diff --git a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/ArtifactRepositoriesView.java b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/ArtifactRepositoriesView.java index fc452cfa89..91312b0f84 100644 --- a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/ArtifactRepositoriesView.java +++ b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/ArtifactRepositoriesView.java @@ -68,8 +68,9 @@ protected int getListenerEventTypes() { @Override protected RepositoryTracker getRepositoryTracker() { - if (tracker == null) - tracker = new ArtifactRepositoryTracker(getProvisioningUI()); + if (tracker == null) { + tracker = SingleRepositoryTracker.createArtifactRepositoryTracker(getProvisioningUI()); + } return tracker; } diff --git a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/MetadataRepositoriesView.java b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/MetadataRepositoriesView.java index cf01e65ecf..83b7a1f17b 100644 --- a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/MetadataRepositoriesView.java +++ b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/MetadataRepositoriesView.java @@ -136,8 +136,9 @@ protected void updateCachesForPreferences() { @Override protected RepositoryTracker getRepositoryTracker() { - if (tracker == null) - tracker = new MetadataRepositoryTracker(getProvisioningUI()); + if (tracker == null) { + tracker = SingleRepositoryTracker.createMetadataRepositoryTracker(getProvisioningUI()); + } return tracker; } } diff --git a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/MetadataRepositoryTracker.java b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/MetadataRepositoryTracker.java deleted file mode 100644 index 5ea9a6378a..0000000000 --- a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/MetadataRepositoryTracker.java +++ /dev/null @@ -1,91 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2017 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 - ******************************************************************************/ - -package org.eclipse.equinox.internal.p2.ui.admin; - -import java.net.URI; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.equinox.internal.p2.ui.ProvUI; -import org.eclipse.equinox.internal.provisional.p2.repository.RepositoryEvent; -import org.eclipse.equinox.p2.core.ProvisionException; -import org.eclipse.equinox.p2.operations.ProvisioningSession; -import org.eclipse.equinox.p2.operations.RepositoryTracker; -import org.eclipse.equinox.p2.repository.IRepository; -import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; -import org.eclipse.equinox.p2.ui.ProvisioningUI; - -public class MetadataRepositoryTracker extends RepositoryTracker { - - ProvisioningUI ui; - - public MetadataRepositoryTracker(ProvisioningUI ui) { - this.ui = ui; - } - - @Override - public URI[] getKnownRepositories(ProvisioningSession session) { - return getMetadataRepositoryManager().getKnownRepositories(getArtifactRepositoryFlags()); - } - - @Override - public void addRepository(URI repoLocation, String nickname, ProvisioningSession session) { - ui.signalRepositoryOperationStart(); - try { - getMetadataRepositoryManager().addRepository(repoLocation); - if (nickname != null) - getMetadataRepositoryManager().setRepositoryProperty(repoLocation, IRepository.PROP_NICKNAME, nickname); - - } finally { - ui.signalRepositoryOperationComplete(new RepositoryEvent(repoLocation, IRepository.TYPE_METADATA, RepositoryEvent.ADDED, true), true); - } - } - - @Override - public void removeRepositories(URI[] repoLocations, ProvisioningSession session) { - ui.signalRepositoryOperationStart(); - try { - for (URI repoLocation : repoLocations) { - getMetadataRepositoryManager().removeRepository(repoLocation); - } - } finally { - ui.signalRepositoryOperationComplete(null, true); - } - } - - @Override - public void refreshRepositories(URI[] locations, ProvisioningSession session, IProgressMonitor monitor) { - ui.signalRepositoryOperationStart(); - SubMonitor mon = SubMonitor.convert(monitor, locations.length * 100); - for (URI location : locations) { - try { - getMetadataRepositoryManager().refreshRepository(location, mon.newChild(100)); - }catch (ProvisionException e) { - //ignore problematic repositories when refreshing - } - } - // We have no idea how many repos may have been added/removed as a result of - // refreshing these, this one, so we do not use a specific repository event to represent it. - ui.signalRepositoryOperationComplete(null, true); - } - - IMetadataRepositoryManager getMetadataRepositoryManager() { - return ProvUI.getMetadataRepositoryManager(ui.getSession()); - } - - @Override - protected boolean contains(URI location, ProvisioningSession session) { - return ProvUI.getMetadataRepositoryManager(session).contains(location); - } -} \ No newline at end of file diff --git a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/ArtifactRepositoryTracker.java b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/SingleRepositoryTracker.java similarity index 55% rename from bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/ArtifactRepositoryTracker.java rename to bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/SingleRepositoryTracker.java index f12434eccf..badf9b9f5f 100644 --- a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/ArtifactRepositoryTracker.java +++ b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/SingleRepositoryTracker.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2010 IBM Corporation and others. + * Copyright (c) 2009, 2023 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Hannes Wellmann - Unify ArtifactRepositoryTracker and MetadataRepositoryTracker to one class ******************************************************************************/ package org.eclipse.equinox.internal.p2.ui.admin; @@ -17,38 +18,53 @@ import java.net.URI; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.equinox.internal.p2.ui.ProvUI; import org.eclipse.equinox.internal.provisional.p2.repository.RepositoryEvent; import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.operations.ProvisioningSession; import org.eclipse.equinox.p2.operations.RepositoryTracker; import org.eclipse.equinox.p2.repository.IRepository; +import org.eclipse.equinox.p2.repository.IRepositoryManager; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; import org.eclipse.equinox.p2.ui.ProvisioningUI; -public class ArtifactRepositoryTracker extends RepositoryTracker { +public class SingleRepositoryTracker extends RepositoryTracker { - ProvisioningUI ui; + public static RepositoryTracker createMetadataRepositoryTracker(ProvisioningUI ui) { + return new SingleRepositoryTracker(ui, IRepository.TYPE_METADATA, IMetadataRepositoryManager.class); + } + + public static RepositoryTracker createArtifactRepositoryTracker(ProvisioningUI ui) { + return new SingleRepositoryTracker(ui, IRepository.TYPE_ARTIFACT, IArtifactRepositoryManager.class); + } - public ArtifactRepositoryTracker(ProvisioningUI ui) { + private final ProvisioningUI ui; + private final int repositoryType; + private final Class> repositoryManagerType; + + private SingleRepositoryTracker(ProvisioningUI ui, int repositoryType, + Class> repositoryManagerType) { this.ui = ui; + this.repositoryType = repositoryType; + this.repositoryManagerType = repositoryManagerType; } @Override public URI[] getKnownRepositories(ProvisioningSession session) { - return getArtifactRepositoryManager().getKnownRepositories(getArtifactRepositoryFlags()); + return getRepositoryManager().getKnownRepositories(getArtifactRepositoryFlags()); } @Override public void addRepository(URI repoLocation, String nickname, ProvisioningSession session) { ui.signalRepositoryOperationStart(); try { - getArtifactRepositoryManager().addRepository(repoLocation); - if (nickname != null) - getArtifactRepositoryManager().setRepositoryProperty(repoLocation, IRepository.PROP_NICKNAME, nickname); + getRepositoryManager().addRepository(repoLocation); + if (nickname != null) { + getRepositoryManager().setRepositoryProperty(repoLocation, IRepository.PROP_NICKNAME, nickname); + } } finally { ui.signalRepositoryOperationComplete( - new RepositoryEvent(repoLocation, IRepository.TYPE_ARTIFACT, RepositoryEvent.ADDED, true), true); + new RepositoryEvent(repoLocation, repositoryType, RepositoryEvent.ADDED, true), true); } } @@ -57,7 +73,7 @@ public void removeRepositories(URI[] repoLocations, ProvisioningSession session) ui.signalRepositoryOperationStart(); try { for (URI repoLocation : repoLocations) { - getArtifactRepositoryManager().removeRepository(repoLocation); + getRepositoryManager().removeRepository(repoLocation); } } finally { ui.signalRepositoryOperationComplete(null, true); @@ -70,8 +86,8 @@ public void refreshRepositories(URI[] locations, ProvisioningSession session, IP SubMonitor mon = SubMonitor.convert(monitor, locations.length * 100); for (URI location : locations) { try { - getArtifactRepositoryManager().refreshRepository(location, mon.newChild(100)); - }catch (ProvisionException e) { + getRepositoryManager().refreshRepository(location, mon.newChild(100)); + } catch (ProvisionException e) { // ignore problematic repositories when refreshing } } @@ -81,12 +97,12 @@ public void refreshRepositories(URI[] locations, ProvisioningSession session, IP ui.signalRepositoryOperationComplete(null, true); } - IArtifactRepositoryManager getArtifactRepositoryManager() { - return ProvUI.getArtifactRepositoryManager(ui.getSession()); + private IRepositoryManager getRepositoryManager() { + return ui.getSession().getProvisioningAgent().getService(repositoryManagerType); } @Override protected boolean contains(URI location, ProvisioningSession session) { - return ProvUI.getArtifactRepositoryManager(session).contains(location); + return session.getProvisioningAgent().getService(repositoryManagerType).contains(location); } } \ No newline at end of file diff --git a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/dialogs/AddArtifactRepositoryDialog.java b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/dialogs/AddArtifactRepositoryDialog.java index 3d7ff4a4a8..91bb95edc9 100644 --- a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/dialogs/AddArtifactRepositoryDialog.java +++ b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/dialogs/AddArtifactRepositoryDialog.java @@ -13,7 +13,7 @@ *******************************************************************************/ package org.eclipse.equinox.internal.p2.ui.admin.dialogs; -import org.eclipse.equinox.internal.p2.ui.admin.ArtifactRepositoryTracker; +import org.eclipse.equinox.internal.p2.ui.admin.SingleRepositoryTracker; import org.eclipse.equinox.internal.p2.ui.dialogs.AddRepositoryDialog; import org.eclipse.equinox.p2.operations.RepositoryTracker; import org.eclipse.equinox.p2.ui.ProvisioningUI; @@ -36,7 +36,7 @@ public AddArtifactRepositoryDialog(Shell parentShell, ProvisioningUI ui) { @Override protected RepositoryTracker getRepositoryTracker() { if (tracker == null) { - tracker = new ArtifactRepositoryTracker(getProvisioningUI()); + tracker = SingleRepositoryTracker.createArtifactRepositoryTracker(getProvisioningUI()); } return tracker; } diff --git a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/dialogs/AddMetadataRepositoryDialog.java b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/dialogs/AddMetadataRepositoryDialog.java index 5f9c64b0b6..6739157d98 100644 --- a/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/dialogs/AddMetadataRepositoryDialog.java +++ b/bundles/org.eclipse.equinox.p2.ui.admin/src/org/eclipse/equinox/internal/p2/ui/admin/dialogs/AddMetadataRepositoryDialog.java @@ -13,7 +13,7 @@ *******************************************************************************/ package org.eclipse.equinox.internal.p2.ui.admin.dialogs; -import org.eclipse.equinox.internal.p2.ui.admin.MetadataRepositoryTracker; +import org.eclipse.equinox.internal.p2.ui.admin.SingleRepositoryTracker; import org.eclipse.equinox.internal.p2.ui.dialogs.AddRepositoryDialog; import org.eclipse.equinox.p2.operations.RepositoryTracker; import org.eclipse.equinox.p2.ui.ProvisioningUI; @@ -36,7 +36,7 @@ public AddMetadataRepositoryDialog(Shell parentShell, ProvisioningUI ui) { @Override protected RepositoryTracker getRepositoryTracker() { if (tracker == null) { - tracker = new MetadataRepositoryTracker(getProvisioningUI()); + tracker = SingleRepositoryTracker.createMetadataRepositoryTracker(getProvisioningUI()); } return tracker; }