Search in sources :

Example 1 with ModuleBuilderSource

use of org.apache.tapestry5.ioc.ModuleBuilderSource 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 ModuleBuilderSource

use of org.apache.tapestry5.ioc.ModuleBuilderSource in project tapestry-5 by apache.

the class SpringModuleDef method createContributionToMasterObjectProvider.

private ContributionDef createContributionToMasterObjectProvider() {
    ContributionDef def = new AbstractContributionDef() {

        @Override
        public String getServiceId() {
            return "MasterObjectProvider";
        }

        @Override
        public void contribute(ModuleBuilderSource moduleSource, ServiceResources resources, OrderedConfiguration configuration) {
            final OperationTracker tracker = resources.getTracker();
            final ApplicationContext context = resources.getService(SERVICE_ID, ApplicationContext.class);
            final ObjectProvider springBeanProvider = new ObjectProvider() {

                @Override
                public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
                    Map beanMap = context.getBeansOfType(objectType);
                    switch(beanMap.size()) {
                        case 0:
                            return null;
                        case 1:
                            Object bean = beanMap.values().iterator().next();
                            return objectType.cast(bean);
                        default:
                            String message = String.format("Spring context contains %d beans assignable to type %s: %s.", beanMap.size(), PlasticUtils.toTypeName(objectType), InternalUtils.joinSorted(beanMap.keySet()));
                            throw new IllegalArgumentException(message);
                    }
                }
            };
            final ObjectProvider springBeanProviderInvoker = new ObjectProvider() {

                @Override
                public <T> T provide(final Class<T> objectType, final AnnotationProvider annotationProvider, final ObjectLocator locator) {
                    return tracker.invoke("Resolving dependency by searching Spring ApplicationContext", new Invokable<T>() {

                        @Override
                        public T invoke() {
                            return springBeanProvider.provide(objectType, annotationProvider, locator);
                        }
                    });
                }
            };
            ObjectProvider outerCheck = new ObjectProvider() {

                @Override
                public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
                    if (!applicationContextCreated.get())
                        return null;
                    return springBeanProviderInvoker.provide(objectType, annotationProvider, locator);
                }
            };
            configuration.add("SpringBean", outerCheck, "after:AnnotationBasedContributions", "after:ServiceOverride");
        }
    };
    return def;
}
Also used : OperationTracker(org.apache.tapestry5.ioc.OperationTracker) ModuleBuilderSource(org.apache.tapestry5.ioc.ModuleBuilderSource) ContributionDef(org.apache.tapestry5.ioc.def.ContributionDef) AbstractContributionDef(org.apache.tapestry5.http.internal.AbstractContributionDef) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) AbstractContributionDef(org.apache.tapestry5.http.internal.AbstractContributionDef) ServiceResources(org.apache.tapestry5.ioc.ServiceResources) Map(java.util.Map)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 AbstractContributionDef (org.apache.tapestry5.http.internal.AbstractContributionDef)1 ModuleBuilderSource (org.apache.tapestry5.ioc.ModuleBuilderSource)1 OperationTracker (org.apache.tapestry5.ioc.OperationTracker)1 ServiceResources (org.apache.tapestry5.ioc.ServiceResources)1 ContributionDef (org.apache.tapestry5.ioc.def.ContributionDef)1 DelegatingInjectionResources (org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources)1 InjectionResources (org.apache.tapestry5.ioc.internal.util.InjectionResources)1 MapInjectionResources (org.apache.tapestry5.ioc.internal.util.MapInjectionResources)1 WrongConfigurationTypeGuard (org.apache.tapestry5.ioc.internal.util.WrongConfigurationTypeGuard)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1