Search in sources :

Example 56 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.

the class EclipseStarter method updateSplash.

private static void updateSplash(Semaphore semaphore, StartupEventListener listener) throws InterruptedException {
    ServiceTracker<StartupMonitor, StartupMonitor> monitorTracker = new ServiceTracker<>(context, StartupMonitor.class.getName(), null);
    try {
        monitorTracker.open();
    } catch (IllegalStateException e) {
        // do nothing; this can happen if the framework shutdown
        return;
    }
    try {
        while (true) {
            StartupMonitor monitor = monitorTracker.getService();
            if (monitor != null) {
                try {
                    monitor.update();
                } catch (Throwable e) {
                // ignore exceptions thrown by the monitor
                }
            }
            // can we acquire the semaphore yet?
            if (semaphore.tryAcquire(50, TimeUnit.MILLISECONDS))
                // done
                break;
        // else still working, spin another update
        }
    } finally {
        if (listener != null) {
            try {
                context.removeBundleListener(listener);
                monitorTracker.close();
            } catch (IllegalStateException e) {
            // do nothing; this can happen if the framework shutdown
            }
        }
    }
}
Also used : StartupMonitor(org.eclipse.osgi.service.runnable.StartupMonitor) ServiceTracker(org.osgi.util.tracker.ServiceTracker)

Example 57 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.

the class Activator method start.

public void start(BundleContext context) throws Exception {
    final boolean[] serviceChanged = { false };
    ServiceListener listener = new ServiceListener() {

        public void serviceChanged(ServiceEvent event) {
            serviceChanged[0] = true;
        }
    };
    context.addServiceListener(listener, "(&(objectClass=java.lang.String)(test=*))");
    final boolean[] modifiedService = { false };
    ServiceTracker tracker = new ServiceTracker(context, FrameworkUtil.createFilter("(&(objectClass=java.lang.String)(test=*))"), new ServiceTrackerCustomizer() {

        public Object addingService(ServiceReference reference) {
            return reference;
        }

        public void modifiedService(ServiceReference reference, Object service) {
            modifiedService[0] = true;
        }

        public void removedService(ServiceReference reference, Object service) {
        // TODO Auto-generated method stub
        }
    });
    tracker.open();
    Hashtable props = new Hashtable();
    props.put("test", "value1");
    ServiceRegistration registration = context.registerService(String.class.getName(), "test", props);
    props.put("test", "value2");
    registration.setProperties(props);
    if (!serviceChanged[0])
        throw new Exception("did not call service listener");
    if (!modifiedService[0])
        throw new Exception("did not call tracker customer");
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) Hashtable(java.util.Hashtable)

Example 58 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.

the class ApplicationAdminTest method testDestroyBeforeStart01.

public void testDestroyBeforeStart01() throws InvalidSyntaxException {
    // $NON-NLS-1$
    ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".simpleApp");
    HashMap args = getArguments();
    HashMap results = (HashMap) args.get(testResults);
    ServiceTrackerCustomizer trackerCustomizer = new ServiceTrackerCustomizer() {

        public Object addingService(ServiceReference reference) {
            ApplicationHandle handle = (ApplicationHandle) getContext().getService(reference);
            handle.destroy();
            return handle;
        }

        public void modifiedService(ServiceReference reference, Object service) {
        // nothing
        }

        public void removedService(ServiceReference reference, Object service) {
        // nothing
        }
    };
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    ServiceTracker tracker = new ServiceTracker(getContext(), FrameworkUtil.createFilter("(&(objectClass=" + ApplicationHandle.class.getName() + ")(" + ApplicationHandle.APPLICATION_DESCRIPTOR + "=" + app.getApplicationId() + "))"), trackerCustomizer);
    tracker.open();
    try {
        ApplicationHandle handle = app.launch(args);
        handle.destroy();
    } catch (Throwable e) {
        // $NON-NLS-1$
        fail("failed to launch simpleApp", e);
    } finally {
        tracker.close();
    }
    String result = (String) results.get(simpleResults);
    // $NON-NLS-1$
    assertNull("Check application result", result);
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer)

Example 59 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.

the class ApplicationAdminTest method testPersistentSchedule02.

