Extend set of bundles visible to BundleTracker used to manage service caches.
Use system bundle context to track more bundles. Extend the cache availbiliy to JNDI calls during bundle start()/stop() methods. The tracker's bundleRemoved is deleted since it will only be called after a bundle transitions from STOPPING->STOPPED at which point the framework has done cleanup and calling ungetService is not needed.
diff --git a/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java b/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
index c835e95..bda5f69 100644
--- a/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
+++ b/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
@@ -119,8 +119,20 @@
public void start(BundleContext context) {
instance = this;
-
- bundleServiceCaches = new BundleTracker<ServiceCache>(context, Bundle.ACTIVE, null) {
+ BundleContext trackerBundleContext;
+ /* Use system context to allow trackers full bundle/service visibility. */
+ if ( !Boolean.getBoolean("org.apache.aries.jndi.trackersUseLocalContext") ){
+ trackerBundleContext = context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext();
+ if (trackerBundleContext==null) {
+ throw new IllegalStateException("Bundle could not aquire system bundle context.");
+ }
+ }
+ else {
+ trackerBundleContext = context;
+ }
+
+ bundleServiceCaches =
+ new BundleTracker<ServiceCache>(trackerBundleContext, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING , null) {
@Override
public ServiceCache addingBundle(Bundle bundle, BundleEvent event) {
return new ServiceCache(bundle.getBundleContext());
@@ -128,17 +140,13 @@
@Override
public void modifiedBundle(Bundle bundle, BundleEvent event, ServiceCache object) {
}
- @Override
- public void removedBundle(Bundle bundle, BundleEvent event, ServiceCache object) {
- object.close();
- }
};
bundleServiceCaches.open();
- initialContextFactories = new CachingServiceTracker<>(context, InitialContextFactory.class, Activator::getInitialContextFactoryInterfaces);
- objectFactories = new CachingServiceTracker<>(context, ObjectFactory.class, Activator::getObjectFactorySchemes);
- icfBuilders = new CachingServiceTracker<>(context, InitialContextFactoryBuilder.class);
- urlObjectFactoryFinders = new CachingServiceTracker<>(context, URLObjectFactoryFinder.class);
+ initialContextFactories = new CachingServiceTracker<>(trackerBundleContext, InitialContextFactory.class, Activator::getInitialContextFactoryInterfaces);
+ objectFactories = new CachingServiceTracker<>(trackerBundleContext, ObjectFactory.class, Activator::getObjectFactorySchemes);
+ icfBuilders = new CachingServiceTracker<>(trackerBundleContext, InitialContextFactoryBuilder.class);
+ urlObjectFactoryFinders = new CachingServiceTracker<>(trackerBundleContext, URLObjectFactoryFinder.class);
if (!disableBuilder(context)) {
try {
@@ -330,17 +338,9 @@
return (List) trackers.computeIfAbsent(clazz, c -> new CachingServiceTracker<>(context, c)).getReferences();
}
- void close() {
- cache.forEach(this::doUngetService);
- }
-
Object doGetService(ServiceReference<?> ref) {
return Utils.doPrivileged(() -> context.getService(ref));
}
-
- void doUngetService(ServiceReference<?> ref, Object svc) {
- Utils.doPrivileged(() -> context.ungetService(ref));
- }
}
}