| 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 - Progress monitor handling and error handling |
| 15 |
******************************************************************************/ |
| 16 |
package org.eclipse.equinox.p2.publisher.actions; |
| 17 |
|
| 18 |
import java.net.URI; |
| 19 |
import java.net.URISyntaxException; |
| 20 |
import java.util.Collection; |
| 21 |
import java.util.HashSet; |
| 22 |
import org.eclipse.core.runtime.*; |
| 23 |
import org.eclipse.equinox.internal.p2.publisher.Activator; |
| 24 |
import org.eclipse.equinox.internal.p2.publisher.Messages; |
| 25 |
import org.eclipse.equinox.p2.metadata.*; |
| 26 |
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription; |
| 27 |
import org.eclipse.equinox.p2.publisher.*; |
| 28 |
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; |
| 29 |
import org.eclipse.osgi.util.NLS; |
| 30 |
|
| 31 |
/** |
| 32 |
* Create a top level IU that lists all the current roots as well as any explicitly identified |
| 33 |
* top level IUs. |
| 34 |
*/ |
| 35 |
public class RootIUAction extends AbstractPublisherAction { |
| 36 |
|
| 37 |
private Version version; |
| 38 |
private String id; |
| 39 |
private String name; |
| 40 |
private Collection<IVersionAdvice> versionAdvice; |
| 41 |
|
| 42 |
public RootIUAction(String id, Version version, String name) { |
| 43 |
this.id = id; |
| 44 |
this.version = version; |
| 45 |
this.name = name; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor) { |
| 50 |
this.info = publisherInfo; |
| 51 |
return generateRootIU(results); |
| 52 |
} |
| 53 |
|
| 54 |
protected IStatus generateRootIU(IPublisherResult result) { |
| 55 |
Collection<? extends IVersionedId> children = getChildren(result); |
| 56 |
InstallableUnitDescription descriptor = createTopLevelIUDescription(children, null, false); |
| 57 |
processUpdateDescriptorAdvice(descriptor, info); |
| 58 |
processCapabilityAdvice(descriptor, info); |
| 59 |
processTouchpointAdvice(descriptor, null, info); |
| 60 |
processInstallableUnitPropertiesAdvice(descriptor, info); |
| 61 |
processLicense(descriptor, info); |
| 62 |
IInstallableUnit rootIU = MetadataFactory.createInstallableUnit(descriptor); |
| 63 |
if (rootIU == null) |
| 64 |
return new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.error_rootIU_generation, new Object[] {name, id, version})); |
| 65 |
result.addIU(rootIU, IPublisherResult.NON_ROOT); |
| 66 |
|
| 67 |
InstallableUnitDescription[] others = processAdditionalInstallableUnitsAdvice(rootIU, info); |
| 68 |
for (int iuIndex = 0; others != null && iuIndex < others.length; iuIndex++) { |
| 69 |
result.addIU(MetadataFactory.createInstallableUnit(others[iuIndex]), IPublisherResult.ROOT); |
| 70 |
} |
| 71 |
|
| 72 |
return Status.OK_STATUS; |
| 73 |
// TODO why do we create a category here? |
| 74 |
// result.addIU(generateDefaultCategory(rootIU, rootCategory), IPublisherResult.NON_ROOT); |
| 75 |
} |
| 76 |
|
| 77 |
protected static void processLicense(InstallableUnitDescription iu, IPublisherInfo info) { |
| 78 |
Collection<ILicenseAdvice> advice = info.getAdvice(null, true, iu.getId(), iu.getVersion(), ILicenseAdvice.class); |
| 79 |
if (advice.size() > 0) { |
| 80 |
// Only process the first license we find for this IU. |
| 81 |
ILicenseAdvice entry = advice.iterator().next(); |
| 82 |
String licenseText = entry.getLicenseText() == null ? "" : entry.getLicenseText(); //$NON-NLS-1$ |
| 83 |
String licenseUrl = entry.getLicenseURL() == null ? "" : entry.getLicenseURL(); //$NON-NLS-1$ |
| 84 |
if (licenseText.length() > 0 || licenseUrl.length() > 0) |
| 85 |
iu.setLicenses(new ILicense[] {MetadataFactory.createLicense(toURIOrNull(licenseUrl), licenseText)}); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private static URI toURIOrNull(String url) { |
| 90 |
if (url == null) |
| 91 |
return null; |
| 92 |
try { |
| 93 |
return URIUtil.fromString(url); |
| 94 |
} catch (URISyntaxException e) { |
| 95 |
return null; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* This was copied over from Generator to match up with the call from generateRootIU (above). |
| 101 |
* It is entirely unclear why it was needed. Should review. |
| 102 |
* Short term fix to ensure IUs that have no corresponding category are not lost. |
| 103 |
* See https://bugs.eclipse.org/bugs/show_bug.cgi?id=211521. |
| 104 |
*/ |
| 105 |
// private IInstallableUnit generateDefaultCategory(IInstallableUnit rootIU) { |
| 106 |
// rootCategory.add(rootIU); |
| 107 |
// |
| 108 |
// InstallableUnitDescription cat = new MetadataFactory.InstallableUnitDescription(); |
| 109 |
// cat.setSingleton(true); |
| 110 |
// String categoryId = rootIU.getId() + ".categoryIU"; //$NON-NLS-1$ |
| 111 |
// cat.setId(categoryId); |
| 112 |
// cat.setVersion(Version.emptyVersion); |
| 113 |
// cat.setProperty(IInstallableUnit.PROP_NAME, rootIU.getProperty(IInstallableUnit.PROP_NAME)); |
| 114 |
// cat.setProperty(IInstallableUnit.PROP_DESCRIPTION, rootIU.getProperty(IInstallableUnit.PROP_DESCRIPTION)); |
| 115 |
// |
| 116 |
// ArrayList required = new ArrayList(rootCategory.size()); |
| 117 |
// for (Iterator iterator = rootCategory.iterator(); iterator.hasNext();) { |
| 118 |
// IInstallableUnit iu = (IInstallableUnit) iterator.next(); |
| 119 |
// required.add(MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, iu.getId(), VersionRange.emptyRange, iu.getFilter(), false, false)); |
| 120 |
// } |
| 121 |
// cat.setRequiredCapabilities((RequiredCapability[]) required.toArray(new RequiredCapability[required.size()])); |
| 122 |
// cat.setCapabilities(new ProvidedCapability[] {MetadataFactory.createProvidedCapability(IInstallableUnit.NAMESPACE_IU_ID, categoryId, Version.emptyVersion)}); |
| 123 |
// cat.setArtifacts(new IArtifactKey[0]); |
| 124 |
// cat.setProperty(IInstallableUnit.PROP_TYPE_CATEGORY, "true"); //$NON-NLS-1$ |
| 125 |
// return MetadataFactory.createInstallableUnit(cat); |
| 126 |
// } |
| 127 |
private Collection<? extends IVersionedId> getChildren(IPublisherResult result) { |
| 128 |
// get any roots that we have accummulated so far and search for |
| 129 |
// children from the advice. |
| 130 |
HashSet<IVersionedId> children = new HashSet<>(); |
| 131 |
Collection<IRootIUAdvice> rootAdvice = info.getAdvice(null, true, null, null, IRootIUAdvice.class); |
| 132 |
if (rootAdvice == null) |
| 133 |
return children; |
| 134 |
for (IRootIUAdvice advice : rootAdvice) { |
| 135 |
Collection<? extends Object> list = advice.getChildren(result); |
| 136 |
if (list != null) |
| 137 |
for (Object object : list) { |
| 138 |
// if the advice is a string, look it up in the result. if not there then |
| 139 |
// query the known metadata repos |
| 140 |
if (object instanceof String) { |
| 141 |
String childId = (String) object; |
| 142 |
IInstallableUnit iu = queryForIU(result, childId, getVersionAdvice(childId)); |
| 143 |
if (iu != null) |
| 144 |
children.add(iu); |
| 145 |
} else if (object instanceof IVersionedId) { |
| 146 |
children.add((IVersionedId) object); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
return children; |
| 151 |
} |
| 152 |
|
| 153 |
private InstallableUnitDescription createTopLevelIUDescription(Collection<? extends IVersionedId> children, Collection<IRequirement> requires, boolean configureLauncherData) { |
| 154 |
InstallableUnitDescription root = new MetadataFactory.InstallableUnitDescription(); |
| 155 |
root.setSingleton(true); |
| 156 |
root.setId(id); |
| 157 |
root.setVersion(version); |
| 158 |
root.setProperty(IInstallableUnit.PROP_NAME, name); |
| 159 |
|
| 160 |
Collection<IRequirement> requiredCapabilities = createIURequirements(children); |
| 161 |
if (requires != null) |
| 162 |
requiredCapabilities.addAll(requires); |
| 163 |
root.setRequirements(requiredCapabilities.toArray(new IRequirement[requiredCapabilities.size()])); |
| 164 |
root.setArtifacts(new IArtifactKey[0]); |
| 165 |
|
| 166 |
root.setProperty(InstallableUnitDescription.PROP_TYPE_PRODUCT, Boolean.TRUE.toString()); |
| 167 |
root.setUpdateDescriptor(MetadataFactory.createUpdateDescriptor(id, VersionRange.emptyRange, IUpdateDescriptor.NORMAL, null)); |
| 168 |
root.setProperty(InstallableUnitDescription.PROP_TYPE_GROUP, Boolean.TRUE.toString()); |
| 169 |
root.setCapabilities(new IProvidedCapability[] {createSelfCapability(id, version)}); |
| 170 |
// TODO why is the type OSGI? |
| 171 |
root.setTouchpointType(PublisherHelper.TOUCHPOINT_OSGI); |
| 172 |
return root; |
| 173 |
} |
| 174 |
|
| 175 |
private Version getVersionAdvice(String iuID) { |
| 176 |
if (versionAdvice == null) { |
| 177 |
versionAdvice = info.getAdvice(null, true, null, null, IVersionAdvice.class); |
| 178 |
if (versionAdvice == null) |
| 179 |
return null; |
| 180 |
} |
| 181 |
for (IVersionAdvice advice : versionAdvice) { |
| 182 |
// TODO have to figure a way to know the namespace here. for now just look everywhere |
| 183 |
Version result = advice.getVersion(IInstallableUnit.NAMESPACE_IU_ID, iuID); |
| 184 |
if (result == null) |
| 185 |
result = advice.getVersion(IVersionAdvice.NS_BUNDLE, iuID); |
| 186 |
if (result == null) |
| 187 |
result = advice.getVersion(IVersionAdvice.NS_FEATURE, iuID); |
| 188 |
if (result != null) |
| 189 |
return result; |
| 190 |
} |
| 191 |
return null; |
| 192 |
} |
| 193 |
} |