| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2018 IBM Corporation 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 |
* IBM Corporation - initial API and implementation |
| 13 |
* Genuitec, LLC - added license support |
| 14 |
* Code 9 - Ongoing development |
| 15 |
* SAP AG - consolidation of publishers for PDE formats |
| 16 |
* Mykola Nikishov - multiple artifact checksums |
| 17 |
*******************************************************************************/ |
| 18 |
package org.eclipse.equinox.spi.p2.publisher; |
| 19 |
|
| 20 |
import java.io.File; |
| 21 |
import java.util.*; |
| 22 |
import org.eclipse.core.runtime.IStatus; |
| 23 |
import org.eclipse.equinox.internal.p2.artifact.processors.checksum.ChecksumUtilities; |
| 24 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 25 |
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey; |
| 26 |
import org.eclipse.equinox.internal.p2.metadata.BasicVersion; |
| 27 |
import org.eclipse.equinox.p2.metadata.*; |
| 28 |
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription; |
| 29 |
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; |
| 30 |
import org.eclipse.equinox.p2.publisher.IPublisherInfo; |
| 31 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; |
| 32 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; |
| 33 |
import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor; |
| 34 |
import org.osgi.framework.Constants; |
| 35 |
|
| 36 |
/** |
| 37 |
* This class was originally the MetadataGeneratorHelper from the Generator. |
| 38 |
* Much of the code has been deprecated and will be removed. |
| 39 |
*/ |
| 40 |
public class PublisherHelper { |
| 41 |
/** |
| 42 |
* A capability namespace representing the type of Eclipse resource (bundle, feature, source bundle, etc) |
| 43 |
* @see IProvidedCapability#getNamespace() |
| 44 |
*/ |
| 45 |
public static final String NAMESPACE_ECLIPSE_TYPE = "org.eclipse.equinox.p2.eclipse.type"; //$NON-NLS-1$ |
| 46 |
|
| 47 |
public static final String NAMESPACE_FLAVOR = "org.eclipse.equinox.p2.flavor"; //$NON-NLS-1$" |
| 48 |
/** |
| 49 |
* A capability name in the {@link #NAMESPACE_ECLIPSE_TYPE} namespace |
| 50 |
* representing a feature |
| 51 |
* @see IProvidedCapability#getName() |
| 52 |
*/ |
| 53 |
public static final String TYPE_ECLIPSE_FEATURE = "feature"; //$NON-NLS-1$ |
| 54 |
|
| 55 |
/** |
| 56 |
* A capability name in the {@link #NAMESPACE_ECLIPSE_TYPE} namespace |
| 57 |
* representing a source bundle |
| 58 |
* @see IProvidedCapability#getName() |
| 59 |
*/ |
| 60 |
public static final String TYPE_ECLIPSE_SOURCE = "source"; //$NON-NLS-1$ |
| 61 |
|
| 62 |
/** |
| 63 |
* A capability namespace representing the localization (translation) |
| 64 |
* of strings from a specified IU in a specified locale |
| 65 |
* @see IProvidedCapability#getNamespace() |
| 66 |
* TODO: this should be in API, probably in IInstallableUnit |
| 67 |
*/ |
| 68 |
public static final String NAMESPACE_IU_LOCALIZATION = "org.eclipse.equinox.p2.localization"; //$NON-NLS-1$ |
| 69 |
|
| 70 |
// Only certain properties in the bundle manifest are assumed to be localized. |
| 71 |
public static final String[] BUNDLE_LOCALIZED_PROPERTIES = {Constants.BUNDLE_NAME, Constants.BUNDLE_DESCRIPTION, Constants.BUNDLE_VENDOR, Constants.BUNDLE_CONTACTADDRESS, Constants.BUNDLE_DOCURL, Constants.BUNDLE_UPDATELOCATION, Constants.BUNDLE_LOCALIZATION}; |
| 72 |
|
| 73 |
public static final String CAPABILITY_NS_JAVA_PACKAGE = "java.package"; //$NON-NLS-1$ |
| 74 |
|
| 75 |
public static final String CAPABILITY_NS_UPDATE_FEATURE = "org.eclipse.update.feature"; //$NON-NLS-1$ |
| 76 |
|
| 77 |
public static final String ECLIPSE_FEATURE_CLASSIFIER = "org.eclipse.update.feature"; //$NON-NLS-1$ |
| 78 |
public static final String OSGI_BUNDLE_CLASSIFIER = "osgi.bundle"; //$NON-NLS-1$ |
| 79 |
public static final String BINARY_ARTIFACT_CLASSIFIER = "binary"; //$NON-NLS-1$ |
| 80 |
|
| 81 |
public static final String INSTALL_FEATURES_FILTER = "(org.eclipse.update.install.features=true)"; //$NON-NLS-1$ |
| 82 |
|
| 83 |
public static final String IU_NAMESPACE = IInstallableUnit.NAMESPACE_IU_ID; |
| 84 |
|
| 85 |
public static final String ECLIPSE_INSTALL_HANDLER_PROP = "org.eclipse.update.installHandler"; //$NON-NLS-1$ |
| 86 |
|
| 87 |
public static final ITouchpointType TOUCHPOINT_NATIVE = MetadataFactory.createTouchpointType("org.eclipse.equinox.p2.native", Version.createOSGi(1, 0, 0)); //$NON-NLS-1$ |
| 88 |
public static final ITouchpointType TOUCHPOINT_OSGI = MetadataFactory.createTouchpointType("org.eclipse.equinox.p2.osgi", Version.createOSGi(1, 0, 0)); //$NON-NLS-1$ |
| 89 |
|
| 90 |
public static final IProvidedCapability FEATURE_CAPABILITY = MetadataFactory.createProvidedCapability(NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_FEATURE, Version.createOSGi(1, 0, 0)); |
| 91 |
|
| 92 |
public static IArtifactDescriptor createArtifactDescriptor(IArtifactKey key, File pathOnDisk) { |
| 93 |
return createArtifactDescriptor(null, null, key, pathOnDisk); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Creates an artifact descriptor for the given key and path. |
| 98 |
* @param info the publisher info |
| 99 |
* @param key the key of the artifact to publish |
| 100 |
* @param pathOnDisk the path of the artifact on disk |
| 101 |
* @return a new artifact descriptor |
| 102 |
*/ |
| 103 |
public static IArtifactDescriptor createArtifactDescriptor(IPublisherInfo info, IArtifactKey key, File pathOnDisk) { |
| 104 |
return createArtifactDescriptor(info, info.getArtifactRepository(), key, pathOnDisk); |
| 105 |
} |
| 106 |
|
| 107 |
private static IArtifactDescriptor createArtifactDescriptor(IPublisherInfo info, IArtifactRepository artifactRepo, IArtifactKey key, File pathOnDisk) { |
| 108 |
IArtifactDescriptor result = artifactRepo != null ? artifactRepo.createArtifactDescriptor(key) : new ArtifactDescriptor(key); |
| 109 |
if (result instanceof ArtifactDescriptor) { |
| 110 |
if (pathOnDisk != null && pathOnDisk.isFile()) { |
| 111 |
ArtifactDescriptor descriptor = (ArtifactDescriptor) result; |
| 112 |
descriptor.setProperty(IArtifactDescriptor.ARTIFACT_SIZE, Long.toString(pathOnDisk.length())); |
| 113 |
descriptor.setProperty(IArtifactDescriptor.DOWNLOAD_SIZE, Long.toString(pathOnDisk.length())); |
| 114 |
|
| 115 |
boolean generateChecksums = info == null || isArtifactGenerateChecksums(info); |
| 116 |
if (generateChecksums) { |
| 117 |
calculateChecksums(pathOnDisk, descriptor); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
return result; |
| 122 |
} |
| 123 |
|
| 124 |
private static void calculateChecksums(File pathOnDisk, ArtifactDescriptor descriptor) { |
| 125 |
// TODO disable specific algorithms |
| 126 |
List<String> checksumsToSkip = Collections.emptyList(); |
| 127 |
Map<String, String> checksums = new HashMap<>(); |
| 128 |
IStatus status = ChecksumUtilities.calculateChecksums(pathOnDisk, checksums, checksumsToSkip); |
| 129 |
if (!status.isOK()) { |
| 130 |
// TODO handle errors in some way |
| 131 |
LogHelper.log(status); |
| 132 |
} |
| 133 |
Map<String, String> checksumProperties = ChecksumUtilities.checksumsToProperties(IArtifactDescriptor.DOWNLOAD_CHECKSUM, checksums); |
| 134 |
descriptor.addProperties(checksumProperties); |
| 135 |
} |
| 136 |
|
| 137 |
public static IProvidedCapability makeTranslationCapability(String hostId, Locale locale) { |
| 138 |
return MetadataFactory.createProvidedCapability(NAMESPACE_IU_LOCALIZATION, locale.toString(), Version.createOSGi(1, 0, 0)); |
| 139 |
} |
| 140 |
|
| 141 |
public static String createDefaultConfigUnitId(String classifier, String configurationFlavor) { |
| 142 |
return configurationFlavor + "." + classifier + ".default"; //$NON-NLS-1$ //$NON-NLS-2$ |
| 143 |
} |
| 144 |
|
| 145 |
public static IInstallableUnit createDefaultFeatureConfigurationUnit(String configurationFlavor) { |
| 146 |
InstallableUnitFragmentDescription cu = new InstallableUnitFragmentDescription(); |
| 147 |
String configUnitId = createDefaultConfigUnitId(ECLIPSE_FEATURE_CLASSIFIER, configurationFlavor); |
| 148 |
cu.setId(configUnitId); |
| 149 |
Version configUnitVersion = Version.createOSGi(1, 0, 0); |
| 150 |
cu.setVersion(configUnitVersion); |
| 151 |
|
| 152 |
// Add capabilities for fragment, self, and describing the flavor supported |
| 153 |
cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); |
| 154 |
cu.setCapabilities(new IProvidedCapability[] {createSelfCapability(configUnitId, configUnitVersion), MetadataFactory.createProvidedCapability(NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); |
| 155 |
|
| 156 |
// Create a required capability on features |
| 157 |
IRequirement[] reqs = new IRequirement[] {MetadataFactory.createRequirement(NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_FEATURE, VersionRange.emptyRange, null, true, true, false)}; |
| 158 |
cu.setHost(reqs); |
| 159 |
|
| 160 |
cu.setFilter(INSTALL_FEATURES_FILTER); |
| 161 |
Map<String, String> touchpointData = new HashMap<>(); |
| 162 |
touchpointData.put("install", "installFeature(feature:${artifact},featureId:default,featureVersion:default)"); //$NON-NLS-1$//$NON-NLS-2$ |
| 163 |
touchpointData.put("uninstall", "uninstallFeature(feature:${artifact},featureId:default,featureVersion:default)"); //$NON-NLS-1$//$NON-NLS-2$ |
| 164 |
cu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); |
| 165 |
|
| 166 |
return MetadataFactory.createInstallableUnit(cu); |
| 167 |
} |
| 168 |
|
| 169 |
public static IInstallableUnit createDefaultConfigurationUnitForSourceBundles(String configurationFlavor) { |
| 170 |
InstallableUnitFragmentDescription cu = new InstallableUnitFragmentDescription(); |
| 171 |
String configUnitId = createDefaultConfigUnitId("source", configurationFlavor); //$NON-NLS-1$ |
| 172 |
cu.setId(configUnitId); |
| 173 |
Version configUnitVersion = Version.createOSGi(1, 0, 0); |
| 174 |
cu.setVersion(configUnitVersion); |
| 175 |
|
| 176 |
// Add capabilities for fragment, self, and describing the flavor supported |
| 177 |
cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); |
| 178 |
cu.setCapabilities(new IProvidedCapability[] {createSelfCapability(configUnitId, configUnitVersion), MetadataFactory.createProvidedCapability(NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); |
| 179 |
|
| 180 |
// Create a required capability on source providers |
| 181 |
IRequirement[] reqs = new IRequirement[] {MetadataFactory.createRequirement(NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_SOURCE, VersionRange.emptyRange, null, true, true, false)}; |
| 182 |
cu.setHost(reqs); |
| 183 |
Map<String, String> touchpointData = new HashMap<>(); |
| 184 |
|
| 185 |
touchpointData.put("install", "addSourceBundle(bundle:${artifact})"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 186 |
touchpointData.put("uninstall", "removeSourceBundle(bundle:${artifact})"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 187 |
cu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); |
| 188 |
return MetadataFactory.createInstallableUnit(cu); |
| 189 |
} |
| 190 |
|
| 191 |
public static ArtifactKey createBinaryArtifactKey(String id, Version version) { |
| 192 |
return new ArtifactKey(BINARY_ARTIFACT_CLASSIFIER, id, version); |
| 193 |
} |
| 194 |
|
| 195 |
public static IProvidedCapability createSelfCapability(String installableUnitId, Version installableUnitVersion) { |
| 196 |
return MetadataFactory.createProvidedCapability(IU_NAMESPACE, installableUnitId, installableUnitVersion); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Convert <code>version</code> into its OSGi equivalent if possible. |
| 201 |
* |
| 202 |
* @param version The version to convert. Can be <code>null</code> |
| 203 |
* @return The converted version or <code>null</code> if the argument was <code>null</code> |
| 204 |
* @throws UnsupportedOperationException if the version could not be converted into an OSGi version |
| 205 |
*/ |
| 206 |
public static org.osgi.framework.Version toOSGiVersion(Version version) { |
| 207 |
if (version == null) |
| 208 |
return null; |
| 209 |
if (version == Version.emptyVersion) |
| 210 |
return org.osgi.framework.Version.emptyVersion; |
| 211 |
if (version == Version.MAX_VERSION) |
| 212 |
return new org.osgi.framework.Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE); |
| 213 |
|
| 214 |
BasicVersion bv = (BasicVersion) version; |
| 215 |
return new org.osgi.framework.Version(bv.getMajor(), bv.getMinor(), bv.getMicro(), bv.getQualifier()); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Create an omni version from an OSGi <code>version</code>. |
| 220 |
* @param version The OSGi version. Can be <code>null</code>. |
| 221 |
* @return The created omni version |
| 222 |
*/ |
| 223 |
public static Version fromOSGiVersion(org.osgi.framework.Version version) { |
| 224 |
if (version == null) { |
| 225 |
return Version.MAX_VERSION; |
| 226 |
} |
| 227 |
if (version.getMajor() == Integer.MAX_VALUE && version.getMinor() == Integer.MAX_VALUE && version.getMicro() == Integer.MAX_VALUE) { |
| 228 |
return Version.MAX_VERSION; |
| 229 |
} |
| 230 |
return Version.createOSGi(version.getMajor(), version.getMinor(), version.getMicro(), version.getQualifier()); |
| 231 |
} |
| 232 |
|
| 233 |
public static org.eclipse.osgi.service.resolver.VersionRange toOSGiVersionRange(VersionRange range) { |
| 234 |
if (range.equals(VersionRange.emptyRange)) { |
| 235 |
return org.eclipse.osgi.service.resolver.VersionRange.emptyRange; |
| 236 |
} |
| 237 |
return new org.eclipse.osgi.service.resolver.VersionRange(toOSGiVersion(range.getMinimum()), range.getIncludeMinimum(), toOSGiVersion(range.getMaximum()), range.getIncludeMinimum()); |
| 238 |
} |
| 239 |
|
| 240 |
public static VersionRange fromOSGiVersionRange(org.eclipse.osgi.service.resolver.VersionRange range) { |
| 241 |
if (range.equals(org.eclipse.osgi.service.resolver.VersionRange.emptyRange)) { |
| 242 |
return VersionRange.emptyRange; |
| 243 |
} |
| 244 |
|
| 245 |
Version min = fromOSGiVersion(range.getLeft()); |
| 246 |
boolean includeMin = range.getIncludeMinimum(); |
| 247 |
|
| 248 |
Version max = fromOSGiVersion(range.getRight()); |
| 249 |
// TODO The OSGi open ended range does not include the max value, where as the p2 does |
| 250 |
// Fix the p2 range to not include maximum as well (how will this affect the projector)?. |
| 251 |
boolean includeMax = Version.MAX_VERSION.equals(max) ? true : range.getIncludeMaximum(); |
| 252 |
|
| 253 |
return new VersionRange(min, includeMin, max, includeMax); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* |
| 258 |
* @return <code>true</code> if md5 sums should be generated <code>false</code> |
| 259 |
* otherwise |
| 260 |
* @since 1.7.0 |
| 261 |
*/ |
| 262 |
public static boolean isArtifactGenerateChecksums(IPublisherInfo publisherInfo) { |
| 263 |
if ((publisherInfo.getArtifactOptions() & IPublisherInfo.A_NO_MD5) != 0) { |
| 264 |
return false; |
| 265 |
} |
| 266 |
return true; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* |
| 271 |
* @return <code>true</code> if existing artifacts should be overwritten |
| 272 |
* <code>false</code> otherwise |
| 273 |
* @since 1.7.0 |
| 274 |
*/ |
| 275 |
public static boolean isArtifactOverwrite(IPublisherInfo publisherInfo) { |
| 276 |
return (publisherInfo.getArtifactOptions() & IPublisherInfo.A_OVERWRITE) != 0; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* |
| 281 |
* @return <code>true</code> if artifacts should published <code>false</code> |
| 282 |
* otherwise |
| 283 |
* @since 1.7.0 |
| 284 |
*/ |
| 285 |
public static boolean isArtifactPublish(IPublisherInfo publisherInfo) { |
| 286 |
int artifactOptions = publisherInfo.getArtifactOptions(); |
| 287 |
return artifactOptions == 0 || (artifactOptions & IPublisherInfo.A_PUBLISH) != 0; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* |
| 292 |
* @return <code>true</code> if the artifact index should be updated |
| 293 |
* <code>false</code> otherwise |
| 294 |
* @since 1.7.0 |
| 295 |
*/ |
| 296 |
public static boolean isArtifactIndex(IPublisherInfo publisherInfo) { |
| 297 |
int artifactOptions = publisherInfo.getArtifactOptions(); |
| 298 |
return artifactOptions == 0 || (artifactOptions & IPublisherInfo.A_INDEX) != 0; |
| 299 |
} |
| 300 |
} |