use of org.apache.meecrowave.jpa.api.PersistenceUnitInfoBuilder in project meecrowave by apache.
the class JpaExtension method initBeans.
void initBeans(@Observes final AfterDeploymentValidation adv, final BeanManager bm) {
if (entityManagerBeans.isEmpty()) {
return;
}
// only not portable part is this config read, could be optional
final ServletContext sc = ServletContext.class.cast(bm.getReference(bm.resolve(bm.getBeans(ServletContext.class)), ServletContext.class, bm.createCreationalContext(null)));
final Meecrowave.Builder config = Meecrowave.Builder.class.cast(sc.getAttribute("meecrowave.configuration"));
final Map<String, String> props = new HashMap<>();
if (config != null) {
ofNullable(config.getProperties()).ifPresent(p -> p.stringPropertyNames().stream().filter(k -> k.startsWith("jpa.property.")).forEach(k -> props.put(k.substring("jpa.property.".length()), p.getProperty(k))));
}
final Map<String, PersistenceUnitInfo> infoIndex = unitBuilders.stream().map(bean -> {
final CreationalContext<?> cc = bm.createCreationalContext(null);
try {
final Bean<?> resolvedBean = bm.resolve(bm.getBeans(PersistenceUnitInfoBuilder.class, bean.getQualifiers().toArray(new Annotation[bean.getQualifiers().size()])));
final PersistenceUnitInfoBuilder builder = PersistenceUnitInfoBuilder.class.cast(bm.getReference(resolvedBean, PersistenceUnitInfoBuilder.class, cc));
if (builder.getManagedClasses().isEmpty()) {
builder.setManagedClassNames(jpaClasses).setExcludeUnlistedClasses(true);
}
props.forEach(builder::addProperty);
return builder.toInfo();
} finally {
cc.release();
}
}).collect(toMap(PersistenceUnitInfo::getPersistenceUnitName, identity()));
entityManagerBeans.forEach((k, e) -> {
PersistenceUnitInfo info = infoIndex.get(k.unitName);
if (info == null) {
info = tryCreateDefaultPersistenceUnit(k.unitName, bm, props);
}
if (info == null) {
// not valid
adv.addDeploymentProblem(new IllegalArgumentException("Didn't find any PersistenceUnitInfoBuilder for " + k));
} else {
e.init(info, bm);
}
});
}
use of org.apache.meecrowave.jpa.api.PersistenceUnitInfoBuilder in project meecrowave by apache.
the class JpaExtension method tryCreateDefaultPersistenceUnit.
private PersistenceUnitInfo tryCreateDefaultPersistenceUnit(final String unitName, final BeanManager bm, final Map<String, String> props) {
final Set<Bean<?>> beans = bm.getBeans(DataSource.class);
final Bean<?> bean = bm.resolve(beans);
if (bean == null || !bm.isNormalScope(bean.getScope())) {
return null;
}
final DataSource ds = DataSource.class.cast(bm.getReference(bean, DataSource.class, bm.createCreationalContext(null)));
final PersistenceUnitInfoBuilder builder = new PersistenceUnitInfoBuilder().setManagedClassNames(jpaClasses).setExcludeUnlistedClasses(true).setUnitName(unitName).setDataSource(ds);
props.forEach(builder::addProperty);
return builder.toInfo();
}
Aggregations