Search in sources :

Example 11 with ServiceResources

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

the class RegistryImpl method findAdvisorsForService.

@Override
public List<ServiceAdvisor> findAdvisorsForService(ServiceDef3 serviceDef) {
    lock.check();
    assert serviceDef != null;
    Logger logger = getServiceLogger(serviceDef.getServiceId());
    Orderer<ServiceAdvisor> orderer = new Orderer<ServiceAdvisor>(logger, true);
    for (Module module : moduleToServiceDefs.keySet()) {
        Set<AdvisorDef> advisorDefs = module.findMatchingServiceAdvisors(serviceDef);
        if (advisorDefs.isEmpty())
            continue;
        ServiceResources resources = new ServiceResourcesImpl(this, module, serviceDef, proxyFactory, logger);
        for (AdvisorDef advisorDef : advisorDefs) {
            ServiceAdvisor advisor = advisorDef.createAdvisor(module, resources);
            orderer.add(advisorDef.getAdvisorId(), advisor, advisorDef.getConstraints());
        }
    }
    return orderer.getOrdered();
}
Also used : ServiceResources(org.apache.tapestry5.ioc.ServiceResources) Logger(org.slf4j.Logger) TapestryIOCModule(org.apache.tapestry5.ioc.modules.TapestryIOCModule) ServiceAdvisor(org.apache.tapestry5.ioc.ServiceAdvisor) AdvisorDef(org.apache.tapestry5.ioc.AdvisorDef) Orderer(org.apache.tapestry5.ioc.internal.util.Orderer)

Example 12 with ServiceResources

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

ServiceResources (org.apache.tapestry5.ioc.ServiceResources)7 Logger (org.slf4j.Logger)5 Orderer (org.apache.tapestry5.ioc.internal.util.Orderer)2 TapestryIOCModule (org.apache.tapestry5.ioc.modules.TapestryIOCModule)2 MethodAdvice (org.apache.tapestry5.plastic.MethodAdvice)2 MethodInvocation (org.apache.tapestry5.plastic.MethodInvocation)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 ExceptionHandlerAssistant (org.apache.tapestry5.ExceptionHandlerAssistant)1 ObjectCreator (org.apache.tapestry5.commons.ObjectCreator)1 AbstractContributionDef (org.apache.tapestry5.http.internal.AbstractContributionDef)1 AdvisorDef (org.apache.tapestry5.ioc.AdvisorDef)1 ModuleBuilderSource (org.apache.tapestry5.ioc.ModuleBuilderSource)1 OperationTracker (org.apache.tapestry5.ioc.OperationTracker)1 ServiceAdvisor (org.apache.tapestry5.ioc.ServiceAdvisor)1 ServiceDecorator (org.apache.tapestry5.ioc.ServiceDecorator)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