Search in sources :

Example 1 with IRequestor

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

the class TranslationObjectSupplier method notifyRequestor.

/**
 * Notify all given {@link IRequestor} about changes for their injected values. This way the
 * dynamic injection is performed.
 *
 * @param requestors
 *            The {@link IRequestor} to inform about the instance changes.
 */
private void notifyRequestor(Collection<IRequestor> requestors) {
    if (requestors != null) {
        for (Iterator<IRequestor> it = requestors.iterator(); it.hasNext(); ) {
            IRequestor requestor = it.next();
            if (!requestor.isValid()) {
                it.remove();
                continue;
            }
            requestor.resolveArguments(false);
            requestor.execute();
        }
    }
}
Also used : IRequestor(org.eclipse.e4.core.di.suppliers.IRequestor)

Example 2 with IRequestor

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

the class PreferencesObjectSupplier method addListener.

private void addListener(String nodePath, String key, final IRequestor requestor) {
    if (requestor == null)
        return;
    synchronized (listenerCache) {
        if (listenerCache.containsKey(nodePath)) {
            HashMap<String, List<PrefInjectionListener>> map = listenerCache.get(nodePath);
            if (map.containsKey(key)) {
                for (PrefInjectionListener listener : map.get(key)) {
                    IRequestor previousRequestor = listener.getRequestor();
                    if (previousRequestor.equals(requestor))
                        // avoid adding duplicate listeners
                        return;
                }
            }
        }
    }
    final IEclipsePreferences node = InstanceScope.INSTANCE.getNode(nodePath);
    PrefInjectionListener listener = new PrefInjectionListener(node, key, requestor);
    node.addPreferenceChangeListener(listener);
    synchronized (listenerCache) {
        HashMap<String, List<PrefInjectionListener>> map = listenerCache.get(nodePath);
        if (map == null) {
            map = new HashMap<>();
            listenerCache.put(nodePath, map);
        }
        List<PrefInjectionListener> listeningRequestors = map.get(key);
        if (listeningRequestors == null) {
            listeningRequestors = new ArrayList<>();
            map.put(key, listeningRequestors);
        }
        listeningRequestors.add(listener);
    }
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IRequestor(org.eclipse.e4.core.di.suppliers.IRequestor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with IRequestor

use of org.eclipse.e4.core.di.suppliers.IRequestor 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 4 with IRequestor

use of org.eclipse.e4.core.di.suppliers.IRequestor 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 5 with IRequestor

use of org.eclipse.e4.core.di.suppliers.IRequestor 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)

Aggregations

IRequestor (org.eclipse.e4.core.di.suppliers.IRequestor)3 BundleContext (org.osgi.framework.BundleContext)3 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 Hashtable (java.util.Hashtable)2 List (java.util.List)2 InjectionException (org.eclipse.e4.core.di.InjectionException)2 Bundle (org.osgi.framework.Bundle)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Collections (java.util.Collections)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 Active (org.eclipse.e4.core.contexts.Active)1 RunAndTrack (org.eclipse.e4.core.contexts.RunAndTrack)1 EventTopic (org.eclipse.e4.core.di.extensions.EventTopic)1 Service (org.eclipse.e4.core.di.extensions.Service)1 ExtendedObjectSupplier (org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier)1 IObjectDescriptor (org.eclipse.e4.core.di.suppliers.IObjectDescriptor)1 Test (org.junit.Test)1 EventHandler (org.osgi.service.event.EventHandler)1 LogService (org.osgi.service.log.LogService)1