Search in sources :

Example 1 with AfterDeploymentValidation

use of javax.enterprise.inject.spi.AfterDeploymentValidation 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 AfterDeploymentValidation

use of javax.enterprise.inject.spi.AfterDeploymentValidation in project microservice_framework by CJSCommonPlatform.

the class InterceptorChainObserverTest method shouldRegisterInterceptor.

@Test
public void shouldRegisterInterceptor() throws Exception {
    final BeanManager beanManager = mock(BeanManager.class);
    final AfterDeploymentValidation event = mock(AfterDeploymentValidation.class);
    final Bean bean_1 = mock(Bean.class);
    final Bean bean_2 = mock(Bean.class);
    final Set<Bean<?>> beans = new HashSet<>();
    beans.add(bean_1);
    beans.add(bean_2);
    when(beanManager.getBeans(eq(Interceptor.class), any(AnnotationLiteral.class))).thenReturn(beans);
    when(bean_1.getBeanClass()).thenReturn(Object.class);
    when(bean_2.getBeanClass()).thenReturn(Object.class);
    interceptorChainObserver.afterDeploymentValidation(event, beanManager);
    final List<Bean<?>> interceptorBeans = interceptorChainObserver.getInterceptorBeans();
    assertThat(interceptorBeans, containsInAnyOrder(bean_1, bean_2));
}
Also used : AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with AfterDeploymentValidation

use of javax.enterprise.inject.spi.AfterDeploymentValidation in project microservice_framework by CJSCommonPlatform.

the class InterceptorChainObserverTest method shouldRegisterDeprecatedInterceptorChainProvider.

@Test
public void shouldRegisterDeprecatedInterceptorChainProvider() throws Exception {
    final BeanManager beanManager = mock(BeanManager.class);
    final AfterDeploymentValidation event = mock(AfterDeploymentValidation.class);
    final Bean bean_1 = mock(Bean.class);
    final Bean bean_2 = mock(Bean.class);
    final Set<Bean<?>> beans = new HashSet<>();
    beans.add(bean_1);
    beans.add(bean_2);
    when(beanManager.getBeans(eq(InterceptorChainProvider.class), any(AnnotationLiteral.class))).thenReturn(beans);
    when(bean_1.getBeanClass()).thenReturn(Object.class);
    when(bean_2.getBeanClass()).thenReturn(Object.class);
    interceptorChainObserver.afterDeploymentValidation(event, beanManager);
    final List<Bean<?>> interceptorBeans = interceptorChainObserver.getInterceptorChainProviderBeans();
    assertThat(interceptorBeans, containsInAnyOrder(bean_1, bean_2));
}
Also used : AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with AfterDeploymentValidation

use of javax.enterprise.inject.spi.AfterDeploymentValidation in project microservice_framework by CJSCommonPlatform.

the class InterceptorChainObserverTest method shouldRegisterBothTypesOfInterceptorChainProvider.

@Test
public void shouldRegisterBothTypesOfInterceptorChainProvider() throws Exception {
    final BeanManager beanManager = mock(BeanManager.class);
    final AfterDeploymentValidation event = mock(AfterDeploymentValidation.class);
    final Bean bean_1 = mock(Bean.class);
    final Bean bean_2 = mock(Bean.class);
    final Set<Bean<?>> interceptorChainEntryProviderBeans = new HashSet<>();
    interceptorChainEntryProviderBeans.add(bean_1);
    final Set<Bean<?>> interceptorChainProviderBeans = new HashSet<>();
    interceptorChainProviderBeans.add(bean_2);
    when(beanManager.getBeans(eq(InterceptorChainEntryProvider.class), any(AnnotationLiteral.class))).thenReturn(interceptorChainEntryProviderBeans);
    when(bean_1.getBeanClass()).thenReturn(Object.class);
    when(beanManager.getBeans(eq(InterceptorChainProvider.class), any(AnnotationLiteral.class))).thenReturn(interceptorChainProviderBeans);
    when(bean_2.getBeanClass()).thenReturn(Object.class);
    interceptorChainObserver.afterDeploymentValidation(event, beanManager);
    final List<Bean<?>> interceptorBeans = interceptorChainObserver.getInterceptorChainProviderBeans();
    assertThat(interceptorBeans, containsInAnyOrder(bean_1, bean_2));
}
Also used : AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with AfterDeploymentValidation

use of javax.enterprise.inject.spi.AfterDeploymentValidation in project microservice_framework by CJSCommonPlatform.

the class InterceptorChainObserverTest method shouldRegisterInterceptorChainEntryProvider.

@Test
public void shouldRegisterInterceptorChainEntryProvider() throws Exception {
    final BeanManager beanManager = mock(BeanManager.class);
    final AfterDeploymentValidation event = mock(AfterDeploymentValidation.class);
    final Bean bean_1 = mock(Bean.class);
    final Bean bean_2 = mock(Bean.class);
    final Set<Bean<?>> beans = new HashSet<>();
    beans.add(bean_1);
    beans.add(bean_2);
    when(beanManager.getBeans(eq(InterceptorChainEntryProvider.class), any(AnnotationLiteral.class))).thenReturn(beans);
    when(bean_1.getBeanClass()).thenReturn(Object.class);
    when(bean_2.getBeanClass()).thenReturn(Object.class);
    interceptorChainObserver.afterDeploymentValidation(event, beanManager);
    final List<Bean<?>> interceptorBeans = interceptorChainObserver.getInterceptorChainProviderBeans();
    assertThat(interceptorBeans, containsInAnyOrder(bean_1, bean_2));
}
Also used : AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AfterDeploymentValidation (javax.enterprise.inject.spi.AfterDeploymentValidation)12 Bean (javax.enterprise.inject.spi.Bean)11 BeanManager (javax.enterprise.inject.spi.BeanManager)11 Test (org.junit.Test)11 HashSet (java.util.HashSet)10 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)10 AfterBeanDiscovery (javax.enterprise.inject.spi.AfterBeanDiscovery)2 BeforeBeanDiscovery (javax.enterprise.inject.spi.BeforeBeanDiscovery)2 Extension (javax.enterprise.inject.spi.Extension)2 ProcessAnnotatedType (javax.enterprise.inject.spi.ProcessAnnotatedType)2 ProcessBean (javax.enterprise.inject.spi.ProcessBean)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 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1