Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.persist.jpa

File FullTextPostProcessor.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
50% of files have more coverage

Code metrics

6
18
5
1
73
63
9
0.5
3.6
5
1.8

Classes

Class Line # Actions
FullTextPostProcessor 17 18 9
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.persist.jpa;
2   
3    import static java.lang.String.format;
4   
5    import io.sunshower.persistence.search.IndexedPersistenceContext;
6    import io.sunshower.persistence.search.SearchableIndexAware;
7    import java.lang.reflect.Field;
8    import javax.persistence.EntityManagerFactory;
9    import org.hibernate.search.jpa.FullTextEntityManager;
10    import org.hibernate.search.jpa.Search;
11    import org.springframework.aop.support.AopUtils;
12    import org.springframework.beans.BeansException;
13    import org.springframework.beans.factory.config.BeanPostProcessor;
14    import org.springframework.context.ApplicationContext;
15    import org.springframework.context.ApplicationContextAware;
16   
 
17    public class FullTextPostProcessor implements BeanPostProcessor, ApplicationContextAware {
18   
19    private EntityManagerFactory entityManagerFactory;
20   
 
21  0 toggle @Override
22    public Object postProcessBeforeInitialization(Object bean, String beanName)
23    throws BeansException {
24  0 return bean;
25    }
26   
 
27  0 toggle @Override
28    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
29  0 final Class<?> injectedClass = bean.getClass();
30  0 final Class<?> type = AopUtils.getTargetClass(bean);
31  0 if (type.isAnnotationPresent(SearchableIndexAware.class)) {
32  0 try {
33  0 inject(injectedClass, type, bean, beanName);
34    } catch (Exception e) {
35  0 throw new IllegalStateException(e);
36    }
37    }
38  0 return bean;
39    }
40   
 
41  0 toggle private void inject(Class<?> injectedClass, Class<?> type, Object bean, String beanName)
42    throws Exception {
43  0 for (final Field field : type.getDeclaredFields()) {
44  0 if (field.isAnnotationPresent(IndexedPersistenceContext.class)) {
45  0 final Class<?> ftype = field.getType();
46  0 if (FullTextEntityManager.class.isAssignableFrom(ftype)) {
47  0 setUnconditionally(injectedClass, bean, field, beanName);
48    } else {
49  0 throw new IllegalStateException(
50    format(
51    "Cannot inject IndexedEntityManager "
52    + "on field '%s' of '%s'. "
53    + "Expected class javax.jpa.EntityManager, got '%s'",
54    field.getName(), type, ftype));
55    }
56    }
57    }
58    }
59   
 
60  0 toggle private void setUnconditionally(Class<?> injectedClass, Object bean, Field field, String beanName)
61    throws IllegalAccessException, NoSuchFieldException {
62   
63  0 field.setAccessible(true);
64  0 FullTextEntityManager fullTextEntityManager =
65    Search.getFullTextEntityManager(entityManagerFactory.createEntityManager());
66  0 field.set(bean, fullTextEntityManager);
67    }
68   
 
69  0 toggle @Override
70    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
71  0 this.entityManagerFactory = applicationContext.getBean(EntityManagerFactory.class);
72    }
73    }