-
Notifications
You must be signed in to change notification settings - Fork 138
/
DataSourceDefinitionHandler.java
344 lines (287 loc) · 13.4 KB
/
DataSourceDefinitionHandler.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (c) 2021, 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 org.glassfish.jdbcruntime.deployment.annotation.handlers;
import com.sun.enterprise.deployment.DataSourceDefinitionDescriptor;
import com.sun.enterprise.deployment.MetadataSource;
import com.sun.enterprise.deployment.ResourceDescriptor;
import com.sun.enterprise.deployment.annotation.context.ResourceContainerContext;
import com.sun.enterprise.deployment.annotation.handlers.AbstractResourceHandler;
import com.sun.enterprise.deployment.annotation.handlers.ResourceAnnotationControl;
import jakarta.annotation.sql.DataSourceDefinition;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.AroundTimeout;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.Interceptors;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.glassfish.apf.AnnotationHandlerFor;
import org.glassfish.apf.AnnotationInfo;
import org.glassfish.apf.AnnotationProcessorException;
import org.glassfish.apf.HandlerProcessingResult;
import org.glassfish.deployment.common.JavaEEResourceType;
import org.jvnet.hk2.annotations.Service;
/**
* @author Jagadish Ramu
*/
@Service
@AnnotationHandlerFor(DataSourceDefinition.class)
public class DataSourceDefinitionHandler extends AbstractResourceHandler {
private static final ResourceAnnotationControl CTRL = new ResourceAnnotationControl(DataSourceDefinition.class);
public DataSourceDefinitionHandler() {
}
@Override
protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, ResourceContainerContext[] rcContexts) throws AnnotationProcessorException {
DataSourceDefinition dataSourceDefnAn = (DataSourceDefinition) ainfo.getAnnotation();
return processAnnotation(dataSourceDefnAn, ainfo, rcContexts);
}
protected HandlerProcessingResult processAnnotation(DataSourceDefinition dataSourceDefnAn, AnnotationInfo aiInfo, ResourceContainerContext[] rcContexts) throws AnnotationProcessorException {
Class<?> annotatedClass = (Class<?>) aiInfo.getAnnotatedElement();
Annotation[] annotations = annotatedClass.getAnnotations();
boolean warClass = isAWebComponentClass(annotations);
boolean ejbClass = isAEjbComponentClass(annotations);
for (ResourceContainerContext context : rcContexts) {
if (!CTRL.canProcessAnnotation(annotatedClass, ejbClass, warClass, context)) {
return getDefaultProcessedResult();
}
Set<ResourceDescriptor> dsdDescs = context.getResourceDescriptors(JavaEEResourceType.DSD);
DataSourceDefinitionDescriptor desc = createDescriptor(dataSourceDefnAn);
if (isDefinitionAlreadyPresent(dsdDescs, desc)) {
merge(dsdDescs, dataSourceDefnAn);
} else {
dsdDescs.add(desc);
}
}
return getDefaultProcessedResult();
}
private boolean isDefinitionAlreadyPresent(Set<ResourceDescriptor> dsdDescs, DataSourceDefinitionDescriptor desc) {
boolean result = false;
for (ResourceDescriptor descriptor : dsdDescs) {
if (descriptor.equals(desc)) {
result = true;
break;
}
}
return result;
}
@Override
public Class<? extends Annotation>[] getTypeDependencies() {
Class<? extends Annotation>[] annotations = getEjbAndWebAnnotationTypes();
List<Class<? extends Annotation>> annotationsList = new ArrayList<>();
for (Class<? extends Annotation> annotation : annotations) {
annotationsList.add(annotation);
}
annotationsList.add(Interceptors.class);
annotationsList.add(Interceptor.class);
annotationsList.add(AroundInvoke.class);
annotationsList.add(AroundTimeout.class);
Class<? extends Annotation>[] result = new Class[annotationsList.size()];
return annotationsList.toArray(result);
}
private void merge(Set<ResourceDescriptor> dsdDescs, DataSourceDefinition defn) {
for (ResourceDescriptor orgdesc : dsdDescs) {
DataSourceDefinitionDescriptor desc = (DataSourceDefinitionDescriptor) orgdesc;
if (desc.getName().equals(defn.name())) {
if (desc.getClassName() == null) {
desc.setClassName(defn.className());
}
if (desc.getDescription() == null) {
if (defn.description() != null && !defn.description().isEmpty()) {
desc.setDescription(defn.description());
}
}
// When either URL or Standard properties are specified in DD, annotation values
// (of URL or standard properties) are ignored.
// DD values will win as either of URL or standard properties will be present
// most of the times.
// Only when neither URL nor standard properties are not present, annotation
// values are considered.
// In such case, standard properties take precedence over URL.
// try only when URL is not set
if (!desc.isServerNameSet() && desc.getUrl() == null) {
// localhost is the default value (even in the descriptor)
if (defn.serverName() != null && !defn.serverName().equals("localhost")) {
desc.setServerName(defn.serverName());
}
}
// try only when URL is not set
if (desc.getPortNumber() == -1 && desc.getUrl() == null) {
if (defn.portNumber() != -1) {
desc.setPortNumber(defn.portNumber());
}
}
// try only when URL is not set
if (desc.getDatabaseName() == null && desc.getUrl() == null) {
if (defn.databaseName() != null && !defn.databaseName().isEmpty()) {
desc.setDatabaseName(defn.databaseName());
}
}
// try only when URL or standard properties are not set
if (desc.getUrl() == null
&& !(desc.getPortNumber() != -1 && desc.getServerName() != null && (desc.getDatabaseName() != null))) {
if (defn.url() != null && !defn.url().isEmpty()) {
desc.setUrl(defn.url());
}
}
if (desc.getUser() == null) {
if (defn.user() != null && !defn.user().isEmpty()) {
desc.setUser(defn.user());
}
}
if (desc.getPassword() == null) {
if (defn.password() != null /* ALLOW EMPTY PASSWORDS && !defn.password().isEmpty() */) {
desc.setPassword(defn.password());
}
}
if (desc.getIsolationLevel() == -1) {
if (defn.isolationLevel() != -1) {
desc.setIsolationLevel(String.valueOf(defn.isolationLevel()));
}
}
if (!desc.isTransactionSet()) {
if (defn.transactional()) {
desc.setTransactional(true);
} else {
desc.setTransactional(false);
}
}
if (desc.getMinPoolSize() == -1) {
if (defn.minPoolSize() != -1) {
desc.setMinPoolSize(defn.minPoolSize());
}
}
if (desc.getMaxPoolSize() == -1) {
if (defn.maxPoolSize() != -1) {
desc.setMaxPoolSize(defn.maxPoolSize());
}
}
if (desc.getInitialPoolSize() == -1) {
if (defn.initialPoolSize() != -1) {
desc.setInitialPoolSize(defn.initialPoolSize());
}
}
if (desc.getMaxIdleTime() == -1) {
if (defn.maxIdleTime() != -1) {
desc.setMaxIdleTime(String.valueOf(defn.maxIdleTime()));
}
}
if (desc.getMaxStatements() == -1) {
if (defn.maxStatements() != -1) {
desc.setMaxStatements(defn.maxStatements());
}
}
if (!desc.isLoginTimeoutSet()) {
if (defn.loginTimeout() != 0) {
desc.setLoginTimeout(String.valueOf(defn.loginTimeout()));
}
}
Properties properties = desc.getProperties();
String[] defnProperties = defn.properties();
if (defnProperties.length > 0) {
for (String property : defnProperties) {
int index = property.indexOf("=");
// found "=" and not at start or end of string
if (index > -1 && index != 0 && index < property.length() - 1) {
String name = property.substring(0, index);
String value = property.substring(index + 1);
// add to properties only when not already present
if (properties.get(name) == null) {
properties.put(name, value);
}
}
}
}
break;
}
}
}
private DataSourceDefinitionDescriptor createDescriptor(DataSourceDefinition defn) {
DataSourceDefinitionDescriptor desc = new DataSourceDefinitionDescriptor();
desc.setMetadataSource(MetadataSource.ANNOTATION);
desc.setName(defn.name());
desc.setClassName(defn.className());
if (defn.description() != null && !defn.description().isEmpty()) {
desc.setDescription(defn.description());
}
if (defn.serverName() != null && !defn.serverName().equals("localhost")) {
desc.setServerName(defn.serverName());
}
if (defn.portNumber() != -1) {
desc.setPortNumber(defn.portNumber());
}
if (defn.databaseName() != null && !defn.databaseName().isEmpty()) {
desc.setDatabaseName(defn.databaseName());
}
if ((desc.getPortNumber() != -1 && desc.getDatabaseName() != null && desc.getServerName() != null)) {
// standard properties are set, ignore URL
} else {
if (defn.url() != null && !defn.url().isEmpty()) {
desc.setUrl(defn.url());
}
}
if (defn.user() != null && !defn.user().isEmpty()) {
desc.setUser(defn.user());
}
if (defn.password() != null /* ALLOW EMPTY PASSWORDS && !defn.password().isEmpty() */) {
desc.setPassword(defn.password());
}
if (defn.isolationLevel() != -1) {
desc.setIsolationLevel(String.valueOf(defn.isolationLevel()));
}
if (defn.transactional()) {
desc.setTransactional(true);
} else {
desc.setTransactional(false);
}
if (defn.minPoolSize() != -1) {
desc.setMinPoolSize(defn.minPoolSize());
}
if (defn.maxPoolSize() != -1) {
desc.setMaxPoolSize(defn.maxPoolSize());
}
if (defn.initialPoolSize() != -1) {
desc.setInitialPoolSize(defn.initialPoolSize());
}
if (defn.maxIdleTime() != -1) {
desc.setMaxIdleTime(String.valueOf(defn.maxIdleTime()));
}
if (defn.maxStatements() != -1) {
desc.setMaxStatements(defn.maxStatements());
}
if (defn.loginTimeout() != 0) {
desc.setLoginTimeout(String.valueOf(defn.loginTimeout()));
}
if (defn.properties() != null) {
Properties properties = desc.getProperties();
String[] defnProperties = defn.properties();
if (defnProperties.length > 0) {
for (String property : defnProperties) {
int index = property.indexOf("=");
// found "=" and not at start or end of string
if (index > -1 && index != 0 && index < property.length() - 1) {
String name = property.substring(0, index);
String value = property.substring(index + 1);
properties.put(name, value);
}
}
}
}
return desc;
}
}