Search in sources :

Example 1 with IInjector

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

the class ProviderHelper method findProvider.

public static ExtendedObjectSupplier findProvider(String qualifier, PrimaryObjectSupplier objectSupplier) {
    synchronized (extendedSuppliers) {
        if (extendedSuppliers.containsKey(qualifier))
            return extendedSuppliers.get(qualifier);
        Bundle bundle = FrameworkUtil.getBundle(ProviderHelper.class);
        if (bundle == null) {
            // In case we are not in an OSGi context, see bug 513883
            return null;
        }
        BundleContext bundleContext = bundle.getBundleContext();
        try {
            String filter = '(' + ExtendedObjectSupplier.SERVICE_CONTEXT_KEY + '=' + qualifier + ')';
            ServiceReference<?>[] refs = bundleContext.getServiceReferences(ExtendedObjectSupplier.SERVICE_NAME, filter);
            if (refs != null && refs.length > 0) {
                ExtendedObjectSupplier supplier;
                // Explicitly sort by ranking if more than one supplier is found
                if (refs.length > 1) {
                    Arrays.sort(refs, Collections.reverseOrder());
                }
                supplier = (ExtendedObjectSupplier) bundleContext.getService(refs[0]);
                if (objectSupplier != null) {
                    IInjector injector = InjectorFactory.getDefault();
                    injector.inject(supplier, objectSupplier);
                }
                extendedSuppliers.put(qualifier, supplier);
                return supplier;
            }
        } catch (InvalidSyntaxException e) {
        // should not happen - we tested the line above
        }
        extendedSuppliers.put(qualifier, null);
        return null;
    }
}
Also used : Bundle(org.osgi.framework.Bundle) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IInjector(org.eclipse.e4.core.di.IInjector) ExtendedObjectSupplier(org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with IInjector

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

the class AtInjectTest method suite.

public static Test suite() {
    IInjector injector = InjectorFactory.getDefault();
    injector.addBinding(SpareTire.class);
    injector.addBinding(Seat.class);
    injector.addBinding(DriversSeat.class);
    injector.addBinding(Cupholder.class);
    injector.addBinding(Tire.class);
    injector.addBinding(FuelTank.class);
    injector.addBinding(Car.class).implementedBy(Convertible.class);
    injector.addBinding(Seat.class).named(Drivers.class.getName()).implementedBy(DriversSeat.class);
    injector.addBinding(Engine.class).implementedBy(V8Engine.class);
    injector.addBinding(Tire.class).named("spare").implementedBy(SpareTire.class);
    Car car = injector.make(Car.class, null);
    return Tck.testsFor(car, true, true);
}
Also used : Car(org.atinject.tck.auto.Car) IInjector(org.eclipse.e4.core.di.IInjector) Engine(org.atinject.tck.auto.Engine) V8Engine(org.atinject.tck.auto.V8Engine)

Example 3 with IInjector

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

the class ProviderInjectionTest method testInvokeWithProvider.

@Test
public synchronized void testInvokeWithProvider() {
    // create context
    IEclipseContext context = EclipseContextFactory.create();
    context.set(String.class.getName(), "abc");
    IInjector injector = InjectorFactory.getDefault();
    injector.addBinding(TestData.class);
    TestInvokeClass userObject = new TestInvokeClass();
    assertEquals(1, ContextInjectionFactory.invoke(userObject, Execute.class, context, null));
    assertNotNull(userObject.provider.get());
    assertEquals("abc", userObject.provider.get().data);
}
Also used : Execute(org.eclipse.e4.core.di.annotations.Execute) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IInjector(org.eclipse.e4.core.di.IInjector) Test(org.junit.Test)

Example 4 with IInjector

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

the class InjectionEventTest method testEventInjection.

@Test
public void testEventInjection() throws InvocationTargetException, InstantiationException {
    IInjector injector = InjectorFactory.getDefault();
    injector.addBinding(MyBinding.class);
    IEclipseContext context = EclipseContextFactory.create();
    InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context);
    // initial state
    assertEquals(0, target.counter1);
    assertNull(target.string1);
    assertEquals(1, target.counter3);
    assertNull(target.string3);
    assertNotNull(target.myBinding);
    // send event1
    helper.sendEvent("e4/test/event1", "event1data");
    assertEquals(1, target.counter1);
    assertEquals("event1data", target.string1);
    assertEquals(1, target.counter3);
    assertNull(target.string3);
    assertNotNull(target.myBinding);
    // send event2
    helper.sendEvent("e4/test/event2", "event2data");
    assertEquals(1, target.counter1);
    assertEquals("event1data", target.string1);
    assertEquals(1, target.counter3);
    assertNull(target.string3);
    assertNotNull(target.myBinding);
    // send event3
    helper.sendEvent("e4/test/event3", "event3data");
    assertEquals(1, target.counter1);
    assertEquals("event1data", target.string1);
    assertEquals(2, target.counter3);
    assertEquals("event3data", target.string3);
    assertNotNull(target.myBinding);
    // send event1 again
    helper.sendEvent("e4/test/event1", "abc");
    assertEquals(2, target.counter1);
    assertEquals("abc", target.string1);
    assertEquals(2, target.counter3);
    assertEquals("event3data", target.string3);
    assertNotNull(target.myBinding);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IInjector(org.eclipse.e4.core.di.IInjector) Test(org.junit.Test)

Example 5 with IInjector

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

the class ProviderInjectionTest method testConstructorWithProvider.

@Test
public synchronized void testConstructorWithProvider() {
    // create context
    IEclipseContext context = EclipseContextFactory.create();
    context.set(String.class.getName(), "abc");
    IInjector injector = InjectorFactory.getDefault();
    injector.addBinding(TestData.class);
    TestConstructorClass userObject = ContextInjectionFactory.make(TestConstructorClass.class, context);
    assertNotNull(userObject);
    assertNotNull(userObject.provider);
    assertNotNull(userObject.provider.get());
    assertEquals("abc", userObject.provider.get().data);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IInjector(org.eclipse.e4.core.di.IInjector) Test(org.junit.Test)

Aggregations

IInjector (org.eclipse.e4.core.di.IInjector)6 Test (org.junit.Test)4 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)3 Car (org.atinject.tck.auto.Car)1 Engine (org.atinject.tck.auto.Engine)1 V8Engine (org.atinject.tck.auto.V8Engine)1 Execute (org.eclipse.e4.core.di.annotations.Execute)1 ExtendedObjectSupplier (org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1