use of org.apache.felix.utils.resource.ResourceImpl in project karaf by apache.
the class OfflineResolver method readRepository.
private static Repository readRepository(Object repository) throws BundleException {
List<Resource> resources = new ArrayList<>();
Collection<Map<String, List<String>>> metadatas;
if (repository instanceof Map) {
metadatas = ((Map<String, Map<String, List<String>>>) repository).values();
} else {
metadatas = (Collection<Map<String, List<String>>>) repository;
}
for (Map<String, List<String>> metadata : metadatas) {
ResourceImpl res = new ResourceImpl();
for (String cap : metadata.get("capabilities")) {
res.addCapabilities(ResourceBuilder.parseCapability(res, cap));
}
if (metadata.containsKey("requirements")) {
for (String req : metadata.get("requirements")) {
res.addRequirements(ResourceBuilder.parseRequirement(res, req));
}
}
resources.add(res);
}
return new BaseRepository(resources);
}
use of org.apache.felix.utils.resource.ResourceImpl in project karaf by apache.
the class Subsystem method requireFeature.
public void requireFeature(String name, String range, boolean mandatory) {
if (mandatory) {
ResourceUtils.addIdentityRequirement(this, name, TYPE_FEATURE, range);
} else {
ResourceImpl res = new ResourceImpl();
ResourceUtils.addIdentityRequirement(res, name, TYPE_FEATURE, range, false);
dependentFeatures.addAll(res.getRequirements(null));
}
}
use of org.apache.felix.utils.resource.ResourceImpl in project karaf by apache.
the class Subsystem method cloneResource.
ResourceImpl cloneResource(Resource resource) {
ResourceImpl res = new ResourceImpl();
for (Capability cap : resource.getCapabilities(null)) {
res.addCapability(new CapabilityImpl(res, cap.getNamespace(), new StringArrayMap<>(cap.getDirectives()), new StringArrayMap<>(cap.getAttributes())));
}
for (Requirement req : resource.getRequirements(null)) {
SimpleFilter sf;
if (req instanceof RequirementImpl) {
sf = ((RequirementImpl) req).getFilter();
} else if (req.getDirectives().containsKey(REQUIREMENT_FILTER_DIRECTIVE)) {
sf = SimpleFilter.parse(req.getDirectives().get(REQUIREMENT_FILTER_DIRECTIVE));
} else {
sf = SimpleFilter.convert(req.getAttributes());
}
res.addRequirement(new RequirementImpl(res, req.getNamespace(), new StringArrayMap<>(req.getDirectives()), new StringArrayMap<>(req.getAttributes()), sf));
}
return res;
}
use of org.apache.felix.utils.resource.ResourceImpl in project karaf by apache.
the class Subsystem method downloadBundles.
/**
* Downloads bundles for all the features in current and child subsystems. But also collects bundles
* as {@link DependencyInfo}.
*
* @param manager The {@link DownloadManager} to use.
* @param featureResolutionRange The feature resolution range to use.
* @param serviceRequirements The {@link FeaturesService.ServiceRequirementsBehavior} behavior to use.
* @param repos The {@link RepositoryManager} to use.
* @param callback The {@link SubsystemResolverCallback} to use.
*/
@SuppressWarnings("InfiniteLoopStatement")
public void downloadBundles(DownloadManager manager, String featureResolutionRange, final FeaturesService.ServiceRequirementsBehavior serviceRequirements, RepositoryManager repos, SubsystemResolverCallback callback) throws Exception {
for (Subsystem child : children) {
child.downloadBundles(manager, featureResolutionRange, serviceRequirements, repos, callback);
}
// collect BundleInfos for given feature - both direct <feature>/<bundle>s and <feature>/<conditional>/<bundle>s
final Map<BundleInfo, Conditional> infos = new HashMap<>();
final Downloader downloader = manager.createDownloader();
if (feature != null) {
for (Conditional cond : feature.getConditional()) {
if (!cond.isBlacklisted()) {
for (final BundleInfo bi : cond.getBundles()) {
// bundles from conditional features will be added as non-mandatory requirements
infos.put(bi, cond);
}
}
}
for (BundleInfo bi : feature.getBundles()) {
infos.put(bi, null);
}
}
// infos.keySet().removeIf(Blacklisting::isBlacklisted);
for (Iterator<BundleInfo> iterator = infos.keySet().iterator(); iterator.hasNext(); ) {
BundleInfo bi = iterator.next();
if (bi.isBlacklisted()) {
iterator.remove();
if (callback != null) {
callback.bundleBlacklisted(bi);
}
}
}
// all downloaded bundles
final Map<String, ResourceImpl> bundles = new ConcurrentHashMap<>();
// resources for locations that were overriden in OSGi mode - to check whether the override should actually
// take place, by checking resource's headers
final Map<String, ResourceImpl> overrides = new ConcurrentHashMap<>();
boolean removeServiceRequirements = serviceRequirementsBehavior(feature, serviceRequirements);
// download collected BundleInfo locations
for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
final BundleInfo bi = entry.getKey();
final String loc = bi.getLocation();
downloader.download(loc, provider -> {
// always download location (could be overriden)
ResourceImpl resource = createResource(loc, getMetadata(provider), removeServiceRequirements);
bundles.put(loc, resource);
if (bi.isOverriden() == BundleInfo.BundleOverrideMode.OSGI) {
// also download original from original bundle URI to check if we should override by comparing
// symbolic name - requires MANIFEST.MF header access. If there should be no override, we'll get
// back to original URI
downloader.download(bi.getOriginalLocation(), provider2 -> {
ResourceImpl originalResource = createResource(bi.getOriginalLocation(), getMetadata(provider2), removeServiceRequirements);
bundles.put(bi.getOriginalLocation(), originalResource);
// an entry in overrides map means that given location was overriden
overrides.put(loc, originalResource);
});
}
});
}
// download direct bundle: requirements - without consulting overrides
for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
final String loc = bundle.getName();
downloader.download(loc, provider -> bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements)));
}
// resolution process
if (feature != null) {
for (Library library : feature.getLibraries()) {
if (library.isExport()) {
final String loc = library.getLocation();
downloader.download(loc, provider -> bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements)));
}
}
}
downloader.await();
// opposite to what we had before. Currently bundles are already overriden at model level, but
// as we finally have access to headers, we can compare symbolic names and if override mode is OSGi, then
// we can restore original resource if there should be no override.
Overrides.override(bundles, overrides);
if (feature != null) {
// Add conditionals
Map<Conditional, Resource> resConds = new HashMap<>();
for (Conditional cond : feature.getConditional()) {
if (cond.isBlacklisted()) {
continue;
}
FeatureResource resCond = FeatureResource.build(feature, cond, featureResolutionRange, bundles);
// feature's subsystem will optionally require conditional feature resource
addIdentityRequirement(this, resCond, false);
// but it's a mandatory requirement in other way
addIdentityRequirement(resCond, this, true);
installable.add(resCond);
resConds.put(cond, resCond);
}
// Add features and make it require given subsystem that represents logical feature requirement
FeatureResource resFeature = FeatureResource.build(feature, featureResolutionRange, bundles);
addIdentityRequirement(resFeature, this);
installable.add(resFeature);
// Add dependencies
for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
final BundleInfo bi = entry.getKey();
final String loc = bi.getLocation();
final Conditional cond = entry.getValue();
ResourceImpl res = bundles.get(loc);
int sl = bi.getStartLevel() <= 0 ? feature.getStartLevel() : bi.getStartLevel();
if (cond != null) {
// bundle of conditional feature will have mandatory requirement on it
addIdentityRequirement(res, resConds.get(cond), true);
}
boolean mandatory = !bi.isDependency() && cond == null;
if (bi.isDependency()) {
addDependency(res, mandatory, bi.isStart(), sl, bi.isBlacklisted());
} else {
doAddDependency(res, mandatory, bi.isStart(), sl, bi.isBlacklisted());
}
}
for (Library library : feature.getLibraries()) {
if (library.isExport()) {
final String loc = library.getLocation();
ResourceImpl res = bundles.get(loc);
addDependency(res, false, false, 0, false);
}
}
for (String uri : feature.getResourceRepositories()) {
BaseRepository repo = repos.getRepository(feature.getRepositoryUrl(), uri);
for (Resource resource : repo.getResources()) {
ResourceImpl res = cloneResource(resource);
addDependency(res, false, true, 0, false);
}
}
}
for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
final String loc = bundle.getName();
boolean dependency = Boolean.parseBoolean(bundle.getAttribute("dependency"));
boolean start = bundle.getAttribute("start") == null || Boolean.parseBoolean(bundle.getAttribute("start"));
boolean blacklisted = bundle.getAttribute("blacklisted") != null && Boolean.parseBoolean(bundle.getAttribute("blacklisted"));
int startLevel = 0;
try {
startLevel = Integer.parseInt(bundle.getAttribute("start-level"));
} catch (NumberFormatException e) {
// Ignore
}
if (dependency) {
addDependency(bundles.get(loc), false, start, startLevel, blacklisted);
} else {
doAddDependency(bundles.get(loc), true, start, startLevel, blacklisted);
// non dependency bundle will be added as osgi.identity req on type=osgi.bundle
addIdentityRequirement(this, bundles.get(loc));
}
}
// Compute dependencies
for (DependencyInfo info : dependencies.values()) {
installable.add(info.resource);
// bundle resource will have a requirement on its feature's subsystem too
// when bundle is declared with dependency="true", it will have a requirement on its region's subsystem
addIdentityRequirement(info.resource, this, info.mandatory);
}
}
use of org.apache.felix.utils.resource.ResourceImpl in project karaf by apache.
the class SubsystemResolver method prepare.
@Override
public void prepare(Map<String, List<Feature>> allFeatures, Map<String, Set<String>> requirements, Map<String, Set<BundleRevision>> system) throws Exception {
// related requirements. Each region's subsystem will also _require_ all child subsystems
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++) {
String childName = String.join("/", Arrays.copyOfRange(parts, 0, i + 1));
ss = getOrCreateChild(ss, childName, parts[i]);
}
for (String requirement : entry.getValue()) {
// #1a. each "[feature:]*" and "requirement:*" requirements are added directly as resource requirements:
// - feature: ns=osgi.identity, 'osgi.identity=f1; type=karaf.feature; filter:="(&(osgi.identity=f1)(type=karaf.feature))"'
// - requirement: as-is
// - bundle: added only as downloadable bundle - used only by assembly builder
ss.require(requirement);
}
}
if (root == null) {
return;
}
// #2. Pre-resolve
// - for each region's subsystem X, feature requirements are changed into child subsystems of X
// - for each feature, any dependant features (<feature>/<feature>) will become non-mandatory (why?)
// child subsystem of the same region's subsystem as original feature
// - for each feature, any conditional (<feature>/<conditional>) will become mandatory (why?)
// child subsystem of the original feature's subsystem
root.build(allFeatures);
// #3. Add system resources
// - from all unmanaged bundles we'll gather Provide-Capability headers' clauses in "osgi.service" namespace
// and Export-Service headers
// - these capabilities will be added to "dummy" Resource added as o.a.k.features.internal.region.Subsystem.installable
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