use of org.apache.karaf.features.internal.resolver.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.karaf.features.internal.resolver.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.karaf.features.internal.resolver.ResourceImpl in project karaf by apache.
the class StaxParser method parseResource.
private static ResourceImpl parseResource(URI repositoryUrl, XMLStreamReader reader) {
try {
if (reader.getAttributeCount() > 0) {
throw new IllegalStateException("Unexpected attribute '" + reader.getAttributeLocalName(0) + "'");
}
ResourceImpl resource = new ResourceImpl();
int event;
while ((event = reader.nextTag()) == START_ELEMENT) {
String element = reader.getLocalName();
switch(element) {
case CAPABILITY:
CapabilityImpl cap = parseCapability(reader, resource);
// Resolve relative resource urls now
if (repositoryUrl != null && ContentNamespace.CONTENT_NAMESPACE.equals(cap.getNamespace())) {
Object url = cap.getAttributes().get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE);
if (url instanceof String) {
url = repositoryUrl.resolve(url.toString()).toString();
cap.getAttributes().put(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, url);
}
}
resource.addCapability(cap);
break;
case REQUIREMENT:
resource.addRequirement(parseRequirement(reader, resource));
break;
default:
while ((event = reader.next()) != END_ELEMENT) {
switch(event) {
case START_ELEMENT:
throw new IllegalStateException("Unexpected element '" + reader.getLocalName() + "' inside 'resource' element");
case CHARACTERS:
throw new IllegalStateException("Unexpected text inside 'resource' element");
default:
break;
}
}
break;
}
}
// Sanity check
sanityCheckEndElement(reader, event, RESOURCE);
return resource;
} catch (Exception e) {
Location loc = reader.getLocation();
if (loc != null) {
throw new IllegalStateException("Error while parsing resource at line " + loc.getLineNumber() + " and column " + loc.getColumnNumber(), e);
} else {
throw new IllegalStateException("Error while parsing resource", e);
}
}
}
use of org.apache.karaf.features.internal.resolver.ResourceImpl in project karaf by apache.
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);
}
}
use of org.apache.karaf.features.internal.resolver.ResourceImpl in project karaf by apache.
the class Subsystem method downloadBundles.
@SuppressWarnings("InfiniteLoopStatement")
public void downloadBundles(DownloadManager manager, Set<String> overrides, String featureResolutionRange, final String serviceRequirements, RepositoryManager repos) throws Exception {
for (Subsystem child : children) {
child.downloadBundles(manager, overrides, featureResolutionRange, serviceRequirements, repos);
}
final Map<String, ResourceImpl> bundles = new ConcurrentHashMap<>();
final Downloader downloader = manager.createDownloader();
final Map<BundleInfo, Conditional> infos = new HashMap<>();
if (feature != null) {
for (Conditional cond : feature.getConditional()) {
for (final BundleInfo bi : cond.getBundles()) {
infos.put(bi, cond);
}
}
for (BundleInfo bi : feature.getBundles()) {
infos.put(bi, null);
}
}
boolean removeServiceRequirements;
if (FeaturesService.SERVICE_REQUIREMENTS_DISABLE.equals(serviceRequirements)) {
removeServiceRequirements = true;
} else if (feature != null && FeaturesService.SERVICE_REQUIREMENTS_DEFAULT.equals(serviceRequirements)) {
removeServiceRequirements = FeaturesNamespaces.URI_1_0_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_1_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_2_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_2_1.equals(feature.getNamespace());
} else {
removeServiceRequirements = false;
}
for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
final BundleInfo bi = entry.getKey();
final String loc = bi.getLocation();
downloader.download(loc, provider -> {
bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
});
}
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));
});
}
for (String override : overrides) {
final String loc = Overrides.extractUrl(override);
downloader.download(loc, provider -> {
bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
});
}
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();
Overrides.override(bundles, overrides);
if (feature != null) {
// Add conditionals
Map<Conditional, Resource> resConds = new HashMap<>();
for (Conditional cond : feature.getConditional()) {
FeatureResource resCond = FeatureResource.build(feature, cond, featureResolutionRange, bundles);
addIdentityRequirement(this, resCond, false);
addIdentityRequirement(resCond, this, true);
installable.add(resCond);
resConds.put(cond, resCond);
}
// Add features
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 (bi.isDependency()) {
addDependency(res, false, bi.isStart(), sl);
} else {
doAddDependency(res, cond == null, bi.isStart(), sl);
}
if (cond != null) {
addIdentityRequirement(res, resConds.get(cond), true);
}
}
for (Library library : feature.getLibraries()) {
if (library.isExport()) {
final String loc = library.getLocation();
ResourceImpl res = bundles.get(loc);
addDependency(res, false, false, 0);
}
}
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);
}
}
}
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"));
int startLevel = 0;
try {
startLevel = Integer.parseInt(bundle.getAttribute("start-level"));
} catch (NumberFormatException e) {
// Ignore
}
if (dependency) {
addDependency(bundles.get(loc), false, start, startLevel);
} else {
doAddDependency(bundles.get(loc), true, start, startLevel);
addIdentityRequirement(this, bundles.get(loc));
}
}
// Compute dependencies
for (DependencyInfo info : dependencies.values()) {
installable.add(info.resource);
addIdentityRequirement(info.resource, this, info.mandatory);
}
}
Aggregations