-
Notifications
You must be signed in to change notification settings - Fork 138
/
ResourceValidator.java
1152 lines (991 loc) · 46.8 KB
/
ResourceValidator.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2017, 2018 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.util;
import com.sun.enterprise.config.serverbeans.Cluster;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.config.serverbeans.ServerTags;
import com.sun.enterprise.deployment.AbstractConnectorResourceDescriptor;
import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.ApplicationClientDescriptor;
import com.sun.enterprise.deployment.BundleDescriptor;
import com.sun.enterprise.deployment.ConnectorDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.EjbReferenceDescriptor;
import com.sun.enterprise.deployment.EntityManagerFactoryReferenceDescriptor;
import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor;
import com.sun.enterprise.deployment.EnvironmentProperty;
import com.sun.enterprise.deployment.JndiNameEnvironment;
import com.sun.enterprise.deployment.ManagedBeanDescriptor;
import com.sun.enterprise.deployment.MessageDestinationReferenceDescriptor;
import com.sun.enterprise.deployment.PersistenceUnitDescriptor;
import com.sun.enterprise.deployment.PersistenceUnitsDescriptor;
import com.sun.enterprise.deployment.ResourceDescriptor;
import com.sun.enterprise.deployment.ResourceEnvReferenceDescriptor;
import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
import com.sun.enterprise.deployment.WebBundleDescriptor;
import com.sun.enterprise.util.LocalStringManagerImpl;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.deployment.DeployCommandParameters;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.api.event.EventListener;
import org.glassfish.api.event.Events;
import org.glassfish.api.naming.SimpleJndiName;
import org.glassfish.deployment.common.DeploymentException;
import org.glassfish.deployment.common.DeploymentProperties;
import org.glassfish.deployment.common.JavaEEResourceType;
import org.glassfish.internal.deployment.Deployment;
import org.glassfish.logging.annotation.LogMessageInfo;
import org.glassfish.resourcebase.resources.api.ResourceConstants;
import org.jvnet.hk2.annotations.Service;
import static org.glassfish.api.naming.SimpleJndiName.JNDI_CTX_JAVA_APP;
import static org.glassfish.api.naming.SimpleJndiName.JNDI_CTX_JAVA_APP_ENV;
import static org.glassfish.api.naming.SimpleJndiName.JNDI_CTX_JAVA_COMPONENT;
import static org.glassfish.api.naming.SimpleJndiName.JNDI_CTX_JAVA_COMPONENT_ENV;
import static org.glassfish.api.naming.SimpleJndiName.JNDI_CTX_JAVA_GLOBAL;
import static org.glassfish.api.naming.SimpleJndiName.JNDI_CTX_JAVA_MODULE;
/**
* Created by Krishna Deepak on 6/9/17.
*/
@Service
public class ResourceValidator implements EventListener, ResourceValidatorVisitor {
private static final Logger LOG = com.sun.enterprise.deployment.util.DOLUtils.deplLogger;
@LogMessageInfo(
message = "JNDI lookup failed for the resource: Name: {0}, Lookup: {1}, Type: {2}.",
level = "SEVERE",
cause = "JNDI lookup for the specified resource failed.",
action = "Configure the required resources before deploying the application.",
comment = "For the method validateJNDIRefs of com.sun.enterprise.deployment.util.ResourceValidator."
)
private static final String RESOURCE_REF_JNDI_LOOKUP_FAILED = "AS-DEPLOYMENT-00026";
@LogMessageInfo(
message = "Resource Adapter not present: RA Name: {0}, Type: {1}.",
level = "SEVERE",
cause = "Resource apapter specified is invalid.",
action = "Configure the required resource adapter."
)
private static final String RESOURCE_REF_INVALID_RA = "AS-DEPLOYMENT-00027";
@LogMessageInfo(message = "Skipping resource validation")
private static final String SKIP_RESOURCE_VALIDATION = "AS-DEPLOYMENT-00028";
private static final Set<SimpleJndiName> DEFAULT_JNDI_NAMES = Set
.of(JNDI_CTX_JAVA_COMPONENT + "DefaultDataSource",
JNDI_CTX_JAVA_COMPONENT + "DefaultJMSConnectionFactory",
JNDI_CTX_JAVA_COMPONENT + "ORB",
JNDI_CTX_JAVA_COMPONENT + "DefaultManagedExecutorService",
JNDI_CTX_JAVA_COMPONENT + "DefaultManagedScheduledExecutorService",
JNDI_CTX_JAVA_COMPONENT + "DefaultManagedThreadFactory",
JNDI_CTX_JAVA_COMPONENT + "DefaultContextService",
JNDI_CTX_JAVA_COMPONENT + "UserTransaction",
JNDI_CTX_JAVA_COMPONENT + "TransactionSynchronizationRegistry",
JNDI_CTX_JAVA_COMPONENT + "BeanManager",
JNDI_CTX_JAVA_COMPONENT + "ValidatorFactory",
JNDI_CTX_JAVA_COMPONENT + "Validator",
JNDI_CTX_JAVA_COMPONENT + "InAppClientContainer",
JNDI_CTX_JAVA_MODULE + "ModuleName",
JNDI_CTX_JAVA_APP + "AppName")
.stream().map(SimpleJndiName::new).collect(Collectors.toUnmodifiableSet());
private String target;
private DeploymentContext dc;
private Application application;
@Inject
private Events events;
@Inject
private Domain domain;
private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(ResourceValidator.class);
@Inject
@Named(ServerEnvironment.DEFAULT_INSTANCE_NAME)
private Server server;
@PostConstruct
public void postConstruct() {
events.register(this);
}
@Override
public void event(Event<?> event) {
if (event.is(Deployment.AFTER_APPLICATION_CLASSLOADER_CREATION)) {
dc = (DeploymentContext) event.hook();
application = dc.getModuleMetaData(Application.class);
DeployCommandParameters commandParams = dc.getCommandParameters(DeployCommandParameters.class);
target = commandParams.target;
if (System.getProperty("deployment.resource.validation", "true").equals("false")) {
LOG.log(Level.INFO, SKIP_RESOURCE_VALIDATION);
return;
}
if (application == null) {
return;
}
AppResources appResources = new AppResources();
parseResources(appResources);
validateResources(appResources);
}
}
/**
* Store all the resources before starting the validation.
*/
private void parseResources(AppResources appResources) {
parseResources(application, appResources);
for (BundleDescriptor bd : application.getBundleDescriptors()) {
if (bd instanceof WebBundleDescriptor || bd instanceof ApplicationClientDescriptor) {
parseResources(bd, appResources);
}
if (bd instanceof EjbBundleDescriptor) {
// Resources from Java files in the ejb.jar which are neither an EJB nor a managed bean are stored here.
// Skip validation for them, validate only Managed Beans.
for (ManagedBeanDescriptor mbd: bd.getManagedBeans()) {
parseResources(mbd, (JndiNameEnvironment) bd, appResources);
}
EjbBundleDescriptor ebd = (EjbBundleDescriptor) bd;
for (EjbDescriptor ejb : ebd.getEjbs()) {
parseEJB(ejb, appResources);
}
}
}
parseManagedBeans(appResources);
// Parse AppScoped resources
String appName = DOLUtils.getApplicationName(application);
@SuppressWarnings("unchecked")
Map<String, List<SimpleJndiName>> resourcesList = (Map<String, List<SimpleJndiName>>) dc
.getTransientAppMetadata().get(ResourceConstants.APP_SCOPED_RESOURCES_JNDI_NAMES);
appResources.storeAppScopedResources(resourcesList, appName);
}
/**
* Code logic from BaseContainer.java. Store portable and non-portable JNDI names in our namespace.
* Internal JNDI names not processed as they will not be called from an application.
*
* @param ejb
*/
private void parseEJB(EjbDescriptor ejb, AppResources appResources) {
SimpleJndiName javaGlobalName = getJavaGlobalJndiNamePrefix(ejb);
boolean disableNonPortableJndiName = false;
// TODO: Need to get the value of system-property
// server.ejb-container.property.disable-nonportable-jndi-names
Boolean disableInDD = ejb.getEjbBundleDescriptor().getDisableNonportableJndiNames();
if (disableInDD != null) {
// explicitly set in glassfish-ejb-jar.xml
disableNonPortableJndiName = disableInDD;
}
SimpleJndiName glassfishSpecificJndiName;
if (disableNonPortableJndiName) {
glassfishSpecificJndiName = null;
} else {
glassfishSpecificJndiName = ejb.getJndiName();
}
if (glassfishSpecificJndiName != null
&& (glassfishSpecificJndiName.isEmpty() || glassfishSpecificJndiName.equals(javaGlobalName))) {
glassfishSpecificJndiName = null;
}
// used to decide whether the javaGlobalName needs to be stored
int countPortableJndiNames = 0;
// interfaces now
if (ejb.isRemoteInterfacesSupported()) {
String intf = ejb.getHomeClassName();
SimpleJndiName fullyQualifiedJavaGlobalName = new SimpleJndiName(javaGlobalName + "!" + intf);
appResources.storeInNamespace(fullyQualifiedJavaGlobalName, ejb);
countPortableJndiNames++;
// non-portable
if(glassfishSpecificJndiName != null) {
appResources.storeInNamespace(glassfishSpecificJndiName, ejb);
}
}
if (ejb.isRemoteBusinessInterfacesSupported()) {
int count = 0;
for (String intf : ejb.getRemoteBusinessClassNames()) {
count++;
SimpleJndiName fullyQualifiedJavaGlobalName = new SimpleJndiName(javaGlobalName + "!" + intf);
appResources.storeInNamespace(fullyQualifiedJavaGlobalName, ejb);
countPortableJndiNames++;
// non-portable - interface specific
if(glassfishSpecificJndiName != null) {
SimpleJndiName remoteJndiName = getRemoteEjbJndiName(true, intf, glassfishSpecificJndiName);
appResources.storeInNamespace(remoteJndiName, ejb);
}
}
// non-portable - if only one remote business interface exists and no remote home interfaces exist,
// then by default this can be used to lookup the remote interface.
if(glassfishSpecificJndiName != null && !ejb.isRemoteInterfacesSupported() && count == 1) {
appResources.storeInNamespace(glassfishSpecificJndiName, ejb);
}
}
if (ejb.isLocalInterfacesSupported()) {
String intf = ejb.getLocalHomeClassName();
SimpleJndiName fullyQualifiedJavaGlobalName = new SimpleJndiName(javaGlobalName + "!" + intf);
appResources.storeInNamespace(fullyQualifiedJavaGlobalName, ejb);
countPortableJndiNames++;
}
if (ejb.isLocalBusinessInterfacesSupported()) {
for (String intf : ejb.getLocalBusinessClassNames()) {
SimpleJndiName fullyQualifiedJavaGlobalName = new SimpleJndiName(javaGlobalName + "!" + intf);
appResources.storeInNamespace(fullyQualifiedJavaGlobalName, ejb);
countPortableJndiNames++;
}
}
if (ejb.isLocalBean()) {
String intf = ejb.getEjbClassName();
SimpleJndiName fullyQualifiedJavaGlobalName = new SimpleJndiName(javaGlobalName + "!" + intf);
appResources.storeInNamespace(fullyQualifiedJavaGlobalName, ejb);
countPortableJndiNames++;
}
if (countPortableJndiNames == 1) {
appResources.storeInNamespace(javaGlobalName, ejb);
}
parseResources(ejb, appResources);
}
private SimpleJndiName getJavaGlobalJndiNamePrefix(EjbDescriptor ejbDescriptor) {
String appName = null;
Application app = ejbDescriptor.getApplication();
if (!app.isVirtual()) {
appName = ejbDescriptor.getApplication().getAppName();
}
EjbBundleDescriptor ejbBundle = ejbDescriptor.getEjbBundleDescriptor();
String modName = ejbBundle.getModuleDescriptor().getModuleName();
String ejbName = ejbDescriptor.getName();
StringBuilder javaGlobalPrefix = new StringBuilder(JNDI_CTX_JAVA_GLOBAL);
if (appName != null) {
javaGlobalPrefix.append(appName);
javaGlobalPrefix.append('/');
}
javaGlobalPrefix.append(modName);
javaGlobalPrefix.append('/');
javaGlobalPrefix.append(ejbName);
return new SimpleJndiName(javaGlobalPrefix.toString());
}
private SimpleJndiName getRemoteEjbJndiName(EjbReferenceDescriptor refDesc) {
String intf = refDesc.isEJB30ClientView() ? refDesc.getEjbInterface() : refDesc.getHomeClassName();
return getRemoteEjbJndiName(refDesc.isEJB30ClientView(), intf, refDesc.getJndiName());
}
private SimpleJndiName getRemoteEjbJndiName(boolean businessView, String interfaceName, SimpleJndiName jndiName) {
String portableFullyQualifiedPortion = "!" + interfaceName;
String glassfishFullyQualifiedPortion = "#" + interfaceName;
if (businessView) {
if (!jndiName.hasCorbaPrefix()) {
if (jndiName.isJavaGlobal()) {
return checkFullyQualifiedJndiName(jndiName, portableFullyQualifiedPortion);
}
return checkFullyQualifiedJndiName(jndiName, glassfishFullyQualifiedPortion);
}
} else {
// Only in the portable global case, convert to a fully-qualified name
if (jndiName.isJavaGlobal()) {
return checkFullyQualifiedJndiName(jndiName, portableFullyQualifiedPortion);
}
}
return jndiName;
}
private static SimpleJndiName checkFullyQualifiedJndiName(SimpleJndiName origJndiName, String fullyQualifiedPortion) {
if (origJndiName.hasSuffix(fullyQualifiedPortion)) {
return origJndiName;
}
return new SimpleJndiName(origJndiName + fullyQualifiedPortion);
}
private void parseManagedBeans(AppResources appResources) {
for (BundleDescriptor bd : application.getBundleDescriptors()) {
for (ManagedBeanDescriptor managedBean : bd.getManagedBeans()) {
appResources.storeInNamespace(managedBean.getGlobalJndiName(), (JndiNameEnvironment) bd);
}
}
}
private void parseResources(BundleDescriptor bd, AppResources appResources) {
if (!(bd instanceof JndiNameEnvironment)) {
return;
}
JndiNameEnvironment env = (JndiNameEnvironment) bd;
for (ResourceReferenceDescriptor next : env.getResourceReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (ResourceEnvReferenceDescriptor next : env.getResourceEnvReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (MessageDestinationReferenceDescriptor next : env.getMessageDestinationReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (EnvironmentProperty next : env.getEnvironmentProperties()) {
parseResources(next, env, appResources);
}
for (ResourceDescriptor next : env.getAllResourcesDescriptors()) {
parseResources(next, env, appResources);
}
for (EntityManagerReferenceDescriptor next : env.getEntityManagerReferenceDescriptors()) {
storeInNamespace(SimpleJndiName.of(next.getName()), env, appResources);
}
for (EntityManagerFactoryReferenceDescriptor next : env.getEntityManagerFactoryReferenceDescriptors()) {
storeInNamespace(SimpleJndiName.of(next.getName()), env, appResources);
}
for (EjbReferenceDescriptor next : env.getEjbReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (ServiceReferenceDescriptor next : env.getServiceReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (PersistenceUnitsDescriptor pus : bd.getExtensionsDescriptors(PersistenceUnitsDescriptor.class)) {
for (PersistenceUnitDescriptor pu : pus.getPersistenceUnitDescriptors()) {
parseResources(pu, env, appResources);
}
}
for (ManagedBeanDescriptor mbd : bd.getManagedBeans()) {
parseResources(mbd, env, appResources);
}
}
/**
* Store resources in ResourceRefDescriptor.
*/
private void parseResources(ResourceReferenceDescriptor resRef, JndiNameEnvironment env, AppResources appResources) {
resRef.checkType();
SimpleJndiName name = getLogicalJNDIName(SimpleJndiName.of(resRef.getName()), env);
String type = resRef.getType();
SimpleJndiName jndiName = resRef.getJndiName();
AppResource resRefResource = new AppResource(name, jndiName, type, env, true);
if (resRef.isURLResource()) {
if (jndiName != null && !jndiName.hasJavaPrefix()) {
try {
// for jndi-name like "http://localhost:8080/index.html"
new URL(jndiName.toString());
resRefResource.noValidation();
} catch (MalformedURLException e) {
// If jndi-name is not an actual url, we might want to lookup the name
}
}
}
if (resRef.isWebServiceContext()) {
resRefResource.noValidation();
}
appResources.store(resRefResource);
}
/**
* Store resources in ResourceEnvRefDescriptor.
*/
private void parseResources(ResourceEnvReferenceDescriptor resEnvRef, JndiNameEnvironment env, AppResources appResources) {
resEnvRef.checkType();
SimpleJndiName name = getLogicalJNDIName(SimpleJndiName.of(resEnvRef.getName()), env);
String type = resEnvRef.getType();
SimpleJndiName jndiName = resEnvRef.getJndiName();
AppResource resEnvRefResource = new AppResource(name, jndiName, type, env, true);
if (resEnvRef.isEJBContext() || resEnvRef.isValidator() || resEnvRef.isValidatorFactory() || resEnvRef.isCDIBeanManager()) {
resEnvRefResource.noValidation();
}
appResources.store(resEnvRefResource);
}
/**
* If the message destination ref is linked to a message destination, fetch the linked destination and validate it.
* We might be duplicating our validation efforts since we are already validating message destination separately.
*/
private void parseResources(MessageDestinationReferenceDescriptor msgDestRef, JndiNameEnvironment env, AppResources appResources) {
SimpleJndiName name = getLogicalJNDIName(SimpleJndiName.of(msgDestRef.getName()), env);
SimpleJndiName jndiName;
if (msgDestRef.isLinkedToMessageDestination()) {
jndiName = msgDestRef.getMessageDestination().getJndiName();
} else {
jndiName = msgDestRef.getJndiName();
}
appResources.store(new AppResource(name, jndiName, msgDestRef.getType(), env, true));
}
/**
* Store references to environment entries.
* Also validate custom resources of primitive data types.
*/
private void parseResources(EnvironmentProperty envProp, JndiNameEnvironment env, AppResources appResources) {
SimpleJndiName name = getLogicalJNDIName(SimpleJndiName.of(envProp.getName()), env);
SimpleJndiName jndiName;
if (envProp.hasLookupName()) {
jndiName = envProp.getLookupName();
} else if (!envProp.getMappedName().isEmpty()) {
jndiName = envProp.getMappedName();
} else {
jndiName = new SimpleJndiName("");
}
AppResource envPropResource = new AppResource(name, jndiName, envProp.getType(), env, true);
// If lookup/mapped name is not present, then we do not need to validate.
if (jndiName.isEmpty()) {
envPropResource.noValidation();
}
appResources.store(envPropResource);
// Store EnvProps even if they do not have a valid lookup element
appResources.storeInNamespace(name, env);
}
/**
* Logic from EjbNamingReferenceManagerImpl.java - Here EJB references get resolved
*/
private void parseResources(EjbReferenceDescriptor ejbRef, JndiNameEnvironment env, AppResources appResources) {
SimpleJndiName name = getLogicalJNDIName(SimpleJndiName.of(ejbRef.getName()), env);
// we only need to worry about those references which are not linked yet
if (ejbRef.getEjbDescriptor() != null) {
appResources.storeInNamespace(name, env);
return;
}
SimpleJndiName jndiName = new SimpleJndiName("");
// Should we use an inverse approach i.e., skip validation only in special cases?
// Not sure if that is required as the below approach works fine while resolving EJB references
boolean validationRequired = false;
// local
if (ejbRef.isLocal()) {
// mapped name has no meaning for local ejb-ref as non-portable JNDI names don't have any meaning in this case?
if (ejbRef.hasLookupName()) {
jndiName = ejbRef.getLookupName();
validationRequired = true;
}
} else {
// remote
// mapped-name takes precedence over lookup name
SimpleJndiName ejbRefJndiName = getJndiName(ejbRef);
if (ejbRefJndiName == null && ejbRef.hasLookupName()) {
jndiName = ejbRef.getLookupName();
validationRequired = true;
} else if (ejbRefJndiName != null && ejbRefJndiName.isJavaApp()
&& !ejbRefJndiName.hasPrefix(JNDI_CTX_JAVA_APP_ENV)) {
// TODO: A case skipped from EjbNamingRefManager
// Why does the below logic exist in the EjbNamingRefMan code?
// Intentionally or not, this resolves the java:app mapped names
// Seems suspicious as the corresponding java:global case is handled in the getRemoteEjbJndiName function call
String appName = DOLUtils.getApplicationName(application);
jndiName = ejbRefJndiName.changePrefix(JNDI_CTX_JAVA_GLOBAL + appName + '/');
validationRequired = true;
} else {
SimpleJndiName remoteJndiName = getRemoteEjbJndiName(ejbRef);
// TODO: CORBA case
if (!remoteJndiName.hasCorbaPrefix()) {
validationRequired = true;
jndiName = remoteJndiName;
}
}
}
appResources.store(new AppResource(name, jndiName, ejbRef.getType(), env, validationRequired));
}
private SimpleJndiName getJndiName(EjbReferenceDescriptor descriptor) {
if (!descriptor.hasJndiName()) {
return null;
}
return descriptor.getJndiName();
}
private void parseResources(ServiceReferenceDescriptor serviceRef, JndiNameEnvironment env,
AppResources appResources) {
SimpleJndiName name = getLogicalJNDIName(SimpleJndiName.of(serviceRef.getName()), env);
if (serviceRef.hasLookupName()) {
SimpleJndiName lookupName = serviceRef.getLookupName();
appResources.store(new AppResource(name, lookupName, serviceRef.getType(), env, true));
} else {
appResources.storeInNamespace(name, env);
}
}
/**
* Store the resource definitions in our namespace.
* CFD and AODD are not valid in an AppClient. O/w need to validate the ra-name in them.
*/
private void parseResources(ResourceDescriptor resourceDescriptor, JndiNameEnvironment env, AppResources appResources) {
JavaEEResourceType type = resourceDescriptor.getResourceType();
if (type == JavaEEResourceType.CFD || type == JavaEEResourceType.AODD) {
if (env instanceof ApplicationClientDescriptor) {
return;
}
// No need to type check as CFD and AODD extend from AbstractConnectorResourceDescriptor
AbstractConnectorResourceDescriptor acrd = (AbstractConnectorResourceDescriptor) resourceDescriptor;
SimpleJndiName raJndiName = SimpleJndiName.of(acrd.getResourceAdapter());
appResources
.store(new AppResource(resourceDescriptor.getJndiName(), raJndiName, type.toString(), env, true));
} else {
// nothing to validate here. store the definitions in our namespace.
storeInNamespace(resourceDescriptor.getJndiName(), env, appResources);
}
}
/**
* Record the Data Source specified in PUD.
*/
private void parseResources(PersistenceUnitDescriptor pu, JndiNameEnvironment env, AppResources appResources) {
SimpleJndiName jtaDataSourceName = pu.getJtaDataSource();
SimpleJndiName nonJtaDataSourceName = pu.getNonJtaDataSource();
if (jtaDataSourceName != null && !jtaDataSourceName.isEmpty()) {
appResources.store(new AppResource(pu.getJndiName(), jtaDataSourceName, "javax.sql.DataSource", env, true));
}
if (nonJtaDataSourceName != null && !nonJtaDataSourceName.isEmpty()) {
appResources.store(new AppResource(pu.getJndiName(), nonJtaDataSourceName, "javax.sql.DataSource", env, true));
}
}
private void parseResources(ManagedBeanDescriptor managedBean, JndiNameEnvironment env, AppResources appResources) {
for (ResourceReferenceDescriptor next : managedBean.getResourceReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (ResourceEnvReferenceDescriptor next : managedBean.getResourceEnvReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (MessageDestinationReferenceDescriptor next : managedBean.getMessageDestinationReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (EjbReferenceDescriptor next : managedBean.getEjbReferenceDescriptors()) {
parseResources(next, env, appResources);
}
for (EnvironmentProperty next : managedBean.getEnvironmentProperties()) {
parseResources(next, env, appResources);
}
for (ResourceDescriptor next : env.getAllResourcesDescriptors()) {
parseResources(next, env, appResources);
}
}
private void parseResources(EjbDescriptor ejb, AppResources appResources) {
for (ResourceReferenceDescriptor next : ejb.getResourceReferenceDescriptors()) {
parseResources(next, ejb, appResources);
}
for (ResourceEnvReferenceDescriptor next : ejb.getResourceEnvReferenceDescriptors()) {
parseResources(next, ejb, appResources);
}
for (MessageDestinationReferenceDescriptor next : ejb.getMessageDestinationReferenceDescriptors()) {
parseResources(next, ejb, appResources);
}
for (EnvironmentProperty next : ejb.getEnvironmentProperties()) {
parseResources(next, ejb, appResources);
}
for (EjbReferenceDescriptor next : ejb.getEjbReferenceDescriptors()) {
parseResources(next, ejb, appResources);
}
for (ResourceDescriptor next : ejb.getAllResourcesDescriptors()) {
parseResources(next, ejb, appResources);
}
}
private void storeInNamespace(SimpleJndiName name, JndiNameEnvironment env, AppResources appResources) {
SimpleJndiName logicalJNDIName = getLogicalJNDIName(name, env);
appResources.storeInNamespace(logicalJNDIName, env);
}
/**
* @param rawName to be converted
* @return The logical JNDI name which has a java: prefix
*/
private SimpleJndiName getLogicalJNDIName(SimpleJndiName rawName, JndiNameEnvironment env) {
SimpleJndiName jndiName = rawName.hasJavaPrefix() ? rawName : rawName.changePrefix(JNDI_CTX_JAVA_COMPONENT_ENV);
boolean treatComponentAsModule = DOLUtils.getTreatComponentAsModule(env);
if (treatComponentAsModule && jndiName.isJavaComponent()) {
return jndiName.changePrefix(JNDI_CTX_JAVA_MODULE);
}
return jndiName;
}
/**
* Convert JNDI names beginning with java:module and java:app to their corresponding java:global
* names.
*
* @return the converted name with java:global JNDI prefix.
*/
private SimpleJndiName convertModuleOrAppJNDIName(SimpleJndiName jndiName, JndiNameEnvironment env) {
if (jndiName == null) {
return null;
}
final BundleDescriptor bd;
if (env instanceof EjbDescriptor) {
bd = ((EjbDescriptor) env).getEjbBundleDescriptor();
} else if (env instanceof BundleDescriptor) {
bd = (BundleDescriptor) env;
} else {
bd = null;
}
if (bd == null) {
return new SimpleJndiName("");
}
final String appName = application.isVirtual() ? null : application.getAppName();
final String moduleName = bd.getModuleDescriptor().getModuleName();
final StringBuilder javaGlobalName = new StringBuilder(JNDI_CTX_JAVA_GLOBAL);
if (jndiName.isJavaApp()) {
if (appName != null) {
javaGlobalName.append(appName);
javaGlobalName.append('/');
}
// Replace java:app/ with the fully-qualified global portion
javaGlobalName.append(jndiName.removePrefix(JNDI_CTX_JAVA_APP));
} else if (jndiName.isJavaModule()) {
if (appName != null) {
javaGlobalName.append(appName);
javaGlobalName.append('/');
}
javaGlobalName.append(moduleName);
javaGlobalName.append('/');
javaGlobalName.append(jndiName.removePrefix(JNDI_CTX_JAVA_MODULE));
} else {
return new SimpleJndiName("");
}
return new SimpleJndiName(javaGlobalName.toString());
}
/**
* Start of validation logic.
*/
private void validateResources(AppResources appResources) {
for (AppResource resource : appResources.myResources) {
if (!resource.validate) {
continue;
}
if (JavaEEResourceType.CFD.name().equals(resource.getType())
|| JavaEEResourceType.AODD.name().equals(resource.getType())) {
validateRAName(resource);
} else {
validateJNDIRefs(resource, appResources.myNamespace);
}
}
// Validate the ra-names of app scoped resources
// RA-name and the type of this resource are stored
List<Map.Entry<String, String>> raNames = (List<Map.Entry<String, String>>) dc.getTransientAppMetadata()
.get(ResourceConstants.APP_SCOPED_RESOURCES_RA_NAMES);
if (raNames == null) {
return;
}
for (Map.Entry<String, String> entry : raNames) {
validateRAName(entry.getKey(), entry.getValue());
}
}
/**
* Validate the resource adapter names of @CFD, @AODD.
*/
private void validateRAName(AppResource resource) {
String raName = resource.getJndiName() == null ? null : resource.getJndiName().toString();
validateRAName(raName, resource.getType());
}
/**
* Strategy to validate the resource adapter name:
* 1) In case of stand-alone RA, look in the domain.xml and for default system RA's
* 2) In case of embedded RA, compare it with names of RAR descriptors
* In case of null ra name, we fail the deployment.
*/
private void validateRAName(String raName, String type) {
// No ra-name specified
if (raName == null || raName.isEmpty()) {
LOG.log(Level.SEVERE, RESOURCE_REF_INVALID_RA, new Object[] {null, type});
throw new DeploymentException(localStrings.getLocalString("enterprise.deployment.util.ra.validation",
"Resource Adapter not present: RA Name: {0}, Type: {1}.", null, type));
}
int poundIndex = raName.indexOf("#");
// Pound not present: check for app named raname in domain.xml, check for system ra's
if (poundIndex < 0) {
if (domain.getApplications().getApplication(raName) != null) {
return;
}
// System RA's - Copied from ConnectorConstants.java
if (raName.equals("jmsra") || raName.equals("__ds_jdbc_ra") || raName.equals("jaxr-ra") ||
raName.equals("__cp_jdbc_ra") || raName.equals("__xa_jdbc_ra") || raName.equals("__dm_jdbc_ra")) {
return;
}
if (isEmbedded(raName)) {
return;
}
} else if (raName.substring(0, poundIndex).equals(application.getAppName())) {
// Embedded RA
// In case the app name does not match, we fail the deployment
raName = raName.substring(poundIndex + 1);
if (isEmbedded(raName)) {
return;
}
}
LOG.log(Level.SEVERE, RESOURCE_REF_INVALID_RA, new Object[] {raName, type});
throw new DeploymentException(localStrings.getLocalString(
"enterprise.deployment.util.ra.validation",
"Resource Adapter not present: RA Name: {0}, Type: {1}.",
raName, type));
}
private boolean isEmbedded(String raName) {
String ranameWithRAR = raName + ".rar";
// check for rar named this
for (BundleDescriptor bd : application.getBundleDescriptors(ConnectorDescriptor.class)) {
if (raName.equals(bd.getModuleName()) || ranameWithRAR.equals(bd.getModuleName())) {
return true;
}
}
return false;
}
/**
* Strategy for validating a given jndi name
* 1) Check in domain.xml
* 2) Check in the resources defined within the app. These have not been binded to the namespace yet.
* 3) Check for resources defined by an earlier application.
* In case a null jndi name is passed, we fail the deployment.
*
* @param resource to be validated.
*/
private void validateJNDIRefs(AppResource resource, JNDINamespace namespace) {
// In case lookup is not present, check if another resource with the same name exists
if (!resource.hasLookup()) {
if (namespace.find(resource.getName(), resource.getEnv())) {
return;
}
LOG.log(Level.SEVERE, RESOURCE_REF_JNDI_LOOKUP_FAILED,
new Object[] {resource.getName(), null, resource.getType()});
throw new DeploymentException(
MessageFormat.format("JNDI lookup failed for the resource: Name: {0}, Lookup: {1}, Type: {2}",
resource.getName(), null, resource.getType()));
}
// hasLookup returned true, so it cannot be null.
SimpleJndiName jndiName = resource.getJndiName();
JndiNameEnvironment env = resource.getEnv();
if (isResourceInDomainXML(jndiName) || DEFAULT_JNDI_NAMES.contains(jndiName)) {
return;
}
// Managed Bean & EJB portable JNDI names
if (jndiName.isJavaModule() || jndiName.isJavaApp()) {
SimpleJndiName newName = convertModuleOrAppJNDIName(jndiName, resource.getEnv());
if (namespace.find(newName, env)) {
return;
}
}
// EJB Non-portable JNDI names
if (!jndiName.hasJavaPrefix()) {
if (namespace.find(jndiName, env)) {
return;
}
}
// convert comp to module if req
SimpleJndiName convertedJndiName = getLogicalJNDIName(jndiName, env);
if (namespace.find(convertedJndiName, env)) {
return;
}
try {
if (loadOnCurrentInstance()) {
InitialContext.doLookup(jndiName.toString());
}
} catch (NamingException e) {
throw new DeploymentException(
MessageFormat.format("JNDI lookup failed for the resource: Name: {0}, Lookup: {1}, Type: {2}",
resource.getName(), jndiName, resource.getType()),
e);
}
}
/**
* Validate the given resource in the corresponding target using domain.xml server beans.
* For resources defined outside the application.
*
* @param jndiName to be validated
* @return True if resource is present in domain.xml in the corresponding target. False otherwise.
*/
private boolean isResourceInDomainXML(SimpleJndiName jndiName) {
if (jndiName == null) {
return false;
}
Server svr = domain.getServerNamed(target);
if (svr != null) {
return svr.isResourceRefExists(jndiName);
}
Cluster cluster = domain.getClusterNamed(target);
return cluster != null && cluster.isResourceRefExists(jndiName);
}
private static class AppResource {
private final SimpleJndiName name;
private final SimpleJndiName lookup;
private final String type;
private final JndiNameEnvironment env;
boolean validate;
private AppResource(SimpleJndiName name, SimpleJndiName lookup, String type, JndiNameEnvironment env, boolean validate) {
this.name = name;
this.lookup = lookup;
this.type = type;
this.env = env;
this.validate = validate;
}
private SimpleJndiName getJndiName() {
return lookup;
}
private JndiNameEnvironment getEnv() {
return env;
}
private SimpleJndiName getName() {
return name;
}
private String getType() {
return type;
}
private boolean hasLookup() {
return lookup != null && !lookup.isEmpty();
}
private void noValidation() {
validate = false;
}
}
private static class AppResources {
List<AppResource> myResources;
JNDINamespace myNamespace;
private AppResources() {
myResources = new ArrayList<>();
myNamespace = new JNDINamespace();
}
/**
* Store in namespace only if it has a valid lookup value. This is because we do not want to store invalid
* resources in our namespace.
*/
private void store(AppResource resource) {
myResources.add(resource);
if (resource.hasLookup()) {
myNamespace.store(resource.name, resource.env);
}
}
/**
* If we know that the name points to a valid resource, directly store in namespace.
*/
private void storeInNamespace(SimpleJndiName name, JndiNameEnvironment env) {
myNamespace.store(name, env);
}
private void storeAppScopedResources(Map<String, List<SimpleJndiName>> resourcesList, String appName) {
myNamespace.storeAppScopedResources(resourcesList, appName);
}
}