| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2017 Code 9 and others. |
| 3 |
* |
| 4 |
* This |
| 5 |
* program and the accompanying materials are made available under the terms of |
| 6 |
* the Eclipse Public License 2.0 which accompanies this distribution, and is |
| 7 |
* available at |
| 8 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: EPL-2.0 |
| 11 |
* |
| 12 |
* Contributors: |
| 13 |
* Code 9 - initial API and implementation |
| 14 |
* IBM - ongoing development |
| 15 |
* SAP - ongoing development |
| 16 |
* Pascal Rapicault - Support for bundled macosx http://bugs.eclipse.org/57349 |
| 17 |
******************************************************************************/ |
| 18 |
package org.eclipse.equinox.p2.publisher; |
| 19 |
|
| 20 |
import java.io.BufferedInputStream; |
| 21 |
import java.io.BufferedOutputStream; |
| 22 |
import java.io.File; |
| 23 |
import java.io.FileInputStream; |
| 24 |
import java.io.IOException; |
| 25 |
import java.io.OutputStream; |
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.Arrays; |
| 28 |
import java.util.Collection; |
| 29 |
import java.util.Collections; |
| 30 |
import java.util.HashSet; |
| 31 |
import java.util.List; |
| 32 |
import java.util.Map; |
| 33 |
import java.util.Map.Entry; |
| 34 |
import java.util.Set; |
| 35 |
|
| 36 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 37 |
import org.eclipse.core.runtime.IStatus; |
| 38 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 39 |
import org.eclipse.core.runtime.Status; |
| 40 |
import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor; |
| 41 |
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils; |
| 42 |
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils.IPathComputer; |
| 43 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 44 |
import org.eclipse.equinox.internal.p2.metadata.IRequiredCapability; |
| 45 |
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; |
| 46 |
import org.eclipse.equinox.internal.p2.metadata.RequiredCapability; |
| 47 |
import org.eclipse.equinox.internal.p2.publisher.Activator; |
| 48 |
import org.eclipse.equinox.internal.p2.publisher.QuotedTokenizer; |
| 49 |
import org.eclipse.equinox.p2.core.ProvisionException; |
| 50 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 51 |
import org.eclipse.equinox.p2.metadata.IProvidedCapability; |
| 52 |
import org.eclipse.equinox.p2.metadata.IRequirement; |
| 53 |
import org.eclipse.equinox.p2.metadata.ITouchpointData; |
| 54 |
import org.eclipse.equinox.p2.metadata.IUpdateDescriptor; |
| 55 |
import org.eclipse.equinox.p2.metadata.IVersionedId; |
| 56 |
import org.eclipse.equinox.p2.metadata.MetadataFactory; |
| 57 |
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription; |
| 58 |
import org.eclipse.equinox.p2.metadata.Version; |
| 59 |
import org.eclipse.equinox.p2.metadata.VersionRange; |
| 60 |
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression; |
| 61 |
import org.eclipse.equinox.p2.publisher.actions.IAdditionalInstallableUnitAdvice; |
| 62 |
import org.eclipse.equinox.p2.publisher.actions.ICapabilityAdvice; |
| 63 |
import org.eclipse.equinox.p2.publisher.actions.IFilterAdvice; |
| 64 |
import org.eclipse.equinox.p2.publisher.actions.IPropertyAdvice; |
| 65 |
import org.eclipse.equinox.p2.publisher.actions.ITouchpointAdvice; |
| 66 |
import org.eclipse.equinox.p2.publisher.actions.IUpdateDescriptorAdvice; |
| 67 |
import org.eclipse.equinox.p2.query.Collector; |
| 68 |
import org.eclipse.equinox.p2.query.IQuery; |
| 69 |
import org.eclipse.equinox.p2.query.IQueryResult; |
| 70 |
import org.eclipse.equinox.p2.query.QueryUtil; |
| 71 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; |
| 72 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; |
| 73 |
import org.eclipse.equinox.p2.repository.artifact.IFileArtifactRepository; |
| 74 |
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; |
| 75 |
|
| 76 |
public abstract class AbstractPublisherAction implements IPublisherAction { |
| 77 |
public static final String CONFIG_ANY = "ANY"; //$NON-NLS-1$ |
| 78 |
public static final String CONFIG_SEGMENT_SEPARATOR = "."; //$NON-NLS-1$ |
| 79 |
|
| 80 |
protected IPublisherInfo info; |
| 81 |
|
| 82 |
/** |
| 83 |
* Convert a list of tokens into an array. The list separator has to be |
| 84 |
* specified. |
| 85 |
*/ |
| 86 |
public static String[] getArrayFromString(String list, String separator) { |
| 87 |
if (list == null || list.trim().equals("")) //$NON-NLS-1$ |
| 88 |
return new String[0]; |
| 89 |
List<String> result = new ArrayList<>(); |
| 90 |
for (QuotedTokenizer tokens = new QuotedTokenizer(list, separator); tokens.hasMoreTokens();) { |
| 91 |
String token = tokens.nextToken().trim(); |
| 92 |
if (!token.equals("")) //$NON-NLS-1$ |
| 93 |
result.add(token); |
| 94 |
} |
| 95 |
return result.toArray(new String[result.size()]); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns a string array of { ws, os, arch } as parsed from the given string |
| 100 |
* |
| 101 |
* @param configSpec the string to parse |
| 102 |
* @return the ws, os, arch form of the given string |
| 103 |
*/ |
| 104 |
public static String[] parseConfigSpec(String configSpec) { |
| 105 |
String[] result = getArrayFromString(configSpec, CONFIG_SEGMENT_SEPARATOR); |
| 106 |
for (int i = 0; i < result.length; i++) |
| 107 |
if (result[i].equals("*")) //$NON-NLS-1$ |
| 108 |
result[i] = CONFIG_ANY; |
| 109 |
|
| 110 |
if (result.length < 3) { |
| 111 |
String[] temp = new String[3]; |
| 112 |
System.arraycopy(result, 0, temp, 0, result.length); |
| 113 |
for (int i = result.length; i < temp.length; i++) { |
| 114 |
temp[i] = CONFIG_ANY; |
| 115 |
} |
| 116 |
result = temp; |
| 117 |
} |
| 118 |
return result; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Returns the canonical form of config spec with the given ws, os and arch. |
| 123 |
* Note that the result is intended to be machine readable (i.e., |
| 124 |
* parseConfigSpec will parse the the result). |
| 125 |
* |
| 126 |
* @param ws the window system |
| 127 |
* @param os the operating system |
| 128 |
* @param arch the machine architecture |
| 129 |
* @return the machine readable format of the given config spec |
| 130 |
*/ |
| 131 |
public static String createConfigSpec(String ws, String os, String arch) { |
| 132 |
return ws + '.' + os + '.' + arch; |
| 133 |
} |
| 134 |
|
| 135 |
protected void addSelfCapability(InstallableUnitDescription root) { |
| 136 |
root.setCapabilities(new IProvidedCapability[] { createSelfCapability(root.getId(), root.getVersion()) }); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns the LDAP filter form that matches the given config spec. Returns an |
| 141 |
* empty String if the spec does not identify an ws, os or arch. |
| 142 |
* |
| 143 |
* @param configSpec a config spec to filter |
| 144 |
* @return the LDAP filter for the given spec. <code>null</code> if the given |
| 145 |
* spec does not parse into a filter. |
| 146 |
*/ |
| 147 |
protected IMatchExpression<IInstallableUnit> createFilterSpec(String configSpec) { |
| 148 |
String ldap = createLDAPString(configSpec); |
| 149 |
if (ldap == null) |
| 150 |
return null; |
| 151 |
return InstallableUnit.parseFilter(ldap); |
| 152 |
} |
| 153 |
|
| 154 |
protected String createLDAPString(String configSpec) { |
| 155 |
String[] config = parseConfigSpec(configSpec); |
| 156 |
if (config[0] != null || config[1] != null || config[2] != null) { |
| 157 |
String filterWs = config[0] != null && !CONFIG_ANY.equalsIgnoreCase(config[0]) |
| 158 |
? "(osgi.ws=" + config[0] + ")" //$NON-NLS-1$ //$NON-NLS-2$ |
| 159 |
: ""; //$NON-NLS-1$ |
| 160 |
String filterOs = config[1] != null && !CONFIG_ANY.equalsIgnoreCase(config[1]) |
| 161 |
? "(osgi.os=" + config[1] + ")" //$NON-NLS-1$ //$NON-NLS-2$ |
| 162 |
: ""; //$NON-NLS-1$ |
| 163 |
String filterArch = config[2] != null && !CONFIG_ANY.equalsIgnoreCase(config[2]) |
| 164 |
? "(osgi.arch=" + config[2] + ")" //$NON-NLS-1$ //$NON-NLS-2$ |
| 165 |
: ""; //$NON-NLS-1$ |
| 166 |
if (filterWs.length() == 0 && filterOs.length() == 0 && filterArch.length() == 0) |
| 167 |
return null; |
| 168 |
return "(& " + filterWs + filterOs + filterArch + ")"; //$NON-NLS-1$ //$NON-NLS-2$ |
| 169 |
} |
| 170 |
return null; |
| 171 |
} |
| 172 |
|
| 173 |
protected boolean filterMatches(IMatchExpression<IInstallableUnit> filter, String configSpec) { |
| 174 |
if (filter == null) |
| 175 |
return true; |
| 176 |
|
| 177 |
String[] config = parseConfigSpec(configSpec); |
| 178 |
return filter.isMatch(InstallableUnit.contextIU(config[0], config[1], config[2])); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns the normalized string form of the given config spec. This is useful |
| 183 |
* for putting in IU ids etc. Note that the result is not intended to be machine |
| 184 |
* readable (i.e., parseConfigSpec may not work on the result). |
| 185 |
* |
| 186 |
* @param configSpec the config spec to format |
| 187 |
* @return the readable format of the given config spec |
| 188 |
*/ |
| 189 |
protected String createIdString(String configSpec) { |
| 190 |
String[] config = parseConfigSpec(configSpec); |
| 191 |
return config[0] + '.' + config[1] + '.' + config[2]; |
| 192 |
} |
| 193 |
|
| 194 |
protected String createCUIdString(String id, String type, String flavor, String configSpec) { |
| 195 |
return flavor + id + "." + type + "." + createIdString(configSpec); //$NON-NLS-1$ //$NON-NLS-2$ |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Creates and returns a collection of RequiredCapabilities for the IUs |
| 200 |
* represented by the given collection. The collection may include a mixture of |
| 201 |
* IInstallableUnits or VersionedNames. |
| 202 |
* |
| 203 |
* @param children descriptions of the IUs on which requirements are to be made |
| 204 |
* @return a collection of RequiredCapabilities representing the given IUs |
| 205 |
*/ |
| 206 |
protected Collection<IRequirement> createIURequirements(Collection<? extends IVersionedId> children) { |
| 207 |
ArrayList<IRequirement> result = new ArrayList<>(children.size()); |
| 208 |
for (IVersionedId next : children) { |
| 209 |
if (next instanceof IInstallableUnit) { |
| 210 |
IInstallableUnit iu = (IInstallableUnit) next; |
| 211 |
VersionRange range = new VersionRange(iu.getVersion(), true, iu.getVersion(), true); |
| 212 |
result.add(MetadataFactory.createRequirement(IInstallableUnit.NAMESPACE_IU_ID, iu.getId(), range, |
| 213 |
iu.getFilter() == null ? null : iu.getFilter(), false, false)); |
| 214 |
} else { |
| 215 |
Version version = next.getVersion(); |
| 216 |
VersionRange range = (version == null || Version.emptyVersion.equals(version)) ? VersionRange.emptyRange |
| 217 |
: new VersionRange(version, true, version, true); |
| 218 |
IMatchExpression<IInstallableUnit> filter = getFilterAdvice(next); |
| 219 |
result.add(MetadataFactory.createRequirement(IInstallableUnit.NAMESPACE_IU_ID, next.getId(), range, |
| 220 |
filter, false, false)); |
| 221 |
} |
| 222 |
} |
| 223 |
return result; |
| 224 |
} |
| 225 |
|
| 226 |
private IMatchExpression<IInstallableUnit> getFilterAdvice(IVersionedId name) { |
| 227 |
if (info == null) |
| 228 |
return null; |
| 229 |
Collection<IFilterAdvice> filterAdvice = info.getAdvice(CONFIG_ANY, true, name.getId(), name.getVersion(), |
| 230 |
IFilterAdvice.class); |
| 231 |
for (IFilterAdvice advice : filterAdvice) { |
| 232 |
IMatchExpression<IInstallableUnit> result = advice.getFilter(name.getId(), name.getVersion(), false); |
| 233 |
if (result != null) |
| 234 |
return result; |
| 235 |
} |
| 236 |
return null; |
| 237 |
} |
| 238 |
|
| 239 |
protected InstallableUnitDescription createIUShell(String id, Version version) { |
| 240 |
InstallableUnitDescription root = new MetadataFactory.InstallableUnitDescription(); |
| 241 |
root.setId(id); |
| 242 |
root.setVersion(version); |
| 243 |
return root; |
| 244 |
} |
| 245 |
|
| 246 |
protected InstallableUnitDescription createParentIU(Collection<? extends IVersionedId> children, String id, |
| 247 |
Version version) { |
| 248 |
InstallableUnitDescription root = createIUShell(id, version); |
| 249 |
root.addRequirements(createIURequirements(children)); |
| 250 |
addSelfCapability(root); |
| 251 |
return root; |
| 252 |
} |
| 253 |
|
| 254 |
// This is to hide FileUtils from other actions |
| 255 |
protected IPathComputer createParentPrefixComputer(int segmentsToKeep) { |
| 256 |
return FileUtils.createParentPrefixComputer(segmentsToKeep); |
| 257 |
} |
| 258 |
|
| 259 |
// This is to hide FileUtils from other actions |
| 260 |
protected IPathComputer createRootPrefixComputer(final File root) { |
| 261 |
return FileUtils.createRootPathComputer(root); |
| 262 |
} |
| 263 |
|
| 264 |
protected IProvidedCapability createSelfCapability(String installableUnitId, Version installableUnitVersion) { |
| 265 |
return MetadataFactory.createProvidedCapability(PublisherHelper.IU_NAMESPACE, installableUnitId, |
| 266 |
installableUnitVersion); |
| 267 |
} |
| 268 |
|
| 269 |
protected static InstallableUnitDescription[] processAdditionalInstallableUnitsAdvice(IInstallableUnit iu, |
| 270 |
IPublisherInfo publisherInfo) { |
| 271 |
Collection<IAdditionalInstallableUnitAdvice> advice = publisherInfo.getAdvice(null, false, iu.getId(), |
| 272 |
iu.getVersion(), IAdditionalInstallableUnitAdvice.class); |
| 273 |
if (advice.isEmpty()) |
| 274 |
return null; |
| 275 |
|
| 276 |
List<InstallableUnitDescription> ius = new ArrayList<>(); |
| 277 |
for (IAdditionalInstallableUnitAdvice entry : advice) { |
| 278 |
InstallableUnitDescription[] others = entry.getAdditionalInstallableUnitDescriptions(iu); |
| 279 |
if (others != null) |
| 280 |
ius.addAll(Arrays.asList(others)); |
| 281 |
} |
| 282 |
return ius.toArray(new InstallableUnitDescription[ius.size()]); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Add all of the advised artifact properties for the given IU and artifact |
| 287 |
* descriptor. |
| 288 |
* |
| 289 |
* @param iu the IU |
| 290 |
* @param descriptor the descriptor to decorate |
| 291 |
* @param info the publisher info supplying the advice |
| 292 |
*/ |
| 293 |
protected static void processArtifactPropertiesAdvice(IInstallableUnit iu, IArtifactDescriptor descriptor, |
| 294 |
IPublisherInfo info) { |
| 295 |
if (!(descriptor instanceof SimpleArtifactDescriptor)) |
| 296 |
return; |
| 297 |
|
| 298 |
Collection<IPropertyAdvice> advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), |
| 299 |
IPropertyAdvice.class); |
| 300 |
for (IPropertyAdvice entry : advice) { |
| 301 |
Map<String, String> props = entry.getArtifactProperties(iu, descriptor); |
| 302 |
if (props == null) |
| 303 |
continue; |
| 304 |
for (Entry<String, String> pe : props.entrySet()) { |
| 305 |
((SimpleArtifactDescriptor) descriptor).setRepositoryProperty(pe.getKey(), pe.getValue()); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Add all of the advised IU properties for the given IU. |
| 312 |
* |
| 313 |
* @param iu the IU to decorate |
| 314 |
* @param info the publisher info supplying the advice |
| 315 |
*/ |
| 316 |
protected static void processInstallableUnitPropertiesAdvice(InstallableUnitDescription iu, IPublisherInfo info) { |
| 317 |
Collection<IPropertyAdvice> advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), |
| 318 |
IPropertyAdvice.class); |
| 319 |
for (IPropertyAdvice entry : advice) { |
| 320 |
Map<String, String> props = entry.getInstallableUnitProperties(iu); |
| 321 |
if (props == null) |
| 322 |
continue; |
| 323 |
for (Entry<String, String> pe : props.entrySet()) { |
| 324 |
iu.setProperty(pe.getKey(), pe.getValue()); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Add any update descriptor advice to the given IU |
| 331 |
* |
| 332 |
* @param iu the IU to decorate |
| 333 |
* @param info the publisher info supplying the advice |
| 334 |
*/ |
| 335 |
protected static void processUpdateDescriptorAdvice(InstallableUnitDescription iu, IPublisherInfo info) { |
| 336 |
Collection<IUpdateDescriptorAdvice> advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), |
| 337 |
IUpdateDescriptorAdvice.class); |
| 338 |
|
| 339 |
if (advice.isEmpty()) |
| 340 |
return; |
| 341 |
|
| 342 |
for (IUpdateDescriptorAdvice entry : advice) { |
| 343 |
// process the IU Descriptor |
| 344 |
IUpdateDescriptor updateDescriptor = entry.getUpdateDescriptor(iu); |
| 345 |
if (updateDescriptor != null) { |
| 346 |
iu.setUpdateDescriptor(updateDescriptor); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Add all of the advised provided and required capabilities for the given |
| 353 |
* installable unit. |
| 354 |
* |
| 355 |
* @param iu the IU to decorate |
| 356 |
* @param info the publisher info supplying the advice |
| 357 |
*/ |
| 358 |
protected static void processCapabilityAdvice(InstallableUnitDescription iu, IPublisherInfo info) { |
| 359 |
Collection<ICapabilityAdvice> advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), |
| 360 |
ICapabilityAdvice.class); |
| 361 |
if (advice.isEmpty()) |
| 362 |
return; |
| 363 |
|
| 364 |
for (ICapabilityAdvice entry : advice) { |
| 365 |
// process required capabilities |
| 366 |
IRequirement[] requiredAdvice = entry.getRequiredCapabilities(iu); |
| 367 |
if (requiredAdvice != null) { |
| 368 |
List<IRequirement> current = iu.getRequirements(); |
| 369 |
Set<IRequirement> resultRequiredCapabilities = new HashSet<>(current); |
| 370 |
|
| 371 |
// remove current required capabilities that match (same name and namespace) |
| 372 |
// advice. |
| 373 |
for (IRequirement currReq : current) { |
| 374 |
IRequiredCapability currentRequiredCapability = toRequiredCapability(currReq); |
| 375 |
if (currentRequiredCapability == null) { |
| 376 |
continue; |
| 377 |
} |
| 378 |
|
| 379 |
for (IRequirement currReqAdvice : requiredAdvice) { |
| 380 |
IRequiredCapability requiredCapability = toRequiredCapability(currReqAdvice); |
| 381 |
if (requiredCapability == null) { |
| 382 |
continue; |
| 383 |
} |
| 384 |
|
| 385 |
if (requiredCapability.getNamespace().equals(currentRequiredCapability.getNamespace()) |
| 386 |
&& requiredCapability.getName().equals(currentRequiredCapability.getName())) { |
| 387 |
resultRequiredCapabilities.remove(currentRequiredCapability); |
| 388 |
break; |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
// add all advice |
| 393 |
resultRequiredCapabilities.addAll(Arrays.asList(requiredAdvice)); |
| 394 |
iu.setRequirements( |
| 395 |
resultRequiredCapabilities.toArray(new IRequirement[resultRequiredCapabilities.size()])); |
| 396 |
} |
| 397 |
|
| 398 |
// process meta required capabilities |
| 399 |
IRequirement[] metaRequiredAdvice = entry.getMetaRequiredCapabilities(iu); |
| 400 |
if (metaRequiredAdvice != null) { |
| 401 |
Collection<IRequirement> current = iu.getMetaRequirements(); |
| 402 |
Set<IRequirement> resultMetaRequiredCapabilities = new HashSet<>(current); |
| 403 |
|
| 404 |
// remove current meta-required capabilities that match (same name and |
| 405 |
// namespace) advice. |
| 406 |
for (IRequirement currMetaReq : current) { |
| 407 |
IRequiredCapability currentMetaRequiredCapability = toRequiredCapability(currMetaReq); |
| 408 |
if (currentMetaRequiredCapability == null) { |
| 409 |
continue; |
| 410 |
} |
| 411 |
|
| 412 |
for (IRequirement currMetaReqAdvice : metaRequiredAdvice) { |
| 413 |
IRequiredCapability metaRequiredCapability = toRequiredCapability(currMetaReqAdvice); |
| 414 |
if (metaRequiredCapability == null) { |
| 415 |
continue; |
| 416 |
} |
| 417 |
|
| 418 |
if (metaRequiredCapability.getNamespace().equals(currentMetaRequiredCapability.getNamespace()) |
| 419 |
&& metaRequiredCapability.getName().equals(currentMetaRequiredCapability.getName())) { |
| 420 |
resultMetaRequiredCapabilities.remove(currentMetaRequiredCapability); |
| 421 |
break; |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
// add all advice |
| 427 |
resultMetaRequiredCapabilities.addAll(Arrays.asList(metaRequiredAdvice)); |
| 428 |
iu.setMetaRequirements(resultMetaRequiredCapabilities |
| 429 |
.toArray(new IRequirement[resultMetaRequiredCapabilities.size()])); |
| 430 |
} |
| 431 |
|
| 432 |
// process provided capabilities |
| 433 |
IProvidedCapability[] providedAdvice = entry.getProvidedCapabilities(iu); |
| 434 |
if (providedAdvice != null) { |
| 435 |
Collection<IProvidedCapability> current = iu.getProvidedCapabilities(); |
| 436 |
Set<IProvidedCapability> resultProvidedCapabilities = new HashSet<>(current); |
| 437 |
for (IProvidedCapability currentProvidedCapability : current) { |
| 438 |
for (IProvidedCapability providedCapability : providedAdvice) { |
| 439 |
if (providedCapability.getNamespace().equals(currentProvidedCapability.getNamespace()) |
| 440 |
&& providedCapability.getName().equals(currentProvidedCapability.getName())) { |
| 441 |
resultProvidedCapabilities.remove(currentProvidedCapability); |
| 442 |
break; |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
resultProvidedCapabilities.addAll(Arrays.asList(providedAdvice)); |
| 447 |
iu.setCapabilities( |
| 448 |
resultProvidedCapabilities.toArray(new IProvidedCapability[resultProvidedCapabilities.size()])); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
protected static IRequiredCapability toRequiredCapability(IRequirement requirement) { |
| 454 |
if (!(requirement instanceof IRequiredCapability)) { |
| 455 |
return null; |
| 456 |
} |
| 457 |
|
| 458 |
IRequiredCapability requiredCapability = (IRequiredCapability) requirement; |
| 459 |
if (!RequiredCapability.isVersionRangeRequirement(requiredCapability.getMatches())) { |
| 460 |
return null; |
| 461 |
} |
| 462 |
|
| 463 |
return requiredCapability; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Adds all applicable touchpoint advice to the given installable unit. |
| 468 |
* |
| 469 |
* @param iu The installable unit to add touchpoint advice to |
| 470 |
* @param currentInstructions The set of touchpoint instructions assembled for |
| 471 |
* this IU so far |
| 472 |
* @param info The publisher info |
| 473 |
*/ |
| 474 |
protected static void processTouchpointAdvice(InstallableUnitDescription iu, |
| 475 |
Map<String, ? extends Object> currentInstructions, IPublisherInfo info) { |
| 476 |
processTouchpointAdvice(iu, currentInstructions, info, null); |
| 477 |
} |
| 478 |
|
| 479 |
protected static void processTouchpointAdvice(InstallableUnitDescription iu, |
| 480 |
Map<String, ? extends Object> currentInstructions, IPublisherInfo info, String configSpec) { |
| 481 |
Collection<ITouchpointAdvice> advice = info.getAdvice(configSpec, false, iu.getId(), iu.getVersion(), |
| 482 |
ITouchpointAdvice.class); |
| 483 |
if (currentInstructions == null) { |
| 484 |
if (advice == null || advice.isEmpty()) |
| 485 |
return; |
| 486 |
currentInstructions = Collections.emptyMap(); |
| 487 |
} |
| 488 |
|
| 489 |
ITouchpointData result = MetadataFactory.createTouchpointData(currentInstructions); |
| 490 |
if (advice != null) { |
| 491 |
for (ITouchpointAdvice entry : advice) { |
| 492 |
result = entry.getTouchpointData(result); |
| 493 |
} |
| 494 |
} |
| 495 |
iu.addTouchpointData(result); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Publishes the artifact by zipping the <code>files</code> using |
| 500 |
* <code>root</code> as a base for relative paths. Then copying the zip into the |
| 501 |
* repository. |
| 502 |
* |
| 503 |
* @param descriptor used to identify the zip. |
| 504 |
* @param inclusion the file to be published. files can be <code>null</code> |
| 505 |
* but no action is taken. |
| 506 |
* @param publisherInfo the publisher info. |
| 507 |
*/ |
| 508 |
protected void publishArtifact(IArtifactDescriptor descriptor, File inclusion, IPublisherInfo publisherInfo) { |
| 509 |
// no files to publish so this is done. |
| 510 |
if (inclusion == null) |
| 511 |
return; |
| 512 |
// if the destination already contains the descriptor, there is nothing to do. |
| 513 |
IArtifactRepository destination = publisherInfo.getArtifactRepository(); |
| 514 |
if (destination == null || destination.contains(descriptor)) |
| 515 |
return; |
| 516 |
|
| 517 |
// if all we are doing is indexing things then add the descriptor and get on |
| 518 |
// with it |
| 519 |
if (!PublisherHelper.isArtifactPublish(publisherInfo)) { |
| 520 |
destination.addDescriptor(descriptor, new NullProgressMonitor()); |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
// if the file is already in the same location the repo will put it, just add |
| 525 |
// the descriptor and exit |
| 526 |
if (destination instanceof IFileArtifactRepository) { |
| 527 |
File descriptorFile = ((IFileArtifactRepository) destination).getArtifactFile(descriptor); |
| 528 |
if (inclusion.equals(descriptorFile)) { |
| 529 |
destination.addDescriptor(descriptor, new NullProgressMonitor()); |
| 530 |
return; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
try { |
| 535 |
OutputStream output = destination.getOutputStream(descriptor); |
| 536 |
if (output == null) |
| 537 |
return; |
| 538 |
output = new BufferedOutputStream(output); |
| 539 |
FileUtils.copyStream(new BufferedInputStream(new FileInputStream(inclusion)), true, output, true); |
| 540 |
} catch (ProvisionException e) { |
| 541 |
LogHelper.log(e.getStatus()); |
| 542 |
} catch (IOException e) { |
| 543 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, "Error publishing artifacts", e)); //$NON-NLS-1$ |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
/** |
| 548 |
* Publishes the artifact by zipping the <code>files</code> using |
| 549 |
* <code>root</code> as a base for relative paths. Then copying the zip into the |
| 550 |
* repository. |
| 551 |
* |
| 552 |
* @param descriptor used to identify the zip. |
| 553 |
* @param inclusions and folders to be included in the zip. files can be |
| 554 |
* null. |
| 555 |
* @param exclusions and folders to be excluded in the zip. files can be |
| 556 |
* null. |
| 557 |
* @param publisherInfo the publisher info. |
| 558 |
* @param prefixComputer |
| 559 |
*/ |
| 560 |
protected void publishArtifact(IArtifactDescriptor descriptor, File[] inclusions, File[] exclusions, |
| 561 |
IPublisherInfo publisherInfo, IPathComputer prefixComputer) { |
| 562 |
// no files to publish so this is done. |
| 563 |
if (inclusions == null || inclusions.length < 1) |
| 564 |
return; |
| 565 |
// if the destination already contains the descriptor, there is nothing to do. |
| 566 |
IArtifactRepository destination = publisherInfo.getArtifactRepository(); |
| 567 |
if (destination == null || destination.contains(descriptor)) |
| 568 |
return; |
| 569 |
// if all we are doing is indexing things then add the descriptor and get on |
| 570 |
// with it |
| 571 |
if (!PublisherHelper.isArtifactPublish(publisherInfo)) { |
| 572 |
destination.addDescriptor(descriptor, new NullProgressMonitor()); |
| 573 |
return; |
| 574 |
} |
| 575 |
|
| 576 |
// TODO need to implement the overwrite story in the repos |
| 577 |
// boolean overwrite = (info.getArtifactOptions() & IPublisherInfo.A_OVERWRITE) |
| 578 |
// > 0; |
| 579 |
// if there is just one file and the mode is as-is, just copy the file into the |
| 580 |
// repo |
| 581 |
// otherwise, zip up the files and copy the zip into the repo |
| 582 |
File tempFile = null; |
| 583 |
try { |
| 584 |
OutputStream output = destination.getOutputStream(descriptor); |
| 585 |
if (output == null) |
| 586 |
return; |
| 587 |
output = new BufferedOutputStream(output); |
| 588 |
tempFile = File.createTempFile("p2.generator", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
| 589 |
FileUtils.zip(inclusions, exclusions, tempFile, prefixComputer); |
| 590 |
if (output != null) |
| 591 |
FileUtils.copyStream(new BufferedInputStream(new FileInputStream(tempFile)), true, output, true); |
| 592 |
} catch (ProvisionException e) { |
| 593 |
LogHelper.log(e.getStatus()); |
| 594 |
} catch (IOException e) { |
| 595 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, "Error publishing artifacts", e)); //$NON-NLS-1$ |
| 596 |
e.printStackTrace(); |
| 597 |
} finally { |
| 598 |
if (tempFile != null) |
| 599 |
tempFile.delete(); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
/** |
| 604 |
* Loop over the known metadata repositories looking for the given IU. Return |
| 605 |
* the first IU found. |
| 606 |
* |
| 607 |
* @param iuId the id of the IU to look for |
| 608 |
* @return the first matching IU or <code>null</code> if none. |
| 609 |
*/ |
| 610 |
protected IInstallableUnit queryForIU(IPublisherResult publisherResult, String iuId, Version version) { |
| 611 |
IQuery<IInstallableUnit> query = QueryUtil.createIUQuery(iuId, version); |
| 612 |
if (version == null || Version.emptyVersion.equals(version)) |
| 613 |
query = QueryUtil.createLatestQuery(query); |
| 614 |
|
| 615 |
IQueryResult<IInstallableUnit> collector = Collector.emptyCollector(); |
| 616 |
NullProgressMonitor progress = new NullProgressMonitor(); |
| 617 |
if (publisherResult != null) |
| 618 |
collector = publisherResult.query(query, progress); |
| 619 |
if (collector.isEmpty() && info.getMetadataRepository() != null) |
| 620 |
collector = info.getMetadataRepository().query(query, progress); |
| 621 |
if (collector.isEmpty() && info.getContextMetadataRepository() != null) |
| 622 |
collector = info.getContextMetadataRepository().query(query, progress); |
| 623 |
if (!collector.isEmpty()) |
| 624 |
return collector.iterator().next(); |
| 625 |
return null; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* Loop over the known metadata repositories looking for the given IU within a |
| 630 |
* particular range |
| 631 |
* |
| 632 |
* @param publisherResult |
| 633 |
* @param iuId the id of the IU to look for |
| 634 |
* @param versionRange the version range to consider |
| 635 |
* @return The the IUs with the matching ids in the given range |
| 636 |
*/ |
| 637 |
protected IQueryResult<IInstallableUnit> queryForIUs(IPublisherResult publisherResult, String iuId, |
| 638 |
VersionRange versionRange) { |
| 639 |
IQuery<IInstallableUnit> query = null; |
| 640 |
IQueryResult<IInstallableUnit> queryResult = Collector.emptyCollector(); |
| 641 |
query = QueryUtil.createIUQuery(iuId, versionRange); |
| 642 |
NullProgressMonitor progress = new NullProgressMonitor(); |
| 643 |
if (publisherResult != null) |
| 644 |
queryResult = publisherResult.query(query, progress); |
| 645 |
if (queryResult.isEmpty() && info.getMetadataRepository() != null) |
| 646 |
queryResult = info.getMetadataRepository().query(query, progress); |
| 647 |
if (queryResult.isEmpty() && info.getContextMetadataRepository() != null) |
| 648 |
queryResult = info.getContextMetadataRepository().query(query, progress); |
| 649 |
return queryResult; |
| 650 |
} |
| 651 |
|
| 652 |
@Override |
| 653 |
public abstract IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor); |
| 654 |
|
| 655 |
public void setPublisherInfo(IPublisherInfo info) { |
| 656 |
this.info = info; |
| 657 |
} |
| 658 |
} |