Search in sources :

Example 6 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project aries by apache.

the class Activator method start.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
     * )
     */
public void start(BundleContext context) throws Exception {
    System.out.println("bundle helloIsolation start");
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        System.out.println("HelloIsolationImpl: system manager is not null");
    } else {
        System.out.println("HelloIsolationImpl: system manager is still null");
    }
    sr = context.registerService(HelloIsolation.class.getName(), new HelloIsolationImpl(), null);
    bt = new BundleTracker(context, Bundle.INSTALLED | Bundle.UNINSTALLED | Bundle.ACTIVE, new BundleTrackerCustomizer() {

        public synchronized Object addingBundle(Bundle bundle, BundleEvent event) {
            if (event == null) {
                System.out.println("HelloIsolation " + bundle.getSymbolicName() + "_" + bundle.getVersion().toString() + " - adding Bundle: " + bundle.getSymbolicName() + " event: null");
            } else {
                System.out.println("HelloIsolation  " + bundle.getSymbolicName() + "_" + bundle.getVersion().toString() + " - adding Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
            }
            addEventCount++;
            return bundle;
        }

        public synchronized void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
            if (event == null) {
                System.out.println("HelloIsolation " + bundle.getSymbolicName() + "_" + bundle.getVersion().toString() + "  - modifying Bundle: " + bundle.getSymbolicName() + " event: null");
            } else {
                System.out.println("HelloIsolation " + bundle.getSymbolicName() + "_" + bundle.getVersion().toString() + " - modifying Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
            }
            modifyEventCount++;
        }

        public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
            if (event == null) {
                System.out.println("HelloIsolation " + bundle.getSymbolicName() + "_" + bundle.getVersion().toString() + " - removing Bundle: " + bundle.getSymbolicName() + " event: null");
            } else {
                System.out.println("HelloIsolation " + bundle.getSymbolicName() + "_" + bundle.getVersion().toString() + " - removing Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
            }
            removeEventCount++;
        }
    });
    bt.open();
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleTracker(org.osgi.util.tracker.BundleTracker) BundleTrackerCustomizer(org.osgi.util.tracker.BundleTrackerCustomizer)

Example 7 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project tomee by apache.

the class ServiceManagerExtender method init.

@Override
public void init() throws Exception {
    if (started != null && started.equals(Boolean.TRUE)) {
        throw new IllegalStateException("ServiceManager is already initialized");
    }
    final DiscoveryRegistry registry = new DiscoveryRegistry();
    SystemInstance.get().setComponent(DiscoveryRegistry.class, registry);
    started = Boolean.FALSE;
    stopped = false;
    final ServerServiceTracker t = new ServerServiceTracker();
    tracker = new BundleTracker(bundleContext, Bundle.ACTIVE | Bundle.STOPPING, t);
    tracker.open();
}
Also used : DiscoveryRegistry(org.apache.openejb.server.DiscoveryRegistry) BundleTracker(org.osgi.util.tracker.BundleTracker)

Example 8 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project aries by apache.

the class BlueprintExtender method stop.

public void stop(BundleContext context) {
    LOGGER.debug("Stopping blueprint extender...");
    stopping = true;
    ServiceUtil.safeUnregisterService(parserServiceReg);
    ServiceUtil.safeUnregisterService(blueprintServiceReg);
    ServiceUtil.safeUnregisterService(quiesceParticipantReg);
    // Orderly shutdown of containers
    while (!containers.isEmpty()) {
        for (Bundle bundle : getBundlesToDestroy()) {
            destroyContainer(bundle);
        }
    }
    if (bt instanceof BundleTracker) {
        ((BundleTracker) bt).close();
    } else if (bt instanceof RecursiveBundleTracker) {
        ((RecursiveBundleTracker) bt).close();
    }
    proxyManager.close();
    this.eventDispatcher.destroy();
    this.handlers.destroy();
    executors.shutdown();
    LOGGER.debug("Blueprint extender stopped");
}
Also used : Bundle(org.osgi.framework.Bundle) RecursiveBundleTracker(org.apache.aries.util.tracker.RecursiveBundleTracker) BundleTracker(org.osgi.util.tracker.BundleTracker) RecursiveBundleTracker(org.apache.aries.util.tracker.RecursiveBundleTracker)

Example 9 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project aries by apache.

the class JndiExtensionTests method testDisableExtensionAndCDIContainerWaits.

public void testDisableExtensionAndCDIContainerWaits() throws Exception {
    BundleTracker<Bundle> bt = new BundleTracker<>(bundle.getBundleContext(), Bundle.RESOLVED | Bundle.ACTIVE, new BundleTrackerCustomizer<Bundle>() {

        @Override
        public Bundle addingBundle(Bundle bundle, BundleEvent arg1) {
            List<BundleCapability> capabilities = bundle.adapt(BundleWiring.class).getCapabilities(CdiConstants.CDI_EXTENSION_NAMESPACE);
            if (capabilities.isEmpty()) {
                return null;
            }
            for (Capability capability : capabilities) {
                if (capability.getAttributes().containsValue("jndi")) {
                    return bundle;
                }
            }
            return null;
        }

        @Override
        public void modifiedBundle(Bundle bundle, BundleEvent arg1, Bundle arg2) {
        }

        @Override
        public void removedBundle(Bundle bundle, BundleEvent arg1, Bundle arg2) {
        }
    });
    bt.open();
    assertFalse(bt.isEmpty());
    Bundle extensionBundle = bt.getBundles()[0];
    Collection<ServiceReference<CdiContainer>> serviceReferences = bundleContext.getServiceReferences(CdiContainer.class, "(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + cdiBundle.getBundleId() + "))");
    assertNotNull(serviceReferences);
    assertFalse(serviceReferences.isEmpty());
    ServiceReference<CdiContainer> serviceReference = serviceReferences.iterator().next();
    CdiEvent.Type state = (CdiEvent.Type) serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE);
    assertEquals(CdiEvent.Type.CREATED, state);
    extensionBundle.stop();
    state = (CdiEvent.Type) serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE);
    assertEquals(CdiEvent.Type.WAITING_FOR_EXTENSIONS, state);
    extensionBundle.start();
    state = (CdiEvent.Type) serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE);
    assertEquals(CdiEvent.Type.CREATED, state);
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) Capability(org.osgi.resource.Capability) Bundle(org.osgi.framework.Bundle) BundleTracker(org.osgi.util.tracker.BundleTracker) ServiceReference(org.osgi.framework.ServiceReference) BundleEvent(org.osgi.framework.BundleEvent) List(java.util.List) CdiEvent(org.osgi.service.cdi.CdiEvent) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 10 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project aries by apache.

