Search in sources :

Example 1 with PersistenceUnitInfoBuilder

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);
        }
    });
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) MappedSuperclass(javax.persistence.MappedSuperclass) ProcessBean(javax.enterprise.inject.spi.ProcessBean) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CreationalContext(javax.enterprise.context.spi.CreationalContext) BeforeBeanDiscovery(javax.enterprise.inject.spi.BeforeBeanDiscovery) Collectors.toMap(java.util.stream.Collectors.toMap) Observes(javax.enterprise.event.Observes) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) Map(java.util.Map) SynchronizationType(javax.persistence.SynchronizationType) DataSource(javax.sql.DataSource) Jpa(org.apache.meecrowave.jpa.api.Jpa) Entity(javax.persistence.Entity) Extension(javax.enterprise.inject.spi.Extension) Optional.ofNullable(java.util.Optional.ofNullable) PersistenceUnitInfoBuilder(org.apache.meecrowave.jpa.api.PersistenceUnitInfoBuilder) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Collection(java.util.Collection) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) WithAnnotations(javax.enterprise.inject.spi.WithAnnotations) Set(java.util.Set) List(java.util.List) Stream(java.util.stream.Stream) Unit(org.apache.meecrowave.jpa.api.Unit) Annotation(java.lang.annotation.Annotation) Function.identity(java.util.function.Function.identity) ServletContext(javax.servlet.ServletContext) Embeddable(javax.persistence.Embeddable) Meecrowave(org.apache.meecrowave.Meecrowave) Bean(javax.enterprise.inject.spi.Bean) BeanManager(javax.enterprise.inject.spi.BeanManager) PersistenceUnitInfoBuilder(org.apache.meecrowave.jpa.api.PersistenceUnitInfoBuilder) HashMap(java.util.HashMap) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Annotation(java.lang.annotation.Annotation) ProcessBean(javax.enterprise.inject.spi.ProcessBean) Bean(javax.enterprise.inject.spi.Bean) CreationalContext(javax.enterprise.context.spi.CreationalContext) ServletContext(javax.servlet.ServletContext) Meecrowave(org.apache.meecrowave.Meecrowave)

Example 2 with PersistenceUnitInfoBuilder

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();
}
Also used : PersistenceUnitInfoBuilder(org.apache.meecrowave.jpa.api.PersistenceUnitInfoBuilder) ProcessBean(javax.enterprise.inject.spi.ProcessBean) Bean(javax.enterprise.inject.spi.Bean) DataSource(javax.sql.DataSource)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)2 ProcessBean (javax.enterprise.inject.spi.ProcessBean)2 DataSource (javax.sql.DataSource)2 PersistenceUnitInfoBuilder (org.apache.meecrowave.jpa.api.PersistenceUnitInfoBuilder)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Set (java.util.Set)1 Function.identity (java.util.function.Function.identity)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Stream (java.util.stream.Stream)1 CreationalContext (javax.enterprise.context.spi.CreationalContext)1 Observes (javax.enterprise.event.Observes)1 AfterBeanDiscovery (javax.enterprise.inject.spi.AfterBeanDiscovery)1 AfterDeploymentValidation (javax.enterprise.inject.spi.AfterDeploymentValidation)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1