public void testPersistentSchedule02() throws InvalidSyntaxException {
    // $NON-NLS-1$
    ScheduledApplication scheduledApp = getScheduleApplication("schedule.1", true);
    ApplicationHandleTracker handleTracker = new ApplicationHandleTracker(getContext());
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    ServiceTracker tracker = new ServiceTracker(getContext(), FrameworkUtil.createFilter("(&(objectClass=" + ApplicationHandle.class.getName() + ")(" + ApplicationHandle.APPLICATION_DESCRIPTOR + "=" + scheduledApp.getApplicationDescriptor().getApplicationId() + "))"), handleTracker);
    tracker.open();
    try {
        ApplicationHandle handle = (ApplicationHandle) tracker.waitForService(61000);
        // $NON-NLS-1$
        assertNotNull("Should find an application handle", handle);
        handleTracker.waitForEvent(handle.getInstanceId(), ApplicationHandle.RUNNING);
        handle.destroy();
        handleTracker.waitForEvent(handle.getInstanceId(), REMOVED);
        HashMap results = null;
        try {
            results = (HashMap) handle.getExitValue(1000);
        } catch (ApplicationException e) {
            // $NON-NLS-1$
            fail("Unexpected application exception waiting for an exit value", e);
        }
        // $NON-NLS-1$
        assertNotNull("Null results", results);
        HashMap args = new HashMap();
        // $NON-NLS-1$
        args.put("test.arg1", Boolean.TRUE);
        // $NON-NLS-1$
        args.put("test.arg2", new Integer(34));
        // $NON-NLS-1$
        args.put("test.arg3", new Long(34));
        for (Iterator iEntries = args.entrySet().iterator(); iEntries.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iEntries.next();
            // $NON-NLS-1$
            assertEquals("key: " + entry.getKey(), entry.getValue(), results.get(entry.getKey()));
        }
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("got interupted", e);
    } finally {
        tracker.close();
        scheduledApp.remove();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker)

Example 60 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project rt.equinox.framework by eclipse.

the class ApplicationAdminTest method testFailedApplication01.

public void testFailedApplication01() throws InvalidSyntaxException {
    // $NON-NLS-1$
    ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".failedApp");
    ApplicationHandleTracker handleTracker = new ApplicationHandleTracker(getContext());
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    ServiceTracker tracker = new ServiceTracker(getContext(), FrameworkUtil.createFilter("(&(objectClass=" + ApplicationHandle.class.getName() + ")(" + ApplicationHandle.APPLICATION_DESCRIPTOR + "=" + app.getApplicationId() + "))"), handleTracker);
    tracker.open();
    try {
        ApplicationHandle handle = app.launch(null);
        handleTracker.waitForEvent(handle.getInstanceId(), REMOVED);
        String[][] events = handleTracker.getEvents();
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        String[][] expected = new String[][] { new String[] { handle.getInstanceId(), "org.eclipse.equinox.app.starting" }, new String[] { handle.getInstanceId(), ApplicationHandle.STOPPING }, new String[] { handle.getInstanceId(), "org.eclipse.equinox.app.stopped" }, new String[] { handle.getInstanceId(), "removed" } };
        // $NON-NLS-1$
        assertEquals("Check expected # events", expected.length, events.length);
        for (int i = 0; i < events.length; i++) {
            // $NON-NLS-1$
            assertEquals("Check expected event id for #" + i, expected[i][0], events[i][0]);
            // $NON-NLS-1$
            assertEquals("Check expected event state for #" + i, expected[i][1], events[i][1]);
        }
    } catch (ApplicationException e) {
        // $NON-NLS-1$
        fail("failed to launch application", e);
    } finally {
        tracker.close();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker)

Aggregations

ServiceTracker (org.osgi.util.tracker.ServiceTracker)115 ServiceReference (org.osgi.framework.ServiceReference)33 Filter (org.osgi.framework.Filter)28 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)19 Hashtable (java.util.Hashtable)18 Activate (org.apache.felix.scr.annotations.Activate)18 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)14 BundleContext (org.osgi.framework.BundleContext)12 ConfigurationException (org.osgi.service.cm.ConfigurationException)10 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 IOException (java.io.IOException)7 Dictionary (java.util.Dictionary)7 Bundle (org.osgi.framework.Bundle)7 Converter (net.heartsome.cat.converter.Converter)6 AndFilter (net.heartsome.cat.converter.util.AndFilter)6 EqFilter (net.heartsome.cat.converter.util.EqFilter)6 Configuration (org.osgi.service.cm.Configuration)6 HashMap (java.util.HashMap)5 CdiContainer (org.osgi.service.cdi.CdiContainer)4