use of org.osgi.resource.Wire in project bnd by bndtools.
the class ResolveProcess method invertWirings.
/**
* Inverts the wiring map from the resolver. Whereas the resolver returns a
* map of resources and the list of wirings FROM each resource, we want to
* know the list of wirings TO that resource. This is in order to show the
* user the reasons for each resource being present in the result.
*/
private static Map<Resource, List<Wire>> invertWirings(Map<Resource, ? extends Collection<Wire>> wirings, AbstractResolveContext rc) {
Map<Resource, List<Wire>> inverted = new HashMap<Resource, List<Wire>>();
for (Entry<Resource, ? extends Collection<Wire>> entry : wirings.entrySet()) {
Resource requirer = entry.getKey();
for (Wire wire : entry.getValue()) {
Resource provider = findResolvedProvider(wire, wirings.keySet(), rc);
// same
if (provider == requirer)
continue;
List<Wire> incoming = inverted.get(provider);
if (incoming == null) {
incoming = new LinkedList<Wire>();
inverted.put(provider, incoming);
}
incoming.add(wire);
}
}
return inverted;
}
use of org.osgi.resource.Wire in project bnd by bndtools.
the class ResolveProcess method resolveRequired.
public Map<Resource, List<Wire>> resolveRequired(Processor properties, Project project, Registry plugins, Resolver resolver, Collection<ResolutionCallback> callbacks, LogService log) throws ResolutionException {
required = new HashMap<Resource, List<Wire>>();
optional = new HashMap<Resource, List<Wire>>();
BndrunResolveContext rc = new BndrunResolveContext(properties, project, plugins, log);
rc.addCallbacks(callbacks);
// 1. Resolve initial requirements
Map<Resource, List<Wire>> wirings;
try {
wirings = resolver.resolve(rc);
} catch (ResolutionException re) {
throw augment(rc, re);
}
// 2. Save initial requirement resolution
Pair<Resource, List<Wire>> initialRequirement = null;
for (Map.Entry<Resource, List<Wire>> wiring : wirings.entrySet()) {
if (rc.getInputResource() == wiring.getKey()) {
initialRequirement = new Pair<Resource, List<Wire>>(wiring.getKey(), wiring.getValue());
break;
}
}
// 3. Save the resolved root resources
final List<Resource> resources = new ArrayList<Resource>();
for (Resource r : rc.getMandatoryResources()) {
reqs: for (Requirement req : r.getRequirements(null)) {
for (Resource found : wirings.keySet()) {
String filterStr = req.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
try {
org.osgi.framework.Filter filter = filterStr != null ? org.osgi.framework.FrameworkUtil.createFilter(filterStr) : null;
for (Capability c : found.getCapabilities(req.getNamespace())) {
if (filter != null && filter.matches(c.getAttributes())) {
resources.add(found);
continue reqs;
}
}
} catch (InvalidSyntaxException e) {
}
}
}
}
// 4. Add any 'osgi.wiring.bundle' requirements
List<Resource> wiredBundles = new ArrayList<Resource>();
for (Resource resource : resources) {
addWiredBundle(wirings, resource, wiredBundles);
}
for (Resource resource : wiredBundles) {
if (!resources.contains(resource)) {
resources.add(resource);
}
}
final Map<Resource, List<Wire>> discoveredOptional = new LinkedHashMap<Resource, List<Wire>>();
// 5. Resolve the rest
BndrunResolveContext rc2 = new BndrunResolveContext(properties, project, plugins, log) {
@Override
public Collection<Resource> getMandatoryResources() {
return resources;
}
@Override
public boolean isInputResource(Resource resource) {
for (Resource r : resources) {
if (GenericResolveContext.resourceIdentityEquals(r, resource)) {
return true;
}
}
return false;
}
@Override
public List<Capability> findProviders(Requirement requirement) {
List<Capability> toReturn = super.findProviders(requirement);
if (toReturn.isEmpty() && isEffective(requirement) && RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(REQUIREMENT_RESOLUTION_DIRECTIVE))) {
for (Capability cap : findProvidersFromRepositories(requirement, new LinkedHashSet<Capability>())) {
Resource optionalRes = cap.getResource();
List<Wire> list = discoveredOptional.get(optionalRes);
if (list == null) {
list = new ArrayList<>();
discoveredOptional.put(optionalRes, list);
}
WireImpl candidateWire = new WireImpl(cap, requirement);
if (!list.contains(candidateWire))
list.add(candidateWire);
}
}
return toReturn;
}
};
rc2.addCallbacks(callbacks);
try {
wirings = resolver.resolve(rc2);
} catch (ResolutionException re) {
throw augment(rc2, re);
}
if (initialRequirement != null) {
wirings.put(initialRequirement.getFirst(), initialRequirement.getSecond());
}
Map<Resource, List<Wire>> result = invertWirings(wirings, rc2);
removeFrameworkAndInputResources(result, rc2);
required.putAll(result);
optional = tidyUpOptional(wirings, discoveredOptional, log);
return result;
}
use of org.osgi.resource.Wire in project bnd by bndtools.
the class ResolveProcess method addWiredBundle.
private void addWiredBundle(Map<Resource, List<Wire>> wirings, Resource resource, List<Resource> result) {
List<Requirement> reqs = resource.getRequirements(BUNDLE_NAMESPACE);
for (Requirement req : reqs) {
List<Wire> wrs = wirings.get(resource);
for (Wire w : wrs) {
if (w.getRequirement().equals(req)) {
Resource res = w.getProvider();
if (res != null) {
if (!result.contains(res)) {
result.add(res);
addWiredBundle(wirings, res, result);
}
}
}
}
}
}
use of org.osgi.resource.Wire in project felix by apache.
the class ResolveContextImpl method getSubstitutionWires.
@Override
public List<Wire> getSubstitutionWires(Wiring wiring) {
// TODO: this is calculating information that probably has been calculated
// already or at least could be calculated quicker taking into account the
// current state. We need to revisit this.
Set<String> exportNames = new HashSet<String>();
for (Capability cap : wiring.getResource().getCapabilities(null)) {
if (PackageNamespace.PACKAGE_NAMESPACE.equals(cap.getNamespace())) {
exportNames.add((String) cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
}
}
// Add fragment exports
for (Wire wire : wiring.getProvidedResourceWires(null)) {
if (HostNamespace.HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
for (Capability cap : wire.getRequirement().getResource().getCapabilities(null)) {
if (PackageNamespace.PACKAGE_NAMESPACE.equals(cap.getNamespace())) {
exportNames.add((String) cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
}
}
}
}
List<Wire> substitutionWires = new ArrayList<Wire>();
for (Wire wire : wiring.getRequiredResourceWires(null)) {
if (PackageNamespace.PACKAGE_NAMESPACE.equals(wire.getCapability().getNamespace())) {
if (exportNames.contains(wire.getCapability().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))) {
substitutionWires.add(wire);
}
}
}
return substitutionWires;
}
use of org.osgi.resource.Wire in project felix by apache.
the class StatefulResolver method fireResolvedEvents.
private void fireResolvedEvents(Map<Resource, List<Wire>> wireMap) {
if (wireMap != null) {
Iterator<Entry<Resource, List<Wire>>> iter = wireMap.entrySet().iterator();
// Iterate over the map to fire necessary RESOLVED events.
while (iter.hasNext()) {
Entry<Resource, List<Wire>> entry = iter.next();
Resource resource = entry.getKey();
if (!(resource instanceof BundleRevision))
continue;
BundleRevision revision = (BundleRevision) resource;
// Fire RESOLVED events for all fragments.
List<BundleRevision> fragments = Util.getFragments(revision.getWiring());
for (int i = 0; i < fragments.size(); i++) {
m_felix.fireBundleEvent(BundleEvent.RESOLVED, fragments.get(i).getBundle());
}
m_felix.fireBundleEvent(BundleEvent.RESOLVED, revision.getBundle());
}
}
}
Aggregations