/
EntityManagerReferenceNode.java
166 lines (146 loc) · 7.06 KB
/
EntityManagerReferenceNode.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
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.deployment.node;
import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor;
import com.sun.enterprise.deployment.InjectionTarget;
import com.sun.enterprise.deployment.xml.TagNames;
import com.sun.enterprise.util.LocalStringManagerImpl;
import jakarta.persistence.PersistenceContextType;
import jakarta.persistence.SynchronizationType;
import java.util.Map;
import org.w3c.dom.Node;
/**
* This node handles all persistence-context-ref xml tag elements
*
* @author Shing Wai Chan
* @version
*/
public class EntityManagerReferenceNode extends DeploymentDescriptorNode {
private static final LocalStringManagerImpl I18N = new LocalStringManagerImpl(EntityManagerReferenceNode.class);
private static final String TRANSACTION = "Transaction";
private static final String EXTENDED = "Extended";
private static final String SYNCHRONIZED = "Synchronized";
private static final String UNSYNCHRONIZED = "Unsynchronized";
// Holds property name during name/value processing.
private String propertyName;
public EntityManagerReferenceNode() {
super();
registerElementHandler(new XMLElement(TagNames.INJECTION_TARGET),
InjectionTargetNode.class, "addInjectionTarget");
}
/**
* all sub-implementation of this class can use a dispatch table to map
* xml element to method name on the descriptor class for setting
* the element value.
*
* @return the map with the element name as a key, the setter method as
* a value
*/
@Override
protected Map getDispatchTable() {
// no need to be synchronized for now
Map table = super.getDispatchTable();
table.put(TagNames.PERSISTENCE_CONTEXT_REF_NAME, "setName");
table.put(TagNames.PERSISTENCE_UNIT_NAME, "setUnitName");
return table;
}
/**
* receives notiification of the value for a particular tag
*
* @param element the xml element
* @param value it's associated value
*/
@Override
public void setElementValue(XMLElement element, String value) {
if (TagNames.PERSISTENCE_CONTEXT_TYPE.equals(element.getQName())) {
EntityManagerReferenceDescriptor entityMgrReferenceDescriptor =
(EntityManagerReferenceDescriptor)getDescriptor();
PersistenceContextType contextType = null;
if (EXTENDED.equals(value)) {
contextType = PersistenceContextType.EXTENDED;
} else if (TRANSACTION.equals(value)) {
contextType = PersistenceContextType.TRANSACTION;
} else {
throw new IllegalArgumentException(I18N.getLocalString(
"enterprise.deployment.node.invalidvalue",
"Invalid value for a tag under {0} : {1}",
new Object[] {TagNames.PERSISTENCE_CONTEXT_TYPE, value}));
}
entityMgrReferenceDescriptor.setPersistenceContextType(contextType);
} else if (TagNames.PERSISTENCE_CONTEXT_SYNCHRONIZATION_TYPE.equals(element.getQName() ) ) {
EntityManagerReferenceDescriptor entityMgrReferenceDescriptor =
(EntityManagerReferenceDescriptor)getDescriptor();
SynchronizationType synchronizationType;
if (SYNCHRONIZED.equals(value)) {
synchronizationType = SynchronizationType.SYNCHRONIZED;
} else if (UNSYNCHRONIZED.equals(value)) {
synchronizationType = SynchronizationType.UNSYNCHRONIZED;
} else {
throw new IllegalArgumentException(I18N.getLocalString(
"enterprise.deployment.node.invalidvalue",
"Invalid value for a tag under {0} : {1}",
new Object[] {TagNames.PERSISTENCE_CONTEXT_SYNCHRONIZATION_TYPE, value}));
}
entityMgrReferenceDescriptor.setSynchronizationType(synchronizationType);
} else if (TagNames.NAME_VALUE_PAIR_NAME.equals(element.getQName())) {
propertyName = value;
} else if (TagNames.NAME_VALUE_PAIR_VALUE.equals(element.getQName())) {
EntityManagerReferenceDescriptor entityMgrReferenceDescriptor =
(EntityManagerReferenceDescriptor)getDescriptor();
entityMgrReferenceDescriptor.addProperty(propertyName, value);
propertyName = null;
} else {
super.setElementValue(element, value);
}
}
/**
* write the descriptor class to a DOM tree and return it
*
* @param parent node in the DOM tree
* @param nodeName name for the root element of this xml fragment
* @param descriptor descriptor to write
* @return the DOM tree top node
*/
public Node writeDescriptor(Node parent, String nodeName, EntityManagerReferenceDescriptor descriptor) {
Node entityMgrRefNode = appendChild(parent, nodeName);
writeLocalizedDescriptions(entityMgrRefNode, descriptor);
appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_CONTEXT_REF_NAME, descriptor.getName());
appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_UNIT_NAME, descriptor.getUnitName());
PersistenceContextType contextType = descriptor.getPersistenceContextType();
String contextTypeString = (contextType != null &&
PersistenceContextType.EXTENDED.equals(contextType)) ?
EXTENDED : TRANSACTION;
appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_CONTEXT_TYPE,
contextTypeString);
for(Map.Entry<String, String> property :
descriptor.getProperties().entrySet()) {
Node propertyNode = appendChild(entityMgrRefNode,
TagNames.PERSISTENCE_PROPERTY);
appendTextChild(propertyNode, TagNames.NAME_VALUE_PAIR_NAME,
property.getKey());
appendTextChild(propertyNode, TagNames.NAME_VALUE_PAIR_VALUE,
property.getValue());
}
if( descriptor.isInjectable() ) {
InjectionTargetNode ijNode = new InjectionTargetNode();
for (InjectionTarget target : descriptor.getInjectionTargets()) {
ijNode.writeDescriptor(entityMgrRefNode, TagNames.INJECTION_TARGET, target);
}
}
return entityMgrRefNode;
}
}