<commit_before>package biz.aQute.resolve.internal;

import static aQute.bnd.osgi.resource.CapReqBuilder.copy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.osgi.framework.Version;
import org.osgi.framework.namespace.BundleNamespace;
import org.osgi.framework.namespace.HostNamespace;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.resource.Capability;
import org.osgi.resource.Namespace;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.osgi.service.log.LogService;
import org.osgi.service.repository.ContentNamespace;
import org.osgi.service.repository.Repository;

import aQute.bnd.build.Project;
import aQute.bnd.build.model.BndEditModel;
import aQute.bnd.build.model.EE;
import aQute.bnd.build.model.clauses.ExportedPackage;
import aQute.bnd.deployer.repository.CapabilityIndex;
import aQute.bnd.header.Attrs;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.resource.CapReqBuilder;
import aQute.bnd.osgi.resource.Filters;
import aQute.bnd.osgi.resource.ResourceBuilder;
import aQute.bnd.service.Registry;
import aQute.bnd.service.resolve.hook.ResolverHook;
import aQute.libg.filters.AndFilter;
import aQute.libg.filters.Filter;
import aQute.libg.filters.LiteralFilter;
import aQute.libg.filters.SimpleFilter;
import biz.aQute.resolve.GenericResolveContext;
import biz.aQute.resolve.ResolutionCallback;

/**
 * This class does the resolving for bundles.
 */
public class BndrunResolveContext extends GenericResolveContext {
	static Set<String>					ignoreNamespaces			= new HashSet<String>();
	static {
		ignoreNamespaces.add(BundleNamespace.BUNDLE_NAMESPACE);
		ignoreNamespaces.add(IdentityNamespace.IDENTITY_NAMESPACE);
		ignoreNamespaces.add(ContentNamespace.CONTENT_NAMESPACE);
		ignoreNamespaces.add(HostNamespace.HOST_NAMESPACE);
	}

	public static final String			RUN_EFFECTIVE_INSTRUCTION	= "-resolve.effective";
	public static final String			PROP_RESOLVE_PREFERENCES	= "-resolve.preferences";

	private Registry					registry;

	private EE							ee;

	private List<ExportedPackage>		sysPkgsExtra				= new ArrayList<ExportedPackage>();
	private Parameters					sysCapsExtraParams			= new Parameters();
	private Parameters					resolvePrefs;

	private Version						frameworkResourceVersion	= null;
	private FrameworkResourceRepository	frameworkResourceRepo;
	private final Processor				properties;
	private Project						project;

