Skip to content

Commit

Permalink
Add non-IU artifact repositories to the planner resolution #874
Browse files Browse the repository at this point in the history
Given that units from non-IU locations are considered when resolving
IUs, it may happen that units are contained in the resolution path,
which are not contained by any repository. This may break previously
functional target definitions.

To resolve this problem, an in-memory artifact and metadata repository
is created, based on the metadata of the non-IU locations. Those
repositories are then added to the provisioning context for further
processing.

Amends dd321b5
  • Loading branch information
ptziegler committed Dec 2, 2023
1 parent dc53e89 commit ec244ca
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009, 2021 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
Expand Down Expand Up @@ -80,6 +80,8 @@ public class Messages extends NLS {
public static String RemoteTargetHandle_invalid_URI;
public static String RemoteTargetHandle_malformed_URL;
public static String RemoteTargetHandle_ioproblem;
public static String VirtualArtifactRepository_0;
public static String VirtualArtifactRepository_1;


static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2009, 2021 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
Expand Down Expand Up @@ -73,4 +73,6 @@ TargetRefrenceLocationFactory_Unsupported_Type=Type {0} is not supported by this
TargetRefrenceLocationFactory_Parsing_Failed=Parsing location content failed: {0}
RemoteTargetHandle_invalid_URI=Invalid URI: {0}
RemoteTargetHandle_malformed_URL=URI {0} can not be converted to an URL: {1}
RemoteTargetHandle_ioproblem=Reading URI {0} failed: {1}
RemoteTargetHandle_ioproblem=Reading URI {0} failed: {1}
VirtualArtifactRepository_0=Target bundle is not resolved: {0}
VirtualArtifactRepository_1=Artifact location not found for descriptor: {0}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2018 EclipseSource Inc. and others.
* Copyright (c) 2010, 2023 EclipseSource Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -17,6 +17,7 @@
package org.eclipse.pde.internal.core.target;

import java.io.File;
import java.lang.reflect.Field;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -32,6 +33,7 @@
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
Expand Down Expand Up @@ -69,6 +71,7 @@
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.planner.IPlanner;
import org.eclipse.equinox.p2.planner.IProfileChangeRequest;
import org.eclipse.equinox.p2.query.CollectionResult;
import org.eclipse.equinox.p2.query.IQuery;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.query.IQueryable;
Expand Down Expand Up @@ -1078,11 +1081,40 @@ private void resolveWithPlanner(ITargetDefinition target, IProgressMonitor monit
request.setInstallableUnitProfileProperty(unit, PROP_INSTALLED_IU, Boolean.toString(true));
}

ProvisioningContext context = new ProvisioningContext(getAgent());
IProvisioningAgent agent = getAgent();
List<IInstallableUnit> extraInstallableUnits = new ArrayList<>();
List<IArtifactRepository> extraArtifactRepositories = new ArrayList<>();
List<IMetadataRepository> extraMetadataRepositories = new ArrayList<>();
getAdditionalProvisionIUs(target, container -> {
List<IInstallableUnit> installableUnits = InstallableUnitGenerator //
.generateInstallableUnits(container.getBundles(), container.getFeatures()) //
.toList();
extraArtifactRepositories.add(new VirtualArtifactRepository(agent, container));
extraMetadataRepositories.add(new VirtualMetadataRepository(agent, installableUnits));
extraInstallableUnits.addAll(installableUnits);
});
ProvisioningContext context = new ProvisioningContext(agent) {
@Override
public IQueryable<IArtifactRepository> getArtifactRepositories(IProgressMonitor monitor) {
return (query, monitor2) -> {
List<IArtifactRepository> artifactRepositories = new ArrayList<>();
try {
IQueryable<IArtifactRepository> oldQueryable = super.getArtifactRepositories(monitor2);
Field f = oldQueryable.getClass().getDeclaredField("repositories");
f.setAccessible(true);
artifactRepositories.addAll((List<IArtifactRepository>) f.get(oldQueryable));

Check warning on line 1105 in ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/P2TargetUtils.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
} catch(Exception e) {
e.printStackTrace();
}
artifactRepositories.addAll(extraArtifactRepositories);
return new CollectionResult(artifactRepositories);

Check warning on line 1110 in ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/P2TargetUtils.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
};
}
};
context.setProperty(ProvisioningContext.FOLLOW_REPOSITORY_REFERENCES, Boolean.toString(true));
context.setMetadataRepositories(getMetadataRepositories(target).toArray(URI[]::new));
context.setArtifactRepositories(getArtifactRepositories(target).toArray(URI[]::new));
context.setExtraInstallableUnits(getAdditionalProvisionIUs(target));
context.setExtraInstallableUnits(extraInstallableUnits);

IProvisioningPlan plan = planner.getProvisioningPlan(request, context, subMonitor.split(20));
IStatus status = plan.getStatus();
Expand Down Expand Up @@ -1553,8 +1585,8 @@ private Collection<URI> getMetadataRepositories(ITargetDefinition target) throws
return result;
}

