|
| 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 | +} |
0 commit comments