use of org.osgi.framework.wiring.BundleCapability in project felix by apache.
the class Felix method getExportedPackages.
/**
* Adds any current active exported packages from the specified bundle
* to the passed in list.
* @param bundle The bundle from which to retrieve exported packages.
* @param list The list to which the exported packages are added
*/
private void getExportedPackages(Bundle bundle, List list) {
// to get all exports.
for (BundleRevision br : bundle.adapt(BundleRevisions.class).getRevisions()) {
List<BundleCapability> caps = (br.getWiring() == null) ? br.getDeclaredCapabilities(null) : br.getWiring().getCapabilities(null);
if ((caps != null) && (caps.size() > 0)) {
for (BundleCapability cap : caps) {
if (cap.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE)) {
String pkgName = (String) cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE);
list.add(new ExportedPackageImpl(this, (BundleImpl) bundle, br, cap));
}
}
}
}
}
use of org.osgi.framework.wiring.BundleCapability in project felix by apache.
the class StatefulResolver method selectSingletonsUsingHooks.
/*
* Groups singletons based on resolver hook filtering and then selects
* the singleton from each group with the highest version that is in
* the resolver hook whitelist. No selection is made if a group already
* has a resolved singleton in it.
*/
private void selectSingletonsUsingHooks(ResolverHookRecord record) throws BundleException {
// Convert singleton bundle revision map into a map using
// bundle capabilities instead, since this is what the resolver
// hooks require.
Map<BundleCapability, Collection<BundleCapability>> allCollisions = new HashMap<BundleCapability, Collection<BundleCapability>>();
for (Entry<String, List<BundleRevision>> entry : m_singletons.entrySet()) {
Collection<BundleCapability> bundleCaps = new ArrayList<BundleCapability>();
for (BundleRevision br : entry.getValue()) {
List<BundleCapability> caps = br.getDeclaredCapabilities(BundleRevision.BUNDLE_NAMESPACE);
if (!caps.isEmpty()) {
bundleCaps.add(caps.get(0));
}
}
for (BundleCapability bc : bundleCaps) {
Collection<BundleCapability> capCopy = new ShrinkableCollection<BundleCapability>(new ArrayList<BundleCapability>(bundleCaps));
capCopy.remove(bc);
allCollisions.put(bc, capCopy);
}
}
// Invoke hooks to allow them to filter singleton collisions.
for (ResolverHook hook : record.getResolverHooks()) {
for (Entry<BundleCapability, Collection<BundleCapability>> entry : allCollisions.entrySet()) {
try {
Felix.m_secureAction.invokeResolverHookSingleton(hook, entry.getKey(), entry.getValue());
} catch (Throwable ex) {
throw new BundleException("Resolver hook exception: " + ex.getMessage(), BundleException.REJECTED_BY_HOOK, ex);
}
}
}
// Create groups according to how the resolver hooks filtered the
// collisions.
List<List<BundleRevision>> groups = new ArrayList<List<BundleRevision>>();
while (!allCollisions.isEmpty()) {
BundleCapability target = allCollisions.entrySet().iterator().next().getKey();
groups.add(groupSingletons(allCollisions, target, new ArrayList<BundleRevision>()));
}
// Now select the singletons available for this resolve operation.
for (List<BundleRevision> group : groups) {
selectSingleton(record, group);
}
}
use of org.osgi.framework.wiring.BundleCapability in project felix by apache.
the class StatefulResolver method isAllowedDynamicImport.
// This method duplicates a lot of logic from:
// ResolverImpl.getDynamicImportCandidates()
// This is only a rough check since it doesn't include resolver hooks.
boolean isAllowedDynamicImport(BundleRevision revision, String pkgName) {
// package be dynamically imported.
if ((revision.getWiring() == null) || pkgName.length() == 0) {
return false;
}
// If the revision doesn't have dynamic imports, then just return
// immediately.
List<BundleRequirement> dynamics = Util.getDynamicRequirements(revision.getWiring().getRequirements(null));
if ((dynamics == null) || dynamics.isEmpty()) {
return false;
}
// attempt to dynamically import it.
for (BundleCapability cap : revision.getWiring().getCapabilities(null)) {
if (cap.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE) && cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).equals(pkgName)) {
return false;
}
}
// we cannot dynamically import it.
if (((BundleWiringImpl) revision.getWiring()).hasPackageSource(pkgName)) {
return false;
}
// Loop through the importer's dynamic requirements to determine if
// there is a matching one for the package from which we want to
// load a class.
Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, Collections.EMPTY_MAP, attrs);
List<BundleCapability> candidates = findProviders(req, false);
// Try to find a dynamic requirement that matches the capabilities.
BundleRequirementImpl dynReq = null;
for (int dynIdx = 0; (candidates.size() > 0) && (dynReq == null) && (dynIdx < dynamics.size()); dynIdx++) {
for (Iterator<BundleCapability> itCand = candidates.iterator(); (dynReq == null) && itCand.hasNext(); ) {
Capability cap = itCand.next();
if (CapabilitySet.matches(cap, ((BundleRequirementImpl) dynamics.get(dynIdx)).getFilter())) {
dynReq = (BundleRequirementImpl) dynamics.get(dynIdx);
}
}
}
// any candidates that do not match it.
if (dynReq != null) {
for (Iterator<BundleCapability> itCand = candidates.iterator(); itCand.hasNext(); ) {
Capability cap = itCand.next();
if (!CapabilitySet.matches(cap, dynReq.getFilter())) {
itCand.remove();
}
}
} else {
candidates.clear();
}
return !candidates.isEmpty();
}
use of org.osgi.framework.wiring.BundleCapability in project felix by apache.
the class StatefulResolver method indexCapabilities.
private synchronized void indexCapabilities(BundleRevision br) {
List<BundleCapability> caps = (Util.isFragment(br) || (br.getWiring() == null)) ? br.getDeclaredCapabilities(null) : br.getWiring().getCapabilities(null);
if (caps != null) {
for (BundleCapability cap : caps) {
// attached hosts for fragments.
if (cap.getRevision() == br) {
CapabilitySet capSet = m_capSets.get(cap.getNamespace());
if (capSet == null) {
capSet = new CapabilitySet(null, true);
m_capSets.put(cap.getNamespace(), capSet);
}
capSet.addCapability(cap);
}
}
}
}
use of org.osgi.framework.wiring.BundleCapability in project felix by apache.
the class StatefulResolver method groupSingletons.
private List<BundleRevision> groupSingletons(Map<BundleCapability, Collection<BundleCapability>> allCollisions, BundleCapability target, List<BundleRevision> group) {
if (!group.contains(target.getRevision())) {
// Add the target since it is implicitly part of the group.
group.add(target.getRevision());
// Recursively add the revisions of any singleton's in the
// target's collisions.
Collection<BundleCapability> collisions = allCollisions.remove(target);
for (BundleCapability collision : collisions) {
groupSingletons(allCollisions, collision, group);
}
// Need to check the values of other collisions for this target
// and add those to the target's group too, since collisions are
// treated as two-way relationships. Repeat until there are no
// collision groups left that contain the target capability.
boolean repeat;
do {
repeat = false;
for (Entry<BundleCapability, Collection<BundleCapability>> entry : allCollisions.entrySet()) {
if (entry.getValue().contains(target)) {
repeat = true;
groupSingletons(allCollisions, entry.getKey(), group);
break;
}
}
} while (repeat);
}
return group;
}
Aggregations