Skip to content

Commit

Permalink
Use new new IRepositoryManager methods to unify manager usages
Browse files Browse the repository at this point in the history
This also unifies the behavior if a repository manager of specific type
cannot be found in ProvisioningHelper.
  • Loading branch information
HannesWell committed Sep 14, 2023
1 parent b774ed7 commit 6d3bd85
Show file tree
Hide file tree
Showing 20 changed files with 360 additions and 554 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import org.eclipse.core.runtime.*;
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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IInstallableUnit> 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<IInstallableUnit> 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<IArtifactKey> 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<String, String> properties) throws ProvisionException {
Expand Down Expand Up @@ -172,15 +121,8 @@ static IQueryResult<IInstallableUnit> 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<URI> getMetadataRepositories(IProvisioningAgent agent) {
return getRepositories(IArtifactRepositoryManager.class, agent);
}

/**
Expand All @@ -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());
}

Expand All @@ -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<URI> 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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
Expand All @@ -315,4 +239,57 @@ static IStatus uninstall(IProvisioningAgent agent, String unitId, String version
return PlanExecutionHelper.executePlan(result, engine, context, progress);
}

private static <T> IRepository<T> addRepository(Class<? extends IRepositoryManager<T>> repositoryManager,
IProvisioningAgent agent, URI location, String nameSuffix, String repoType) {
IRepositoryManager<T> 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 <T> void removeRepository(Class<? extends IRepositoryManager<T>> repositoryManager,
IProvisioningAgent agent, URI location) {
IRepositoryManager<T> manager = getRepositoryManager(agent, repositoryManager);
manager.removeRepository(location);
}

private static <T> IRepository<T> getRepository(Class<? extends IRepositoryManager<T>> repositoryManager,
IProvisioningAgent agent, URI location) {
IRepositoryManager<T> 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 <T> List<URI> getRepositories(Class<? extends IRepositoryManager<T>> repositoryManager,
IProvisioningAgent agent) {
IRepositoryManager<T> manager = getRepositoryManager(agent, repositoryManager);
URI[] repos = manager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
return Arrays.asList(repos);
}

private static <T> IRepositoryManager<T> getRepositoryManager(IProvisioningAgent agent,
Class<? extends IRepositoryManager<T>> repositoryManager) {
IRepositoryManager<T> manager = agent.getService(repositoryManager);
if (manager == null) {
throw new IllegalStateException("No repository manager found for type " + repositoryManager); //$NON-NLS-1$
}
return manager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 6d3bd85

Please sign in to comment.