Search in sources :

Example 46 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 47 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)

Example 48 with BundleEvent

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

the class Activator method automatic.

void automatic(File reportDir) throws IOException {
    final List<Bundle> queue = new Vector<Bundle>();
    if (!reportDir.exists() && !reportDir.mkdirs()) {
        throw new IOException("Could not create directory " + reportDir);
    }
    trace("using %s", reportDir);
    trace("adding Bundle Listener for getting test bundle events");
    context.addBundleListener(new SynchronousBundleListener() {

        public void bundleChanged(BundleEvent event) {
            if (event.getType() == BundleEvent.STARTED) {
                checkBundle(queue, event.getBundle());
            }
        }
    });
    for (Bundle b : context.getBundles()) {
        checkBundle(queue, b);
    }
    trace("starting queue");
    int result = 0;
    outer: while (active) {
        Bundle bundle;
        synchronized (queue) {
            while (queue.isEmpty() && active) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    trace("tests bundle queue interrupted");
                    thread.interrupt();
                    break outer;
                }
            }
        }
        try {
            bundle = queue.remove(0);
            trace("received bundle to test: %s", bundle.getLocation());
            try (Writer report = getReportWriter(reportDir, bundle)) {
                trace("test will run");
                result += test(bundle, (String) bundle.getHeaders().get(aQute.bnd.osgi.Constants.TESTCASES), report);
                trace("test ran");
                if (queue.isEmpty() && !continuous) {
                    trace("queue " + queue);
                    System.exit(result);
                }
            }
        } catch (Exception e) {
            error("Not sure what happened anymore %s", e);
            System.exit(-2);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) IOException(java.io.IOException) Vector(java.util.Vector) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) IOException(java.io.IOException) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 49 with BundleEvent

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

the class ConfigurationMetadataUtil method registerAnnotationClasses.

/**
     * Search classpath for given class names to scan for and register all classes with @Configuration annotation.
     * @param bundleContext Bundle context
     * @param classNames Java class names
     */
public static void registerAnnotationClasses(BundleContext bundleContext, String... classNames) {
    Bundle bundle = new RegisterConfigurationMetadataBundle(bundleContext, Bundle.ACTIVE, classNames);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 50 with BundleEvent

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

the class BundleEventUtil method startDummyBundle.

/**
     * Simulate a bundle STARTED event with a given set of classes simulated to be found in the bundle's classpath. 
     */
public static Bundle startDummyBundle(BundleContext bundleContext, Class... classes) {
    DummyBundle bundle = new DummyBundle(bundleContext, classes);
    bundle.setState(Bundle.ACTIVE);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
    return bundle;
}
Also used : BundleEvent(org.osgi.framework.BundleEvent)

Aggregations

BundleEvent (org.osgi.framework.BundleEvent)83 Bundle (org.osgi.framework.Bundle)49 Test (org.junit.Test)24 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)17 BundleListener (org.osgi.framework.BundleListener)15 FeatureDTO (com.amitinside.featureflags.dto.FeatureDTO)11 File (java.io.File)10 IOException (java.io.IOException)10 BundleException (org.osgi.framework.BundleException)10 BundleContext (org.osgi.framework.BundleContext)9 FrameworkEvent (org.osgi.framework.FrameworkEvent)7 StartLevel (org.osgi.service.startlevel.StartLevel)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 BundleTracker (org.osgi.util.tracker.BundleTracker)5 Map (java.util.Map)4 ServiceReference (org.osgi.framework.ServiceReference)4 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)4 FileInputStream (java.io.FileInputStream)3