Search in sources :

Example 6 with IObjectDescriptor

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

the class PreferencesObjectSupplier method getNodePath.

private String getNodePath(IObjectDescriptor descriptor, Class<?> requestingObject) {
    if (descriptor == null)
        return null;
    Preference qualifier = descriptor.getQualifier(Preference.class);
    String nodePath = qualifier.nodePath();
    if (nodePath == null || nodePath.length() == 0) {
        if (requestingObject == null)
            return null;
        nodePath = FrameworkUtil.getBundle(requestingObject).getSymbolicName();
    }
    return nodePath;
}
Also used : Preference(org.eclipse.e4.core.di.extensions.Preference)

Example 7 with IObjectDescriptor

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

the class ContextObjectSupplier method get.

@Override
public void get(IObjectDescriptor[] descriptors, Object[] actualArgs, final IRequestor requestor, boolean initial, boolean track, boolean group) {
    final String[] keys = new String[descriptors.length];
    final boolean[] active = new boolean[descriptors.length];
    for (int i = 0; i < descriptors.length; i++) {
        String key = getKey(descriptors[i]);
        if ((actualArgs[i] == IInjector.NOT_A_VALUE))
            keys[i] = key;
        else if (// allow provider to override IEclipseContext
        ECLIPSE_CONTEXT_NAME.equals(key))
            keys[i] = ECLIPSE_CONTEXT_NAME;
        else
            keys[i] = null;
        if (descriptors[i] == null)
            active[i] = false;
        else
            active[i] = (descriptors[i].hasQualifier(Active.class));
    }
    if (requestor != null && track) {
        // only track if requested
        if (initial) {
            RunAndTrack trackable = new ContextInjectionListener(context, actualArgs, keys, active, requestor, group);
            context.runAndTrack(trackable);
        } else {
            // we do track if this is done inside a computation, but don't create another runnable
            fillArgs(actualArgs, keys, active);
        }
    } else {
        if (descriptors.length > 0) {
            pauseRecording();
            try {
                fillArgs(actualArgs, keys, active);
            } finally {
                resumeRecording();
            }
        }
    }
}
Also used : Active(org.eclipse.e4.core.contexts.Active) RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack)

Example 8 with IObjectDescriptor

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

the class OSGiObjectSupplier method get.

@Override
public Object get(IObjectDescriptor descriptor, IRequestor requestor, boolean track, boolean group) {
    final Class<?> requestingObjectClass = requestor.getRequestingObjectClass();
    final Type desiredType = descriptor.getDesiredType();
    if (BundleContext.class.equals(desiredType)) {
        final Bundle bundle = FrameworkUtil.getBundle(requestingObjectClass);
        // Iff track is request and there is no listener yet, lets track the bundle
        if (track) {
            if (!requestor2listener.containsKey(requestor)) {
                track(bundle, requestor);
            }
        // Handlers only executed once and thus don't track the BC/Bundle.
        // Still guard to now de-register a non-existing listener.
        } else if (requestor2listener.containsKey(requestor)) {
            untrack(requestor);
        }
        final BundleContext bundleContext = bundle.getBundleContext();
        if (bundleContext != null) {
            return bundleContext;
        } else if (descriptor.getQualifier(Optional.class) != null) {
            // Do not have a bundle context but requestor has marked the parameter/field optional
            return null;
        }
        // $NON-NLS-1$  //$NON-NLS-2$
        throw new InjectionException("Unable to inject BundleContext: " + bundle.getSymbolicName() + " bundle is not active or starting/stopping");
    } else if (Bundle.class.equals(desiredType)) {
        // state. However, the requestor will go away along with its bundle anyway.
        return FrameworkUtil.getBundle(requestingObjectClass);
    }
    // Annotation used with unsupported type
    return null;
}
Also used : InjectionException(org.eclipse.e4.core.di.InjectionException) Type(java.lang.reflect.Type) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext)

Example 9 with IObjectDescriptor

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

the class ServiceSupplier method get.

@Override
public Object get(IObjectDescriptor descriptor, IRequestor requestor, boolean track, boolean group) {
    Type desiredType = descriptor.getDesiredType();
    Bundle b = FrameworkUtil.getBundle(requestor.getRequestingObjectClass());
    Service qualifier = descriptor.getQualifier(Service.class);
    if (desiredType instanceof ParameterizedType) {
        ParameterizedType t = (ParameterizedType) desiredType;
        if (t.getRawType() == Collections.class || t.getRawType() == List.class) {
            return handleCollection(b, t.getActualTypeArguments()[0], requestor, track && qualifier.dynamic(), qualifier);
        }
    }
    return handleSingle(b, desiredType, requestor, descriptor, track && qualifier.dynamic(), qualifier);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Bundle(org.osgi.framework.Bundle) LogService(org.osgi.service.log.LogService) Service(org.eclipse.e4.core.di.extensions.Service) ArrayList(java.util.ArrayList) List(java.util.List) Collections(java.util.Collections)

Example 10 with IObjectDescriptor

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

the class ExtendedSupplierInjectionTests method testSupplierOrdering.

/**
 * bug 428837: ensure suppliers are ranked by service.ranking
 */
@Test
public void testSupplierOrdering() {
    BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
    ExtendedObjectSupplier supplier = new ExtendedObjectSupplier() {

        @Override
        public Object get(IObjectDescriptor descriptor, IRequestor requestor, boolean track, boolean group) {
            // TODO Auto-generated method stub
            return null;
        }
    };
    Dictionary<String, Object> properties = new Hashtable<>();
    properties.put(ExtendedObjectSupplier.SERVICE_CONTEXT_KEY, EventTopic.class.getName());
    properties.put(Constants.SERVICE_RANKING, 100);
    ServiceRegistration<?> sr = bc.registerService(ExtendedObjectSupplier.SERVICE_NAME, supplier, properties);
    try {
        assertEquals(supplier, ProviderHelper.findProvider(EventTopic.class.getName(), null));
    } finally {
        sr.unregister();
    }
}
Also used : IObjectDescriptor(org.eclipse.e4.core.di.suppliers.IObjectDescriptor) EventTopic(org.eclipse.e4.core.di.extensions.EventTopic) IRequestor(org.eclipse.e4.core.di.suppliers.IRequestor) Hashtable(java.util.Hashtable) ExtendedObjectSupplier(org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

IObjectDescriptor (org.eclipse.e4.core.di.suppliers.IObjectDescriptor)7 Type (java.lang.reflect.Type)4 IBinding (org.eclipse.e4.core.di.IBinding)4 ExtendedObjectSupplier (org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier)3 Annotation (java.lang.annotation.Annotation)2 InjectionException (org.eclipse.e4.core.di.InjectionException)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2 Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Named (javax.inject.Named)1 Active (org.eclipse.e4.core.contexts.Active)1 RunAndTrack (org.eclipse.e4.core.contexts.RunAndTrack)1 Creatable (org.eclipse.e4.core.di.annotations.Creatable)1 EventTopic (org.eclipse.e4.core.di.extensions.EventTopic)1 Preference (org.eclipse.e4.core.di.extensions.Preference)1