Search in sources :

Example 1 with MasterObjectProvider

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

the class RegistryImpl method getObject.

@Override
public <T> T getObject(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator, Module localModule) {
    lock.check();
    AnnotationProvider effectiveProvider = annotationProvider != null ? annotationProvider : new NullAnnotationProvider();
    // We do a check here for known marker/type combinations, so that you can use a marker
    // annotation
    // to inject into a contribution method that contributes to MasterObjectProvider.
    // We also force a contribution into MasterObjectProvider to accomplish the same thing.
    T result = findServiceByMarkerAndType(objectType, annotationProvider, localModule);
    if (result != null)
        return result;
    MasterObjectProvider masterProvider = getService(IOCConstants.MASTER_OBJECT_PROVIDER_SERVICE_ID, MasterObjectProvider.class);
    return masterProvider.provide(objectType, effectiveProvider, locator, true);
}
Also used : NullAnnotationProvider(org.apache.tapestry5.commons.internal.NullAnnotationProvider) NullAnnotationProvider(org.apache.tapestry5.commons.internal.NullAnnotationProvider) MasterObjectProvider(org.apache.tapestry5.ioc.services.MasterObjectProvider)

Example 2 with MasterObjectProvider

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

the class RegistryImpl method getOrderedConfiguration.

@Override
@SuppressWarnings("unchecked")
public <T> List<T> getOrderedConfiguration(ServiceDef3 serviceDef, Class<T> objectType) {
    lock.check();
    String serviceId = serviceDef.getServiceId();
    Logger logger = getServiceLogger(serviceId);
    Orderer<T> orderer = new Orderer<T>(logger);
    Map<String, OrderedConfigurationOverride<T>> overrides = CollectionFactory.newCaseInsensitiveMap();
    // TAP5-2129. NOTICE: if someday an ordering between modules is added, this should be reverted
    // or a notice added to the documentation.
    List<Module> modules = new ArrayList<Module>(moduleToServiceDefs.keySet());
    Collections.sort(modules, new ModuleComparator());
    for (Module m : modules) addToOrderedConfiguration(orderer, overrides, objectType, serviceDef, m);
    if (serviceId.equals("MasterObjectProvider")) {
        ObjectProvider contribution = new ObjectProvider() {

            @Override
            public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
                return findServiceByMarkerAndType(objectType, annotationProvider, null);
            }
        };
        orderer.add("ServiceByMarker", (T) contribution);
    }
    for (OrderedConfigurationOverride<T> override : overrides.values()) override.apply();
    final List<T> result = orderer.getOrdered();
    if (!isServiceConfigurationListenerServiceDef(serviceDef)) {
        serviceConfigurationListener.onOrderedConfiguration(serviceDef, result);
    }
    return result;
}
Also used : NullAnnotationProvider(org.apache.tapestry5.commons.internal.NullAnnotationProvider) Logger(org.slf4j.Logger) MasterObjectProvider(org.apache.tapestry5.ioc.services.MasterObjectProvider) TapestryIOCModule(org.apache.tapestry5.ioc.modules.TapestryIOCModule) Orderer(org.apache.tapestry5.ioc.internal.util.Orderer)

Example 3 with MasterObjectProvider

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

NullAnnotationProvider (org.apache.tapestry5.commons.internal.NullAnnotationProvider)2 MasterObjectProvider (org.apache.tapestry5.ioc.services.MasterObjectProvider)2 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 Orderer (org.apache.tapestry5.ioc.internal.util.Orderer)1 TapestryIOCModule (org.apache.tapestry5.ioc.modules.TapestryIOCModule)1 Logger (org.slf4j.Logger)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