01: package org.vraptor.plugin.jpa;
02:
03: import java.lang.reflect.Field;
04: import java.util.List;
05:
06: import javax.persistence.EntityManager;
07:
08: import org.vraptor.component.BeanConstructor;
09: import org.vraptor.component.ComponentType;
10: import org.vraptor.component.ComponentTypeWrapper;
11: import org.vraptor.interceptor.InterceptorType;
12:
13: /**
14: * Wraps the standard {@link ComponentType} providing JPA support and
15: * {@link EntityManager} injection.
16: *
17: * @author Fabio Kung
18: * @since 2.3.2
19: */
20: class JavaPersistenceComponent extends ComponentTypeWrapper {
21:
22: private EntityManagerIntrospector introspector;
23:
24: JavaPersistenceComponent(ComponentType component,
25: EntityManagerIntrospector introspector) {
26: super (component);
27: this .introspector = introspector;
28: }
29:
30: @Override
31: public List<InterceptorType> getInterceptors() {
32: List<InterceptorType> interceptors = super .getInterceptors();
33: InterceptorType interceptorType = InterceptorType
34: .getType(JavaPersistenceInterceptor.class);
35: if (!interceptors.contains(interceptorType)) {
36: interceptors.add(interceptorType);
37: }
38: return interceptors;
39: }
40:
41: @Override
42: public BeanConstructor getConstructor() {
43: Field field = this .introspector.getEntityManagerField(this );
44: // wrap if the component has any @PersistenceContext
45: BeanConstructor constructor;
46: if (field == null) {
47: constructor = super .getConstructor();
48: } else {
49: constructor = new EntityManagerInjector(super
50: .getConstructor(), this.introspector);
51: }
52:
53: return constructor;
54: }
55: }
|