Skip to content

Commit

Permalink
Strip down ReactorRepositoryManager
Browse files Browse the repository at this point in the history
Currently ReactorRepositoryManager is also involved in computing target
platforms that is actually called on other places with similar context
and only referenced on single places.

This strip down ReactorRepositoryManager to providing the
PublishingRepository and inline the computation of platforms to allow
more local optimiziations.
  • Loading branch information
laeubi committed Dec 8, 2023
1 parent fc5af13 commit 0d4fe15
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 206 deletions.
12 changes: 6 additions & 6 deletions tycho-api/src/main/java/org/eclipse/tycho/TargetPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public interface TargetPlatform {
* Key under which the final target platform is stored in the reactor project instances.
*/
String FINAL_TARGET_PLATFORM_KEY = "org.eclipse.tycho.core.TychoConstants/targetPlatform";
String PRELIMINARY_TARGET_PLATFORM_KEY = "org.eclipse.tycho.core.TychoConstants/dependencyOnlyTargetPlatform";

/**
* Returns an artifact of the given type, id and matching version. The version reference string
Expand Down Expand Up @@ -77,12 +78,11 @@ ArtifactKey resolveArtifact(String type, String id, String versionRef)

default ResolvedArtifactKey resolvePackage(String packageName, String versionRef)
throws DependencyResolutionException, IllegalArtifactReferenceException {
ArtifactKey packageJar = resolveArtifact(PublisherHelper.CAPABILITY_NS_JAVA_PACKAGE, packageName,
versionRef);
File location = getArtifactLocation(new DefaultArtifactKey(ArtifactType.TYPE_ECLIPSE_PLUGIN,
packageJar.getId(), packageJar.getVersion()));
return ResolvedArtifactKey.of(ArtifactType.TYPE_ECLIPSE_PLUGIN, packageJar.getId(),
packageJar.getVersion(), location);
ArtifactKey packageJar = resolveArtifact(PublisherHelper.CAPABILITY_NS_JAVA_PACKAGE, packageName, versionRef);
File location = getArtifactLocation(
new DefaultArtifactKey(ArtifactType.TYPE_ECLIPSE_PLUGIN, packageJar.getId(), packageJar.getVersion()));
return ResolvedArtifactKey.of(ArtifactType.TYPE_ECLIPSE_PLUGIN, packageJar.getId(), packageJar.getVersion(),
location);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
import org.eclipse.tycho.core.DependencyResolver;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.p2.repository.GAV;
import org.eclipse.tycho.p2.repository.PublishingRepository;
import org.eclipse.tycho.p2.target.facade.PomDependencyCollector;
import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory;
import org.eclipse.tycho.p2resolver.PreliminaryTargetPlatformImpl;
import org.eclipse.tycho.repository.registry.facade.ReactorRepositoryManager;

@Component(role = TargetPlatformService.class)
Expand All @@ -54,6 +57,10 @@ public class DefaultTargetPlatformService implements TargetPlatformService {
@Requirement
ReactorRepositoryManager repositoryManager;

@Requirement
P2ResolverFactory p2ResolverFactory;
private TargetPlatformFactory tpFactory;

@Override
public Optional<TargetPlatform> getTargetPlatform() throws DependencyResolutionException {
MavenSession session = legacySupport.getSession();
Expand Down Expand Up @@ -81,12 +88,73 @@ public Optional<TargetPlatform> getTargetPlatform(ReactorProject project) throws
List<ReactorProjectIdentities> upstreamProjects = getReferencedTychoProjects(project);
PomDependencyCollector pomDependenciesCollector = dependencyResolver.resolvePomDependencies(session,
project.adapt(MavenProject.class));
TargetPlatform finalTargetPlatform = repositoryManager.computeFinalTargetPlatform(project, upstreamProjects,
TargetPlatform finalTargetPlatform = computeFinalTargetPlatform(project, upstreamProjects,
pomDependenciesCollector);
return Optional.ofNullable(finalTargetPlatform);
}
}

/**
* Computes the (immutable) target platform with final p2 metadata and attaches it to the given
* project.
*
* @param project
* the reactor project to compute the target platform for.
* @param upstreamProjects
* Other projects in the reactor which have already been built and may be referenced
* by the given project.
*/
private TargetPlatform computeFinalTargetPlatform(ReactorProject project,
List<? extends ReactorProjectIdentities> upstreamProjects, PomDependencyCollector pomDependencyCollector) {
synchronized (project) {
PreliminaryTargetPlatformImpl preliminaryTargetPlatform = getRegisteredPreliminaryTargetPlatform(project);
if (preliminaryTargetPlatform == null) {
MavenSession session = project.adapt(MavenSession.class);
if (session == null) {
session = legacySupport.getSession();
if (session == null) {
return null;
}
}
MavenProject mavenProject = project.adapt(MavenProject.class);
if (mavenProject == null) {
return null;
}
preliminaryTargetPlatform = (PreliminaryTargetPlatformImpl) dependencyResolver
.computePreliminaryTargetPlatform(session, mavenProject, DefaultReactorProject.adapt(session));

}
List<PublishingRepository> upstreamProjectResults = getBuildResults(upstreamProjects);
TargetPlatform result = getTpFactory().createTargetPlatformWithUpdatedReactorContent(
preliminaryTargetPlatform, upstreamProjectResults, pomDependencyCollector);

project.setContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY, result);
return result;
}
}

