Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create/load/refereshRepository() methods to IRepositoryManager #311

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -33,7 +33,7 @@
import org.eclipse.equinox.p2.repository.artifact.spi.AbstractArtifactRepository;
import org.eclipse.osgi.util.NLS;

public class CompositeArtifactRepository extends AbstractArtifactRepository implements ICompositeRepository<IArtifactKey> {

Check warning on line 36 in bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/CompositeArtifactRepository.java

View workflow job for this annotation

GitHub Actions / build / Verify Linux

CompositeArtifactRepository illegally implements ICompositeRepository<T>

Check warning on line 36 in bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/CompositeArtifactRepository.java

View workflow job for this annotation

GitHub Actions / build / Verify Windows

CompositeArtifactRepository illegally implements ICompositeRepository<T>

Check warning on line 36 in bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/CompositeArtifactRepository.java

View workflow job for this annotation

GitHub Actions / build / Verify MacOS

CompositeArtifactRepository illegally implements ICompositeRepository<T>

static final public String REPOSITORY_TYPE = CompositeArtifactRepository.class.getName();
static final private Integer REPOSITORY_VERSION = 1;
Expand All @@ -58,28 +58,8 @@
* @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 @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.equinox.p2.console;singleton:=true
Bundle-Version: 1.3.100.qualifier
Bundle-Version: 1.3.200.qualifier
Bundle-Activator: org.eclipse.equinox.internal.p2.console.Activator
Bundle-Vendor: %providerName
Bundle-Localization: plugin
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
Loading