Skip to content

Commit c77b32b

Browse files
ptzieglerlaeubi
authored andcommitted
Add non-IU artifact repositories to the planner resolution #874
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
1 parent 770e615 commit c77b32b

File tree

7 files changed

+379
-15
lines changed

7 files changed

+379
-15
lines changed

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/Messages.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2009, 2021 IBM Corporation and others.
2+
* Copyright (c) 2009, 2023 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -80,6 +80,8 @@ public class Messages extends NLS {
8080
public static String RemoteTargetHandle_invalid_URI;
8181
public static String RemoteTargetHandle_malformed_URL;
8282
public static String RemoteTargetHandle_ioproblem;
83+
public static String VirtualArtifactRepository_0;
84+
public static String VirtualArtifactRepository_1;
8385

8486

8587
static {

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/Messages.properties

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
###############################################################################
2-
# Copyright (c) 2009, 2021 IBM Corporation and others.
2+
# Copyright (c) 2009, 2023 IBM Corporation and others.
33
#
44
# This program and the accompanying materials
55
# are made available under the terms of the Eclipse Public License 2.0
@@ -73,4 +73,6 @@ TargetRefrenceLocationFactory_Unsupported_Type=Type {0} is not supported by this
7373
TargetRefrenceLocationFactory_Parsing_Failed=Parsing location content failed: {0}
7474
RemoteTargetHandle_invalid_URI=Invalid URI: {0}
7575
RemoteTargetHandle_malformed_URL=URI {0} can not be converted to an URL: {1}
76-
RemoteTargetHandle_ioproblem=Reading URI {0} failed: {1}
76+
RemoteTargetHandle_ioproblem=Reading URI {0} failed: {1}
77+
VirtualArtifactRepository_0=Target bundle is not resolved: {0}
78+
VirtualArtifactRepository_1=Artifact location not found for descriptor: {0}

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/target/P2TargetUtils.java

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2010, 2018 EclipseSource Inc. and others.
2+
* Copyright (c) 2010, 2023 EclipseSource Inc. and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -1078,11 +1078,24 @@ private void resolveWithPlanner(ITargetDefinition target, IProgressMonitor monit
10781078
request.setInstallableUnitProfileProperty(unit, PROP_INSTALLED_IU, Boolean.toString(true));
10791079
}
10801080

1081-
ProvisioningContext context = new ProvisioningContext(getAgent());
1081+
List<IArtifactRepository> extraArtifactRepositories = new ArrayList<>();
1082+
List<IMetadataRepository> extraMetadataRepositories = new ArrayList<>();
1083+
addAdditionalProvisionIUs(target, extraArtifactRepositories, extraMetadataRepositories);
1084+
ProvisioningContext context = new ProvisioningContext(getAgent()) {
1085+
@Override
1086+
public IQueryable<IArtifactRepository> getArtifactRepositories(IProgressMonitor monitor) {
1087+
return QueryUtil.compoundQueryable(super.getArtifactRepositories(monitor),
1088+
(query, ignore) -> query.perform(extraArtifactRepositories.iterator()));
1089+
}
1090+
@Override
1091+
public IQueryable<IInstallableUnit> getMetadata(IProgressMonitor monitor) {
1092+
return QueryUtil.compoundQueryable(super.getMetadata(monitor),
1093+
QueryUtil.compoundQueryable(extraMetadataRepositories));
1094+
}
1095+
};
10821096
context.setProperty(ProvisioningContext.FOLLOW_REPOSITORY_REFERENCES, Boolean.toString(true));
10831097
context.setMetadataRepositories(getMetadataRepositories(target).toArray(URI[]::new));
10841098
context.setArtifactRepositories(getArtifactRepositories(target).toArray(URI[]::new));
1085-
context.setExtraInstallableUnits(getAdditionalProvisionIUs(target));
10861099

10871100
IProvisioningPlan plan = planner.getProvisioningPlan(request, context, subMonitor.split(20));
10881101
IStatus status = plan.getStatus();
@@ -1553,8 +1566,9 @@ private Collection<URI> getMetadataRepositories(ITargetDefinition target) throws
15531566
return result;
15541567
}
15551568

1556-
private List<IInstallableUnit> getAdditionalProvisionIUs(ITargetDefinition target) throws CoreException {
1557-
List<IInstallableUnit> result = new ArrayList<>();
1569+
private void addAdditionalProvisionIUs(ITargetDefinition target,
1570+
Collection<IArtifactRepository> extraArtifactRepositories,
1571+
Collection<IMetadataRepository> extraMetadataRepositories) throws CoreException {
15581572
ITargetLocation[] containers = target.getTargetLocations();
15591573
if (containers != null) {
15601574
for (ITargetLocation container : containers) {
@@ -1564,17 +1578,20 @@ private List<IInstallableUnit> getAdditionalProvisionIUs(ITargetDefinition targe
15641578
}
15651579
if (container instanceof TargetReferenceBundleContainer targetRefContainer) {
15661580
ITargetDefinition referencedTargetDefinition = targetRefContainer.getTargetDefinition();
1567-
result.addAll(getAdditionalProvisionIUs(referencedTargetDefinition));
1581+
addAdditionalProvisionIUs(referencedTargetDefinition, extraArtifactRepositories,
1582+
extraMetadataRepositories);
15681583
continue;
15691584
}
15701585
if (!container.isResolved()) {
15711586
container.resolve(target, new NullProgressMonitor());
15721587
}
1573-
InstallableUnitGenerator.generateInstallableUnits(container.getBundles(), container.getFeatures())
1574-
.forEach(result::add);
1588+
extraArtifactRepositories.add(new VirtualArtifactRepository(getAgent(), container));
1589+
List<IInstallableUnit> installableUnits = InstallableUnitGenerator //
1590+
.generateInstallableUnits(container.getBundles(), container.getFeatures()) //
1591+
.toList();
1592+
extraMetadataRepositories.add(new VirtualMetadataRepository(getAgent(), installableUnits));
15751593
}
15761594
}
1577-
return result;
15781595
}
15791596

15801597
private static final String NATIVE_ARTIFACTS = "nativeArtifacts"; //$NON-NLS-1$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Patrick Ziegler and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Patrick Ziegler - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.pde.internal.core.target;
15+
16+
import java.io.File;
17+
import java.io.FileNotFoundException;
18+
import java.io.InputStream;
19+
import java.io.OutputStream;
20+
import java.io.PrintWriter;
21+
import java.net.URI;
22+
import java.nio.file.Files;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import java.util.UUID;
26+
import java.util.jar.JarEntry;
27+
import java.util.jar.JarOutputStream;
28+
29+
import org.eclipse.core.runtime.Assert;
30+
import org.eclipse.core.runtime.CoreException;
31+
import org.eclipse.core.runtime.IProgressMonitor;
32+
import org.eclipse.core.runtime.IStatus;
33+
import org.eclipse.core.runtime.MultiStatus;
34+
import org.eclipse.core.runtime.Status;
35+
import org.eclipse.core.runtime.SubMonitor;
36+
import org.eclipse.equinox.frameworkadmin.BundleInfo;
37+
import org.eclipse.equinox.internal.p2.metadata.expression.QueryResult;
38+
import org.eclipse.equinox.p2.core.IProvisioningAgent;
39+
import org.eclipse.equinox.p2.core.ProvisionException;
40+
import org.eclipse.equinox.p2.metadata.IArtifactKey;
41+
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
42+
import org.eclipse.equinox.p2.publisher.eclipse.FeaturesAction;
43+
import org.eclipse.equinox.p2.query.IQuery;
44+
import org.eclipse.equinox.p2.query.IQueryResult;
45+
import org.eclipse.equinox.p2.query.IQueryable;
46+
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
47+
import org.eclipse.equinox.p2.repository.artifact.IArtifactRequest;
48+
import org.eclipse.equinox.p2.repository.artifact.spi.AbstractArtifactRepository;
49+
import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor;
50+
import org.eclipse.osgi.util.NLS;
51+
import org.eclipse.pde.core.target.ITargetLocation;
52+
import org.eclipse.pde.core.target.TargetBundle;
53+
import org.eclipse.pde.core.target.TargetFeature;
54+
import org.eclipse.pde.internal.core.PDECore;
55+
import org.eclipse.pde.internal.core.ifeature.IFeature;
56+
import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
57+
58+
/**
59+
* In-Memory representation of a artifact repository based on a non-IU target
60+
* location. This repository is used during the planner resolution of an IU
61+
* target location to supply artifacts from other (non-IU) locations.
62+
*/
63+
@SuppressWarnings("restriction")
64+
public class VirtualArtifactRepository extends AbstractArtifactRepository {
65+
private static final String NAME = "Non-IU Artifact Repository @ "; //$NON-NLS-1$
66+
private static final String DESCRIPTION = """
67+
In-Memory repository created for a single Non-IU repository, used
68+
during the planner resolution of a real IU repository.
69+
"""; //$NON-NLS-1$
70+
private static final String MEMORY_PREFIX = "memory://"; //$NON-NLS-1$
71+
// BundleInfo or IFeatureModel
72+
private final Map<IArtifactDescriptor, Object> artifacts = new HashMap<>();
73+
74+
public VirtualArtifactRepository(IProvisioningAgent agent, ITargetLocation targetLocation) {
75+
super(agent, NAME + getLocationSafe(targetLocation), targetLocation.getType(), null,
76+
URI.create(MEMORY_PREFIX + UUID.randomUUID()), DESCRIPTION + '\n' + targetLocation.serialize(), null,
77+
null);
78+
Assert.isTrue(targetLocation.isResolved());
79+
for (TargetBundle targetBundle : targetLocation.getBundles()) {
80+
if (!targetBundle.getStatus().isOK()) {
81+
PDECore.log(Status.warning(NLS.bind(Messages.VirtualArtifactRepository_0, targetBundle)));
82+
continue;
83+
}
84+
BundleInfo bundleInfo = targetBundle.getBundleInfo();
85+
IArtifactKey artifactKey = BundlesAction.createBundleArtifactKey(bundleInfo.getSymbolicName(),
86+
bundleInfo.getVersion());
87+
IArtifactDescriptor artifactDesriptor = new ArtifactDescriptor(artifactKey);
88+
artifacts.put(artifactDesriptor, bundleInfo);
89+
}
90+
for (TargetFeature targetFeature : targetLocation.getFeatures()) {
91+
IArtifactKey artifactKey = FeaturesAction.createFeatureArtifactKey(targetFeature.getId(),
92+
targetFeature.getVersion());
93+
IArtifactDescriptor artifactDesriptor = new ArtifactDescriptor(artifactKey);
94+
artifacts.put(artifactDesriptor, targetFeature.getFeatureModel());
95+
}
96+
}
97+
98+
private static String getLocationSafe(ITargetLocation targetLocation) {
99+
try {
100+
return targetLocation.getLocation(false);
101+
} catch (CoreException e) {
102+
return "<unknown>"; //$NON-NLS-1$
103+
}
104+
}
105+
106+
@Override
107+
public IStatus getRawArtifact(IArtifactDescriptor descriptor, OutputStream destination, IProgressMonitor monitor) {
108+
Object artifactModel = artifacts.get(descriptor);
109+
if (artifactModel == null) {
110+
return Status.error(NLS.bind(Messages.VirtualArtifactRepository_1, descriptor));
111+
}
112+
try {
113+
transferArtifact(artifactModel, destination);
114+
return Status.OK_STATUS;
115+
} catch (Exception e) {
116+
return Status.error(e.getLocalizedMessage(), e);
117+
}
118+
}
119+
120+
private void transferArtifact(Object artifactModel, OutputStream destination) throws Exception {
121+
if (artifactModel instanceof BundleInfo bundleInfo) {
122+
URI location = bundleInfo.getLocation();
123+
if (location == null) {
124+
throw new FileNotFoundException(bundleInfo.getSymbolicName());
125+
}
126+
try (InputStream is = location.toURL().openStream()) {
127+
is.transferTo(destination);
128+
}
129+
} else if (artifactModel instanceof IFeatureModel featureModel) {
130+
String installLocation = featureModel.getInstallLocation();
131+
if (installLocation != null) {
132+
File featureJar = new File(installLocation);
133+
if (featureJar.isFile()) {
134+
Files.copy(featureJar.toPath(), destination);
135+
return;
136+
}
137+
}
138+
IFeature feature = featureModel.getFeature();
139+
// Generate in-memory feature jar (with only the feature.xml)
140+
JarOutputStream jos = new JarOutputStream(destination);
141+
jos.putNextEntry(new JarEntry("feature.xml")); //$NON-NLS-1$
142+
PrintWriter printWriter = new PrintWriter(jos);
143+
feature.write("", printWriter); //$NON-NLS-1$
144+
printWriter.flush();
145+
jos.finish();
146+
} else {
147+
throw new IllegalArgumentException(artifactModel.toString());
148+
}
149+
}
150+
151+
@Override
152+
public IQueryable<IArtifactDescriptor> descriptorQueryable() {
153+
return (query, monitor) -> query.perform(artifacts.keySet().iterator());
154+
}
155+
156+
@Override
157+
public IQueryResult<IArtifactKey> query(IQuery<IArtifactKey> query, IProgressMonitor monitor) {
158+
return new QueryResult<>(artifacts.keySet().stream().map(IArtifactDescriptor::getArtifactKey).iterator());
159+
}
160+
161+
@Override
162+
public boolean contains(IArtifactDescriptor descriptor) {
163+
return artifacts.containsKey(descriptor);
164+
}
165+
166+
@Override
167+
public boolean contains(IArtifactKey key) {
168+
return artifacts.keySet().stream().anyMatch(descriptor -> key.equals(descriptor.getArtifactKey()));
169+
}
170+
171+
@Override
172+
public IStatus getArtifact(IArtifactDescriptor descriptor, OutputStream destination, IProgressMonitor monitor) {
173+
return getRawArtifact(descriptor, destination, monitor);
174+
}
175+
176+
@Override
177+
public IArtifactDescriptor[] getArtifactDescriptors(IArtifactKey key) {
178+
return artifacts.keySet().stream() //
179+
.filter(descriptor -> key.equals(descriptor.getArtifactKey())) //
180+
.toArray(IArtifactDescriptor[]::new);
181+
}
182+
183+
@Override
184+
public IStatus getArtifacts(IArtifactRequest[] requests, IProgressMonitor monitor) {
185+
MultiStatus multiStatus = new MultiStatus(getClass(), IStatus.INFO, "Perform Artifact Requests"); //$NON-NLS-1$
186+
SubMonitor subMonitor = SubMonitor.convert(monitor, requests.length);
187+
for (IArtifactRequest request : requests) {
188+
request.perform(this, subMonitor.split(1));
189+
multiStatus.add(request.getResult());
190+
}
191+
return multiStatus.isOK() ? Status.OK_STATUS : multiStatus;
192+
}
193+
194+
@Override
195+
public OutputStream getOutputStream(IArtifactDescriptor descriptor) throws ProvisionException {
196+
throw new ProvisionException("Artifact repository must not be modified!"); //$NON-NLS-1$
197+
}
198+
199+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Patrick Ziegler and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Patrick Ziegler - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.pde.internal.core.target;
15+
16+
import java.util.Collection;
17+
import java.util.Collections;
18+
import java.util.List;
19+
20+
import org.eclipse.core.runtime.IProgressMonitor;
21+
import org.eclipse.equinox.p2.core.IProvisioningAgent;
22+
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
23+
import org.eclipse.equinox.p2.query.IQuery;
24+
import org.eclipse.equinox.p2.query.IQueryResult;
25+
import org.eclipse.equinox.p2.repository.IRepositoryReference;
26+
import org.eclipse.equinox.p2.repository.metadata.spi.AbstractMetadataRepository;
27+
28+
/**
29+
* In-Memory representation of a metadata repository based on a non-IU target
30+
* location. This repository is used during the planner resolution of an IU
31+
* target location to supply the metadata from other (non-IU) locations.
32+
*/
33+
public class VirtualMetadataRepository extends AbstractMetadataRepository {
34+
private final List<IInstallableUnit> installableUnits;
35+
36+
public VirtualMetadataRepository(IProvisioningAgent agent, List<IInstallableUnit> installableUnits) {
37+
super(agent);
38+
this.installableUnits = List.copyOf(installableUnits);
39+
}
40+
41+
42+
@Override
43+
public Collection<IRepositoryReference> getReferences() {
44+
return Collections.emptySet();
45+
}
46+
47+
@Override
48+
public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
49+
return query.perform(installableUnits.iterator());
50+
}
51+
52+
@Override
53+
public void initialize(RepositoryState state) {
54+
// nothing to do
55+
}
56+
}

ui/org.eclipse.pde.ui.tests/META-INF/MANIFEST.MF

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Require-Bundle: org.eclipse.pde.ui,
3030
org.eclipse.equinox.p2.repository,
3131
org.eclipse.equinox.p2.metadata,
3232
org.eclipse.equinox.p2.engine,
33+
org.eclipse.equinox.p2.publisher.eclipse,
3334
org.eclipse.ui.forms,
3435
org.eclipse.ui.workbench.texteditor,
3536
org.eclipse.jface.text,

0 commit comments

Comments
 (0)