use of org.osgi.framework.Bundle in project karaf by apache.
the class Deployer method computeBundlesToRefresh.
private static void computeBundlesToRefresh(Map<Bundle, String> toRefresh, Collection<Bundle> bundles, Map<Resource, Bundle> resources, Map<Resource, List<Wire>> resolution) {
// Compute the new list of fragments
Map<Bundle, Set<Resource>> newFragments = new HashMap<>();
for (Bundle bundle : bundles) {
newFragments.put(bundle, new HashSet<>());
}
if (resolution != null) {
for (Resource res : resolution.keySet()) {
for (Wire wire : resolution.get(res)) {
if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
Bundle bundle;
if (wire.getProvider() instanceof BundleRevision) {
bundle = ((BundleRevision) wire.getProvider()).getBundle();
} else {
bundle = resources.get(wire.getProvider());
}
if (bundle != null) {
Bundle b = resources.get(wire.getRequirer());
Resource r = b != null ? b.adapt(BundleRevision.class) : wire.getRequirer();
newFragments.get(bundle).add(r);
}
}
}
}
}
// Main loop
int size;
Map<Bundle, Resource> bndToRes = new HashMap<>();
for (Map.Entry<Resource, Bundle> entry : resources.entrySet()) {
bndToRes.put(entry.getValue(), entry.getKey());
}
do {
size = toRefresh.size();
main: for (Bundle bundle : bundles) {
Resource resource = bndToRes.get(bundle);
// This bundle is not managed
if (resource == null) {
resource = bundle.adapt(BundleRevision.class);
}
// Continue if we already know about this bundle
if (toRefresh.containsKey(bundle)) {
continue;
}
// Ignore non resolved bundle
BundleWiring wiring = bundle.adapt(BundleWiring.class);
if (wiring == null) {
continue;
}
// Ignore bundles that won't be wired
List<Wire> newWires = resolution != null ? resolution.get(resource) : null;
if (newWires == null) {
continue;
}
// Check if this bundle is a host and its fragments changed
Set<Resource> oldFragments = new HashSet<>();
for (BundleWire wire : wiring.getProvidedWires(null)) {
if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
oldFragments.add(wire.getRequirer());
}
}
if (!oldFragments.containsAll(newFragments.get(bundle))) {
toRefresh.put(bundle, "Attached fragments changed: " + new ArrayList<>(newFragments.get(bundle)));
break;
}
// Compare the old and new resolutions
Set<Resource> wiredBundles = new HashSet<>();
for (BundleWire wire : wiring.getRequiredWires(null)) {
BundleRevision rev = wire.getProvider();
Bundle provider = rev.getBundle();
if (toRefresh.containsKey(provider)) {
// The bundle is wired to a bundle being refreshed,
// so we need to refresh it too
toRefresh.put(bundle, "Wired to " + provider.getSymbolicName() + "/" + provider.getVersion() + " which is being refreshed");
continue main;
}
Resource res = bndToRes.get(provider);
wiredBundles.add(res != null ? res : rev);
}
Map<Resource, Requirement> wiredResources = new HashMap<>();
for (Wire wire : newWires) {
// Handle only packages, hosts, and required bundles
String namespace = wire.getRequirement().getNamespace();
if (!namespace.equals(BundleNamespace.BUNDLE_NAMESPACE) && !namespace.equals(PackageNamespace.PACKAGE_NAMESPACE) && !namespace.equals(HostNamespace.HOST_NAMESPACE)) {
continue;
}
// Ignore non-resolution time requirements
String effective = wire.getRequirement().getDirectives().get(Namespace.CAPABILITY_EFFECTIVE_DIRECTIVE);
if (effective != null && !Namespace.EFFECTIVE_RESOLVE.equals(effective)) {
continue;
}
// Ignore non bundle resources
if (!isBundle(wire.getProvider())) {
continue;
}
if (!wiredResources.containsKey(wire.getProvider())) {
wiredResources.put(wire.getProvider(), wire.getRequirement());
}
}
if (!wiredBundles.containsAll(wiredResources.keySet())) {
Map<Resource, Requirement> newResources = new HashMap<>(wiredResources);
newResources.keySet().removeAll(wiredBundles);
StringBuilder sb = new StringBuilder();
sb.append("Should be wired to: ");
boolean first = true;
for (Map.Entry<Resource, Requirement> entry : newResources.entrySet()) {
if (!first) {
sb.append(", ");
} else {
first = false;
}
Resource res = entry.getKey();
Requirement req = entry.getValue();
sb.append(getSymbolicName(res)).append("/").append(getVersion(res));
sb.append(" (through ");
sb.append(req);
sb.append(")");
}
toRefresh.put(bundle, sb.toString());
}
}
} while (toRefresh.size() > size);
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class Deployer method getServiceUsage.
private static int getServiceUsage(ServiceReference<?> ref, Collection<Bundle> bundles) {
Bundle[] usingBundles = ref.getUsingBundles();
int nb = 0;
if (usingBundles != null) {
for (Bundle bundle : usingBundles) {
if (bundles.contains(bundle)) {
nb++;
}
}
}
return nb;
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class TestBase method createDummyBundle.
public Bundle createDummyBundle(long id, final String symbolicName, Dictionary<String, String> headers) {
Bundle bundle = EasyMock.createNiceMock(Bundle.class);
// Be aware that this means all bundles are treated as different
expect(bundle.compareTo(EasyMock.anyObject())).andReturn(1).anyTimes();
expect(bundle.getBundleId()).andReturn(id).anyTimes();
expect(bundle.getSymbolicName()).andReturn(symbolicName).anyTimes();
expect(bundle.getHeaders()).andReturn(headers).anyTimes();
BundleStartLevel sl = EasyMock.createMock(BundleStartLevel.class);
expect(sl.isPersistentlyStarted()).andReturn(true).anyTimes();
expect(bundle.adapt(BundleStartLevel.class)).andReturn(sl).anyTimes();
replay(bundle, sl);
return bundle;
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class Main method installAndStartBundles.
private void installAndStartBundles(ArtifactResolver resolver, BundleContext context, List<BundleInfo> bundles) {
for (BundleInfo bundleInfo : bundles) {
try {
Bundle b;
if (bundleInfo.uri.toString().startsWith("reference:file:")) {
URI temp = URI.create(bundleInfo.uri.toString().substring("reference:file:".length()));
URI resolvedURI = resolver.resolve(temp);
URI finalUri = URI.create("reference:file:" + config.karafBase.toURI().relativize(resolvedURI));
b = context.installBundle(finalUri.toString());
} else {
URI resolvedURI = resolver.resolve(bundleInfo.uri);
b = context.installBundle(bundleInfo.uri.toString(), resolvedURI.toURL().openStream());
}
b.adapt(BundleStartLevel.class).setStartLevel(bundleInfo.startLevel);
if (isNotFragment(b)) {
b.start();
}
} catch (Exception e) {
throw new RuntimeException("Error installing bundle listed in " + STARTUP_PROPERTIES_FILE_NAME + " with url: " + bundleInfo.uri + " and startlevel: " + bundleInfo.startLevel, e);
}
}
}
use of org.osgi.framework.Bundle in project karaf by apache.
the class StartupListener method getBundleStats.
public BundleStats getBundleStats() {
Bundle[] bundles = context.getBundles();
int numActive = 0;
int numBundles = bundles.length;
for (Bundle bundle : bundles) {
if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null) {
numBundles--;
} else if (bundle.getState() == Bundle.ACTIVE) {
numActive++;
}
}
BundleStats stats = new BundleStats();
stats.numActive = numActive;
stats.numTotal = numBundles;
return stats;
}
Aggregations