the class InternalRecursiveBundleTracker method customizedProcessBundle.

protected void customizedProcessBundle(BundleTrackerCustomizer btc, Bundle b, BundleEvent event, boolean removing) {
    if (b instanceof CompositeBundle) {
        CompositeBundle cb = (CompositeBundle) b;
        // check if the compositeBundle is already tracked in the
        // BundleTrackerFactory
        String bundleScope = cb.getSymbolicName() + "_" + cb.getVersion().toString();
        List<BundleTracker> btList = BundleTrackerFactory.getBundleTrackerList(bundleScope);
        // or when the tracker is being closed.
        if (event == null && !!!removing) {
            if (cb.getState() == Bundle.INSTALLED || cb.getState() == Bundle.RESOLVED || cb.getState() == Bundle.STARTING || cb.getState() == Bundle.ACTIVE) {
                openTracker(btc, cb, bundleScope, mask);
            }
        } else {
            // if we are removing, or the event is of the right type then we need to shutdown.
            if (removing || event.getType() == BundleEvent.STOPPED || event.getType() == BundleEvent.UNRESOLVED || event.getType() == BundleEvent.UNINSTALLED) {
                // if CompositeBundle is being stopped, let's remove the bundle
                // tracker(s) associated with the composite bundle
                String bundleId = b.getSymbolicName() + "/" + b.getVersion();
                alreadyRecursedContexts.remove(bundleId);
                if (btList != null) {
                    // unregister the bundlescope off the factory and close
                    // bundle trackers
                    BundleTrackerFactory.unregisterAndCloseBundleTracker(bundleScope);
                }
            } else if (event.getType() == BundleEvent.INSTALLED || event.getType() == BundleEvent.RESOLVED || event.getType() == BundleEvent.STARTING) {
                openTracker(btc, cb, bundleScope, mask);
            }
        }
    }
}
Also used : BundleTracker(org.osgi.util.tracker.BundleTracker) CompositeBundle(org.osgi.service.framework.CompositeBundle)

Aggregations

BundleTracker (org.osgi.util.tracker.BundleTracker)11 Bundle (org.osgi.framework.Bundle)7 BundleEvent (org.osgi.framework.BundleEvent)4 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)3 RecursiveBundleTracker (org.apache.aries.util.tracker.RecursiveBundleTracker)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 NamespaceHandlerRegistryImpl (org.apache.aries.blueprint.namespace.NamespaceHandlerRegistryImpl)1 ScheduledExecutorServiceWrapper (org.apache.aries.blueprint.utils.threading.ScheduledExecutorServiceWrapper)1 ScheduledExecutorServiceFactory (org.apache.aries.blueprint.utils.threading.ScheduledExecutorServiceWrapper.ScheduledExecutorServiceFactory)1 ProxyManager (org.apache.aries.proxy.ProxyManager)1 HelloIsolation (org.apache.aries.subsystem.example.helloIsolation.HelloIsolation)1 InstallInfo (org.apache.aries.subsystem.scope.InstallInfo)1 Scope (org.apache.aries.subsystem.scope.Scope)1 ScopeUpdate (org.apache.aries.subsystem.scope.ScopeUpdate)1