| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2023 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 |
* EclipseSource - ongoing development |
| 14 |
* Todor Boev |
| 15 |
*******************************************************************************/ |
| 16 |
package org.eclipse.equinox.internal.p2.metadata; |
| 17 |
|
| 18 |
import static org.eclipse.equinox.internal.p2.metadata.InstallableUnit.MEMBER_PROVIDED_CAPABILITIES; |
| 19 |
import static org.eclipse.equinox.internal.p2.metadata.ProvidedCapability.MEMBER_NAME; |
| 20 |
import static org.eclipse.equinox.internal.p2.metadata.ProvidedCapability.MEMBER_NAMESPACE; |
| 21 |
import static org.eclipse.equinox.internal.p2.metadata.ProvidedCapability.MEMBER_VERSION; |
| 22 |
import static org.eclipse.equinox.p2.metadata.Version.MAX_VERSION; |
| 23 |
import static org.eclipse.equinox.p2.metadata.VersionRange.emptyRange; |
| 24 |
|
| 25 |
import java.util.Arrays; |
| 26 |
import java.util.List; |
| 27 |
import org.eclipse.core.runtime.Assert; |
| 28 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 29 |
import org.eclipse.equinox.p2.metadata.Version; |
| 30 |
import org.eclipse.equinox.p2.metadata.VersionRange; |
| 31 |
import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil; |
| 32 |
import org.eclipse.equinox.p2.metadata.expression.IExpression; |
| 33 |
import org.eclipse.equinox.p2.metadata.expression.IExpressionFactory; |
| 34 |
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression; |
| 35 |
|
| 36 |
/** |
| 37 |
* A required capability represents some external constraint on an {@link IInstallableUnit}. |
| 38 |
* Each capability represents something an {@link IInstallableUnit} needs that |
| 39 |
* it expects to be provided by another {@link IInstallableUnit}. Capabilities are |
| 40 |
* entirely generic, and are intended to be capable of representing anything that |
| 41 |
* an {@link IInstallableUnit} may need either at install time, or at runtime. |
| 42 |
* <p> |
| 43 |
* Capabilities are segmented into namespaces. Anyone can introduce new |
| 44 |
* capability namespaces. Some well-known namespaces are introduced directly |
| 45 |
* by the provisioning framework. |
| 46 |
* |
| 47 |
* @see IInstallableUnit#NAMESPACE_IU_ID |
| 48 |
* |
| 49 |
* @noreference This class is not intended to be referenced by clients. |
| 50 |
*/ |
| 51 |
public class RequiredCapability extends Requirement implements IRequiredCapability { |
| 52 |
private static final IExpression ALL = ExpressionUtil.parse( |
| 53 |
String.format("%s.exists(x | x.%s == $0 && x.%s == $1)", //$NON-NLS-1$ |
| 54 |
MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAME, MEMBER_NAMESPACE)); |
| 55 |
|
| 56 |
private static final IExpression STRICT = ExpressionUtil.parse( |
| 57 |
String.format("%s.exists(x | x.%s == $0 && x.%s == $1 && x.%s == $2)", //$NON-NLS-1$ |
| 58 |
MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAME, MEMBER_NAMESPACE, MEMBER_VERSION)); |
| 59 |
|
| 60 |
private static final IExpression OPEN_I = ExpressionUtil.parse( |
| 61 |
String.format("%s.exists(x | x.%s == $0 && x.%s == $1 && x.%s >= $2)", //$NON-NLS-1$ |
| 62 |
MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAME, MEMBER_NAMESPACE, MEMBER_VERSION)); |
| 63 |
|
| 64 |
private static final IExpression OPEN_N = ExpressionUtil.parse( |
| 65 |
String.format("%s.exists(x | x.%s == $0 && x.%s == $1 && x.%s > $2)", //$NON-NLS-1$ |
| 66 |
MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAME, MEMBER_NAMESPACE, MEMBER_VERSION)); |
| 67 |
|
| 68 |
private static final IExpression CLOSED_II = ExpressionUtil.parse( |
| 69 |
String.format("%s.exists(x | x.%s == $0 && x.%s == $1 && x.%s >= $2 && x.%s <= $3)", //$NON-NLS-1$ |
| 70 |
MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAME, MEMBER_NAMESPACE, MEMBER_VERSION, MEMBER_VERSION)); |
| 71 |
|
| 72 |
private static final IExpression CLOSED_IN = ExpressionUtil.parse( |
| 73 |
String.format("%s.exists(x | x.%s == $0 && x.%s == $1 && x.%s >= $2 && x.%s < $3)", //$NON-NLS-1$ |
| 74 |
MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAME, MEMBER_NAMESPACE, MEMBER_VERSION, MEMBER_VERSION)); |
| 75 |
|
| 76 |
private static final IExpression CLOSED_NI = ExpressionUtil.parse( |
| 77 |
String.format("%s.exists(x | x.%s == $0 && x.%s == $1 && x.%s > $2 && x.%s <= $3)", //$NON-NLS-1$ |
| 78 |
MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAME, MEMBER_NAMESPACE, MEMBER_VERSION, MEMBER_VERSION)); |
| 79 |
|
| 80 |
private static final IExpression CLOSED_NN = ExpressionUtil.parse( |
| 81 |
String.format("%s.exists(x | x.%s == $0 && x.%s == $1 && x.%s > $2 && x.%s < $3)", //$NON-NLS-1$ |
| 82 |
MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAME, MEMBER_NAMESPACE, MEMBER_VERSION, MEMBER_VERSION)); |
| 83 |
|
| 84 |
private static final List<IExpression> PREDEFINED = Arrays.asList( |
| 85 |
ALL, STRICT, OPEN_I, OPEN_N, CLOSED_II, CLOSED_IN, CLOSED_NI, CLOSED_NN); |
| 86 |
|
| 87 |
/** |
| 88 |
* TODO Remove. This is a private impl class. Users must call the analogous MetadataFactory.createRequirement() |
| 89 |
* @deprecated To be removed once CBI is fixed. |
| 90 |
* @noreference This constructor is not intended to be referenced by clients. |
| 91 |
*/ |
| 92 |
@Deprecated |
| 93 |
public RequiredCapability(String namespace, String name, VersionRange range, String filter, boolean optional, boolean multiple) { |
| 94 |
this(namespace, name, range, filter, optional, multiple, true); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* TODO Remove. This is a private impl class. Users must call the analogous MetadataFactory.createRequirement() |
| 99 |
* @deprecated To be removed once CBI is fixed. |
| 100 |
* @noreference This constructor is not intended to be referenced by clients. |
| 101 |
*/ |
| 102 |
@Deprecated |
| 103 |
public RequiredCapability(String namespace, String name, VersionRange range, String filter, boolean optional, boolean multiple, boolean greedy) { |
| 104 |
this(namespace, name, range, InstallableUnit.parseFilter(filter), optional ? 0 : 1, multiple ? Integer.MAX_VALUE : 1, greedy, null); |
| 105 |
} |
| 106 |
|
| 107 |
public RequiredCapability(String namespace, String name, VersionRange range, IMatchExpression<IInstallableUnit> filter, int min, int max, boolean greedy, String description) { |
| 108 |
super(createMatchExpressionFromRange(namespace, name, range), filter, min, max, greedy, description); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public String getNamespace() { |
| 113 |
return extractNamespace(matchExpression); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public String getName() { |
| 118 |
return extractName(matchExpression); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public VersionRange getRange() { |
| 123 |
return extractRange(matchExpression); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public String toString() { |
| 128 |
return getNamespace() + "; " + getName() + " " + getRange(); //$NON-NLS-1$ //$NON-NLS-2$ |
| 129 |
} |
| 130 |
|
| 131 |
public static IMatchExpression<IInstallableUnit> createMatchExpressionFromRange(String namespace, String name, |
| 132 |
VersionRange range) { |
| 133 |
Assert.isNotNull(namespace); |
| 134 |
Assert.isNotNull(name); |
| 135 |
|
| 136 |
IExpressionFactory factory = ExpressionUtil.getFactory(); |
| 137 |
|
| 138 |
// All versions |
| 139 |
if (range == null || range.equals(emptyRange)) { |
| 140 |
return factory.matchExpression(ALL, name, namespace); |
| 141 |
} |
| 142 |
|
| 143 |
// Exact version |
| 144 |
if (range.getMinimum().equals(range.getMaximum())) { |
| 145 |
return factory.matchExpression(STRICT, name, namespace, range.getMinimum()); |
| 146 |
} |
| 147 |
|
| 148 |
// Open range |
| 149 |
if (range.getMaximum().equals(MAX_VERSION)) { |
| 150 |
// Open inclusive or non-inclusive |
| 151 |
IExpression expr = range.getIncludeMinimum() ? OPEN_I : OPEN_N; |
| 152 |
return factory.matchExpression(expr, name, namespace, range.getMinimum()); |
| 153 |
} |
| 154 |
|
| 155 |
// Closed range |
| 156 |
IExpression expr = range.getIncludeMinimum() |
| 157 |
// Left inclusive. Right inclusive or non-inclusive |
| 158 |
? (range.getIncludeMaximum() ? CLOSED_II : CLOSED_IN) |
| 159 |
// Left non-inclusive. Right inclusive or non-inclusive |
| 160 |
: (range.getIncludeMaximum() ? CLOSED_NI : CLOSED_NN); |
| 161 |
return factory.matchExpression(expr, name, namespace, range.getMinimum(), range.getMaximum()); |
| 162 |
} |
| 163 |
|
| 164 |
public static String extractName(IMatchExpression<IInstallableUnit> matchExpression) { |
| 165 |
assertVersionRangeRequirement(matchExpression); |
| 166 |
return (String) matchExpression.getParameters()[0]; |
| 167 |
} |
| 168 |
|
| 169 |
public static String extractNamespace(IMatchExpression<IInstallableUnit> matchExpression) { |
| 170 |
assertVersionRangeRequirement(matchExpression); |
| 171 |
return (String) matchExpression.getParameters()[1]; |
| 172 |
} |
| 173 |
|
| 174 |
public static VersionRange extractRange(IMatchExpression<IInstallableUnit> matchExpression) { |
| 175 |
assertVersionRangeRequirement(matchExpression); |
| 176 |
|
| 177 |
IExpression expr = ExpressionUtil.getOperand(matchExpression); |
| 178 |
Object[] params = matchExpression.getParameters(); |
| 179 |
|
| 180 |
return switch (params.length) { |
| 181 |
case 2 -> emptyRange; // No version parameter |
| 182 |
case 3 -> { // One version parameter: strict or one of the open ranges |
| 183 |
Version v = (Version) params[2]; |
| 184 |
if (expr.equals(STRICT)) { |
| 185 |
yield new VersionRange(v, true, v, true); |
| 186 |
} |
| 187 |
yield new VersionRange(v, expr.equals(OPEN_I), MAX_VERSION, true); |
| 188 |
} |
| 189 |
default -> { // Two version parameters: one of the closed ranges |
| 190 |
Version left = (Version) params[2]; |
| 191 |
boolean leftInclusive = expr.equals(CLOSED_II) || expr.equals(CLOSED_IN); |
| 192 |
Version right = (Version) params[3]; |
| 193 |
boolean rightInclusive = expr.equals(CLOSED_II) || expr.equals(CLOSED_NI); |
| 194 |
yield new VersionRange(left, leftInclusive, right, rightInclusive); |
| 195 |
} |
| 196 |
}; |
| 197 |
} |
| 198 |
|
| 199 |
public static boolean isStrictVersionRequirement(IMatchExpression<IInstallableUnit> matchExpression) { |
| 200 |
return STRICT == ExpressionUtil.getOperand(matchExpression); |
| 201 |
} |
| 202 |
|
| 203 |
public static boolean isVersionRangeRequirement(IMatchExpression<IInstallableUnit> matchExpression) { |
| 204 |
return PREDEFINED.contains(ExpressionUtil.getOperand(matchExpression)); |
| 205 |
} |
| 206 |
|
| 207 |
private static void assertVersionRangeRequirement(IMatchExpression<IInstallableUnit> matchExpression) { |
| 208 |
if (!isVersionRangeRequirement(matchExpression)) { |
| 209 |
throw new IllegalArgumentException(); |
| 210 |
} |
| 211 |
} |
| 212 |
} |