/
Configuration.java
309 lines (258 loc) · 12.4 KB
/
Configuration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.jpa.config;
import java.util.HashMap;
import java.util.Map;
import org.jipijapa.plugin.spi.PersistenceUnitMetadata;
/**
* configuration properties that may appear in persistence.xml
*
* @author Scott Marlow
*/
public class Configuration {
/**
* name of the AS module that contains the persistence provider
*/
public static final String PROVIDER_MODULE = "jboss.as.jpa.providerModule";
/**
* Hibernate 4.3.x (default) persistence provider
*/
public static final String PROVIDER_MODULE_HIBERNATE4_3 = "org.hibernate";
/**
* Hibernate 4.1.x persistence provider, note that Hibernate 4.1.x is expected to be in the 4.1 slot
*/
public static final String PROVIDER_MODULE_HIBERNATE4_1 = "org.hibernate:4.1";
/**
* Hibernate OGM persistence provider
*/
public static final String PROVIDER_MODULE_HIBERNATE_OGM = "org.hibernate.ogm";
public static final String PROVIDER_MODULE_ECLIPSELINK = "org.eclipse.persistence";
public static final String PROVIDER_MODULE_TOPLINK = "oracle.toplink";
public static final String PROVIDER_MODULE_DATANUCLEUS = "org.datanucleus";
public static final String PROVIDER_MODULE_DATANUCLEUS_GAE = "org.datanucleus:appengine";
public static final String PROVIDER_MODULE_OPENJPA = "org.apache.openjpa";
/**
* default if no PROVIDER_MODULE is specified.
*/
public static final String PROVIDER_MODULE_DEFAULT = PROVIDER_MODULE_HIBERNATE4_3;
/**
* Hibernate 4.1.x persistence provider class
*/
public static final String PROVIDER_CLASS_HIBERNATE4_1 = "org.hibernate.ejb.HibernatePersistence";
/**
* Hibernate 4.3.x persistence provider class
*/
public static final String PROVIDER_CLASS_HIBERNATE = "org.hibernate.jpa.HibernatePersistenceProvider";
/**
* Hibernate OGM persistence provider class
*/
public static final String PROVIDER_CLASS_HIBERNATE_OGM = "org.hibernate.ogm.jpa.HibernateOgmPersistence";
/**
* TopLink provider class names
*/
public static final String PROVIDER_CLASS_TOPLINK_ESSENTIALS = "oracle.toplink.essentials.PersistenceProvider";
public static final String PROVIDER_CLASS_TOPLINK = "oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider";
/**
* EclipseLink provider class name
*/
public static final String PROVIDER_CLASS_ECLIPSELINK = "org.eclipse.persistence.jpa.PersistenceProvider";
/**
* DataNucleus provider
*/
public static final String PROVIDER_CLASS_DATANUCLEUS = "org.datanucleus.api.jpa.PersistenceProviderImpl";
/**
* DataNucleus provider GAE
*/
public static final String PROVIDER_CLASS_DATANUCLEUS_GAE = "org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider";
public static final String PROVIDER_CLASS_OPENJPA = "org.apache.openjpa.persistence.PersistenceProviderImpl";
/**
* default provider class
*/
public static final String PROVIDER_CLASS_DEFAULT = PROVIDER_CLASS_HIBERNATE;
/**
* if the PROVIDER_MODULE is this value, it is expected that the application has its own provider
* in the deployment.
*/
public static final String PROVIDER_MODULE_APPLICATION_SUPPLIED = "application";
public static final String ADAPTER_MODULE_OPENJPA = "org.jboss.as.jpa.openjpa";
/**
* name of the AS module that contains the persistence provider adapter
*/
public static final String ADAPTER_MODULE = "jboss.as.jpa.adapterModule";
/**
* defaults to true, if changed to false (in the persistence.xml),
* the JPA container will not start the persistence unit service.
*/
public static final String JPA_CONTAINER_MANAGED = "jboss.as.jpa.managed";
public static final String JPA_DEFAULT_PERSISTENCE_UNIT = "wildfly.jpa.default-unit";
/**
* defaults to true, if false, persistence unit will not support javax.persistence.spi.ClassTransformer Interface
* which means no application class rewriting
*/
public static final String JPA_CONTAINER_CLASS_TRANSFORMER = "jboss.as.jpa.classtransformer";
/**
* set to false to force a single phase persistence unit bootstrap to be used (default is true
* which uses two phases to start the persistence unit).
*/
public static final String JPA_ALLOW_TWO_PHASE_BOOTSTRAP = "wildfly.jpa.twophasebootstrap";
/**
* set to false to ignore default data source (defaults to true)
*/
private static final String JPA_ALLOW_DEFAULT_DATA_SOURCE_USE = "wildfly.jpa.allowdefaultdatasourceuse";
/**
* set to true to defer detaching entities until persistence context is closed (WFLY-3674)
*/
private static final String JPA_DEFER_DETACH = "jboss.as.jpa.deferdetach";
/**
* unique name for the persistence unit that is unique across all deployments (
* defaults to include the application name prepended to the persistence unit name)
*/
private static final String JPA_SCOPED_PERSISTENCE_UNIT_NAME = "jboss.as.jpa.scopedname";
/**
* name of the persistence provider adapter class
*/
public static final String ADAPTER_CLASS = "jboss.as.jpa.adapterClass";
/**
* name of the Hibernate Search module name configuration setting in persistence unit definition
*/
public static final String HIBERNATE_SEARCH_MODULE = "wildfly.jpa.hibernate.search.module";
/**
* name of the Hibernate Search module name
*/
public static final String PROVIDER_MODULE_HIBERNATE_SEARCH = "org.hibernate.search.orm:main";
private static final String EE_DEFAULT_DATASOURCE = "java:comp/DefaultDataSource";
// key = provider class name, value = module name
private static final Map<String, String> providerClassToModuleName = new HashMap<String, String>();
static {
// always choose the default hibernate version for the Hibernate provider class mapping
// if the user wants a different version. they can specify the provider module name
providerClassToModuleName.put(PROVIDER_CLASS_HIBERNATE, PROVIDER_MODULE_HIBERNATE4_3);
// WFLY-2136/HHH-8543 to make migration to Hibernate 4.3.x easier, we also map the (now)
// deprecated PROVIDER_CLASS_HIBERNATE4_1 to the org.hibernate:main module
// when PROVIDER_CLASS_HIBERNATE4_1 is no longer in a future Hibernate version (5.x?)
// we can map PROVIDER_CLASS_HIBERNATE4_1 to org.hibernate:4.3 at that time.
// persistence units can set "jboss.as.jpa.providerModule=org.hibernate:4.1" to use Hibernate 4.1.x/4.2.x
providerClassToModuleName.put(PROVIDER_CLASS_HIBERNATE4_1, PROVIDER_MODULE_HIBERNATE4_3);
providerClassToModuleName.put(PROVIDER_CLASS_HIBERNATE_OGM, PROVIDER_MODULE_HIBERNATE_OGM);
providerClassToModuleName.put(PROVIDER_CLASS_TOPLINK_ESSENTIALS, PROVIDER_MODULE_TOPLINK);
providerClassToModuleName.put(PROVIDER_CLASS_TOPLINK, PROVIDER_MODULE_TOPLINK);
providerClassToModuleName.put(PROVIDER_CLASS_ECLIPSELINK, PROVIDER_MODULE_ECLIPSELINK);
providerClassToModuleName.put(PROVIDER_CLASS_DATANUCLEUS, PROVIDER_MODULE_DATANUCLEUS);
providerClassToModuleName.put(PROVIDER_CLASS_DATANUCLEUS_GAE, PROVIDER_MODULE_DATANUCLEUS_GAE);
providerClassToModuleName.put(PROVIDER_CLASS_OPENJPA, PROVIDER_MODULE_OPENJPA);
}
/**
* Get the provider module name for the specified provider class.
*
* @param providerClassName the PU class name
* @return provider module name or null if not known
*/
public static String getProviderModuleNameFromProviderClassName(final String providerClassName) {
return providerClassToModuleName.get(providerClassName);
}
/**
* Determine if class file transformer is needed for the specified persistence unit
*
* if the persistence provider is Hibernate and use_class_enhancer is not true, don't need a class transformer.
* for other persistence providers, the transformer is assumed to be needed.
*
* @param pu the PU
* @return true if class file transformer support is needed for pu
*/
public static boolean needClassFileTransformer(PersistenceUnitMetadata pu) {
boolean result = true;
String provider = pu.getPersistenceProviderClassName();
if (pu.getProperties().containsKey(Configuration.JPA_CONTAINER_CLASS_TRANSFORMER)) {
result = Boolean.parseBoolean(pu.getProperties().getProperty(Configuration.JPA_CONTAINER_CLASS_TRANSFORMER));
}
else if (isHibernateProvider(provider)) {
String useHibernateClassEnhancer = pu.getProperties().getProperty("hibernate.ejb.use_class_enhancer");
result = "true".equals(useHibernateClassEnhancer);
}
return result;
}
private static boolean isHibernateProvider(String provider) {
return provider == null ||
PROVIDER_CLASS_HIBERNATE.equals(provider) ||
PROVIDER_CLASS_HIBERNATE4_1.equals(provider);
}
// key = provider class name, value = adapter module name
private static final Map<String, String> providerClassToAdapterModuleName = new HashMap<String, String>();
static {
providerClassToAdapterModuleName.put(PROVIDER_CLASS_OPENJPA, ADAPTER_MODULE_OPENJPA);
}
public static String getProviderAdapterModuleNameFromProviderClassName(final String providerClassName) {
return providerClassToAdapterModuleName.get(providerClassName);
}
public static String getDefaultProviderModuleName() {
return PROVIDER_MODULE_DEFAULT;
}
/**
* Determine if two phase persistence unit start is allowed
*
* @param pu
* @return
*/
public static boolean allowTwoPhaseBootstrap(PersistenceUnitMetadata pu) {
boolean result = true;
if (EE_DEFAULT_DATASOURCE.equals(pu.getJtaDataSourceName())) {
result = false;
}
if (pu.getProperties().containsKey(Configuration.JPA_ALLOW_TWO_PHASE_BOOTSTRAP)) {
result = Boolean.parseBoolean(pu.getProperties().getProperty(Configuration.JPA_ALLOW_TWO_PHASE_BOOTSTRAP));
}
return result;
}
/**
* Determine if the default data-source should be used
*
* @param pu
* @return true if the default data-source should be used
*/
public static boolean allowDefaultDataSourceUse(PersistenceUnitMetadata pu) {
boolean result = true;
if (pu.getProperties().containsKey(Configuration.JPA_ALLOW_DEFAULT_DATA_SOURCE_USE)) {
result = Boolean.parseBoolean(pu.getProperties().getProperty(Configuration.JPA_ALLOW_DEFAULT_DATA_SOURCE_USE));
}
return result;
}
/**
* Return true if detaching of managed entities should be deferred until the entity manager is closed.
* Note: only applies to transaction scoped entity managers used without an active JTA transaction.
*
* @param properties
* @return
*/
public static boolean deferEntityDetachUntilClose(final Map<String, Object> properties) {
boolean result = false;
if ( properties.containsKey(JPA_DEFER_DETACH))
result = Boolean.parseBoolean((String)properties.get(JPA_DEFER_DETACH));
return result;
}
public static String getScopedPersistenceUnitName(PersistenceUnitMetadata pu) {
Object name = pu.getProperties().get(JPA_SCOPED_PERSISTENCE_UNIT_NAME);
if (name instanceof String) {
return (String)name;
}
return null;
}
}