Search in sources :

Example 1 with ComponentContextImpl

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

the class BindMethod method getParameters.

@Override
protected Object[] getParameters(Method method, BindParameters bp) {
    ComponentContextImpl key = bp.getComponentContext();
    Object[] result = new Object[m_paramTypes.size()];
    RefPair<?, ?> refPair = bp.getRefPair();
    int i = 0;
    for (ParamType pt : m_paramTypes) {
        switch(pt) {
            case serviceReference:
                result[i++] = refPair.getRef();
                break;
            case serviceObjects:
                result[i++] = bp.getComponentContext().getComponentServiceObjectsHelper().getServiceObjects(refPair.getRef());
                break;
            case map:
                result[i++] = new ReadOnlyDictionary(refPair.getRef());
                break;
            case serviceType:
                result[i++] = refPair.getServiceObject(key);
                break;
            default:
                throw new IllegalStateException("unexpected ParamType: " + pt);
        }
    }
    return result;
}
Also used : ReadOnlyDictionary(org.apache.felix.scr.impl.helper.ReadOnlyDictionary) ComponentContextImpl(org.apache.felix.scr.impl.manager.ComponentContextImpl) ParamType(org.apache.felix.scr.impl.inject.BindMethod.ParamType)

Example 2 with ComponentContextImpl

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

the class BindMethodTest method testMethod.

private void testMethod(final String methodName, final T1 component, final DSVersion dsVersion, final String expectCallPerformed) {
    ComponentContainer container = newContainer();
    SingleComponentManager icm = new SingleComponentManager(container, new ComponentMethodsImpl());
    BindMethod bm = new BindMethod(methodName, component.getClass(), FakeService.class.getName(), dsVersion, false);
    RefPair refPair = new SingleRefPair(m_serviceReference);
    ComponentContextImpl<T1> cc = new ComponentContextImpl(icm, new MockBundle(), null);
    assertTrue(bm.getServiceObject(cc, refPair, m_context, icm));
    BindParameters bp = new BindParameters(cc, refPair);
    bm.invoke(component, bp, null, icm);
    assertEquals(expectCallPerformed, component.callPerformed);
}
Also used : SingleComponentManager(org.apache.felix.scr.impl.manager.SingleComponentManager) ComponentContextImpl(org.apache.felix.scr.impl.manager.ComponentContextImpl) BindMethod(org.apache.felix.scr.impl.inject.BindMethod) RefPair(org.apache.felix.scr.impl.manager.RefPair) SingleRefPair(org.apache.felix.scr.impl.manager.SingleRefPair) BindParameters(org.apache.felix.scr.impl.inject.BindParameters) ComponentContainer(org.apache.felix.scr.impl.manager.ComponentContainer) FakeService(org.apache.felix.scr.impl.manager.components.FakeService) MockBundle(org.apache.felix.scr.impl.MockBundle) T1(org.apache.felix.scr.impl.manager.components.T1) SingleRefPair(org.apache.felix.scr.impl.manager.SingleRefPair)

Example 3 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) throws InvocationTargetException {
    @SuppressWarnings("rawtypes") 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 == ValueType.ref_map || this.valueType == ValueType.ref_tuple) {
                if (this.metadata.isStatic()) {
                    return MethodResult.REACTIVATE;
                }
                final Object obj = ValueUtils.getValue(componentInstance.getClass().getName(), valueType, field.getType(), key, refPair);
                this.setFieldValue(componentInstance, obj);
                bp.getComponentContext().getBoundValues(metadata.getName()).put(refPair, obj);
            }
        } else // bind needs always be done
        {
            final Object obj = ValueUtils.getValue(componentInstance.getClass().getName(), valueType, field.getType(), 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 = ValueUtils.getValue(componentInstance.getClass().getName(), valueType, field.getType(), 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 == ValueType.ref_map || this.valueType == ValueType.ref_tuple) {
                if (!this.metadata.isStatic()) {
                    final Object obj = ValueUtils.getValue(componentInstance.getClass().getName(), valueType, field.getType(), 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)

Example 4 with ComponentContextImpl

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

the class ActivateMethodTest method checkMethod.

/**
 * Checks whether a method with the given name can be found for the
 * activate/deactivate method parameter list and whether the method returns
 * the expected description when called.
 *
 * @param obj
 * @param methodName
 * @param methodDesc
 * @param version DSVersion tested
 */
private void checkMethod(BaseObject obj, String methodName, String methodDesc, DSVersion version) {
    ComponentContainer<?> container = newContainer();
    SingleComponentManager<?> icm = new SingleComponentManager(container, new ComponentMethodsImpl());
    ActivateMethod am = new ActivateMethod(methodName, methodName != null, obj.getClass(), version, false, false);
    am.invoke(obj, new ActivatorParameter(new ComponentContextImpl(icm, m_bundle, null), -1), null);
    Method m = am.getMethod();
    assertNotNull(m);
    assertEquals(methodName, m.getName());
    assertEquals(methodDesc, obj.getCalledMethod());
}
Also used : SingleComponentManager(org.apache.felix.scr.impl.manager.SingleComponentManager) ComponentContextImpl(org.apache.felix.scr.impl.manager.ComponentContextImpl) ActivatorParameter(org.apache.felix.scr.impl.inject.ActivatorParameter) ComponentMethodsImpl(org.apache.felix.scr.impl.inject.ComponentMethodsImpl) AcceptMethod(org.apache.felix.scr.impl.metadata.instances.AcceptMethod) Method(java.lang.reflect.Method)

Example 5 with ComponentContextImpl

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

the class ActivateMethodTest method ensureMethodNotFoundMethod.

/**
 * Ensures no method with the given name accepting any of the
 * activate/deactive method parameters can be found.
 *
 * @param obj
 * @param methodName
 * @param version DS version tested
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 */
private void ensureMethodNotFoundMethod(BaseObject obj, String methodName, DSVersion version) {
    ComponentContainer container = newContainer();
    SingleComponentManager icm = new SingleComponentManager(container, new ComponentMethodsImpl());
    ActivateMethod am = new ActivateMethod(methodName, methodName != null, obj.getClass(), version, false, false);
    am.invoke(obj, new ActivatorParameter(new ComponentContextImpl(icm, m_bundle, null), -1), null);
    Method m = am.getMethod();
    assertNull(m);
    assertNull(obj.getCalledMethod());
}
Also used : SingleComponentManager(org.apache.felix.scr.impl.manager.SingleComponentManager) ComponentContextImpl(org.apache.felix.scr.impl.manager.ComponentContextImpl) ActivatorParameter(org.apache.felix.scr.impl.inject.ActivatorParameter) ComponentContainer(org.apache.felix.scr.impl.manager.ComponentContainer) ComponentMethodsImpl(org.apache.felix.scr.impl.inject.ComponentMethodsImpl) AcceptMethod(org.apache.felix.scr.impl.metadata.instances.AcceptMethod) Method(java.lang.reflect.Method)

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