Search in sources :

Example 61 with ServiceTracker

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

the class ApplicationAdminTest method testExitValue03.

public void testExitValue03() {
    // getExitValue test when called from a service listener during service property modified (STOPPING)
    // $NON-NLS-1$
    ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".exitValueApp");
    final Object[] result = new Object[1];
    ServiceTrackerCustomizer trackerCustomizer = new ServiceTrackerCustomizer() {

        public Object addingService(ServiceReference reference) {
            return getContext().getService(reference);
        }

        public void modifiedService(ServiceReference reference, Object service) {
            if (// $NON-NLS-1$
            !"org.eclipse.equinox.app.stopped".equals(reference.getProperty(ApplicationHandle.APPLICATION_STATE)))
                return;
            try {
                result[0] = ((ApplicationHandle) service).getExitValue(10000);
            } catch (Exception e) {
                result[0] = e;
            }
        }

        public void removedService(ServiceReference reference, Object service) {
        // nothing
        }
    };
    ServiceTracker tracker = null;
    try {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        tracker = new ServiceTracker(getContext(), FrameworkUtil.createFilter("(&(objectClass=" + ApplicationHandle.class.getName() + ")(" + ApplicationHandle.APPLICATION_DESCRIPTOR + "=" + app.getApplicationId() + "))"), trackerCustomizer);
    } catch (InvalidSyntaxException e) {
        // $NON-NLS-1$
        fail("unexpected syntax exception for tracker", e);
    }
    tracker.open();
    ApplicationHandle handle = null;
    try {
        handle = app.launch(null);
    } catch (ApplicationException e) {
        // $NON-NLS-1$
        fail("failed to launch simpleApp", e);
    }
    // $NON-NLS-1$
    assertNotNull("app handle is null", handle);
    String value = null;
    try {
        value = (String) handle.getExitValue(10000);
    } catch (Exception e) {
        // $NON-NLS-1$
        fail("unexpected exception waiting for exit value", e);
    }
    // $NON-NLS-1$
    assertNotNull("value is null", value);
    // $NON-NLS-1$
    assertEquals("exit value is incorrect", ExitValueApp.exitValue, value);
    // $NON-NLS-1$
    assertEquals("value from service unregister is different", value, result[0]);
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) SetupException(org.eclipse.core.tests.session.SetupManager.SetupException)

Example 62 with ServiceTracker

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

the class ApplicationAdminTest method testDestroyBeforeStart02.

public void testDestroyBeforeStart02() throws InvalidSyntaxException {
    // $NON-NLS-1$
    ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".testMainThreaded01");
    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 63 with ServiceTracker

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

the class ApplicationAdminTest method testExitValue02.

public void testExitValue02() {
    // getExitValue test when called from a service listener during service unregistration
    // $NON-NLS-1$
    ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".exitValueApp");
    final Object[] result = new Object[1];
    ServiceTrackerCustomizer trackerCustomizer = new ServiceTrackerCustomizer() {

        public Object addingService(ServiceReference reference) {
            return getContext().getService(reference);
        }

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

        public void removedService(ServiceReference reference, Object service) {
            try {
                result[0] = ((ApplicationHandle) service).getExitValue(10000);
            } catch (Exception e) {
                result[0] = e;
            }
        }
    };
    ServiceTracker tracker = null;
    try {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        tracker = new ServiceTracker(getContext(), FrameworkUtil.createFilter("(&(objectClass=" + ApplicationHandle.class.getName() + ")(" + ApplicationHandle.APPLICATION_DESCRIPTOR + "=" + app.getApplicationId() + "))"), trackerCustomizer);
    } catch (InvalidSyntaxException e) {
        // $NON-NLS-1$
        fail("unexpected syntax exception for tracker", e);
    }
    tracker.open();
    ApplicationHandle handle = null;
    try {
        handle = app.launch(null);
    } catch (ApplicationException e) {
        // $NON-NLS-1$
        fail("failed to launch simpleApp", e);
    }
    // $NON-NLS-1$
    assertNotNull("app handle is null", handle);
    String value = null;
    try {
        value = (String) handle.getExitValue(10000);
    } catch (Exception e) {
        // $NON-NLS-1$
        fail("unexpected exception waiting for exit value", e);
    }
    // $NON-NLS-1$
    assertNotNull("value is null", value);
    // $NON-NLS-1$
    assertEquals("exit value is incorrect", ExitValueApp.exitValue, value);
    // $NON-NLS-1$
    assertEquals("value from service unregister is different", value, result[0]);
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) SetupException(org.eclipse.core.tests.session.SetupManager.SetupException)

Example 64 with ServiceTracker

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

the class ApplicationAdminTest method testHandleEvents01.

public void testHandleEvents01() throws InvalidSyntaxException {
    // $NON-NLS-1$
    ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".simpleApp");
    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(), ApplicationHandle.RUNNING);
        handle.destroy();
        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.RUNNING }, 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)

Example 65 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project Payara by payara.

the class FelixWebConsoleExtensionActivator method configureConsole.

private void configureConsole() {
    tracker = new ServiceTracker(context, ConfigurationAdmin.class.getName(), null) {

        @Override
        public Object addingService(ServiceReference reference) {
            try {
                ConfigurationAdmin ca = ConfigurationAdmin.class.cast(context.getService(reference));
                org.osgi.service.cm.Configuration config = null;
                config = ca.getConfiguration(WEBCONSOLE_PID, null);
                Dictionary old = config.getProperties();
                Dictionary newProps = new Hashtable();
                newProps.put(PROP_HTTP_SERVICE_SELECTOR, HTTP_SERVICE_SELECTOR);
                newProps.put(PROP_REALM, REALM);
                if (old != null) {
                    old.remove(Constants.SERVICE_PID);
                }
                if (!newProps.equals(old)) {
                    if (config.getBundleLocation() != null) {
                        config.setBundleLocation(null);
                    }
                    config.update(newProps);
                }
            } catch (IOException e) {
                logger.logp(Level.INFO, "FelixWebConsoleExtensionActivator", "addingService", "Failed to update webconsole configuration", e);
            }
            return null;
        }
    };
    tracker.open();
}
Also used : Dictionary(java.util.Dictionary) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Hashtable(java.util.Hashtable) IOException(java.io.IOException) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

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