Search in sources :

Example 6 with ComponentContextImpl

use of org.apache.felix.scr.impl.manager.ComponentContextImpl in project felix by apache.

the class FieldHandler method updateField.

private MethodResult updateField(final METHOD_TYPE mType, final Object componentInstance, final BindParameters bp, final SimpleLogger logger) throws InvocationTargetException {
    final ComponentContextImpl key = bp.getComponentContext();
    final RefPair<?, ?> refPair = bp.getRefPair();
    if (!this.metadata.isMultiple()) {
        // unbind needs only be done, if reference is dynamic and optional
        if (mType == METHOD_TYPE.UNBIND) {
            if (this.metadata.isOptional() && !this.metadata.isStatic()) {
                // we only reset if it was previously set with this value
                if (bp.getComponentContext().getBoundValues(metadata.getName()).size() == 1) {
                    this.setFieldValue(componentInstance, null);
                }
            }
            bp.getComponentContext().getBoundValues(metadata.getName()).remove(refPair);
        } else // for a static reference we need a reactivation
        if (mType == METHOD_TYPE.UPDATED) {
            if (this.valueType == ParamType.map || this.valueType == ParamType.tuple) {
                if (this.metadata.isStatic()) {
                    return MethodResult.REACTIVATE;
                }
                final Object obj = getValue(key, refPair);
                this.setFieldValue(componentInstance, obj);
                bp.getComponentContext().getBoundValues(metadata.getName()).put(refPair, obj);
            }
        } else // bind needs always be done
        {
            final Object obj = getValue(key, refPair);
            this.setFieldValue(componentInstance, obj);
            bp.getComponentContext().getBoundValues(metadata.getName()).put(refPair, obj);
        }
    } else {
        // bind: replace or update the field
        if (mType == METHOD_TYPE.BIND) {
            final Object obj = getValue(key, refPair);
            bp.getComponentContext().getBoundValues(metadata.getName()).put(refPair, obj);
            if (metadata.isReplace()) {
                this.setFieldValue(componentInstance, getReplaceCollection(bp));
            } else {
                @SuppressWarnings("unchecked") final Collection<Object> col = (Collection<Object>) this.getFieldValue(componentInstance);
                col.add(obj);
            }
        } else // unbind needs only be done, if reference is dynamic
        if (mType == METHOD_TYPE.UNBIND) {
            if (!metadata.isStatic()) {
                final Object obj = bp.getComponentContext().getBoundValues(metadata.getName()).remove(refPair);
                if (metadata.isReplace()) {
                    this.setFieldValue(componentInstance, getReplaceCollection(bp));
                } else {
                    @SuppressWarnings("unchecked") final Collection<Object> col = (Collection<Object>) this.getFieldValue(componentInstance);
                    col.remove(obj);
                }
            }
        } else // updated needs only be done, if the value type is map or tuple
        if (mType == METHOD_TYPE.UPDATED) {
            if (this.valueType == ParamType.map || this.valueType == ParamType.tuple) {
                if (!this.metadata.isStatic()) {
                    final Object obj = getValue(key, refPair);
                    final Object oldObj = bp.getComponentContext().getBoundValues(metadata.getName()).put(refPair, obj);
                    if (metadata.isReplace()) {
                        this.setFieldValue(componentInstance, getReplaceCollection(bp));
                    } else {
                        @SuppressWarnings("unchecked") final Collection<Object> col = (Collection<Object>) this.getFieldValue(componentInstance);
                        col.add(obj);
                        col.remove(oldObj);
                    }
                } else {
                    // if it's static we need to reactivate
                    return MethodResult.REACTIVATE;
                }
            }
        }
    }
    return MethodResult.VOID;
}
Also used : ComponentContextImpl(org.apache.felix.scr.impl.manager.ComponentContextImpl) Collection(java.util.Collection)

Aggregations

ComponentContextImpl (org.apache.felix.scr.impl.manager.ComponentContextImpl)6 SingleComponentManager (org.apache.felix.scr.impl.manager.SingleComponentManager)3 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 ActivatorParameter (org.apache.felix.scr.impl.inject.ActivatorParameter)2 ComponentMethodsImpl (org.apache.felix.scr.impl.inject.ComponentMethodsImpl)2 ComponentContainer (org.apache.felix.scr.impl.manager.ComponentContainer)2 AcceptMethod (org.apache.felix.scr.impl.metadata.instances.AcceptMethod)2 MockBundle (org.apache.felix.scr.impl.MockBundle)1 ReadOnlyDictionary (org.apache.felix.scr.impl.helper.ReadOnlyDictionary)1 BindMethod (org.apache.felix.scr.impl.inject.BindMethod)1 ParamType (org.apache.felix.scr.impl.inject.BindMethod.ParamType)1 BindParameters (org.apache.felix.scr.impl.inject.BindParameters)1 RefPair (org.apache.felix.scr.impl.manager.RefPair)1 SingleRefPair (org.apache.felix.scr.impl.manager.SingleRefPair)1 FakeService (org.apache.felix.scr.impl.manager.components.FakeService)1 T1 (org.apache.felix.scr.impl.manager.components.T1)1