-
Notifications
You must be signed in to change notification settings - Fork 138
/
ComponentValidator.java
1024 lines (874 loc) · 43.1 KB
/
ComponentValidator.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) 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.util;
import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.BundleDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.InjectionCapable;
import com.sun.enterprise.deployment.InjectionTarget;
import com.sun.enterprise.deployment.ManagedBeanDescriptor;
import com.sun.enterprise.deployment.MessageDestinationDescriptor;
import com.sun.enterprise.deployment.MessageDestinationReferenceDescriptor;
import com.sun.enterprise.deployment.ResourceEnvReferenceDescriptor;
import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
import com.sun.enterprise.deployment.RunAsIdentityDescriptor;
import com.sun.enterprise.deployment.ServiceRefPortInfo;
import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
import com.sun.enterprise.deployment.WebServiceEndpoint;
import com.sun.enterprise.deployment.types.EjbReference;
import com.sun.enterprise.deployment.types.MessageDestinationReferencer;
import com.sun.enterprise.util.LocalStringManagerImpl;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.Principal;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.security.auth.Subject;
import org.glassfish.api.deployment.archive.ArchiveType;
import org.glassfish.logging.annotation.LogMessageInfo;
/**
* @author dochez
*/
public class ComponentValidator extends DefaultDOLVisitor implements ComponentVisitor {
private static final Logger LOG = DOLUtils.deplLogger;
@LogMessageInfo(message = "Could not load {0}", level="FINE")
private static final String LOAD_ERROR = "AS-DEPLOYMENT-00014";
private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(ComponentValidator.class);
protected BundleDescriptor bundleDescriptor;
protected Application application;
@Override
public void accept(BundleDescriptor bundleDescriptor) {
this.bundleDescriptor = bundleDescriptor;
super.accept(bundleDescriptor);
}
/**
* Visits a message destination referencer for the last J2EE
* component visited
* @param msgDestReferencer the message destination referencer
*/
@Override
protected void accept(MessageDestinationReferencer msgDestReferencer) {
if (msgDestReferencer.isLinkedToMessageDestination()) {
// if it is linked to a logical destination
return;
} else if (msgDestReferencer.ownedByMessageDestinationRef()
&& msgDestReferencer.getMessageDestinationRefOwner().getJndiName() != null) {
// if it is referred to a physical destination
return;
} else {
MessageDestinationDescriptor msgDest = msgDestReferencer.resolveLinkName();
if (msgDest == null) {
String linkName = msgDestReferencer.getMessageDestinationLinkName();
LOG.log(Level.WARNING, DOLUtils.INVALID_DESC_MAPPING,
new Object[] {"message-destination", linkName});
} else {
if (msgDestReferencer instanceof MessageDestinationReferenceDescriptor) {
((MessageDestinationReferenceDescriptor) msgDestReferencer).setJndiName(msgDest.getJndiName());
}
}
}
}
/**
* @return the Application object if any
*/
protected Application getApplication() {
return application;
}
/**
* @return the bundleDescriptor we are visiting
*/
protected BundleDescriptor getBundleDescriptor() {
return bundleDescriptor;
}
/**
* @param intfName
* @return default jndi name for a given interface name
*/
//XXX this is first implementation. It does not handle two ejb with same
// interface in different jar
protected String getDefaultEjbJndiName(String intfName) {
return intfName;
}
private enum EjbIntfType {
NONE,
REMOTE_HOME,
REMOTE_BUSINESS,
LOCAL_HOME,
LOCAL_BUSINESS,
NO_INTF_LOCAL_BUSINESS
}
private static class EjbIntfInfo {
Set<EjbDescriptor> ejbs;
// Only set when there is one ejb in the set.
// Otherwise, value = NONE
EjbIntfType intfType;
}
@Override
protected void accept(EjbReference ejbRef) {
LOG.log(Level.FINE, "Visiting Ref {0}", ejbRef);
if (ejbRef.getEjbDescriptor() != null) {
return;
}
// let's try to derive the ejb-ref-type first it is not defined
if (ejbRef.getType() == null) {
// if it's EJB30 (no home/local home), it must be session
if (ejbRef.isEJB30ClientView()) {
ejbRef.setType("Session");
} else {
// if home interface has findByPrimaryKey method,
// it's entity, otherwise it's session
String homeIntf = ejbRef.getEjbHomeInterface();
BundleDescriptor referringJar = ejbRef.getReferringBundleDescriptor();
if (referringJar == null) {
referringJar = getBundleDescriptor();
}
ClassLoader classLoader = referringJar.getClassLoader();
Class<?> clazz = null;
try {
clazz = classLoader.loadClass(homeIntf);
for (Method method : clazz.getDeclaredMethods()) {
if (method.getName().equals("findByPrimaryKey")) {
ejbRef.setType("Entity");
break;
}
}
if (ejbRef.getType() == null) {
ejbRef.setType("Session");
}
} catch(Exception e) {
LogRecord lr = new LogRecord(Level.FINE, LOAD_ERROR);
Object args[] = { homeIntf };
lr.setParameters(args);
lr.setThrown(e);
LOG.log(lr);
}
}
}
//
// NOTE : In the 3.0 local/remote business view, the local vs.
// remote designation is not always detectable from the interface
// itself.
//
// That means
//
// 1) we need to figure it out during this stage of the processing
// 2) the EjbReferenceDescriptor.isLocal() operations shouldn't be
// be used before the post-application validation stage since its
// value would be unreliable.
// 3) We can't write out the standard deployment descriptors to XML
// until the full application has been processed, including this
// validation stage.
//
// During @EJB processing, setLocal() is set to false if
// local vs. remote is ambiguous. setLocal() is set to true within this
// method upon successfuly resolution to a local business interface.
//
if (ejbRef.getJndiName()!=null && ejbRef.getJndiName().length()!=0) {
// ok this is getting a little complicated here
// the jndi name is not null, if this is a remote ref, proceed with resolution
// if this is a local ref, proceed with resolution only if ejb-link is null
if (!ejbRef.isLocal() || ejbRef.getLinkName()==null) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Ref " + ejbRef.getName() + " is bound to Ejb with JNDI Name " + ejbRef.getJndiName());
}
if (getEjbDescriptors() != null) {
for (Object element : getEjbDescriptors()) {
EjbDescriptor ejb = (EjbDescriptor) element;
if (ejbRef.getJndiName().equals(ejb.getJndiName())) {
ejbRef.setEjbDescriptor(ejb);
return;
}
}
}
}
}
// If the reference does not have an ejb-link or jndi-name or lookup string associated
// with it, attempt to resolve it by checking against all the ejbs
// within the application. If no match is found, just fall through
// and let the existing error-checking logic kick in.
if (( (ejbRef.getJndiName() == null) ||
(ejbRef.getJndiName().length() == 0) )
&&
( (ejbRef.getLinkName() == null) ||
(ejbRef.getLinkName().length() == 0) )
&& !ejbRef.hasLookupName() ) {
Map<String, EjbIntfInfo> ejbIntfInfoMap = getEjbIntfMap();
if ( ejbIntfInfoMap.size() > 0 ) {
String interfaceToMatch = ejbRef.isEJB30ClientView() ?
ejbRef.getEjbInterface() : ejbRef.getEjbHomeInterface();
if ( interfaceToMatch == null ) {
String msg = localStrings.getLocalString(
"enterprise.deployment.util.no_remoteinterface",
"Cannot resolve reference {0} because it does not declare a remote interface or " +
"remote home interface of target bean",
new Object[]{ejbRef});
throw new IllegalArgumentException(msg);
}
EjbIntfInfo intfInfo = ejbIntfInfoMap.get(interfaceToMatch);
// make sure exactly one match
if ( intfInfo != null ) {
int numMatches = intfInfo.ejbs.size();
if( numMatches == 1 ) {
EjbDescriptor target = intfInfo.ejbs.iterator().next();
BundleDescriptor targetModule =
target.getEjbBundleDescriptor();
BundleDescriptor sourceModule =
ejbRef.getReferringBundleDescriptor();
Application app = targetModule.getApplication();
//
// It's much cleaner to derive the ejb-link value
// and set that instead of the descriptor. This way,
// if there are multiple ejb-jars within the .ear that
// each have an ejb with the target bean's ejb-name,
// there won't be any ambiguity about which one is
// the correct target. It's not so much a problem
// during this phase of the processing, but if the
// fully-qualified ejb-link name is required and is not
// written out, there could be non-deterministic
// behavior when the application is re-loaded.
// Let the ejb-link processing logic handle the
// conversion to ejb descriptor.
//
// If the ejb reference and the target ejb are defined
// within the same ejb-jar, the ejb-link will only
// be set to ejb-name. This is done regardless of
// whether the ejb-jar is within an .ear or is
// stand-alone. The ejb-link processing
// logic will always check the current ejb-jar
// first so there won't be any ambiguity.
String ejbLinkName = target.getName();
if (!sourceModule.isPackagedAsSingleModule(targetModule)) {
String relativeUri = null;
if( sourceModule == app ) {
// Now that dependencies can be defined within application.xml
// it's possible for source module to be the Application object.
// In this case, just use the target module uri as the relative
// uri.
relativeUri = targetModule.getModuleDescriptor().getArchiveUri();
} else {
// Since there are at least two modules, we
// must be within an application.
relativeUri = getApplication().getRelativeUri(sourceModule, targetModule);
}
ejbLinkName = relativeUri + "#" + ejbLinkName;
}
ejbRef.setLinkName(ejbLinkName);
} else {
String msg = localStrings.getLocalString(
"enterprise.deployment.util.multiple_ejbs_with_interface",
"Cannot resolve reference {0} because there are {1} ejbs in the application with interface {2}.",
new Object[] {ejbRef, numMatches, interfaceToMatch});
throw new IllegalArgumentException(msg);
}
}
}
}
// now all cases fall back here, we need to resolve through the link-name
if (ejbRef.getLinkName()==null) {
// if no link name if present, and this is a local ref, this is an
// error (unless there is a lookup string) because we must resolve all
// local refs within the app and we cannot resolve it
if (ejbRef.isLocal()) {
if( ejbRef.hasLookupName() ) {
return;
}
LOG.severe("Cannot resolve reference " + ejbRef);
throw new RuntimeException("Cannot resolve reference " + ejbRef);
} else {
// this is a remote interface, jndi will eventually contain the referenced
// ejb ref, apply default jndi name if there is none
if (!ejbRef.hasJndiName() && !ejbRef.hasLookupName()) {
String jndiName = getDefaultEjbJndiName(
ejbRef.isEJB30ClientView() ?
ejbRef.getEjbInterface() : ejbRef.getEjbHomeInterface());
ejbRef.setJndiName(jndiName);
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Applying default to ejb reference: " + ejbRef);
}
}
return;
}
}
// Beginning of ejb-link resolution
// save anticipated types for checking if interfaces are compatible
String homeClassName = ejbRef.getEjbHomeInterface();
String intfClassName = ejbRef.getEjbInterface();
// save anticipated type for checking if bean type is compatible
String type = ejbRef.getType();
EjbDescriptor ejbReferee=null;
String linkName = ejbRef.getLinkName();
int ind = linkName.lastIndexOf('#');
if ( ind != -1 ) {
// link has a relative path from referring EJB JAR,
// of form "../products/product.jar#ProductEJB"
String ejbName = linkName.substring(ind+1);
String jarPath = linkName.substring(0, ind);
BundleDescriptor referringJar = ejbRef.getReferringBundleDescriptor();
if (referringJar==null) {
ejbRef.setReferringBundleDescriptor(getBundleDescriptor());
referringJar = getBundleDescriptor();
}
if (getApplication()!=null) {
BundleDescriptor refereeJar = null;
if( referringJar instanceof Application ) {
refereeJar = ((Application)referringJar).getModuleByUri(jarPath);
} else {
refereeJar =
getApplication().getRelativeBundle(referringJar, jarPath);
}
if( (refereeJar != null) &&
refereeJar instanceof EjbBundleDescriptor ) {
// this will throw an exception if ejb is not found
ejbReferee =
((EjbBundleDescriptor)refereeJar).getEjbByName(ejbName);
}
}
}
else {
// Handle an unqualified ejb-link, which is just an ejb-name.
// If we're in an application and currently processing an
// ejb-reference defined within an ejb-jar, first check
// the current ejb-jar for an ejb-name match. From a spec
// perspective, the deployer can't depend on this behavior,
// but it's still better to have deterministic results. In
// addition, in the case of automatic-linking, the fully-qualified
// "#" ejb-link syntax is not used when the ejb reference and
// target ejb are within the same ejb-jar. Checking the
// ejb-jar first will ensure the correct linking behavior for
// that case.
Application app = getApplication();
EjbBundleDescriptor ebd = getEjbBundleDescriptor();
if (app != null && ebd != null && ebd.hasEjbByName(linkName)) {
ejbReferee = ebd.getEjbByName(linkName);
} else if (app != null && app.hasEjbByName(linkName)) {
ejbReferee = app.getEjbByName(ejbRef.getLinkName());
} else if (getEjbDescriptor()!=null) {
try {
ejbReferee = getEjbDescriptor().getEjbBundleDescriptor().getEjbByName(ejbRef.getLinkName());
} catch (IllegalArgumentException e) {
// this may happen when we have no application and the ejb ref
// cannot be resolved to a ejb in the bundle. The ref will
// probably be resolved when the application is assembled.
LOG.warning("Unresolved <ejb-link>: "+linkName);
return;
}
}
}
if (ejbReferee==null) {
// we could not resolve through the ejb-link. if this is a local ref, this
// is an error, if this is a remote ref, this should be also an error at
// runtime but maybe the jndi name will be specified by deployer so
// a warning should suffice
if (ejbRef.isLocal()) {
LOG.severe("Unresolved <ejb-link>: "+linkName);
throw new RuntimeException("Error: Unresolved <ejb-link>: "+linkName);
} else {
final ArchiveType moduleType = ejbRef.getReferringBundleDescriptor().getModuleType();
if(moduleType != null && moduleType.equals(DOLUtils.carType())) {
// Because no annotation processing is done within ACC runtime, this case typically
// arises for remote @EJB annotations, so don't log it as warning.
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Unresolved <ejb-link>: "+linkName);
}
} else {
LOG.warning("Unresolved <ejb-link>: "+linkName);
}
return;
}
} else {
if( ejbRef.isEJB30ClientView() ) {
BundleDescriptor referringBundle =
ejbRef.getReferringBundleDescriptor();
// If we can verify that the current ejb 3.0 reference is defined
// in any Application Client module or in a stand-alone web module
// it must be remote business.
if( ( (referringBundle == null) && (getEjbBundleDescriptor() == null) )
||
((referringBundle != null) && (referringBundle.getModuleType() == DOLUtils.carType()))
||
( (getApplication() == null) &&
(referringBundle.getModuleType() != null && referringBundle.getModuleType().equals(DOLUtils.warType())) ) ) {
ejbRef.setLocal(false);
// Double-check that target has a remote business interface of this
// type. This will handle the common error case that the target
// EJB has intended to support a remote business interface but
// has not used @Remote to specify it, in which case
// the interface was assigned the default of local business.
if( !ejbReferee.getRemoteBusinessClassNames().contains
(intfClassName) ) {
String msg = "Target ejb " + ejbReferee.getName() + " for " +
" remote ejb 3.0 reference " + ejbRef.getName() +
" does not expose a remote business interface of type " +
intfClassName;
throw new RuntimeException(msg);
}
} else if(ejbReferee.getLocalBusinessClassNames().contains(intfClassName)) {
ejbRef.setLocal(true);
} else if(ejbReferee.getRemoteBusinessClassNames().contains(intfClassName)) {
ejbRef.setLocal(false);
} else {
if (ejbReferee.isLocalBean()) {
ejbRef.setLocal(true);
} else {
String msg = "Warning : Unable to determine local " +
" business vs. remote business designation for " +
" EJB 3.0 ref " + ejbRef;
throw new RuntimeException(msg);
}
}
}
ejbRef.setEjbDescriptor(ejbReferee);
}
// if we are here, we must have resolved the reference
if(LOG.isLoggable(Level.FINE)) {
if (getEjbDescriptor() != null){
LOG.fine("Done Visiting " + getEjbDescriptor().getName() + " reference " + ejbRef);
}
}
// check that declared types are compatible with expected values
// if there is a target ejb descriptor available
if( ejbRef.isEJB30ClientView() ) {
Set<String> targetBusinessIntfs = ejbRef.isLocal() ?
ejbReferee.getLocalBusinessClassNames() :
ejbReferee.getRemoteBusinessClassNames();
EjbDescriptor ejbDesc = ejbRef.getEjbDescriptor();
// If it's neither a business interface nor a no-interface view
if( !targetBusinessIntfs.contains(intfClassName) &&
( ejbDesc.isLocalBean() &&
!(intfClassName.equals(ejbReferee.getEjbClassName()))) ) {
LOG.log(Level.WARNING,
"enterprise.deployment.backend.ejbRefTypeMismatch",
new Object[] {ejbRef.getName() , intfClassName,
ejbReferee.getName(), ( ejbRef.isLocal() ?
"Local Business" : "Remote Business"),
targetBusinessIntfs.toString()});
// We can only figure out what the correct type should be
// if there is only 1 target remote/local business intf.
if( targetBusinessIntfs.size() == 1 ) {
Iterator iter = targetBusinessIntfs.iterator();
ejbRef.setEjbInterface((String)iter.next());
}
}
} else {
String targetHome = ejbRef.isLocal() ?
ejbReferee.getLocalHomeClassName() :
ejbReferee.getHomeClassName();
if( !homeClassName.equals(targetHome) ) {
LOG.log(Level.WARNING,
"enterprise.deployment.backend.ejbRefTypeMismatch",
new Object[] {ejbRef.getName() , homeClassName,
ejbReferee.getName(), ( ejbRef.isLocal() ?
"Local Home" : "Remote Home"), targetHome});
if( targetHome != null ) {
ejbRef.setEjbHomeInterface(targetHome);
}
}
String targetComponentIntf = ejbRef.isLocal() ?
ejbReferee.getLocalClassName() :
ejbReferee.getRemoteClassName();
// In some cases for 2.x style @EJBs that point to Entity beans
// the interface class cannot be derived, so only do the
// check if the intf is known.
if( (intfClassName != null) &&
!intfClassName.equals(targetComponentIntf) ) {
LOG.log(Level.WARNING,
"enterprise.deployment.backend.ejbRefTypeMismatch",
new Object[] {ejbRef.getName() , intfClassName,
ejbReferee.getName(), ( ejbRef.isLocal() ?
"Local" : "Remote"), targetComponentIntf});
if( targetComponentIntf != null ) {
ejbRef.setEjbInterface(targetComponentIntf);
}
}
}
// set jndi name in ejb ref
ejbRef.setJndiName(ejbReferee.getJndiName());
if (!type.equals(ejbRef.getType())) {
// if they don't match
// print a warning and reset the type in ejb ref
LOG.log(Level.WARNING, DOLUtils.INVALID_DESC_MAPPING,
new Object[] {ejbRef.getName() , type});
ejbRef.setType(ejbRef.getType());
}
}
protected Collection getEjbDescriptors() {
if (getApplication() != null) {
return getApplication().getEjbDescriptors();
} else if (getEjbBundleDescriptor()!=null) {
return getEjbBundleDescriptor().getEjbs();
} else {
return new HashSet<EjbDescriptor>();
}
}
protected EjbDescriptor getEjbDescriptor() {
return null;
}
protected EjbBundleDescriptor getEjbBundleDescriptor() {
return null;
}
/**
* Returns a map of interface name -> EjbIntfInfo based on all the ejbs
* within the application or stand-alone module. Only RemoteHome,
* RemoteBusiness, LocalHome, and LocalBusiness are eligible for map.
*/
private Map<String, EjbIntfInfo> getEjbIntfMap() {
Map<String, EjbIntfInfo> intfInfoMap = new HashMap<>();
for (Object element : getEjbDescriptors()) {
EjbDescriptor next = (EjbDescriptor) element;
if( next.isRemoteInterfacesSupported() ) {
addIntfInfo(intfInfoMap, next.getHomeClassName(),
EjbIntfType.REMOTE_HOME, next);
}
if( next.isRemoteBusinessInterfacesSupported() ) {
for(String nextIntf : next.getRemoteBusinessClassNames()) {
addIntfInfo(intfInfoMap, nextIntf,
EjbIntfType.REMOTE_BUSINESS, next);
}
}
if( next.isLocalInterfacesSupported() ) {
addIntfInfo(intfInfoMap, next.getLocalHomeClassName(),
EjbIntfType.LOCAL_HOME, next);
}
if( next.isLocalBusinessInterfacesSupported() ) {
for(String nextIntf : next.getLocalBusinessClassNames()) {
addIntfInfo(intfInfoMap, nextIntf,
EjbIntfType.LOCAL_BUSINESS, next);
}
}
if (next.isLocalBean()) {
addIntfInfo(intfInfoMap, next.getEjbClassName(),
EjbIntfType.NO_INTF_LOCAL_BUSINESS, next);
}
}
return intfInfoMap;
}
private void addIntfInfo(Map<String, EjbIntfInfo> intfInfoMap,
String intf, EjbIntfType intfType,
EjbDescriptor ejbDesc) {
EjbIntfInfo intfInfo = intfInfoMap.get(intf);
if( intfInfo == null ) {
EjbIntfInfo newInfo = new EjbIntfInfo();
newInfo.ejbs = new HashSet<>();
newInfo.ejbs.add(ejbDesc);
newInfo.intfType = intfType;
intfInfoMap.put(intf, newInfo);
} else {
intfInfo.ejbs.add(ejbDesc);
// Since there's more than one match, reset intf type.
intfInfo.intfType = EjbIntfType.NONE;
}
}
/**
* Visits a service reference for the last J2EE component visited
*
* @param the service reference
*/
@Override
protected void accept(ServiceReferenceDescriptor serviceRef) {
Set portsInfo = serviceRef.getPortsInfo();
for (Object element : portsInfo) {
ServiceRefPortInfo next = (ServiceRefPortInfo) element;
if( next.hasPortComponentLinkName() &&
!next.isLinkedToPortComponent() ) {
WebServiceEndpoint portComponentLink = next.resolveLinkName();
if( portComponentLink == null ) {
String linkName = next.getPortComponentLinkName();
LOG.log(Level.WARNING, DOLUtils.INVALID_DESC_MAPPING,
new Object[] {"port-component" , linkName});
}
}
}
}
@Override
protected void accept(ResourceReferenceDescriptor resRef) {
computeRuntimeDefault(resRef);
}
@Override
protected void accept(ResourceEnvReferenceDescriptor resourceEnvRef) {
if (resourceEnvRef.getJndiName() == null ||
resourceEnvRef.getJndiName().length() == 0) {
Map<String, ManagedBeanDescriptor> managedBeanMap = getManagedBeanMap();
String refType = resourceEnvRef.getRefType();
if( managedBeanMap.containsKey(refType) ) {
ManagedBeanDescriptor desc = managedBeanMap.get(refType);
// In app-client, keep lookup local to JVM so it doesn't need to access
// server's global JNDI namespace for managed bean.
String jndiName = ( bundleDescriptor.getModuleType() == DOLUtils.carType() )
? desc.getAppJndiName() : desc.getGlobalJndiName();
resourceEnvRef.setJndiName(jndiName);
resourceEnvRef.setIsManagedBean(true);
resourceEnvRef.setManagedBeanDescriptor(desc);
}
}
computeRuntimeDefault(resourceEnvRef);
}
protected void accept(MessageDestinationReferenceDescriptor msgDestRef) {
computeRuntimeDefault(msgDestRef);
}
@Override
protected void accept(MessageDestinationDescriptor msgDest) {
computeRuntimeDefault(msgDest);
}
/**
* visits all entries within the component environment for which
* isInjectable() == true.
* @param injectable InjectionCapable environment dependency
*/
protected void accept(InjectionCapable injectable) {
acceptWithCL(injectable);
acceptWithoutCL(injectable);
}
// we need to split the accept(InjectionCapable) into two parts:
// one needs classloader and one doesn't. This is needed because
// in the standalone war case, the classloader is not created
// untill the web module is being started.
protected void acceptWithCL(InjectionCapable injectable) {
// If parsed from deployment descriptor, we need to determine whether
// the inject target name refers to an injection field or an
// injection method for each injection target
for (InjectionTarget target : injectable.getInjectionTargets()) {
if( (target.getFieldName() == null) &&
(target.getMethodName() == null) ) {
String injectTargetName = target.getTargetName();
String targetClassName = target.getClassName();
ClassLoader classLoader = getBundleDescriptor().getClassLoader();
Class targetClazz = null;
try {
targetClazz = classLoader.loadClass(targetClassName);
} catch(ClassNotFoundException cnfe) {
// @@@
// Don't treat this as a fatal error for now. One known issue
// is that all .xml, even web.xml, is processed within the
// appclient container during startup. In that case, there
// are issues with finding .classes in .wars due to the
// structure of the returned client .jar and the way the
// classloader is formed.
if (LOG.isLoggable(Level.FINE)) {
LOG.fine
("Injection class " + targetClassName + " not found for " +
injectable);
}
return;
}
// Spec requires that we attempt to match on method before field.
boolean matched = false;
// The only information we have is method name, so iterate
// through the methods find a match. There is no overloading
// allowed for injection methods, so any match is considered
// the only possible match.
String setterMethodName = TypeUtil.
propertyNameToSetterMethod(injectTargetName);
// method can have any access type so use getDeclaredMethods()
for(Method next : targetClazz.getDeclaredMethods()) {
// only when the method name matches and the method
// has exactly one parameter, we find a match
if( next.getName().equals(setterMethodName) &&
next.getParameterTypes().length == 1) {
target.setMethodName(next.getName());
if( injectable.getInjectResourceType() == null ) {
Class[] paramTypes = next.getParameterTypes();
if (paramTypes.length == 1) {
String resourceType = paramTypes[0].getName();
injectable.setInjectResourceType(resourceType);
}
}
matched = true;
break;
}
}
if( !matched ) {
// In the case of injection fields, inject target name ==
// field name. Field can have any access type.
try {
Field f = targetClazz.getDeclaredField(injectTargetName);
target.setFieldName(injectTargetName);
if( injectable.getInjectResourceType() == null ) {
String resourceType = f.getType().getName();
injectable.setInjectResourceType(resourceType);
}
matched = true;
} catch(NoSuchFieldException nsfe) {
String msg = "No matching injection setter method or " +
"injection field found for injection property " +
injectTargetName + " on class " + targetClassName +
" for component dependency " + injectable;
throw new RuntimeException(msg, nsfe);
}
}
}
}
}
protected void acceptWithoutCL(InjectionCapable injectable) {
}
/**
* Set a default RunAs principal to given RunAsIdentityDescriptor
* if necessary.
* @param runAs
* @param application
* @exception RuntimeException
*/
protected void computeRunAsPrincipalDefault(RunAsIdentityDescriptor runAs,
Application application) {
// for backward compatibile
if (runAs != null &&
(runAs.getRoleName() == null ||
runAs.getRoleName().length() == 0)) {
LOG.log(Level.WARNING,
"enterprise.deployment.backend.emptyRoleName");
return;
}
if (runAs != null &&
(runAs.getPrincipal() == null ||
runAs.getPrincipal().length() == 0) &&
application != null && application.getRoleMapper() != null) {
String principalName = null;
String roleName = runAs.getRoleName();
final Subject fs = (Subject)application.getRoleMapper().getRoleToSubjectMapping().get(roleName);
if (fs != null) {
principalName = (String)AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
Set<Principal> pset = fs.getPrincipals();
Principal prin = null;
if (pset.size() > 0) {
prin = pset.iterator().next();
LOG.log(Level.WARNING,
"enterprise.deployment.backend.computeRunAsPrincipal",
new Object[] { prin.getName() });
}
return (prin != null) ? prin.getName() : null;
}
});
}
if (principalName == null || principalName.length() == 0) {
throw new RuntimeException("The RunAs role " + "\"" + roleName + "\"" +
" is not mapped to a principal.");
}
runAs.setPrincipal(principalName);
}
}
/**
* Get a map of bean class to managed bean descriptor for the managed beans
* defined within the current module.
*/
private Map<String, ManagedBeanDescriptor> getManagedBeanMap() {
BundleDescriptor thisBundle = getBundleDescriptor();
Set<ManagedBeanDescriptor> managedBeans = new HashSet<>();
// Make sure we're dealing with the top-level bundle descriptor when looking
// for managed beans
if( thisBundle != null ) {
Object desc = thisBundle.getModuleDescriptor().getDescriptor();
if( desc instanceof BundleDescriptor ) {
managedBeans = ((BundleDescriptor)desc).getManagedBeans();
}
}
Map<String, ManagedBeanDescriptor> managedBeanMap = new HashMap<>();
for(ManagedBeanDescriptor managedBean : managedBeans ) {
String beanClassName = managedBean.getBeanClassName();
managedBeanMap.put(beanClassName, managedBean);
}
return managedBeanMap;
}
/**
* Set runtime default value for ResourceReferenceDescriptor.
*/
private void computeRuntimeDefault(ResourceReferenceDescriptor resRef) {
if (resRef.getType() != null && resRef.getType().equals("org.omg.CORBA.ORB")) {
resRef.setJndiName("java:comp/ORB");
}
else if (resRef.getJndiName() == null ||
resRef.getJndiName().length() == 0) {
if (resRef.getType() != null) {
if (resRef.getType().equals("javax.sql.DataSource")) {
resRef.setLookupName("java:comp/DefaultDataSource");
} else if (resRef.getType().equals("jakarta.jms.ConnectionFactory")) {
resRef.setLookupName("java:comp/DefaultJMSConnectionFactory");
} else {
resRef.setJndiName(getDefaultResourceJndiName(resRef.getName()));
}
} else {
resRef.setJndiName(getDefaultResourceJndiName(resRef.getName()));
}
}
}
/**
* Set runtime default value for ResourceEnvReferenceDescriptor.
*/
private void computeRuntimeDefault(ResourceEnvReferenceDescriptor resourceEnvRef) {
if (resourceEnvRef.getRefType() != null && resourceEnvRef.getRefType().equals(
"jakarta.transaction.UserTransaction")) {
resourceEnvRef.setJndiName("java:comp/UserTransaction");
}
else if (resourceEnvRef.getRefType() != null && resourceEnvRef.getRefType().equals("jakarta.transaction.TransactionSynchronizationRegistry")) {
resourceEnvRef.setJndiName(
"java:comp/TransactionSynchronizationRegistry");
}
else if (resourceEnvRef.getJndiName() == null ||
resourceEnvRef.getJndiName().length() == 0) {
if (resourceEnvRef.getRefType() != null) {
if (resourceEnvRef.getRefType().equals("jakarta.enterprise.concurrent.ManagedExecutorService")) {
resourceEnvRef.setLookupName("java:comp/DefaultManagedExecutorService");
} else if (resourceEnvRef.getRefType().equals("jakarta.enterprise.concurrent.ManagedScheduledExecutorService")) {
resourceEnvRef.setLookupName("java:comp/DefaultManagedScheduledExecutorService");
} else if (resourceEnvRef.getRefType().equals("jakarta.enterprise.concurrent.ManagedThreadFactory")) {
resourceEnvRef.setLookupName("java:comp/DefaultManagedThreadFactory");
} else if (resourceEnvRef.getRefType().equals("jakarta.enterprise.concurrent.ContextService")) {
resourceEnvRef.setLookupName("java:comp/DefaultContextService");
} else {
resourceEnvRef.setJndiName(getDefaultResourceJndiName(resourceEnvRef.getName()));
}
} else {
resourceEnvRef.setJndiName(getDefaultResourceJndiName(resourceEnvRef.getName()));
}
}
}
/**
* Set runtime default value for MessageDestinationReferenceDescriptor.
*/
private void computeRuntimeDefault(MessageDestinationReferenceDescriptor msgDestRef) {
if (msgDestRef.getJndiName() == null ||