Search in sources :

Example 6 with Service

use of org.eclipse.e4.core.di.extensions.Service in project eclipse.platform.runtime by eclipse.

the class ServiceContextTest method testServiceRanking.

@Test
public void testServiceRanking() throws InterruptedException {
    BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
    IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context);
    TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext);
    assertNull(bean.testService);
    ServiceReference<TestServiceController> ref = context.getServiceReference(TestServiceController.class);
    TestServiceController controller = context.getService(ref);
    try {
        controller.enableTestServiceB();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.testService);
        assertSame(TestServiceB.class, bean.testService.getClass());
        assertNotNull(bean.testOtherService);
        assertSame(TestServiceB.class, bean.testOtherService.getClass());
        // enable a service with a lower ranking
        controller.enableTestServiceA();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.testService);
        // we expect that still the highest ranked service is injected
        assertSame(TestServiceB.class, bean.testService.getClass());
        assertNotNull(bean.testOtherService);
        assertSame(TestServiceB.class, bean.testOtherService.getClass());
        controller.disableTestServiceB();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.testService);
        assertSame(TestServiceA.class, bean.testService.getClass());
        assertNotNull(bean.testOtherService);
        assertSame(TestServiceA.class, bean.testOtherService.getClass());
        controller.disableTestServiceA();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNull(bean.testService);
        assertNull(bean.testOtherService);
    } finally {
        controller.disableTestServiceA();
        controller.disableTestServiceB();
        // give the service registry and the injection some time to ensure
        // clear state after this test
        Thread.sleep(100);
    }
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 7 with Service

use of org.eclipse.e4.core.di.extensions.Service in project eclipse.platform.runtime by eclipse.

the class ServiceContextTest method testOptionalReferences.

@Test
public void testOptionalReferences() throws InterruptedException {
    BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
    IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context);
    TestBean bean = ContextInjectionFactory.make(TestBean.class, serviceContext);
    assertNull(bean.testService);
    ServiceReference<TestServiceController> ref = context.getServiceReference(TestServiceController.class);
    TestServiceController controller = context.getService(ref);
    try {
        controller.enableTestServiceA();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.testService);
        assertSame(TestServiceA.class, bean.testService.getClass());
        assertNotNull(bean.testOtherService);
        assertSame(TestServiceA.class, bean.testOtherService.getClass());
        controller.enableTestServiceB();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.testService);
        assertSame(TestServiceB.class, bean.testService.getClass());
        assertNotNull(bean.testOtherService);
        assertSame(TestServiceB.class, bean.testOtherService.getClass());
        controller.disableTestServiceB();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNotNull(bean.testService);
        assertSame(TestServiceA.class, bean.testService.getClass());
        assertNotNull(bean.testOtherService);
        assertSame(TestServiceA.class, bean.testOtherService.getClass());
        controller.disableTestServiceA();
        // give the service registry and the injection some time
        Thread.sleep(100);
        assertNull(bean.testService);
        assertNull(bean.testOtherService);
    } finally {
        controller.disableTestServiceA();
        controller.disableTestServiceB();
        // give the service registry and the injection some time to ensure
        // clear state after this test
        Thread.sleep(100);
    }
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 8 with Service

use of org.eclipse.e4.core.di.extensions.Service in project eclipse.platform.runtime by eclipse.

the class ServiceContextTest method testServiceContextAsParent.

/**
 * Tests accessing OSGi services through a child context that is not aware of them.
 */
@Test
public void testServiceContextAsParent() {
    IEclipseContext child = context.createChild("child");
    DebugOptions service = (DebugOptions) child.get(DebugOptions.class.getName());
    assertNotNull(service);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) DebugOptions(org.eclipse.osgi.service.debug.DebugOptions) Test(org.junit.Test)

Example 9 with Service

use of org.eclipse.e4.core.di.extensions.Service in project eclipse.platform.runtime by eclipse.

the class ServiceContextTest method testServiceRemovalOnContextDispose.

/**
 * Tests that OSGi services are released when their context is disposed.
 */
@Test
public void testServiceRemovalOnContextDispose() {
    StringPrintService stringPrint1 = new StringPrintService();
    BundleContext bundleContext = CoreTestsActivator.getDefault().getBundleContext();
    Bundle otherBundle = null;
    for (Bundle b : bundleContext.getBundles()) {
        if (b.getSymbolicName().equals("org.eclipse.core.tests.harness")) {
            otherBundle = b;
            break;
        }
    }
    assertNotNull(otherBundle);
    IEclipseContext otherServiceContext = EclipseContextFactory.getServiceContext(otherBundle.getBundleContext());
    ServiceRegistration<?> reg1 = bundleContext.registerService(PrintService.SERVICE_NAME, stringPrint1, null);
    try {
        ServiceReference<?> ref = reg1.getReference();
        PrintService service = (PrintService) otherServiceContext.get(PrintService.SERVICE_NAME);
        assertEquals("1.0", stringPrint1, service);
        assertEquals("1.1", 1, ref.getUsingBundles().length);
        service = null;
        otherServiceContext.dispose();
        assertNull("2.0", ref.getUsingBundles());
    } finally {
        reg1.unregister();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 10 with Service

use of org.eclipse.e4.core.di.extensions.Service in project yamcs-studio by yamcs.

the class OPIView method findPlaceholder.

/**
 * Find the MPlaceholder corresponding to this MPart in the MPerspective. This may have persisted information
 * relevant to loading this OPIView.
 *
 * @return corresponding placeholder
 */
private MPlaceholder findPlaceholder() {
    // do not remove casting - RAP 3.0 still needs it
    final IEclipseContext localContext = (IEclipseContext) getViewSite().getService(IEclipseContext.class);
    final MPart part = localContext.get(MPart.class);
    final EModelService service = (EModelService) PlatformUI.getWorkbench().getService(EModelService.class);
    final IEclipseContext globalContext = (IEclipseContext) PlatformUI.getWorkbench().getService(IEclipseContext.class);
    final MApplication app = globalContext.get(MApplication.class);
    final List<MPlaceholder> phs = service.findElements(app, null, MPlaceholder.class, null);
    for (MPlaceholder ph : phs) {
        if (ph.getRef() == part) {
            return ph;
        }
    }
    return null;
}
Also used : MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) EModelService(org.eclipse.e4.ui.workbench.modeling.EModelService) MPlaceholder(org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MApplication(org.eclipse.e4.ui.model.application.MApplication)

Aggregations

IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)7 Test (org.junit.Test)5 BundleContext (org.osgi.framework.BundleContext)4 MApplication (org.eclipse.e4.ui.model.application.MApplication)3 MPlaceholder (org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder)2 MPart (org.eclipse.e4.ui.model.application.ui.basic.MPart)2 EModelService (org.eclipse.e4.ui.workbench.modeling.EModelService)2 Bundle (org.osgi.framework.Bundle)2 ParsedAnyData (alma.acs.eventbrowser.parts.ParsedAnyData)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Inject (javax.inject.Inject)1 AlphavantageQuoteFeed (name.abuchen.portfolio.online.impl.AlphavantageQuoteFeed)1 CanExecute (org.eclipse.e4.core.di.annotations.CanExecute)1 Execute (org.eclipse.e4.core.di.annotations.Execute)1 Optional (org.eclipse.e4.core.di.annotations.Optional)1 Service (org.eclipse.e4.core.di.extensions.Service)1