All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bndtools.tasks.AnalyseBundleResolutionJob Maven / Gradle / Ivy

The newest version!
package bndtools.tasks;

import static java.util.Collections.emptyList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Predicate;

import org.bndtools.api.ILogger;
import org.bndtools.api.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.osgi.resource.Capability;
import org.osgi.resource.Namespace;

import aQute.bnd.build.model.EE;
import aQute.bnd.osgi.resource.ResourceUtils;
import aQute.lib.io.IO;
import bndtools.model.resolution.RequirementWrapper;

public class AnalyseBundleResolutionJob extends Job {

	private static final ILogger					logger	= Logger.getLogger(AnalyseBundleResolutionJob.class);

	private final Set		loaders;

	private Map>	requirements;
	private Map>			capabilities;
	private EE										ee;

	public AnalyseBundleResolutionJob(String name, Set loaders) {
		this(name, loaders, null);
	}

	public AnalyseBundleResolutionJob(String name, Set loaders, EE ee) {
		super(name);
		this.loaders = loaders;
		this.ee = ee;
	}

	private static  void mergeMaps(Map> from, Map> into) {
		for (Entry> entry : from.entrySet()) {
			K key = entry.getKey();

			List list = into.get(key);
			if (list == null) {
				list = new ArrayList<>();
				into.put(key, list);
			}

			list.addAll(entry.getValue());
		}
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		try {


			// Load all the capabilities and requirements
			Map> allCaps = new HashMap<>();
			Map> allReqs = new HashMap<>();
			for (CapReqLoader loader : loaders) {
				try {
					Map> caps = loader.loadCapabilities();
					mergeMaps(caps, allCaps);

					Map> reqs = loader.loadRequirements();
					mergeMaps(reqs, allReqs);
				} catch (Exception e) {
					logger.logError("Error in Bnd resolution analysis.", e);
				} finally {
					IO.close(loader);
				}
			}

			// Check for resolved requirements
			for (String namespace : allReqs.keySet()) {
				List rws = allReqs.getOrDefault(namespace, emptyList());
				List candidates = allCaps.getOrDefault(namespace, emptyList());

				List javaCandidates = ee == null ? emptyList()
					: ee.getResource()
						.getCapabilities(namespace);

				outer: for (RequirementWrapper rw : rws) {
					String filterDirective = rw.requirement.getDirectives()
						.get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
					if (filterDirective == null) {
						continue;
					}
					Predicate predicate = ResourceUtils.filterMatcher(rw.requirement);
					for (Capability cand : candidates) {
						if (predicate.test(cand)) {
							rw.resolved = true;
							continue outer;
						}
					}
					for (Capability cand : javaCandidates) {
						if (predicate.test(cand)) {
							rw.java = true;
							continue outer;
						}
					}
				}
			}

			// Generate the final results
			// Set resultFiles = builderMap.keySet();
			// resultFileArray = resultFiles.toArray(new File[0]);

			this.requirements = allReqs;
			this.capabilities = allCaps;

			// showResults(resultFileArray, importResults, exportResults);
			return Status.OK_STATUS;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Map> getRequirements() {
		return Collections.unmodifiableMap(requirements);
	}

	public Map> getCapabilities() {
		return Collections.unmodifiableMap(capabilities);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy