Search in sources :

Example 31 with BundleEvent

use of org.osgi.framework.BundleEvent 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 32 with BundleEvent

use of org.osgi.framework.BundleEvent in project aries by apache.

the class BundleEventDataTest method testToCompositeData.

@Test
public void testToCompositeData() throws Exception {
    BundleEvent event = mock(BundleEvent.class);
    Bundle bundle = mock(Bundle.class);
    when(event.getBundle()).thenReturn(bundle);
    when(bundle.getSymbolicName()).thenReturn("test");
    when(bundle.getBundleId()).thenReturn(new Long(4));
    when(bundle.getLocation()).thenReturn("location");
    when(event.getType()).thenReturn(BundleEvent.INSTALLED);
    BundleEventData eventData = new BundleEventData(event);
    CompositeData eventCompositeData = eventData.toCompositeData();
    assertEquals(new Long(4), (Long) eventCompositeData.get(IDENTIFIER));
    assertEquals("test", (String) eventCompositeData.get(SYMBOLIC_NAME));
    assertEquals(new Integer(BundleEvent.INSTALLED), (Integer) eventCompositeData.get(EVENT));
    assertEquals("location", (String) eventCompositeData.get(LOCATION));
}
Also used : Bundle(org.osgi.framework.Bundle) CompositeData(javax.management.openmbean.CompositeData) BundleEvent(org.osgi.framework.BundleEvent) Test(org.junit.Test)

Example 33 with BundleEvent

use of org.osgi.framework.BundleEvent in project aries by apache.

the class RecursiveBundleTrackerTest method testCompositeLifeCycle.

@Test
public void testCompositeLifeCycle() {
    makeSUT();
    CompositeBundle cb = composite("test.composite", "1.0.0");
    assertNoTrackers();
    // full lifecycle
    sut.addingBundle(cb, new BundleEvent(BundleEvent.INSTALLED, cb));
    assertTracker(cb);
    sut.modifiedBundle(cb, new BundleEvent(BundleEvent.RESOLVED, cb), cb);
    sut.modifiedBundle(cb, new BundleEvent(BundleEvent.STARTING, cb), cb);
    sut.modifiedBundle(cb, new BundleEvent(BundleEvent.STARTED, cb), cb);
    sut.modifiedBundle(cb, new BundleEvent(BundleEvent.STOPPING, cb), cb);
    sut.removedBundle(cb, new BundleEvent(BundleEvent.STOPPED, cb), cb);
    assertNoTrackers();
    // short lifecycle
    sut.addingBundle(cb, new BundleEvent(BundleEvent.INSTALLED, cb));
    assertTracker(cb);
    sut.modifiedBundle(cb, new BundleEvent(BundleEvent.RESOLVED, cb), cb);
    sut.removedBundle(cb, new BundleEvent(BundleEvent.UNRESOLVED, cb), cb);
    assertNoTrackers();
    // shortest lifecycle
    sut.addingBundle(cb, new BundleEvent(BundleEvent.INSTALLED, cb));
    assertTracker(cb);
    sut.removedBundle(cb, new BundleEvent(BundleEvent.UNINSTALLED, cb), cb);
    assertNoTrackers();
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) CompositeBundle(org.osgi.service.framework.CompositeBundle) Test(org.junit.Test)

Example 34 with BundleEvent

use of org.osgi.framework.BundleEvent in project aries by apache.

the class BlueprintContainerUseSystemContextTest method regiserHook.

@Before
public void regiserHook() {
    final BundleContext systemContext = context().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext();
    eventHook = context().registerService(EventHook.class, new EventHook() {

        @Override
        public void event(BundleEvent event, Collection contexts) {
            if ("org.apache.aries.blueprint.sample".equals(event.getBundle().getSymbolicName())) {
                // hide sample from everything but the system bundle
                // TODO on R6 we should be able to even try hiding from the system bundle
                // R5 it was not clear if hooks could hide from the system bundle
                // equinox R5 does allow hiding from system bundle
                contexts.retainAll(Collections.singleton(systemContext));
            }
        }
    }, null);
    findHook = context().registerService(FindHook.class, new FindHook() {

        @Override
        public void find(BundleContext context, Collection bundles) {
            if (context.equals(systemContext)) {
                // equinox R5 does allow hiding from system bundle
                return;
            }
            for (Iterator iBundles = bundles.iterator(); iBundles.hasNext(); ) {
                if ("org.apache.aries.blueprint.sample".equals(((Bundle) iBundles.next()).getSymbolicName())) {
                    // hide sample from everything
                    iBundles.remove();
                }
            }
        }
    }, null);
}
Also used : EventHook(org.osgi.framework.hooks.bundle.EventHook) FindHook(org.osgi.framework.hooks.bundle.FindHook) Iterator(java.util.Iterator) BundleEvent(org.osgi.framework.BundleEvent) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 35 with BundleEvent

use of org.osgi.framework.BundleEvent in project bnd by bndtools.

the class GC method start.

@Override
public void start(BundleContext context) throws Exception {
    this.context = context;
    context.addBundleListener(new BundleListener() {

        @Override
        public void bundleChanged(BundleEvent event) {
            if (event.getType() == BundleEvent.UNINSTALLED) {
                Bundle b = event.getBundle();
                String embedded = b.getHeaders().get("Bnd-Embedded");
                if (embedded != null) {
                    uninstalled(b);
                }
            }
        }
    });
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener)

Aggregations

BundleEvent (org.osgi.framework.BundleEvent)53 Bundle (org.osgi.framework.Bundle)41 Test (org.junit.Test)12 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)12 BundleListener (org.osgi.framework.BundleListener)9 FrameworkEvent (org.osgi.framework.FrameworkEvent)7 StartLevel (org.osgi.service.startlevel.StartLevel)7 BundleException (org.osgi.framework.BundleException)6 File (java.io.File)5 BundleContext (org.osgi.framework.BundleContext)5 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Endpoint (javax.xml.ws.Endpoint)3 BundleTracker (org.osgi.util.tracker.BundleTracker)3 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2