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);
}
}
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);
}
}
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);
}
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();
}
}
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;
}
Aggregations