|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2010, 2023 bndtools project 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 | + * Neil Bartlett <[email protected]> - initial API and implementation |
| 13 | + * PK Søreide <[email protected]> - ongoing enhancements |
| 14 | + * Ferry Huberts <[email protected]> - ongoing enhancements |
| 15 | + * Peter Kriens <[email protected]> - ongoing enhancements |
| 16 | + * BJ Hargrave <[email protected]> - ongoing enhancements |
| 17 | + * Sean Bright <[email protected]> - ongoing enhancements |
| 18 | +*******************************************************************************/ |
| 19 | + |
| 20 | +package bndtools.tasks; |
| 21 | + |
| 22 | +import static java.util.Collections.emptyList; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Map.Entry; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.function.Predicate; |
| 32 | + |
| 33 | +import org.bndtools.api.ILogger; |
| 34 | +import org.bndtools.api.Logger; |
| 35 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 36 | +import org.eclipse.core.runtime.IStatus; |
| 37 | +import org.eclipse.core.runtime.Status; |
| 38 | +import org.eclipse.core.runtime.jobs.Job; |
| 39 | +import org.osgi.resource.Capability; |
| 40 | +import org.osgi.resource.Namespace; |
| 41 | + |
| 42 | +import aQute.bnd.build.model.EE; |
| 43 | +import aQute.bnd.osgi.resource.ResourceUtils; |
| 44 | +import aQute.lib.io.IO; |
| 45 | +import bndtools.model.resolution.RequirementWrapper; |
| 46 | + |
| 47 | +public class AnalyseBundleResolutionJob extends Job { |
| 48 | + |
| 49 | + private static final ILogger logger = Logger.getLogger(AnalyseBundleResolutionJob.class); |
| 50 | + |
| 51 | + private final Set<? extends CapReqLoader> loaders; |
| 52 | + |
| 53 | + private Map<String, List<RequirementWrapper>> requirements; |
| 54 | + private Map<String, List<Capability>> capabilities; |
| 55 | + private EE ee; |
| 56 | + |
| 57 | + public AnalyseBundleResolutionJob(String name, Set<? extends CapReqLoader> loaders) { |
| 58 | + this(name, loaders, null); |
| 59 | + } |
| 60 | + |
| 61 | + public AnalyseBundleResolutionJob(String name, Set<? extends CapReqLoader> loaders, EE ee) { |
| 62 | + super(name); |
| 63 | + this.loaders = loaders; |
| 64 | + this.ee = ee; |
| 65 | + } |
| 66 | + |
| 67 | + private static <K, V> void mergeMaps(Map<K, List<V>> from, Map<K, List<V>> into) { |
| 68 | + for (Entry<K, List<V>> entry : from.entrySet()) { |
| 69 | + K key = entry.getKey(); |
| 70 | + |
| 71 | + List<V> list = into.get(key); |
| 72 | + if (list == null) { |
| 73 | + list = new ArrayList<>(); |
| 74 | + into.put(key, list); |
| 75 | + } |
| 76 | + |
| 77 | + list.addAll(entry.getValue()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected IStatus run(IProgressMonitor monitor) { |
| 83 | + try { |
| 84 | + |
| 85 | + |
| 86 | + // Load all the capabilities and requirements |
| 87 | + Map<String, List<Capability>> allCaps = new HashMap<>(); |
| 88 | + Map<String, List<RequirementWrapper>> allReqs = new HashMap<>(); |
| 89 | + for (CapReqLoader loader : loaders) { |
| 90 | + try { |
| 91 | + Map<String, List<Capability>> caps = loader.loadCapabilities(); |
| 92 | + mergeMaps(caps, allCaps); |
| 93 | + |
| 94 | + Map<String, List<RequirementWrapper>> reqs = loader.loadRequirements(); |
| 95 | + mergeMaps(reqs, allReqs); |
| 96 | + } catch (Exception e) { |
| 97 | + logger.logError("Error in Bnd resolution analysis.", e); |
| 98 | + } finally { |
| 99 | + IO.close(loader); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Check for resolved requirements |
| 104 | + for (String namespace : allReqs.keySet()) { |
| 105 | + List<RequirementWrapper> rws = allReqs.getOrDefault(namespace, emptyList()); |
| 106 | + List<Capability> candidates = allCaps.getOrDefault(namespace, emptyList()); |
| 107 | + |
| 108 | + List<Capability> javaCandidates = ee == null ? emptyList() |
| 109 | + : ee.getResource() |
| 110 | + .getCapabilities(namespace); |
| 111 | + |
| 112 | + outer: for (RequirementWrapper rw : rws) { |
| 113 | + String filterDirective = rw.requirement.getDirectives() |
| 114 | + .get(Namespace.REQUIREMENT_FILTER_DIRECTIVE); |
| 115 | + if (filterDirective == null) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + Predicate<Capability> predicate = ResourceUtils.filterMatcher(rw.requirement); |
| 119 | + for (Capability cand : candidates) { |
| 120 | + if (predicate.test(cand)) { |
| 121 | + rw.resolved = true; |
| 122 | + continue outer; |
| 123 | + } |
| 124 | + } |
| 125 | + for (Capability cand : javaCandidates) { |
| 126 | + if (predicate.test(cand)) { |
| 127 | + rw.java = true; |
| 128 | + continue outer; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Generate the final results |
| 135 | + // Set<File> resultFiles = builderMap.keySet(); |
| 136 | + // resultFileArray = resultFiles.toArray(new File[0]); |
| 137 | + |
| 138 | + this.requirements = allReqs; |
| 139 | + this.capabilities = allCaps; |
| 140 | + |
| 141 | + // showResults(resultFileArray, importResults, exportResults); |
| 142 | + return Status.OK_STATUS; |
| 143 | + } catch (Exception e) { |
| 144 | + throw new RuntimeException(e); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + public Map<String, List<RequirementWrapper>> getRequirements() { |
| 149 | + return Collections.unmodifiableMap(requirements); |
| 150 | + } |
| 151 | + |
| 152 | + public Map<String, List<Capability>> getCapabilities() { |
| 153 | + return Collections.unmodifiableMap(capabilities); |
| 154 | + } |
| 155 | +} |
0 commit comments