Search in sources :

Example 21 with ObjectLocator

use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.

the class JpaTestModule method configurePersistenceUnit.

@Contribute(EntityManagerSource.class)
public static void configurePersistenceUnit(MappedConfiguration<String, PersistenceUnitConfigurer> cfg, final ObjectLocator objectLocator) {
    PersistenceUnitConfigurer configurer = new PersistenceUnitConfigurer() {

        @Override
        public void configure(TapestryPersistenceUnitInfo unitInfo) {
            unitInfo.transactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL).persistenceProviderClassName("org.eclipse.persistence.jpa.PersistenceProvider").excludeUnlistedClasses(true).addProperty("javax.persistence.jdbc.user", "sa").addProperty("javax.persistence.jdbc.password", "sa").addProperty("javax.persistence.jdbc.driver", "org.h2.Driver").addProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:jpatest").addProperty("eclipselink.ddl-generation", "create-or-extend-tables").addProperty("eclipselink.logging.level", "FINE").addManagedClass(ThingOne.class).addManagedClass(ThingTwo.class).addManagedClass(VersionedThing.class);
            unitInfo.getProperties().put("javax.persistence.bean.manager", objectLocator.autobuild(TapestryCDIBeanManagerForJPAEntityListeners.class));
        }
    };
    // cfg.add("jpatest", configurer);
    cfg.add("TestUnit", configurer);
}
Also used : PersistenceUnitConfigurer(org.apache.tapestry5.jpa.PersistenceUnitConfigurer) ThingTwo(org.apache.tapestry5.jpa.test.entities.ThingTwo) TapestryCDIBeanManagerForJPAEntityListeners(org.apache.tapestry5.internal.jpa.TapestryCDIBeanManagerForJPAEntityListeners) TapestryPersistenceUnitInfo(org.apache.tapestry5.jpa.TapestryPersistenceUnitInfo) Contribute(org.apache.tapestry5.ioc.annotations.Contribute)

Example 22 with ObjectLocator

use of org.apache.tapestry5.commons.ObjectLocator 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

Asset (org.apache.tapestry5.Asset)5 AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)5 ObjectLocator (org.apache.tapestry5.commons.ObjectLocator)5 Test (org.testng.annotations.Test)5 ObjectProvider (org.apache.tapestry5.commons.ObjectProvider)4 Path (org.apache.tapestry5.annotations.Path)3 Map (java.util.Map)2 ComponentResources (org.apache.tapestry5.ComponentResources)2 NullAnnotationProvider (org.apache.tapestry5.commons.internal.NullAnnotationProvider)2 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 IntermediateType (org.apache.tapestry5.ioc.annotations.IntermediateType)2 MasterObjectProvider (org.apache.tapestry5.ioc.services.MasterObjectProvider)2 SymbolSource (org.apache.tapestry5.ioc.services.SymbolSource)2 AssetSource (org.apache.tapestry5.services.AssetSource)2 ObjectStreamException (java.io.ObjectStreamException)1 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 DateFormat (java.text.DateFormat)1