Skip to content

Commit

Permalink
Provide utility to obtain the BREE from a EE requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Jun 27, 2024
1 parent 40fe485 commit 5597825
Showing 1 changed file with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@
*******************************************************************************/
package org.eclipse.pde.internal.core.util;

import static org.osgi.framework.namespace.ExecutionEnvironmentNamespace.CAPABILITY_VERSION_ATTRIBUTE;
import static org.osgi.framework.namespace.ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand All @@ -38,6 +45,8 @@
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.core.build.IBuild;
Expand All @@ -50,6 +59,9 @@
import org.eclipse.pde.internal.core.project.PDEProject;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;

public class ManifestUtils {

Expand Down Expand Up @@ -77,7 +89,7 @@ public class ManifestUtils {
* <p>
* If this method is being called from a dev mode workspace, the returned map
* should be passed to {@link TargetWeaver#weaveManifest(Map, File)} so that the
* bundle classpath can be corrected.
* bundle classpath can be corrected.
* </p>
* <p>
* This method is called by
Expand Down Expand Up @@ -285,4 +297,61 @@ private static String splitOnComma(String value) {
sb.append(values[values.length - 1]);
return sb.toString();
}


// Simple filters are for example (&(version=17)(osgi.ee=JavaSE))
private static final Pattern SIMPLE_EE_FILTER;
private static final int NAME_GROUP = 2;
private static final int VERSION_GROUP = 3;
static {
String nameSegment = "\\(" + EXECUTION_ENVIRONMENT_NAMESPACE + "=([\\w/]+)\\)"; //$NON-NLS-1$//$NON-NLS-2$
String versionSegment = "\\(" + CAPABILITY_VERSION_ATTRIBUTE + "=([\\d\\.]+)\\)"; //$NON-NLS-1$ //$NON-NLS-2$
SIMPLE_EE_FILTER = Pattern.compile("\\(&(" + nameSegment + ")|(" + versionSegment + ")\\)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}

private static final List<Map<String, String>> AVAILABLE_EE_PROPERTIES = createEEProperties();

private static List<Map<String, String>> createEEProperties() {
IExecutionEnvironment[] availableEEs = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments();
List<Map<String, String>> eeProperties = Arrays.stream(availableEEs).map(ee -> {
String id = ee.getId();
int versionSeparator = id.lastIndexOf('-');
if (versionSeparator < 0) {
throw new IllegalArgumentException("Missing version-separator in EE Id"); //$NON-NLS-1$
}
String name = id.substring(0, versionSeparator);
String version = id.substring(versionSeparator + 1);
return Map.of(EXECUTION_ENVIRONMENT_NAMESPACE, name, CAPABILITY_VERSION_ATTRIBUTE, version);
}).toList();
return eeProperties;
}

public static List<String> parseRequiredEEsFromFilter(String eeFilter) {
if (eeFilter.chars().anyMatch(Character::isWhitespace)) {
eeFilter = eeFilter.replaceAll("\\s*", ""); //$NON-NLS-1$ //$NON-NLS-2$
}
Matcher matcher = SIMPLE_EE_FILTER.matcher(eeFilter);
if (matcher.matches()) { // fast track
String name = matcher.group(NAME_GROUP);
String version = matcher.group(VERSION_GROUP);
if (name != null && version != null) {
return List.of(name + "-" + version); //$NON-NLS-1$
}
}
try { // complex filter. Collect all matching EEs
Filter filter = FrameworkUtil.createFilter(eeFilter);
List<String> requiredEEs = new ArrayList<>();
for (Map<String, String> ee : AVAILABLE_EE_PROPERTIES) {
if (filter.matches(ee)) {
String name = ee.get(EXECUTION_ENVIRONMENT_NAMESPACE);
String version = ee.get(CAPABILITY_VERSION_ATTRIBUTE);
requiredEEs.add(name + "-" + version); //$NON-NLS-1$
}
}
return requiredEEs;
} catch (InvalidSyntaxException e) { // should not happen
throw new IllegalArgumentException("Invalid execution environment filter", e); //$NON-NLS-1$
}
}

}

0 comments on commit 5597825

Please sign in to comment.