private List<PublishingRepository> getBuildResults(List<? extends ReactorProjectIdentities> projects) {
List<PublishingRepository> results = new ArrayList<>(projects.size());
for (ReactorProjectIdentities project : projects) {
results.add(repositoryManager.getPublishingRepository(project));
}
return results;
}

public synchronized TargetPlatformFactory getTpFactory() {
if (tpFactory == null) {
tpFactory = p2ResolverFactory.getTargetPlatformFactory();
}
return tpFactory;
}

private PreliminaryTargetPlatformImpl getRegisteredPreliminaryTargetPlatform(ReactorProject project) {
return project.getContextValue(
TargetPlatform.PRELIMINARY_TARGET_PLATFORM_KEY) instanceof PreliminaryTargetPlatformImpl preliminaryTargetPlatformImpl
? preliminaryTargetPlatformImpl
: null;
}

private List<ReactorProjectIdentities> getReferencedTychoProjects(ReactorProject reactorProject)
throws DependencyResolutionException {
List<ReactorProjectIdentities> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.eclipse.tycho.IDependencyMetadata;
import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType;
import org.eclipse.tycho.IllegalArtifactReferenceException;
import org.eclipse.tycho.MavenArtifactRepositoryReference;
import org.eclipse.tycho.MavenDependencyDescriptor;
import org.eclipse.tycho.MavenRepositoryLocation;
import org.eclipse.tycho.OptionalResolutionAction;
Expand Down Expand Up @@ -91,9 +92,11 @@
import org.eclipse.tycho.p2.repository.LocalRepositoryP2Indices;
import org.eclipse.tycho.p2.target.facade.PomDependencyCollector;
import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub;
import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory;
import org.eclipse.tycho.p2maven.repository.P2ArtifactRepositoryLayout;
import org.eclipse.tycho.repository.registry.facade.ReactorRepositoryManager;
import org.eclipse.tycho.resolver.P2MetadataProvider;
import org.eclipse.tycho.targetplatform.TargetDefinition.MavenGAVLocation;
import org.eclipse.tycho.targetplatform.TargetDefinitionFile;

@Component(role = DependencyResolver.class, hint = P2DependencyResolver.ROLE_HINT, instantiationStrategy = "per-lookup")
Expand Down Expand Up @@ -140,6 +143,8 @@ public class P2DependencyResolver extends AbstractLogEnabled implements Dependen
@Requirement
private MavenDependenciesResolver dependenciesResolver;

private TargetPlatformFactory tpFactory;

@Override
public void setupProjects(final MavenSession session, final MavenProject project,
final ReactorProject reactorProject) {
Expand Down Expand Up @@ -212,8 +217,39 @@ public TargetPlatform computePreliminaryTargetPlatform(MavenSession session, Mav
.setIgnoreLocalArtifacts(configuration.getIgnoreLocalArtifacts() == LocalArtifactHandling.ignore);
tpConfiguration.setReferencedRepositoryMode(configuration.getReferencedRepositoryMode());

return reactorRepositoryManager.computePreliminaryTargetPlatform(reactorProject, tpConfiguration, ee,
reactorProjects);
return computePreliminaryTargetPlatform(reactorProject, tpConfiguration, ee, reactorProjects);
}

/**
* Computes the target platform with dependency-only p2 metadata and attaches it to the given
* project.
*
* @param project
* the reactor project to compute the target platform for.
* @param resolvedEnvironment
*/
private TargetPlatform computePreliminaryTargetPlatform(ReactorProject project,
TargetPlatformConfigurationStub tpConfiguration, ExecutionEnvironmentConfiguration eeConfiguration,
List<ReactorProject> reactorProjects) {
//
// at this point, there is only incomplete ("dependency-only") metadata for the reactor projects
TargetPlatform result = getTpFactory().createTargetPlatform(tpConfiguration, eeConfiguration, reactorProjects,
project);
project.setContextValue(TargetPlatform.PRELIMINARY_TARGET_PLATFORM_KEY, result);

List<MavenArtifactRepositoryReference> repositoryReferences = tpConfiguration.getTargetDefinitions().stream()
.flatMap(definition -> definition.getLocations().stream()).filter(MavenGAVLocation.class::isInstance)
.map(MavenGAVLocation.class::cast).flatMap(location -> location.getRepositoryReferences().stream())
.toList();
project.setContextValue(TychoConstants.CTX_REPOSITORY_REFERENCE, repositoryReferences);
return result;
}

public synchronized TargetPlatformFactory getTpFactory() {
if (tpFactory == null) {
tpFactory = resolverFactory.getTargetPlatformFactory();
}
return tpFactory;
}

private ReactorProject getThisReactorProject(MavenSession session, MavenProject project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.tycho.Interpolator;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.core.shared.MavenContext;
import org.eclipse.tycho.p2.repository.PublishingRepository;
import org.eclipse.tycho.p2.tools.publisher.PublishProductToolImpl;
Expand All @@ -40,24 +41,36 @@ public class PublisherServiceFactoryImpl implements PublisherServiceFactory {

@Override
public PublisherService createPublisher(ReactorProject project, List<TargetEnvironment> environments) {
P2TargetPlatform targetPlatform = (P2TargetPlatform) reactorRepoManager.getFinalTargetPlatform(project);
P2TargetPlatform targetPlatform = (P2TargetPlatform) getFinalTargetPlatform(project);
PublisherActionRunner publisherRunner = getPublisherRunnerForProject(targetPlatform, environments);
PublishingRepository publishingRepository = reactorRepoManager.getPublishingRepository(project.getIdentities());
PublishingRepository publishingRepository = reactorRepoManager.getPublishingRepository(project);

return new PublisherServiceImpl(publisherRunner, project.getBuildQualifier(), publishingRepository);
}

@Override
public PublishProductTool createProductPublisher(ReactorProject project, List<TargetEnvironment> environments,
String buildQualifier, Interpolator interpolator) {
P2TargetPlatform targetPlatform = (P2TargetPlatform) reactorRepoManager.getFinalTargetPlatform(project);
P2TargetPlatform targetPlatform = (P2TargetPlatform) getFinalTargetPlatform(project);
PublisherActionRunner publisherRunner = getPublisherRunnerForProject(targetPlatform, environments);
PublishingRepository publishingRepository = reactorRepoManager.getPublishingRepository(project.getIdentities());
PublishingRepository publishingRepository = reactorRepoManager.getPublishingRepository(project);

return new PublishProductToolImpl(publisherRunner, publishingRepository, targetPlatform, buildQualifier,
interpolator, mavenContext.getLogger());
}

/**
* Returns the target platform with final p2 metadata for the given project.
*/
private TargetPlatform getFinalTargetPlatform(ReactorProject project) {
TargetPlatform targetPlatform = (TargetPlatform) project
.getContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY);
if (targetPlatform == null) {
throw new IllegalStateException("Target platform is missing");
}
return targetPlatform;
}

private PublisherActionRunner getPublisherRunnerForProject(P2TargetPlatform targetPlatform,
List<TargetEnvironment> environments) {
checkCollaborators();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,133 +13,23 @@
*******************************************************************************/
package org.eclipse.tycho.p2resolver;

import java.util.ArrayList;
import java.util.List;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.tycho.ExecutionEnvironmentConfiguration;
import org.eclipse.tycho.MavenArtifactRepositoryReference;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.ReactorProjectIdentities;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.DependencyResolver;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.resolver.P2ResolverFactory;
import org.eclipse.tycho.p2.repository.PublishingRepository;
import org.eclipse.tycho.p2.repository.module.PublishingRepositoryImpl;
import org.eclipse.tycho.p2.target.facade.PomDependencyCollector;
import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub;
import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory;
import org.eclipse.tycho.repository.registry.facade.ReactorRepositoryManager;
import org.eclipse.tycho.targetplatform.TargetDefinition.MavenGAVLocation;

@Component(role = ReactorRepositoryManager.class)
public class ReactorRepositoryManagerImpl implements ReactorRepositoryManager {

private static final String PRELIMINARY_TARGET_PLATFORM_KEY = ReactorRepositoryManagerImpl.class.getName()
+ "/dependencyOnlyTargetPlatform";

@Requirement
IProvisioningAgent agent;
@Requirement
P2ResolverFactory p2ResolverFactory;

@Requirement(hint = P2DependencyResolver.ROLE_HINT)
DependencyResolver p2Resolver;

@Requirement
LegacySupport legacySupport;
private TargetPlatformFactory tpFactory;

@Override
public PublishingRepository getPublishingRepository(ReactorProjectIdentities project) {
return new PublishingRepositoryImpl(agent, project);
}

@Override
public TargetPlatform computePreliminaryTargetPlatform(ReactorProject project,
TargetPlatformConfigurationStub tpConfiguration, ExecutionEnvironmentConfiguration eeConfiguration,
List<ReactorProject> reactorProjects) {
//
// at this point, there is only incomplete ("dependency-only") metadata for the reactor projects
TargetPlatform result = getTpFactory().createTargetPlatform(tpConfiguration, eeConfiguration, reactorProjects,
project);
project.setContextValue(PRELIMINARY_TARGET_PLATFORM_KEY, result);

List<MavenArtifactRepositoryReference> repositoryReferences = tpConfiguration.getTargetDefinitions().stream()
.flatMap(definition -> definition.getLocations().stream()).filter(MavenGAVLocation.class::isInstance)
.map(MavenGAVLocation.class::cast).flatMap(location -> location.getRepositoryReferences().stream())
.toList();
project.setContextValue(TychoConstants.CTX_REPOSITORY_REFERENCE, repositoryReferences);
return result;
}

@Override
public TargetPlatform computeFinalTargetPlatform(ReactorProject project,
List<? extends ReactorProjectIdentities> upstreamProjects, PomDependencyCollector pomDependencyCollector) {
synchronized (project) {
PreliminaryTargetPlatformImpl preliminaryTargetPlatform = getRegisteredPreliminaryTargetPlatform(project);
if (preliminaryTargetPlatform == null) {
MavenSession session = project.adapt(MavenSession.class);
if (session == null) {
session = legacySupport.getSession();
if (session == null) {
return null;
}
}
MavenProject mavenProject = project.adapt(MavenProject.class);
if (mavenProject == null) {
return null;
}
preliminaryTargetPlatform = (PreliminaryTargetPlatformImpl) p2Resolver
.computePreliminaryTargetPlatform(session, mavenProject, DefaultReactorProject.adapt(session));

}
List<PublishingRepository> upstreamProjectResults = getBuildResults(upstreamProjects);
TargetPlatform result = getTpFactory().createTargetPlatformWithUpdatedReactorContent(
preliminaryTargetPlatform, upstreamProjectResults, pomDependencyCollector);

project.setContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY, result);
return result;
}
}

private PreliminaryTargetPlatformImpl getRegisteredPreliminaryTargetPlatform(ReactorProject project) {
return project.getContextValue(
PRELIMINARY_TARGET_PLATFORM_KEY) instanceof PreliminaryTargetPlatformImpl preliminaryTargetPlatformImpl
? preliminaryTargetPlatformImpl
: null;
}

private List<PublishingRepository> getBuildResults(List<? extends ReactorProjectIdentities> projects) {
List<PublishingRepository> results = new ArrayList<>(projects.size());
for (ReactorProjectIdentities project : projects) {
results.add(getPublishingRepository(project));
}
return results;
}

@Override
public TargetPlatform getFinalTargetPlatform(ReactorProject project) {
TargetPlatform targetPlatform = (TargetPlatform) project
.getContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY);
if (targetPlatform == null) {
throw new IllegalStateException("Target platform is missing");
}
return targetPlatform;
}

public synchronized TargetPlatformFactory getTpFactory() {
if (tpFactory == null) {
tpFactory = p2ResolverFactory.getTargetPlatformFactory();
}
return tpFactory;
}

}
Loading

0 comments on commit 0d4fe15

Please sign in to comment.