Search in sources :

Example 71 with BundleEvent

use of org.osgi.framework.BundleEvent in project eclipse.platform.runtime by eclipse.

the class OSGiObjectSupplier method track.

private void track(final Bundle bundle, final IRequestor requestor) {
    // A _synchronous_ BundleListener asserts that the BC is un-injected,
    // _before_ it becomes invalid (state-wise).
    BundleListener listener = new SynchronousBundleListener() {

        @Override
        public void bundleChanged(BundleEvent event) {
            if (event.getBundle().equals(bundle)) {
                if (requestor.isValid()) {
                    requestor.resolveArguments(false);
                    requestor.execute();
                }
            }
        }
    };
    synchronized (requestor2listener) {
        localBundleContext.addBundleListener(listener);
        requestor2listener.put(requestor, listener);
    }
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 72 with BundleEvent

use of org.osgi.framework.BundleEvent in project eclipse.platform.runtime by eclipse.

the class Expressions method createClassCaches.

private static void createClassCaches() {
    if (fgKnownClasses == null) {
        fgKnownClasses = new WeakHashMap<>();
        fgNotFoundClasses = new WeakHashMap<>();
        BundleContext bundleContext = ExpressionPlugin.getDefault().getBundleContext();
        BundleListener listener = new BundleListener() {

            @Override
            public void bundleChanged(BundleEvent event) {
                // Invalidate the caches if any of the bundles is stopped
                if (event.getType() == BundleEvent.STOPPED) {
                    synchronized (Expressions.class) {
                        fgKnownClasses.clear();
                        fgNotFoundClasses.clear();
                    }
                }
            }
        };
        ExpressionPlugin.fgBundleListener = listener;
        bundleContext.addBundleListener(listener);
    }
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) BundleContext(org.osgi.framework.BundleContext)

Example 73 with BundleEvent

use of org.osgi.framework.BundleEvent in project smarthome by eclipse.

the class XmlDocumentBundleTracker method open.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public final synchronized void open() {
    relevantBundlesTracker = new BundleTracker(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE, null) {

        @Override
        public Object addingBundle(Bundle bundle, BundleEvent event) {
            return isBundleRelevant(bundle) ? bundle : null;
        }
    };
    relevantBundlesTracker.open();
    super.open();
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleTracker(org.osgi.util.tracker.BundleTracker)

Example 74 with BundleEvent

use of org.osgi.framework.BundleEvent in project smarthome by eclipse.

the class AutomationResourceBundlesEventQueue method run.

/**
 * When a new event for a bundle providing automation resources is received, this will causes a creation of a new
 * thread if there is no other created yet. If the thread already exists, then it will be notified for the event.
 * Starting the thread will cause the execution of this method in separate thread.
 * <p>
 * The general contract of this method <code>run</code> is invoking of the
 * {@link #processBundleChanged(BundleEvent)} method and executing it in separate thread.
 *
 * @see java.lang.Thread#run()
 */
@Override
public void run() {
    boolean waitForEvents = true;
    while (true) {
        List<BundleEvent> lQueue = null;
        synchronized (this) {
            if (closed) {
                notifyAll();
                return;
            }
            if (queue.isEmpty()) {
                if (waitForEvents) {
                    try {
                        wait(180000);
                    } catch (Throwable t) {
                    }
                    waitForEvents = false;
                    continue;
                }
                running = false;
                runningThread = null;
                notifyAll();
                return;
            }
            lQueue = queue;
            shared = true;
        }
        Iterator<BundleEvent> events = lQueue.iterator();
        while (events.hasNext()) {
            BundleEvent event = events.next();
            try {
                processBundleChanged(event);
                synchronized (this) {
                    if (closed) {
                        notifyAll();
                        return;
                    }
                }
            } catch (Throwable t) {
                if (!closed && !(t instanceof IllegalStateException)) {
                    logger.warn("Processing bundle event {}, for automation resource bundle '{}' failed", event.getType(), event.getBundle().getSymbolicName(), t);
                }
            }
        }
        synchronized (this) {
            if (shared) {
                queue.clear();
            }
            shared = false;
            waitForEvents = true;
            notifyAll();
        }
    }
}
Also used : BundleEvent(org.osgi.framework.BundleEvent)

Example 75 with BundleEvent

use of org.osgi.framework.BundleEvent in project feature-flags-for-osgi by amitjoy.

the class FeatureManagerProviderTest method testGetFeaturesFromMetatypeXMLDescriptorWithoutName.

@Test
public void testGetFeaturesFromMetatypeXMLDescriptorWithoutName() throws Exception {
    final FeatureManagerProvider manager = new FeatureManagerProvider();
    manager.setConfigurationAdmin(configurationAdmin);
    manager.setMetaTypeService(metaTypeService);
    manager.activate(bundleContext1);
    final MetaTypeExtender extender = manager.getExtender();
    final String[] pids = new String[] { "a" };
    final BundleEvent bundleEvent = new BundleEvent(BundleEvent.STARTED, bundle);
    when(metaTypeService.getMetaTypeInformation(bundle)).thenReturn(metaTypeInfo);
    when(metaTypeInfo.getPids()).thenReturn(pids);
    when(metaTypeInfo.getObjectClassDefinition("a", null)).thenReturn(ocd);
    when(ocd.getAttributeDefinitions(ALL)).thenReturn(new AttributeDefinition[] { ad });
    mockADWithoutName();
    when(bundleContext1.getBundle(0)).thenReturn(systemBundle);
    when(bundle.getState()).thenReturn(ACTIVE);
    when(bundle.getBundleContext()).thenReturn(bundleContext1);
    extender.addingBundle(bundle, bundleEvent);
    Thread.sleep(1000);
    FeatureDTO feature = manager.getFeatures().collect(Collectors.toList()).get(0);
    assertEquals(FEATURE_ID, feature.name);
    assertEquals(FEATURE_ID, feature.id);
    assertEquals(FEATURE_DESC, feature.description);
    assertTrue(feature.isEnabled);
    feature = manager.getFeatures(FEATURE_ID).findFirst().get();
    assertEquals(FEATURE_ID, feature.name);
    assertEquals(FEATURE_ID, feature.id);
    assertEquals(FEATURE_DESC, feature.description);
    assertTrue(feature.isEnabled);
    feature = manager.getFeatures(FEATURE_ID).findAny().get();
    assertEquals(FEATURE_ID, feature.name);
    assertEquals(FEATURE_DESC, feature.description);
    assertTrue(feature.isEnabled);
    manager.unsetConfigurationAdmin(configurationAdmin);
    manager.unsetMetaTypeService(metaTypeService);
    manager.deactivate(bundleContext1);
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) FeatureDTO(com.amitinside.featureflags.dto.FeatureDTO) Test(org.junit.Test)

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