	public BndrunResolveContext(BndEditModel runModel, Registry registry, LogService log) {
		super(log);
		try {
			this.registry = registry;
			this.properties = runModel.getProperties();
			this.project = runModel.getProject();
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public BndrunResolveContext(Processor runModel, Project project, Registry registry, LogService log) {
		super(log);
		try {
			this.registry = registry;
			this.properties = runModel;
			this.project = project;
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected synchronized void init() {
		if (initialised)
			return;

		try {
			loadEE();
			loadSystemPackagesExtra();
			loadSystemCapabilitiesExtra();
			loadRepositories();
			constructBlacklist();
			loadEffectiveSet();
			findFramework();
			loadpaths(systemCapabilityIndex);
			constructInputRequirements();
			loadPreferences();
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
		super.init();
	}

	/**
	 * Load the capabilities and packages from the -testpath and -runpath since
	 * we make those available. Testpath is only added whent he Test-Cases
	 * header is set in the lowest properties file.
	 */
	private void loadpaths(CapabilityIndex capabilityIndex) throws Exception {
		loadpath(capabilityIndex, Constants.DISTRO, "-provided");
		loadpath(capabilityIndex, Constants.RUNPATH, "-runpath");
		if (properties.get(Constants.TESTCASES) != null && !properties.is(Constants.NOJUNITOSGI))
			loadpath(capabilityIndex, Constants.TESTPATH, "-testpath");
	}

	/**
	 * Read the bundles from a path (test/run) and copy their exported packages
	 * to the packages extra list and their capabilities to the capabilities
	 * extra list just like the launcher does.
	 */
	private void loadpath(CapabilityIndex capabilityIndex, String path, String source) throws Exception {

		//
		// First gather all the resources on the buildpath
		//

		Parameters runpath = new Parameters(properties.mergeProperties(path));

		Set<Resource> resources = new LinkedHashSet<Resource>();

		for (Map.Entry<String,Attrs> e : runpath.entrySet()) {
			String bsn = e.getKey();
			String version = e.getValue().getVersion();
			if (!getResources(resources, bsn, version)) {
				// fallback for non-indexed things
				// need a more permanent solutions

			}
		}

		//
		// Now for each resource, get all its capabilities and
		// add them to the capability index with the system as
		// owner
		//

		for (Resource r : resources) {
			for (Capability c : r.getCapabilities(null)) {
				if (ignoreNamespaces.contains(c.getNamespace()))
					continue;

				Capability copy = copy(c, systemResource);<commit_msg>[resolve] Use the containers instead of assuming that version in a path is a valid version<commit_after>package biz.aQute.resolve.internal;

import static aQute.bnd.osgi.resource.CapReqBuilder.copy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.jar.Manifest;

import org.osgi.framework.Version;
import org.osgi.framework.namespace.BundleNamespace;
import org.osgi.framework.namespace.HostNamespace;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.resource.Capability;
import org.osgi.resource.Namespace;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.osgi.service.log.LogService;
import org.osgi.service.repository.ContentNamespace;
import org.osgi.service.repository.Repository;

import aQute.bnd.build.Container;
import aQute.bnd.build.Project;
import aQute.bnd.build.model.BndEditModel;
import aQute.bnd.build.model.EE;
import aQute.bnd.build.model.clauses.ExportedPackage;
import aQute.bnd.deployer.repository.CapabilityIndex;
import aQute.bnd.header.Attrs;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Domain;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Verifier;
import aQute.bnd.osgi.resource.CapReqBuilder;
import aQute.bnd.osgi.resource.Filters;
import aQute.bnd.osgi.resource.ResourceBuilder;
import aQute.bnd.service.Registry;
import aQute.bnd.service.Strategy;
import aQute.bnd.service.resolve.hook.ResolverHook;
import aQute.libg.filters.AndFilter;
import aQute.libg.filters.Filter;
import aQute.libg.filters.LiteralFilter;
import aQute.libg.filters.SimpleFilter;
import biz.aQute.resolve.GenericResolveContext;
import biz.aQute.resolve.ResolutionCallback;

/**
 * This class does the resolving for bundles.
 */
public class BndrunResolveContext extends GenericResolveContext {
	static Set<String>					ignoreNamespaces			= new HashSet<String>();
	static {
		ignoreNamespaces.add(BundleNamespace.BUNDLE_NAMESPACE);
		ignoreNamespaces.add(IdentityNamespace.IDENTITY_NAMESPACE);
		ignoreNamespaces.add(ContentNamespace.CONTENT_NAMESPACE);
		ignoreNamespaces.add(HostNamespace.HOST_NAMESPACE);
	}

	public static final String			RUN_EFFECTIVE_INSTRUCTION	= "-resolve.effective";
	public static final String			PROP_RESOLVE_PREFERENCES	= "-resolve.preferences";

	private Registry					registry;

	private EE							ee;

	private List<ExportedPackage>		sysPkgsExtra				= new ArrayList<ExportedPackage>();
	private Parameters					sysCapsExtraParams			= new Parameters();
	private Parameters					resolvePrefs;

	private Version						frameworkResourceVersion	= null;
	private FrameworkResourceRepository	frameworkResourceRepo;
	private final Processor				properties;
	private Project						project;

	public BndrunResolveContext(BndEditModel runModel, Registry registry, LogService log) {
		super(log);
		try {
			this.registry = registry;
			this.properties = runModel.getProperties();
			this.project = runModel.getProject();
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public BndrunResolveContext(Processor runModel, Project project, Registry registry, LogService log) {
		super(log);
		try {
			this.registry = registry;
			this.properties = runModel;
			this.project = project;
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected synchronized void init() {
		if (initialised)
			return;

		try {
			loadEE();
			loadSystemPackagesExtra();
			loadSystemCapabilitiesExtra();
			loadRepositories();
			constructBlacklist();
			loadEffectiveSet();
			findFramework();
			loadpaths(systemCapabilityIndex);
			constructInputRequirements();
			loadPreferences();
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
		super.init();
	}

	/**
	 * Load the capabilities and packages from the -testpath and -runpath since
	 * we make those available. Testpath is only added whent he Test-Cases
	 * header is set in the lowest properties file.
	 */
	private void loadpaths(CapabilityIndex capabilityIndex) throws Exception {
		loadpath(capabilityIndex, Constants.DISTRO, "-provided");
		loadpath(capabilityIndex, Constants.RUNPATH, "-runpath");
		if (properties.get(Constants.TESTCASES) != null && !properties.is(Constants.NOJUNITOSGI))
			loadpath(capabilityIndex, Constants.TESTPATH, "-testpath");
	}

	/**
	 * Read the bundles from a path (test/run) and copy their exported packages
	 * to the packages extra list and their capabilities to the capabilities
	 * extra list just like the launcher does.
	 */
	private void loadpath(CapabilityIndex capabilityIndex, String path, String source) throws Exception {

		//
		// First gather all the resources on the buildpath
		//
		Set<Resource> resources = new LinkedHashSet<Resource>();

		String totalpath = properties.mergeProperties(path);
		List<Container> containers = Container.flatten(project.getBundles(Strategy.HIGHEST, totalpath, path));
		for (Container container : containers) {
			Manifest m = container.getManifest();
			if (m != null) {
				Domain domain = Domain.domain(m);
				Entry<String,Attrs> bsne = domain.getBundleSymbolicName();
				if (bsne != null && Verifier.isBsn(bsne.getKey())) {
					String version = domain.getBundleVersion();
					if (version != null && Verifier.isVersion(version)) {
						if (!getResources(resources, bsne.getKey(), version)) {
							// fallback for non-indexed things
							// need a more permanent solutions

						}
					}
				}
			}
		}

		//
		// Now for each resource, get all its capabilities and
		// add them to the capability index with the system as
		// owner
		//

		for (Resource r : resources) {
			for (Capability c : r.getCapabilities(null)) {
				if (ignoreNamespaces.contains(c.getNamespace()))
					continue;

				Capability copy = copy(c, systemResource);
{'fraction_non_alphanumeric': 0.10062240663900415, 'fraction_numerical': 0.0, 'mean_word_length': 4.5834942084942085, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1567485', 'n_tokens_mistral': 3869, 'n_tokens_neox': 3513, 'n_words': 831}