Search in sources :

Example 1 with DelegatingInjectionResources

use of org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources in project tapestry-5 by apache.

the class ContributionDefImpl method invokeMethod.

private <T> void invokeMethod(ModuleBuilderSource source, ServiceResources resources, Class<T> parameterType, T parameterValue) {
    Map<Class, Object> resourceMap = CollectionFactory.newMap();
    resourceMap.put(parameterType, parameterValue);
    resourceMap.put(ObjectLocator.class, resources);
    resourceMap.put(Logger.class, resources.getLogger());
    InjectionResources injectionResources = new MapInjectionResources(resourceMap);
    for (Class t : CONFIGURATION_TYPES) {
        if (parameterType != t) {
            injectionResources = new DelegatingInjectionResources(new WrongConfigurationTypeGuard(resources.getServiceId(), t, parameterType), injectionResources);
        }
    }
    Throwable fail = null;
    Object moduleInstance = InternalUtils.isStatic(contributorMethod) ? null : source.getModuleBuilder();
    try {
        ObjectCreator[] parameters = InternalUtils.calculateParametersForMethod(contributorMethod, resources, injectionResources, resources.getTracker());
        contributorMethod.invoke(moduleInstance, InternalUtils.realizeObjects(parameters));
    } catch (InvocationTargetException ex) {
        fail = ex.getTargetException();
    } catch (Exception ex) {
        fail = ex;
    }
    if (fail != null)
        throw new RuntimeException(IOCMessages.contributionMethodError(contributorMethod, fail), fail);
}
Also used : WrongConfigurationTypeGuard(org.apache.tapestry5.ioc.internal.util.WrongConfigurationTypeGuard) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) DelegatingInjectionResources(org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources) DelegatingInjectionResources(org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with DelegatingInjectionResources

use of org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources in project tapestry-5 by apache.

the class AbstractServiceCreator method createInjectionResources.

/**
 * Returns a map (based on injectionResources) that includes (possibly) an additional mapping containing the
 * collected configuration data. This involves scanning the parameters and generic types.
 */
protected final InjectionResources createInjectionResources() {
    InjectionResources core = new MapInjectionResources(injectionResources);
    InjectionResources configurations = new InjectionResources() {

        private boolean seenOne;

        @Override
        public <T> T findResource(Class<T> resourceType, Type genericType) {
            ConfigurationType thisType = PARAMETER_TYPE_TO_CONFIGURATION_TYPE.get(resourceType);
            if (thisType == null)
                return null;
            if (seenOne)
                throw new RuntimeException(IOCMessages.tooManyConfigurationParameters(creatorDescription));
            seenOne = true;
            switch(thisType) {
                case UNORDERED:
                    return resourceType.cast(getUnorderedConfiguration(genericType));
                case ORDERED:
                    return resourceType.cast(getOrderedConfiguration(genericType));
                case MAPPED:
                    return resourceType.cast(getMappedConfiguration(genericType));
            }
            return null;
        }
    };
    return new DelegatingInjectionResources(core, configurations);
}
Also used : ConfigurationType(org.apache.tapestry5.ioc.internal.ConfigurationType) ConfigurationType(org.apache.tapestry5.ioc.internal.ConfigurationType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) DelegatingInjectionResources(org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources) DelegatingInjectionResources(org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources)

Aggregations

DelegatingInjectionResources (org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources)2 InjectionResources (org.apache.tapestry5.ioc.internal.util.InjectionResources)2 MapInjectionResources (org.apache.tapestry5.ioc.internal.util.MapInjectionResources)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ConfigurationType (org.apache.tapestry5.ioc.internal.ConfigurationType)1 WrongConfigurationTypeGuard (org.apache.tapestry5.ioc.internal.util.WrongConfigurationTypeGuard)1