private List<IInstallableUnit> getAdditionalProvisionIUs(ITargetDefinition target) throws CoreException {
List<IInstallableUnit> result = new ArrayList<>();
private void getAdditionalProvisionIUs(ITargetDefinition target, Consumer<ITargetLocation> callback)
throws CoreException {
ITargetLocation[] containers = target.getTargetLocations();
if (containers != null) {
for (ITargetLocation container : containers) {
Expand All @@ -1564,17 +1596,15 @@ private List<IInstallableUnit> getAdditionalProvisionIUs(ITargetDefinition targe
}
if (container instanceof TargetReferenceBundleContainer targetRefContainer) {
ITargetDefinition referencedTargetDefinition = targetRefContainer.getTargetDefinition();
result.addAll(getAdditionalProvisionIUs(referencedTargetDefinition));
getAdditionalProvisionIUs(referencedTargetDefinition, callback);
continue;
}
if (!container.isResolved()) {
container.resolve(target, new NullProgressMonitor());
}
InstallableUnitGenerator.generateInstallableUnits(container.getBundles(), container.getFeatures())
.forEach(result::add);
callback.accept(container);
}
}
return result;
}

private static final String NATIVE_ARTIFACTS = "nativeArtifacts"; //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*******************************************************************************
* Copyright (c) 2023 Patrick Ziegler 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:
* Patrick Ziegler - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.internal.core.target;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.equinox.frameworkadmin.BundleInfo;
import org.eclipse.equinox.internal.p2.metadata.expression.QueryResult;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.equinox.p2.publisher.eclipse.FeaturesAction;
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.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRequest;
import org.eclipse.equinox.p2.repository.artifact.spi.AbstractArtifactRepository;
import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.core.target.ITargetLocation;
import org.eclipse.pde.core.target.TargetBundle;
import org.eclipse.pde.core.target.TargetFeature;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.ifeature.IFeature;
import org.eclipse.pde.internal.core.ifeature.IFeatureModel;

import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.WriteResource;

/**
* In-Memory representation of a artifact repository based on a non-IU target
* location. This repository is used during the planner resolution of an IU
* target location to supply artifacts from other (non-IU) locations.
*/
@SuppressWarnings("restriction")
public class VirtualArtifactRepository extends AbstractArtifactRepository {
private static final String NAME = "Non-IU Artifact Repository @ "; //$NON-NLS-1$
private static final String DESCRIPTION = """
In-Memory repository created for a single Non-IU repository, used
during the planner resolution of a real IU repository.
"""; // $NON-NLS-1$ //$NON-NLS-1$
private static final String MEMORY_PREFIX = "memory://"; //$NON-NLS-1$
// BundleInfo or IFeatureModel
private final Map<IArtifactDescriptor, Object> artifacts = new HashMap<>();

public VirtualArtifactRepository(IProvisioningAgent agent, ITargetLocation targetLocation) {
super(agent, NAME + getLocationSafe(targetLocation), targetLocation.getType(), null,
URI.create(MEMORY_PREFIX + UUID.randomUUID()), DESCRIPTION + '\n' + targetLocation.serialize(), null,
null);
Assert.isTrue(targetLocation.isResolved());
for (TargetBundle targetBundle : targetLocation.getBundles()) {
if (!targetBundle.getStatus().isOK()) {
PDECore.log(Status.warning(NLS.bind(Messages.VirtualArtifactRepository_0, targetBundle)));
continue;
}
BundleInfo bundleInfo = targetBundle.getBundleInfo();
IArtifactKey artifactKey = BundlesAction.createBundleArtifactKey(bundleInfo.getSymbolicName(),
bundleInfo.getVersion());
IArtifactDescriptor artifactDesriptor = new ArtifactDescriptor(artifactKey);
artifacts.put(artifactDesriptor, bundleInfo);
}
for (TargetFeature targetFeature : targetLocation.getFeatures()) {
IArtifactKey artifactKey = FeaturesAction.createFeatureArtifactKey(targetFeature.getId(),
targetFeature.getVersion());
IArtifactDescriptor artifactDesriptor = new ArtifactDescriptor(artifactKey);
artifacts.put(artifactDesriptor, targetFeature.getFeatureModel());
}
}

private static String getLocationSafe(ITargetLocation targetLocation) {
try {
return targetLocation.getLocation(false);
} catch (CoreException e) {
return "<unknown>"; //$NON-NLS-1$
}
}

@Override
public IStatus getRawArtifact(IArtifactDescriptor descriptor, OutputStream destination, IProgressMonitor monitor) {
Object artifactModel = artifacts.get(descriptor);
if (artifactModel == null) {
return Status.error(NLS.bind(Messages.VirtualArtifactRepository_1, descriptor));
}
try (InputStream is = getArtifactInputStream(artifactModel)) {
is.transferTo(destination);
return Status.OK_STATUS;
} catch (Exception e) {
return Status.error(e.getLocalizedMessage(), e);
}
}

private InputStream getArtifactInputStream(Object artifactModel) throws Exception {
if (artifactModel instanceof BundleInfo bundleInfo) {
URI location = bundleInfo.getLocation();
if (location == null) {
throw new FileNotFoundException(bundleInfo.getSymbolicName());
}
return location.toURL().openStream();
} else if (artifactModel instanceof IFeatureModel featureModel) {
String installLocation = featureModel.getInstallLocation();
if (installLocation != null) {
// return new FileInputStream(new File(installLocation));
}
IFeature feature = featureModel.getFeature();
// Generate in-memory feature.xml
ByteArrayOutputStream featureContent = new ByteArrayOutputStream();
try (PrintWriter writer = new PrintWriter(featureContent)) {
feature.write("", writer); //$NON-NLS-1$
}
// Generate in-memory feature jar (with only the feature.xml)
try (Jar jar = new Jar(feature.getId() + '_' + feature.getVersion())) {
jar.putResource("feature.xml", new WriteResource() { //$NON-NLS-1$
@Override
public void write(OutputStream out) throws Exception {
out.write(featureContent.toByteArray());
}

@Override
public long lastModified() {
return 0L;
}
});
ByteArrayOutputStream featureJar = new ByteArrayOutputStream();
jar.write(featureJar);
return new ByteArrayInputStream(featureJar.toByteArray());
}
}
throw new IllegalArgumentException(artifactModel.toString());
}

@Override
public IQueryable<IArtifactDescriptor> descriptorQueryable() {
return (query, monitor) -> query.perform(artifacts.keySet().iterator());
}

@Override
public IQueryResult<IArtifactKey> query(IQuery<IArtifactKey> query, IProgressMonitor monitor) {
return new QueryResult<>(artifacts.keySet().stream().map(IArtifactDescriptor::getArtifactKey).iterator());
}

@Override
public boolean contains(IArtifactDescriptor descriptor) {
return artifacts.containsKey(descriptor);
}

@Override
public boolean contains(IArtifactKey key) {
return artifacts.keySet().stream().anyMatch(descriptor -> key.equals(descriptor.getArtifactKey()));
}

@Override
public IStatus getArtifact(IArtifactDescriptor descriptor, OutputStream destination, IProgressMonitor monitor) {
return getRawArtifact(descriptor, destination, monitor);
}

@Override
public IArtifactDescriptor[] getArtifactDescriptors(IArtifactKey key) {
return artifacts.keySet().stream() //
.filter(descriptor -> key.equals(descriptor.getArtifactKey())) //
.toArray(IArtifactDescriptor[]::new);
}

@Override
public IStatus getArtifacts(IArtifactRequest[] requests, IProgressMonitor monitor) {
MultiStatus multiStatus = new MultiStatus(getClass(), IStatus.INFO, "Perform Artifact Requests"); //$NON-NLS-1$
SubMonitor subMonitor = SubMonitor.convert(monitor, requests.length);
boolean isOk = true;
for (IArtifactRequest request : requests) {
request.perform(this, subMonitor.split(1));
multiStatus.add(request.getResult());
isOk &= request.getResult().isOK();
}
return isOk ? Status.OK_STATUS : multiStatus;
}

@Override
public OutputStream getOutputStream(IArtifactDescriptor descriptor) throws ProvisionException {
throw new ProvisionException("Artifact repository must not be modified!"); //$NON-NLS-1$
}

}
Loading

0 comments on commit ec244ca

Please sign in to comment.