/
PersistenceRefProcessor.java
236 lines (205 loc) · 12.6 KB
/
PersistenceRefProcessor.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
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.jpa.processor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.PersistenceContextType;
import jakarta.persistence.SynchronizationType;
import org.jboss.as.ee.component.BindingConfiguration;
import org.jboss.as.ee.component.ComponentDescription;
import org.jboss.as.ee.component.DeploymentDescriptorEnvironment;
import org.jboss.as.ee.component.EEApplicationClasses;
import org.jboss.as.ee.component.InjectionSource;
import org.jboss.as.ee.component.LookupInjectionSource;
import org.jboss.as.ee.component.ResourceInjectionTarget;
import org.jboss.as.ee.component.deployers.AbstractDeploymentDescriptorBindingsProcessor;
import org.jboss.as.jpa.config.JPADeploymentSettings;
import org.jboss.as.jpa.container.PersistenceUnitSearch;
import org.jboss.as.jpa.injectors.PersistenceContextInjectionSource;
import org.jboss.as.jpa.injectors.PersistenceUnitInjectionSource;
import org.jboss.as.jpa.messages.JpaLogger;
import org.jboss.as.jpa.service.PersistenceUnitServiceImpl;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUtils;
import org.jboss.as.server.deployment.JPADeploymentMarker;
import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
import org.jboss.metadata.javaee.spec.Environment;
import org.jboss.metadata.javaee.spec.PersistenceContextReferenceMetaData;
import org.jboss.metadata.javaee.spec.PersistenceContextReferencesMetaData;
import org.jboss.metadata.javaee.spec.PersistenceContextSynchronizationType;
import org.jboss.metadata.javaee.spec.PersistenceContextTypeDescription;
import org.jboss.metadata.javaee.spec.PersistenceUnitReferenceMetaData;
import org.jboss.metadata.javaee.spec.PersistenceUnitReferencesMetaData;
import org.jboss.metadata.javaee.spec.PropertiesMetaData;
import org.jboss.metadata.javaee.spec.PropertyMetaData;
import org.jboss.metadata.javaee.spec.RemoteEnvironment;
import org.jboss.msc.service.ServiceName;
import org.jipijapa.plugin.spi.PersistenceUnitMetadata;
/**
* Deployment processor responsible for processing persistence unit / context references from deployment descriptors.
*
* @author Stuart Douglas
*/
public class PersistenceRefProcessor extends AbstractDeploymentDescriptorBindingsProcessor {
@Override
protected List<BindingConfiguration> processDescriptorEntries(DeploymentUnit deploymentUnit, DeploymentDescriptorEnvironment environment, ResourceInjectionTarget resourceInjectionTarget, final ComponentDescription componentDescription, ClassLoader classLoader, DeploymentReflectionIndex deploymentReflectionIndex, final EEApplicationClasses applicationClasses) throws
DeploymentUnitProcessingException {
List<BindingConfiguration> bindings = new ArrayList<BindingConfiguration>();
bindings.addAll(getPersistenceUnitRefs(deploymentUnit, environment, classLoader, deploymentReflectionIndex, resourceInjectionTarget));
bindings.addAll(getPersistenceContextRefs(deploymentUnit, environment, classLoader, deploymentReflectionIndex, resourceInjectionTarget));
return bindings;
}
/**
* Resolves persistence-unit-ref
*
* @param environment The environment to resolve the elements for
* @param classLoader The deployment class loader
* @param deploymentReflectionIndex The reflection index
* @return The bindings for the environment entries
*/
private List<BindingConfiguration> getPersistenceUnitRefs(DeploymentUnit deploymentUnit, DeploymentDescriptorEnvironment environment, ClassLoader classLoader, DeploymentReflectionIndex deploymentReflectionIndex, ResourceInjectionTarget resourceInjectionTarget) throws
DeploymentUnitProcessingException {
final List<BindingConfiguration> bindingConfigurations = new ArrayList<BindingConfiguration>();
if (environment.getEnvironment() == null) {
return bindingConfigurations;
}
PersistenceUnitReferencesMetaData persistenceUnitRefs = environment.getEnvironment().getPersistenceUnitRefs();
if (persistenceUnitRefs != null) {
if (!persistenceUnitRefs.isEmpty()) {
JPADeploymentMarker.mark(deploymentUnit);
}
for (PersistenceUnitReferenceMetaData puRef : persistenceUnitRefs) {
String name = puRef.getName();
String persistenceUnitName = puRef.getPersistenceUnitName();
String lookup = puRef.getLookupName();
if (!isEmpty(lookup) && !isEmpty(persistenceUnitName)) {
throw JpaLogger.ROOT_LOGGER.cannotSpecifyBoth("<lookup-name>", lookup, "persistence-unit-name", persistenceUnitName, "<persistence-unit-ref/>", resourceInjectionTarget);
}
if (!name.startsWith("java:")) {
name = environment.getDefaultContext() + name;
}
// our injection (source) comes from the local (ENC) lookup, no matter what.
LookupInjectionSource injectionSource = new LookupInjectionSource(name);
//add any injection targets
processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, puRef, EntityManagerFactory.class);
BindingConfiguration bindingConfiguration = null;
if (!isEmpty(lookup)) {
bindingConfiguration = new BindingConfiguration(name, new LookupInjectionSource(lookup));
} else {
InjectionSource puBindingSource = this.getPersistenceUnitBindingSource(deploymentUnit, persistenceUnitName);
bindingConfiguration = new BindingConfiguration(name, puBindingSource);
}
bindingConfigurations.add(bindingConfiguration);
}
}
return bindingConfigurations;
}
/**
* Resolves persistence-unit-ref
*
* @param environment The environment to resolve the elements for
* @param classLoader The deployment class loader
* @param deploymentReflectionIndex The reflection index
* @return The bindings for the environment entries
*/
private List<BindingConfiguration> getPersistenceContextRefs(DeploymentUnit deploymentUnit, DeploymentDescriptorEnvironment environment, ClassLoader classLoader, DeploymentReflectionIndex deploymentReflectionIndex, ResourceInjectionTarget resourceInjectionTarget) throws
DeploymentUnitProcessingException {
List<BindingConfiguration> bindingConfigurations = new ArrayList<BindingConfiguration>();
final RemoteEnvironment remoteEnvironment = environment.getEnvironment();
if (remoteEnvironment == null) {
return bindingConfigurations;
}
if (remoteEnvironment instanceof Environment) {
PersistenceContextReferencesMetaData persistenceUnitRefs = ((Environment) remoteEnvironment).getPersistenceContextRefs();
if (persistenceUnitRefs != null) {
for (PersistenceContextReferenceMetaData puRef : persistenceUnitRefs) {
String name = puRef.getName();
String persistenceUnitName = puRef.getPersistenceUnitName();
String lookup = puRef.getLookupName();
if (!isEmpty(lookup) && !isEmpty(persistenceUnitName)) {
throw JpaLogger.ROOT_LOGGER.cannotSpecifyBoth("<lookup-name>", lookup, "persistence-unit-name", persistenceUnitName, "<persistence-context-ref/>", resourceInjectionTarget);
}
if (!name.startsWith("java:")) {
name = environment.getDefaultContext() + name;
}
// our injection (source) comes from the local (ENC) lookup, no matter what.
LookupInjectionSource injectionSource = new LookupInjectionSource(name);
//add any injection targets
processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, puRef, EntityManager.class);
BindingConfiguration bindingConfiguration = null;
if (!isEmpty(lookup)) {
bindingConfiguration = new BindingConfiguration(name, new LookupInjectionSource(lookup));
} else {
PropertiesMetaData properties = puRef.getProperties();
Map<String, String> map = new HashMap<>();
if (properties != null) {
for (PropertyMetaData prop : properties) {
map.put(prop.getKey(), prop.getValue());
}
}
PersistenceContextType type = (puRef.getPersistenceContextType() == null || puRef.getPersistenceContextType() == PersistenceContextTypeDescription.TRANSACTION) ? PersistenceContextType.TRANSACTION : PersistenceContextType.EXTENDED ;
SynchronizationType synchronizationType =
(puRef.getPersistenceContextSynchronization() == null || PersistenceContextSynchronizationType.Synchronized.equals(puRef.getPersistenceContextSynchronization()))?
SynchronizationType.SYNCHRONIZED: SynchronizationType.UNSYNCHRONIZED;
InjectionSource pcBindingSource = this.getPersistenceContextBindingSource(deploymentUnit, persistenceUnitName, type, synchronizationType, map);
bindingConfiguration = new BindingConfiguration(name, pcBindingSource);
}
bindingConfigurations.add(bindingConfiguration);
}
}
}
return bindingConfigurations;
}
private InjectionSource getPersistenceUnitBindingSource(final DeploymentUnit deploymentUnit, final String unitName) throws
DeploymentUnitProcessingException {
final String searchName;
if (isEmpty(unitName)) {
searchName = null;
} else {
searchName = unitName;
}
final PersistenceUnitMetadata pu = PersistenceUnitSearch.resolvePersistenceUnitSupplier(deploymentUnit, searchName);
if (null == pu) {
throw new DeploymentUnitProcessingException(JpaLogger.ROOT_LOGGER.persistenceUnitNotFound(searchName, deploymentUnit));
}
String scopedPuName = pu.getScopedPersistenceUnitName();
ServiceName puServiceName = getPuServiceName(scopedPuName);
return new PersistenceUnitInjectionSource(puServiceName, deploymentUnit.getServiceRegistry(), EntityManagerFactory.class.getName(), pu);
}
private InjectionSource getPersistenceContextBindingSource(
final DeploymentUnit deploymentUnit,
final String unitName,
PersistenceContextType type,
SynchronizationType synchronizationType,
Map properties) throws
DeploymentUnitProcessingException {
PersistenceUnitMetadata pu = getPersistenceUnit(deploymentUnit, unitName);
String scopedPuName = pu.getScopedPersistenceUnitName();
ServiceName puServiceName = getPuServiceName(scopedPuName);
// get deployment settings from top level du (jboss-all.xml is only parsed at the top level).
final JPADeploymentSettings jpaDeploymentSettings = DeploymentUtils.getTopDeploymentUnit(deploymentUnit).getAttachment(JpaAttachments.DEPLOYMENT_SETTINGS_KEY);
return new PersistenceContextInjectionSource(type, synchronizationType, properties, puServiceName, deploymentUnit.getServiceRegistry(), scopedPuName, EntityManager.class.getName(), pu, jpaDeploymentSettings);
}
private PersistenceUnitMetadata getPersistenceUnit(final DeploymentUnit deploymentUnit, final String puName)
throws DeploymentUnitProcessingException {
PersistenceUnitMetadata pu = PersistenceUnitSearch.resolvePersistenceUnitSupplier(deploymentUnit, puName);
if (null == pu) {
throw new DeploymentUnitProcessingException(JpaLogger.ROOT_LOGGER.persistenceUnitNotFound(puName, deploymentUnit));
}
return pu;
}
private ServiceName getPuServiceName(String scopedPuName)
throws DeploymentUnitProcessingException {
return PersistenceUnitServiceImpl.getPUServiceName(scopedPuName);
}
private boolean isEmpty(String string) {
return string == null || string.isEmpty();
}
}