1
1
/*
2
- * Copyright 2014 the original author or authors.
2
+ * Copyright 2014-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import static org .springframework .beans .factory .BeanFactoryUtils .*;
20
20
21
21
import java .util .ArrayList ;
22
- import java .util .Arrays ;
23
22
import java .util .Collection ;
23
+ import java .util .Collections ;
24
24
import java .util .HashSet ;
25
25
import java .util .List ;
26
26
import java .util .Set ;
33
33
import org .springframework .beans .factory .config .BeanDefinition ;
34
34
import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
35
35
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
36
+ import org .springframework .jndi .JndiObjectFactoryBean ;
36
37
import org .springframework .orm .jpa .AbstractEntityManagerFactoryBean ;
38
+ import org .springframework .util .ClassUtils ;
37
39
38
40
/**
39
41
* Utility methods to work with {@link BeanDefinition} instances from {@link BeanFactoryPostProcessor}s.
42
44
*/
43
45
public class BeanDefinitionUtils {
44
46
47
+ private static final String JNDI_OBJECT_FACTORY_BEAN = "org.springframework.jndi.JndiObjectFactoryBean" ;
48
+ private static final List <Class <?>> EMF_TYPES ;
49
+
50
+ static {
51
+
52
+ List <Class <?>> types = new ArrayList <Class <?>>();
53
+ types .add (EntityManagerFactory .class );
54
+ types .add (AbstractEntityManagerFactoryBean .class );
55
+
56
+ if (ClassUtils .isPresent (JNDI_OBJECT_FACTORY_BEAN , ClassUtils .getDefaultClassLoader ())) {
57
+ types .add (JndiObjectFactoryBean .class );
58
+ }
59
+
60
+ EMF_TYPES = Collections .unmodifiableList (types );
61
+ }
62
+
45
63
/**
46
64
* Return all bean names for bean definitions that will result in an {@link EntityManagerFactory} eventually. We're
47
65
* checking for {@link EntityManagerFactory} and the well-known factory beans here to avoid eager initialization of
@@ -71,24 +89,15 @@ public static Iterable<String> getEntityManagerFactoryBeanNames(ListableBeanFact
71
89
* @param beanFactory must not be {@literal null}.
72
90
* @return
73
91
*/
74
- @ SuppressWarnings ("unchecked" )
75
92
public static Collection <EntityManagerFactoryBeanDefinition > getEntityManagerFactoryBeanDefinitions (
76
93
ConfigurableListableBeanFactory beanFactory ) {
77
94
78
95
List <EntityManagerFactoryBeanDefinition > definitions = new ArrayList <EntityManagerFactoryBeanDefinition >();
79
96
80
- for (Class <?> type : Arrays . asList ( EntityManagerFactory . class , AbstractEntityManagerFactoryBean . class ) ) {
97
+ for (Class <?> type : EMF_TYPES ) {
81
98
82
99
for (String name : beanFactory .getBeanNamesForType (type , true , false )) {
83
-
84
- String transformedName = transformedBeanName (name );
85
-
86
- EntityManagerFactoryBeanDefinition definition = new EntityManagerFactoryBeanDefinition ( //
87
- transformedName , //
88
- beanFactory , //
89
- beanFactory .getBeanDefinition (transformedName ));
90
-
91
- definitions .add (definition );
100
+ registerEntityManagerFactoryBeanDefinition (transformedBeanName (name ), beanFactory , definitions );
92
101
}
93
102
}
94
103
@@ -101,6 +110,28 @@ public static Collection<EntityManagerFactoryBeanDefinition> getEntityManagerFac
101
110
return definitions ;
102
111
}
103
112
113
+ /**
114
+ * Registers an {@link EntityManagerFactoryBeanDefinition} for the bean with the given name. Drops
115
+ * {@link JndiObjectFactoryBean} instances that don't point to an {@link EntityManagerFactory} bean as expected type.
116
+ *
117
+ * @param name
118
+ * @param beanFactory
119
+ * @param definitions
120
+ */
121
+ private static void registerEntityManagerFactoryBeanDefinition (String name ,
122
+ ConfigurableListableBeanFactory beanFactory , List <EntityManagerFactoryBeanDefinition > definitions ) {
123
+
124
+ BeanDefinition definition = beanFactory .getBeanDefinition (name );
125
+
126
+ if (JNDI_OBJECT_FACTORY_BEAN .equals (definition .getBeanClassName ())) {
127
+ if (!definition .getPropertyValues ().get ("expectedType" ).equals (EntityManagerFactory .class .getName ())) {
128
+ return ;
129
+ }
130
+ }
131
+
132
+ definitions .add (new EntityManagerFactoryBeanDefinition (name , beanFactory ));
133
+ }
134
+
104
135
/**
105
136
* Returns the {@link BeanDefinition} with the given name, obtained from the given {@link BeanFactory} or one of its
106
137
* parents.
@@ -134,21 +165,18 @@ public static BeanDefinition getBeanDefinition(String name, ConfigurableListable
134
165
public static class EntityManagerFactoryBeanDefinition {
135
166
136
167
private final String beanName ;
137
- private final BeanFactory beanFactory ;
138
- private final BeanDefinition beanDefinition ;
168
+ private final ConfigurableListableBeanFactory beanFactory ;
139
169
140
170
/**
141
171
* Creates a new {@link EntityManagerFactoryBeanDefinition}.
142
172
*
143
173
* @param beanName
144
174
* @param beanFactory
145
- * @param beanDefinition
146
175
*/
147
- public EntityManagerFactoryBeanDefinition (String beanName , BeanFactory beanFactory , BeanDefinition beanDefinition ) {
176
+ public EntityManagerFactoryBeanDefinition (String beanName , ConfigurableListableBeanFactory beanFactory ) {
148
177
149
178
this .beanName = beanName ;
150
179
this .beanFactory = beanFactory ;
151
- this .beanDefinition = beanDefinition ;
152
180
}
153
181
154
182
/**
@@ -175,7 +203,7 @@ public BeanFactory getBeanFactory() {
175
203
* @return
176
204
*/
177
205
public BeanDefinition getBeanDefinition () {
178
- return beanDefinition ;
206
+ return beanFactory . getBeanDefinition ( beanName ) ;
179
207
}
180
208
}
181
209
}
0 commit comments