use of io.fabric8.agent.model.Capability in project fabric8 by jboss-fuse.
the class RequirementSort method collectDependencies.
@SuppressWarnings("unchecked")
private static <T extends Resource> Set<T> collectDependencies(T resource, CapabilitySet capSet) {
Set<T> result = new LinkedHashSet<>();
for (Requirement requirement : resource.getRequirements(null)) {
String filter = requirement.getDirectives().get(Constants.FILTER_DIRECTIVE);
SimpleFilter sf = (filter != null) ? SimpleFilter.parse(filter) : new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
for (Capability cap : capSet.match(sf, true)) {
result.add((T) cap.getResource());
}
}
return result;
}
use of io.fabric8.agent.model.Capability in project fabric8 by jboss-fuse.
the class FeatureResource method build.
public static FeatureResource build(Feature feature, String featureRange, Map<String, ? extends Resource> locToRes) throws BundleException {
FeatureResource resource = new FeatureResource(feature);
for (BundleInfo info : feature.getBundles()) {
if (!info.isDependency()) {
Resource res = locToRes.get(info.getLocation());
if (res == null) {
throw new IllegalStateException("Resource not found for url " + info.getLocation());
}
addIdentityRequirement(resource, res);
}
}
for (Dependency dep : feature.getDependencies()) {
if (!dep.isDependency()) {
addDependency(resource, dep, featureRange);
}
}
for (Capability cap : feature.getCapabilities()) {
resource.addCapabilities(ResourceBuilder.parseCapability(resource, cap.getValue()));
}
for (Requirement req : feature.getRequirements()) {
resource.addRequirements(ResourceBuilder.parseRequirement(resource, req.getValue()));
}
return resource;
}
use of io.fabric8.agent.model.Capability in project fabric8 by jboss-fuse.
the class SubsystemResolveContext method findProviders.
@Override
public List<Capability> findProviders(Requirement requirement) {
List<Capability> caps = new ArrayList<Capability>();
Region requirerRegion = getRegion(requirement.getResource());
if (requirerRegion != null) {
Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
if (res != null && !res.isEmpty()) {
caps.addAll(res);
} else if (globalRepository != null) {
// Only bring in external resources for non optional requirements
if (!RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(RESOLUTION_DIRECTIVE))) {
resMap = globalRepository.findProviders(Collections.singleton(requirement));
res = resMap != null ? resMap.get(requirement) : null;
if (res != null && !res.isEmpty()) {
caps.addAll(res);
}
}
}
// Use the digraph to prune non visible capabilities
Visitor visitor = new Visitor(caps);
requirerRegion.visitSubgraph(visitor);
Collection<Capability> allowed = visitor.getAllowed();
caps.retainAll(allowed);
// the parent one
if (caps.size() > 1) {
Map<String, Resource> providers = new HashMap<String, Resource>();
for (Capability cap : caps) {
Resource resource = cap.getResource();
String id = getSymbolicName(resource) + "|" + getVersion(resource);
Resource prev = providers.get(id);
if (prev != null && prev != resource) {
Region r1 = getRegion(prev);
Region r2 = getRegion(resource);
boolean r2canSeeR1 = isResourceVisibleFromRegion(prev, r2);
boolean r1canSeeR2 = isResourceVisibleFromRegion(resource, r1);
if (r1canSeeR2 && r2canSeeR1) {
// r1 and r2 can see each other
int reqDiff = prev.getRequirements(null).size() - resource.getRequirements(null).size();
if (reqDiff == 0) {
String r1Name = getRegion(prev).getName();
String r2Name = getRegion(resource).getName();
int c = r1Name.compareTo(r2Name);
if (c == 0) {
// One of the resource has to be a bundle, use that one
c = (prev instanceof BundleRevision) ? -1 : +1;
}
resource = c < 0 ? prev : resource;
} else {
// one of the resource has less requirements, so use this one
// This can be the case when one resource has conditionals, which adds further
// requirements to the condition feature.
resource = reqDiff < 0 ? prev : resource;
}
} else {
// only one region can see the other, grab the correct
resource = r1canSeeR2 ? prev : resource;
}
}
providers.put(id, resource);
}
for (Iterator<Capability> it = caps.iterator(); it.hasNext(); ) {
Capability cap = it.next();
if (!providers.values().contains(cap.getResource())) {
it.remove();
}
}
}
// Sort caps
Collections.sort(caps, candidateComparator);
}
return caps;
}
use of io.fabric8.agent.model.Capability in project fabric8 by jboss-fuse.
the class SubsystemResolver method associateFragments.
private void associateFragments() {
SimpleFilter sf = createFilter(IDENTITY_NAMESPACE, "*", CAPABILITY_TYPE_ATTRIBUTE, TYPE_SUBSYSTEM);
for (Map.Entry<Resource, List<Wire>> entry : wiring.entrySet()) {
final Resource resource = entry.getKey();
final Requirement requirement = getSubsystemRequirement(resource);
if (isFragment(resource)) {
List<Wire> wires = entry.getValue();
final Resource host = wires.get(0).getProvider();
final Wire wire = findMatchingWire(sf, wiring.get(host));
if (wire != null) {
wires.add(new Wire() {
@Override
public Capability getCapability() {
return wire.getCapability();
}
@Override
public Requirement getRequirement() {
return requirement;
}
@Override
public Resource getProvider() {
return wire.getProvider();
}
@Override
public Resource getRequirer() {
return resource;
}
});
}
}
}
}
use of io.fabric8.agent.model.Capability in project fabric8 by jboss-fuse.
the class SubsystemResolver method prepare.
public void prepare(Collection<Feature> allFeatures, Map<String, Set<String>> requirements, Map<String, Set<BundleRevision>> system) throws Exception {
// Build subsystems on the fly
for (Map.Entry<String, Set<String>> entry : requirements.entrySet()) {
String[] parts = entry.getKey().split("/");
if (root == null) {
root = new Subsystem(parts[0]);
} else if (!root.getName().equals(parts[0])) {
throw new IllegalArgumentException("Can not use multiple roots: " + root.getName() + ", " + parts[0]);
}
Subsystem ss = root;
for (int i = 1; i < parts.length; i++) {
ss = getOrCreateChild(ss, parts[i]);
}
for (String requirement : entry.getValue()) {
ss.require(requirement);
}
}
if (root == null) {
return;
}
// Pre-resolve
root.build(allFeatures);
// Add system resources
BundleRevision sysBundleRev = null;
boolean hasEeCap = false;
for (Map.Entry<String, Set<BundleRevision>> entry : system.entrySet()) {
Subsystem ss = null;
String[] parts = entry.getKey().split("/");
String path = parts[0];
if (path.equals(root.getName())) {
ss = root;
}
for (int i = 1; ss != null && i < parts.length; i++) {
path += "/" + parts[i];
ss = ss.getChild(path);
}
if (ss != null) {
ResourceImpl dummy = new ResourceImpl("dummy", "dummy", Version.emptyVersion);
for (BundleRevision res : entry.getValue()) {
// We need to explicitely provide service capabilities for bundles
// We use both actual services and services declared from the headers
// TODO: use actual services
Map<String, String> headers = new DictionaryAsMap<>(res.getBundle().getHeaders());
Resource tmp = ResourceBuilder.build(res.getBundle().getLocation(), headers);
for (Capability cap : tmp.getCapabilities(ServiceNamespace.SERVICE_NAMESPACE)) {
dummy.addCapability(new CapabilityImpl(dummy, cap.getNamespace(), cap.getDirectives(), cap.getAttributes()));
}
ss.addSystemResource(res);
for (Capability cap : res.getCapabilities(null)) {
hasEeCap |= cap.getNamespace().equals(EXECUTION_ENVIRONMENT_NAMESPACE);
}
if (res.getBundle().getBundleId() == 0) {
sysBundleRev = res;
}
}
ss.addSystemResource(dummy);
}
}
// Under Equinox, the osgi.ee capabilities are not provided by the system bundle
if (!hasEeCap && sysBundleRev != null) {
String provideCaps = sysBundleRev.getBundle().getHeaders().get(PROVIDE_CAPABILITY);
environmentResource = new ResourceImpl("environment", "karaf.environment", Version.emptyVersion);
environmentResource.addCapabilities(ResourceBuilder.parseCapability(environmentResource, provideCaps));
root.addSystemResource(environmentResource);
}
}
Aggregations