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);
}
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;
}
Aggregations