| 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 |
* SAP - ongoing development |
| 15 |
* Todor Boev |
| 16 |
*******************************************************************************/ |
| 17 |
package org.eclipse.equinox.internal.p2.metadata; |
| 18 |
|
| 19 |
import java.util.Collections; |
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.List; |
| 22 |
import java.util.Map; |
| 23 |
import java.util.Map.Entry; |
| 24 |
import java.util.Objects; |
| 25 |
import java.util.stream.Stream; |
| 26 |
import org.eclipse.core.runtime.Assert; |
| 27 |
import org.eclipse.equinox.p2.metadata.IProvidedCapability; |
| 28 |
import org.eclipse.equinox.p2.metadata.Version; |
| 29 |
import org.eclipse.equinox.p2.metadata.expression.IMemberProvider; |
| 30 |
import org.eclipse.osgi.util.NLS; |
| 31 |
|
| 32 |
/** |
| 33 |
* Describes a capability as exposed or required by an installable unit |
| 34 |
*/ |
| 35 |
public class ProvidedCapability implements IProvidedCapability, IMemberProvider { |
| 36 |
/** Used for fast access from P2 queries to the {@link #getNamespace} method */ |
| 37 |
public static final String MEMBER_NAMESPACE = "namespace"; //$NON-NLS-1$ |
| 38 |
/** Used for fast access from P2 queries to the {@link #getName} method */ |
| 39 |
public static final String MEMBER_NAME = "name"; //$NON-NLS-1$ |
| 40 |
/** Used for fast access from P2 queries to the {@link #getVersion} method */ |
| 41 |
public static final String MEMBER_VERSION = "version"; //$NON-NLS-1$ |
| 42 |
/** Used for fast access from P2 queries to the {@link #getProperties} method */ |
| 43 |
public static final String MEMBER_PROPERTIES = "properties"; //$NON-NLS-1$ |
| 44 |
|
| 45 |
private final String namespace; |
| 46 |
private final Map<String, Object> properties; |
| 47 |
|
| 48 |
public ProvidedCapability(String namespace, Map<String, Object> props) { |
| 49 |
Assert.isNotNull(namespace, NLS.bind(Messages.provided_capability_namespace_not_defined, null)); |
| 50 |
this.namespace = namespace; |
| 51 |
|
| 52 |
Assert.isNotNull(props); |
| 53 |
Assert.isTrue(!props.isEmpty()); |
| 54 |
props.forEach(this::assertValidValueType); |
| 55 |
|
| 56 |
Map<String, Object> resolvedProps = new HashMap<>(props); |
| 57 |
|
| 58 |
// Verify the name |
| 59 |
Assert.isTrue(resolvedProps.containsKey(namespace) && (resolvedProps.get(namespace) instanceof String), |
| 60 |
NLS.bind(Messages.provided_capability_name_not_defined, namespace)); |
| 61 |
|
| 62 |
// Verify the version |
| 63 |
Object version = resolvedProps.get(PROPERTY_VERSION); |
| 64 |
if (version != null) { |
| 65 |
Assert.isTrue(version instanceof Version); |
| 66 |
} else { |
| 67 |
resolvedProps.put(PROPERTY_VERSION, Version.emptyVersion); |
| 68 |
} |
| 69 |
|
| 70 |
this.properties = Collections.unmodifiableMap(resolvedProps); |
| 71 |
} |
| 72 |
|
| 73 |
public ProvidedCapability(String namespace, String name, Version version) { |
| 74 |
Assert.isNotNull(namespace, NLS.bind(Messages.provided_capability_namespace_not_defined, null)); |
| 75 |
Assert.isNotNull(name, NLS.bind(Messages.provided_capability_name_not_defined, namespace)); |
| 76 |
this.namespace = namespace; |
| 77 |
this.properties = Map.of(namespace, name, // |
| 78 |
PROPERTY_VERSION, version == null ? Version.emptyVersion : version); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public String toString() { |
| 83 |
StringBuilder str = new StringBuilder(); |
| 84 |
str.append(namespace); |
| 85 |
|
| 86 |
for (Entry<String, Object> attr : properties.entrySet()) { |
| 87 |
String key = attr.getKey(); |
| 88 |
Object val = attr.getValue(); |
| 89 |
String type = val.getClass().getSimpleName(); |
| 90 |
|
| 91 |
str.append("; ").append(key).append(":").append(type).append("=").append(val); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 92 |
} |
| 93 |
|
| 94 |
return str.toString(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public int hashCode() { |
| 99 |
return Objects.hash(namespace, properties); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public boolean equals(Object other) { |
| 104 |
if (other == null) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
return other instanceof IProvidedCapability otherCapability // |
| 108 |
&& namespace.equals(otherCapability.getNamespace()) |
| 109 |
&& properties.equals(otherCapability.getProperties()); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public String getNamespace() { |
| 114 |
return namespace; |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public String getName() { |
| 119 |
return (String) properties.get(namespace); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public Version getVersion() { |
| 124 |
return (Version) properties.get(PROPERTY_VERSION); |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public Map<String, Object> getProperties() { |
| 129 |
return properties; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public Object getMember(String memberName) { |
| 134 |
return switch (memberName) { |
| 135 |
case MEMBER_NAMESPACE -> namespace; |
| 136 |
case MEMBER_NAME -> properties.get(namespace); |
| 137 |
case MEMBER_VERSION -> properties.get(PROPERTY_VERSION); |
| 138 |
case MEMBER_PROPERTIES -> properties; |
| 139 |
default -> throw new IllegalArgumentException(String.format("No such member: %s", memberName)); //$NON-NLS-1$ |
| 140 |
}; |
| 141 |
} |
| 142 |
|
| 143 |
private void assertValidValueType(String key, Object prop) { |
| 144 |
if (prop instanceof List<?>) { |
| 145 |
int idx = 0; |
| 146 |
for (Object scalar : (List<?>) prop) { |
| 147 |
assertValidScalarType(String.format("%s[%s]", key, idx++), scalar); //$NON-NLS-1$ |
| 148 |
} |
| 149 |
} else { |
| 150 |
assertValidScalarType(key, prop); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
private void assertValidScalarType(String key, Object scalar) { |
| 155 |
Class<?> clazz = scalar.getClass(); |
| 156 |
Stream<Class<?>> supportedClasses = Stream.of(Version.class, String.class, Long.class, Integer.class, |
| 157 |
Short.class, Byte.class, Double.class, Float.class, Boolean.class, Character.class); |
| 158 |
if (supportedClasses.noneMatch(t -> t.isAssignableFrom(clazz))) { |
| 159 |
throw new IllegalArgumentException(String.format("Invalid type %s of property %s", scalar.getClass(), key)); //$NON-NLS-1$ |
| 160 |
} |
| 161 |
} |
| 162 |
} |