-
Notifications
You must be signed in to change notification settings - Fork 108
/
InstanceFactory.java
1030 lines (862 loc) · 41.7 KB
/
InstanceFactory.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) 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.faces.application.applicationimpl;
import static com.sun.faces.application.ApplicationImpl.THIS_LIBRARY;
import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.DateTimeConverterUsesSystemTimezone;
import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.RegisterConverterPropertyEditors;
import static com.sun.faces.util.Util.isEmpty;
import static com.sun.faces.util.Util.loadClass;
import static com.sun.faces.util.Util.notNull;
import static com.sun.faces.util.Util.notNullNamedObject;
import static jakarta.faces.application.Resource.COMPONENT_RESOURCE_KEY;
import static jakarta.faces.component.UIComponent.ATTRS_WITH_DECLARED_DEFAULT_VALUES;
import static jakarta.faces.component.UIComponent.BEANINFO_KEY;
import static jakarta.faces.component.UIComponent.COMPOSITE_COMPONENT_TYPE_KEY;
import static java.beans.PropertyEditorManager.findEditor;
import static java.text.MessageFormat.format;
import static java.util.Collections.unmodifiableMap;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;
import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jakarta.el.ExpressionFactory;
import jakarta.el.ValueExpression;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.faces.FacesException;
import jakarta.faces.application.Application;
import jakarta.faces.application.Resource;
import jakarta.faces.component.UIComponent;
import jakarta.faces.component.behavior.Behavior;
import jakarta.faces.context.FacesContext;
import jakarta.faces.convert.Converter;
import jakarta.faces.convert.DateTimeConverter;
import jakarta.faces.render.RenderKit;
import jakarta.faces.render.Renderer;
import jakarta.faces.validator.Validator;
import jakarta.faces.view.ViewDeclarationLanguage;
import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.application.ConverterPropertyEditorFactory;
import com.sun.faces.application.ViewMemberInstanceFactoryMetadataMap;
import com.sun.faces.cdi.CdiUtils;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.ReflectionUtils;
import com.sun.faces.util.Util;
public class InstanceFactory {
// Log instance for this class
private static final Logger LOGGER = FacesLogger.APPLICATION.getLogger();
private static final String CONTEXT = "context";
private static final String COMPONENT_EXPRESSION = "componentExpression";
private static final String COMPONENT_TYPE = "componentType";
private static final String COMPONENT_CLASS = "componentClass";
private static final Map<String, Class<?>[]> STANDARD_CONV_ID_TO_TYPE_MAP = new HashMap<>(8, 1.0f);
private static final Map<Class<?>, String> STANDARD_TYPE_TO_CONV_ID_MAP = new HashMap<>(16, 1.0f);
static {
STANDARD_CONV_ID_TO_TYPE_MAP.put("jakarta.faces.Byte", new Class<?>[] { Byte.TYPE, Byte.class });
STANDARD_CONV_ID_TO_TYPE_MAP.put("jakarta.faces.Boolean", new Class<?>[] { Boolean.TYPE, Boolean.class });
STANDARD_CONV_ID_TO_TYPE_MAP.put("jakarta.faces.Character", new Class<?>[] { Character.TYPE, Character.class });
STANDARD_CONV_ID_TO_TYPE_MAP.put("jakarta.faces.Short", new Class<?>[] { Short.TYPE, Short.class });
STANDARD_CONV_ID_TO_TYPE_MAP.put("jakarta.faces.Integer", new Class<?>[] { Integer.TYPE, Integer.class });
STANDARD_CONV_ID_TO_TYPE_MAP.put("jakarta.faces.Long", new Class<?>[] { Long.TYPE, Long.class });
STANDARD_CONV_ID_TO_TYPE_MAP.put("jakarta.faces.Float", new Class<?>[] { Float.TYPE, Float.class });
STANDARD_CONV_ID_TO_TYPE_MAP.put("jakarta.faces.Double", new Class<?>[] { Double.TYPE, Double.class });
for (Map.Entry<String, Class<?>[]> entry : STANDARD_CONV_ID_TO_TYPE_MAP.entrySet()) {
Class<?>[] types = entry.getValue();
String key = entry.getKey();
for (Class<?> clazz : types) {
STANDARD_TYPE_TO_CONV_ID_MAP.put(clazz, key);
}
}
}
private final String[] STANDARD_BY_TYPE_CONVERTER_CLASSES = { "java.math.BigDecimal", "java.lang.Boolean", "java.lang.Byte", "java.lang.Character",
"java.lang.Double", "java.lang.Float", "java.lang.Integer", "java.lang.Long", "java.lang.Short", "java.lang.Enum" };
private Map<Class<?>, Object> converterTypeMap;
private boolean registerPropertyEditors;
private boolean passDefaultTimeZone;
private TimeZone systemTimeZone;
private static final class ComponentResourceClassNotFound {
}
//
// These four maps store store "identifier" | "class name"
// mappings.
//
private ViewMemberInstanceFactoryMetadataMap<String, Object> componentMap;
private ViewMemberInstanceFactoryMetadataMap<String, Object> behaviorMap;
private ViewMemberInstanceFactoryMetadataMap<String, Object> converterIdMap;
private ViewMemberInstanceFactoryMetadataMap<String, Object> validatorMap;
private Set<String> defaultValidatorIds;
private volatile Map<String, String> defaultValidatorInfo;
private final ApplicationAssociate associate;
/**
* Stores the bean manager.
*/
private BeanManager beanManager;
public InstanceFactory(ApplicationAssociate applicationAssociate) {
associate = applicationAssociate;
componentMap = new ViewMemberInstanceFactoryMetadataMap<>(new ConcurrentHashMap<>());
converterIdMap = new ViewMemberInstanceFactoryMetadataMap<>(new ConcurrentHashMap<>());
converterTypeMap = new ConcurrentHashMap<>();
validatorMap = new ViewMemberInstanceFactoryMetadataMap<>(new ConcurrentHashMap<>());
defaultValidatorIds = new LinkedHashSet<>();
behaviorMap = new ViewMemberInstanceFactoryMetadataMap<>(new ConcurrentHashMap<>());
WebConfiguration webConfig = WebConfiguration.getInstance(FacesContext.getCurrentInstance().getExternalContext());
registerPropertyEditors = webConfig.isOptionEnabled(RegisterConverterPropertyEditors);
passDefaultTimeZone = webConfig.isOptionEnabled(DateTimeConverterUsesSystemTimezone);
if (passDefaultTimeZone) {
systemTimeZone = TimeZone.getDefault();
}
}
/*
* @see jakarta.faces.application.Application#addComponent(java.lang.String, java.lang.String)
*/
public void addComponent(String componentType, String componentClass) {
notNull(COMPONENT_TYPE, componentType);
notNull(COMPONENT_CLASS, componentClass);
if (LOGGER.isLoggable(FINE) && componentMap.containsKey(componentType)) {
LOGGER.log(FINE, "componentType {0} has already been registered. Replacing existing component class type {1} with {2}.",
new Object[] { componentType, componentMap.get(componentType), componentClass });
}
componentMap.put(componentType, componentClass);
if (LOGGER.isLoggable(FINE)) {
LOGGER.fine(MessageFormat.format("added component of type ''{0}'' and class ''{1}''", componentType, componentClass));
}
}
public UIComponent createComponent(ValueExpression componentExpression, FacesContext context, String componentType) throws FacesException {
notNull(COMPONENT_EXPRESSION, componentExpression);
notNull(CONTEXT, context);
notNull(COMPONENT_TYPE, componentType);
return createComponentApplyAnnotations(context, componentExpression, componentType, null, true);
}
public UIComponent createComponent(String componentType) throws FacesException {
notNull(COMPONENT_TYPE, componentType);
return createComponentApplyAnnotations(FacesContext.getCurrentInstance(), componentType, null, true);
}
public UIComponent createComponent(FacesContext context, Resource componentResource, ExpressionFactory expressionFactory) throws FacesException {
// RELEASE_PENDING (rlubke,driscoll) this method needs review.
notNull(CONTEXT, context);
notNull("componentResource", componentResource);
UIComponent result = null;
// Use the application defined in the FacesContext as we may be calling
// overriden methods
Application app = context.getApplication();
ViewDeclarationLanguage vdl = app.getViewHandler().getViewDeclarationLanguage(context, context.getViewRoot().getViewId());
BeanInfo componentMetadata = vdl.getComponentMetadata(context, componentResource);
if (componentMetadata != null) {
BeanDescriptor componentBeanDescriptor = componentMetadata.getBeanDescriptor();
// Step 1. See if the composite component author explicitly
// gave a componentType as part of the composite component metadata
ValueExpression valueExpression = (ValueExpression) componentBeanDescriptor.getValue(COMPOSITE_COMPONENT_TYPE_KEY);
if (valueExpression != null) {
String componentType = (String) valueExpression.getValue(context.getELContext());
if (!isEmpty(componentType)) {
result = app.createComponent(componentType);
}
}
}
// Step 2. If that didn't work, if a script based resource can be
// found for the scriptComponentResource, see if a component can be generated from it
if (result == null) {
Resource scriptComponentResource = vdl.getScriptComponentResource(context, componentResource);
if (scriptComponentResource != null) {
result = createComponentFromScriptResource(context, scriptComponentResource, componentResource);
}
}
// Step 3. Use the libraryName of the resource as the java package
// and use the resourceName as the class name. See
// if a Java class can be loaded
if (result == null) {
String packageName = componentResource.getLibraryName();
String className = componentResource.getResourceName();
className = packageName + '.' + className.substring(0, className.lastIndexOf('.'));
try {
Class<?> clazz = (Class<?>) componentMap.get(className);
if (clazz == null) {
clazz = loadClass(className, this);
}
if (clazz != ComponentResourceClassNotFound.class) {
if (!associate.isDevModeEnabled()) {
componentMap.put(className, clazz);
}
result = (UIComponent) clazz.getDeclaredConstructor().newInstance();
}
} catch (ClassNotFoundException ex) {
if (!associate.isDevModeEnabled()) {
componentMap.put(className, ComponentResourceClassNotFound.class);
}
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException ie) {
throw new FacesException(ie);
}
}
// Step 4. Use jakarta.faces.NamingContainer as the component type
if (result == null) {
result = app.createComponent("jakarta.faces.NamingContainer");
}
result.setRendererType("jakarta.faces.Composite");
Map<String, Object> attrs = result.getAttributes();
attrs.put(COMPONENT_RESOURCE_KEY, componentResource);
attrs.put(BEANINFO_KEY, componentMetadata);
associate.getAnnotationManager().applyComponentAnnotations(context, result);
pushDeclaredDefaultValuesToAttributesMap(context, componentMetadata, attrs, result, expressionFactory);
return result;
}
public UIComponent createComponent(FacesContext context, String componentType, String rendererType) {
notNull(CONTEXT, context);
notNull(COMPONENT_TYPE, componentType);
return createComponentApplyAnnotations(context, componentType, rendererType, true);
}
public UIComponent createComponent(ValueExpression componentExpression, FacesContext context, String componentType, String rendererType) {
notNull(COMPONENT_EXPRESSION, componentExpression);
notNull(CONTEXT, context);
notNull(COMPONENT_TYPE, componentType);
return createComponentApplyAnnotations(context, componentExpression, componentType, rendererType, true);
}
/*
* @see jakarta.faces.application.Application#getComponentTypes()
*/
public Iterator<String> getComponentTypes() {
return componentMap.keySet().iterator();
}
/*
* @see jakarta.faces.application.Application#addBehavior(String, String)
*/
public void addBehavior(String behaviorId, String behaviorClass) {
notNull("behaviorId", behaviorId);
notNull("behaviorClass", behaviorClass);
if (LOGGER.isLoggable(FINE) && behaviorMap.containsKey(behaviorId)) {
LOGGER.log(FINE, "behaviorId {0} has already been registered. Replacing existing behavior class type {1} with {2}.",
new Object[] { behaviorId, behaviorMap.get(behaviorId), behaviorClass });
}
behaviorMap.put(behaviorId, behaviorClass);
if (LOGGER.isLoggable(FINE)) {
LOGGER.fine(MessageFormat.format("added behavior of type ''{0}'' class ''{1}''", behaviorId, behaviorClass));
}
}
/*
* @see jakarta.faces.application.Application#createBehavior(String)
*/
public Behavior createBehavior(String behaviorId) throws FacesException {
notNull("behaviorId", behaviorId);
Behavior behavior = createCDIBehavior(behaviorId);
if (behavior != null) {
return behavior;
}
behavior = newThing(behaviorId, behaviorMap);
notNullNamedObject(behavior, behaviorId, "faces.cannot_instantiate_behavior_error");
if (LOGGER.isLoggable(FINE)) {
LOGGER.fine(MessageFormat.format("created behavior of type ''{0}''", behaviorId));
}
associate.getAnnotationManager().applyBehaviorAnnotations(FacesContext.getCurrentInstance(), behavior);
return behavior;
}
/*
* @see jakarta.faces.application.Application#getBehaviorIds()
*/
public Iterator<String> getBehaviorIds() {
return behaviorMap.keySet().iterator();
}
public void addConverter(String converterId, String converterClass) {
notNull("converterId", converterId);
notNull("converterClass", converterClass);
if (LOGGER.isLoggable(FINE) && converterIdMap.containsKey(converterId)) {
LOGGER.log(FINE, "converterId {0} has already been registered. Replacing existing converter class type {1} with {2}.",
new Object[] { converterId, converterIdMap.get(converterId), converterClass });
}
converterIdMap.put(converterId, converterClass);
Class<?>[] types = STANDARD_CONV_ID_TO_TYPE_MAP.get(converterId);
if (types != null) {
for (Class<?> clazz : types) {
// go directly against map to prevent cyclic method calls
converterTypeMap.put(clazz, converterClass);
addPropertyEditorIfNecessary(clazz);
}
}
if (LOGGER.isLoggable(FINE)) {
LOGGER.fine(format("added converter of type ''{0}'' and class ''{1}''", converterId, converterClass));
}
}
/*
* @see jakarta.faces.application.Application#addConverter(Class, String)
*/
public void addConverter(Class<?> targetClass, String converterClass) {
notNull("targetClass", targetClass);
notNull("converterClass", converterClass);
String converterId = STANDARD_TYPE_TO_CONV_ID_MAP.get(targetClass);
if (converterId != null) {
addConverter(converterId, converterClass);
} else {
if (LOGGER.isLoggable(FINE) && converterTypeMap.containsKey(targetClass)) {
LOGGER.log(FINE, "converter target class {0} has already been registered. Replacing existing converter class type {1} with {2}.",
new Object[] { targetClass.getName(), converterTypeMap.get(targetClass), converterClass });
}
converterTypeMap.put(targetClass, converterClass);
addPropertyEditorIfNecessary(targetClass);
}
if (LOGGER.isLoggable(FINE)) {
LOGGER.fine(format("added converter of class type ''{0}''", converterClass));
}
}
/*
* @see jakarta.faces.application.Application#createConverter(String)
*/
public Converter<?> createConverter(String converterId) {
notNull("converterId", converterId);
Converter<?> converter = createCDIConverter(converterId);
if (converter != null) {
return converter;
}
converter = newThing(converterId, converterIdMap);
notNullNamedObject(converter, converterId, "faces.cannot_instantiate_converter_error");
if (LOGGER.isLoggable(FINE)) {
LOGGER.fine(MessageFormat.format("created converter of type ''{0}''", converterId));
}
if (passDefaultTimeZone && converter instanceof DateTimeConverter) {
((DateTimeConverter) converter).setTimeZone(systemTimeZone);
}
associate.getAnnotationManager().applyConverterAnnotations(FacesContext.getCurrentInstance(), converter);
return converter;
}
/*
* @see jakarta.faces.application.Application#createConverter(Class)
*/
public Converter createConverter(Class<?> targetClass) {
notNull("targetClass", targetClass);
Converter returnVal = null;
BeanManager beanManager = getBeanManager();
returnVal = CdiUtils.createConverter(beanManager, targetClass);
if (returnVal != null) {
return returnVal;
}
returnVal = (Converter) newConverter(targetClass, converterTypeMap, targetClass);
if (returnVal != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName()));
}
if (passDefaultTimeZone && returnVal instanceof DateTimeConverter) {
((DateTimeConverter) returnVal).setTimeZone(systemTimeZone);
}
associate.getAnnotationManager().applyConverterAnnotations(FacesContext.getCurrentInstance(), returnVal);
return returnVal;
}
// Search for converters registered to interfaces implemented by
// targetClass
Class<?>[] interfaces = targetClass.getInterfaces();
if (interfaces != null) {
for (int i = 0; i < interfaces.length; i++) {
returnVal = createConverterBasedOnClass(interfaces[i], targetClass);
if (returnVal != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName()));
}
if (passDefaultTimeZone && returnVal instanceof DateTimeConverter) {
((DateTimeConverter) returnVal).setTimeZone(systemTimeZone);
}
associate.getAnnotationManager().applyConverterAnnotations(FacesContext.getCurrentInstance(), returnVal);
return returnVal;
}
}
}
// Search for converters registered to superclasses of targetClass
Class<?> superclass = targetClass.getSuperclass();
if (superclass != null) {
returnVal = createConverterBasedOnClass(superclass, targetClass);
if (returnVal != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName()));
}
if (passDefaultTimeZone && returnVal instanceof DateTimeConverter) {
((DateTimeConverter) returnVal).setTimeZone(systemTimeZone);
}
associate.getAnnotationManager().applyConverterAnnotations(FacesContext.getCurrentInstance(), returnVal);
return returnVal;
}
}
return returnVal;
}
/*
* @see jakarta.faces.application.Application#getConverterIds()
*/
public Iterator<String> getConverterIds() {
return converterIdMap.keySet().iterator();
}
/*
* @see jakarta.faces.application.Application#getConverterTypes()
*/
public Iterator<Class<?>> getConverterTypes() {
return converterTypeMap.keySet().iterator();
}
/*
* @see jakarta.faces.application.Application#addValidator(String, String)
*/
public void addValidator(String validatorId, String validatorClass) {
notNull("validatorId", validatorId);
notNull("validatorClass", validatorClass);
if (LOGGER.isLoggable(Level.FINE) && validatorMap.containsKey(validatorId)) {
LOGGER.log(Level.FINE, "validatorId {0} has already been registered. Replacing existing validator class type {1} with {2}.",
new Object[] { validatorId, validatorMap.get(validatorId), validatorClass });
}
validatorMap.put(validatorId, validatorClass);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(MessageFormat.format("added validator of type ''{0}'' class ''{1}''", validatorId, validatorClass));
}
}
/*
* @see jakarta.faces.application.Application#createValidator(String)
*/
public Validator<?> createValidator(String validatorId) throws FacesException {
notNull("validatorId", validatorId);
Validator<?> validator = createCDIValidator(validatorId);
if (validator != null) {
return validator;
}
validator = newThing(validatorId, validatorMap);
notNullNamedObject(validator, validatorId, "faces.cannot_instantiate_validator_error");
if (LOGGER.isLoggable(FINE)) {
LOGGER.fine(MessageFormat.format("created validator of type ''{0}''", validatorId));
}
associate.getAnnotationManager().applyValidatorAnnotations(FacesContext.getCurrentInstance(), validator);
return validator;
}
/*
* @see jakarta.faces.application.Application#getValidatorIds()
*/
public Iterator<String> getValidatorIds() {
return validatorMap.keySet().iterator();
}
/*
* @see jakarta.faces.application.Application#addDefaultValidatorId(String)
*/
public synchronized void addDefaultValidatorId(String validatorId) {
notNull("validatorId", validatorId);
defaultValidatorInfo = null;
defaultValidatorIds.add(validatorId);
}
/*
* @see jakarta.faces.application.Application#getDefaultValidatorInfo()
*/
public Map<String, String> getDefaultValidatorInfo() {
if (defaultValidatorInfo == null) {
synchronized (this) {
if (defaultValidatorInfo == null) {
defaultValidatorInfo = new LinkedHashMap<>();
if (!defaultValidatorIds.isEmpty()) {
for (String id : defaultValidatorIds) {
String validatorClass;
Object result = validatorMap.get(id);
if (null != result) {
if (result instanceof Class) {
validatorClass = ((Class<?>) result).getName();
} else {
validatorClass = result.toString();
}
defaultValidatorInfo.put(id, validatorClass);
}
}
}
}
}
defaultValidatorInfo = unmodifiableMap(defaultValidatorInfo);
}
return defaultValidatorInfo;
}
// --------------------------------------------------------- Private Methods
private UIComponent createComponentFromScriptResource(FacesContext context, Resource scriptComponentResource, Resource componentResource) {
UIComponent result = null;
String className = scriptComponentResource.getResourceName();
int lastDot = className.lastIndexOf('.');
className = className.substring(0, lastDot);
try {
Class<?> componentClass = (Class<?>) componentMap.get(className);
if (componentClass == null) {
componentClass = Util.loadClass(className, this);
}
if (!associate.isDevModeEnabled()) {
componentMap.put(className, componentClass);
}
result = (UIComponent) componentClass.getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException ex) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
if (result != null) {
// Make sure the resource is there for the annotation processor.
result.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
// In case there are any "this" references,
// make sure they can be resolved.
context.getAttributes().put(THIS_LIBRARY, componentResource.getLibraryName());
try {
associate.getAnnotationManager().applyComponentAnnotations(context, result);
} finally {
context.getAttributes().remove(THIS_LIBRARY);
}
}
return result;
}
/**
* Leveraged by
* {@link Application#createComponent(jakarta.el.ValueExpression, jakarta.faces.context.FacesContext, String)} and
* {@link Application#createComponent(jakarta.el.ValueExpression, jakarta.faces.context.FacesContext, String, String)}.
* This method will apply any component and render annotations that may be present.
*/
private UIComponent createComponentApplyAnnotations(FacesContext ctx, ValueExpression componentExpression, String componentType, String rendererType,
boolean applyAnnotations) {
UIComponent c;
try {
c = (UIComponent) componentExpression.getValue(ctx.getELContext());
if (c == null) {
c = this.createComponentApplyAnnotations(ctx, componentType, rendererType, applyAnnotations);
componentExpression.setValue(ctx.getELContext(), c);
} else if (applyAnnotations) {
applyAnnotations(ctx, rendererType, c);
}
} catch (Exception ex) {
throw new FacesException(ex);
}
return c;
}
/**
* Leveraged by {@link Application#createComponent(String)} and
* {@link Application#createComponent(jakarta.faces.context.FacesContext, String, String)} This method will apply any
* component and render annotations that may be present.
*/
private UIComponent createComponentApplyAnnotations(FacesContext ctx, String componentType, String rendererType, boolean applyAnnotations) {
UIComponent component;
try {
component = newThing(componentType, componentMap);
} catch (Exception ex) {
if (LOGGER.isLoggable(SEVERE)) {
LOGGER.log(Level.SEVERE, "faces.cannot_instantiate_component_error", componentType);
}
throw new FacesException(ex);
}
notNullNamedObject(component, componentType, "faces.cannot_instantiate_component_error");
if (LOGGER.isLoggable(FINE)) {
LOGGER.log(FINE, MessageFormat.format("Created component with component type of ''{0}''", componentType));
}
if (applyAnnotations) {
applyAnnotations(ctx, rendererType, component);
}
return component;
}
/**
* Process any annotations associated with this component/renderer.
*/
private void applyAnnotations(FacesContext ctx, String rendererType, UIComponent c) {
if (c != null && ctx != null) {
associate.getAnnotationManager().applyComponentAnnotations(ctx, c);
if (rendererType != null) {
RenderKit rk = ctx.getRenderKit();
Renderer r = null;
if (rk != null) {
r = rk.getRenderer(c.getFamily(), rendererType);
if (r != null) {
c.setRendererType(rendererType);
associate.getAnnotationManager().applyRendererAnnotations(ctx, r, c);
}
}
if ((rk == null || r == null) && LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Unable to create Renderer with rendererType {0} for component with component type of {1}",
new Object[] { rendererType, c.getFamily() });
}
}
}
}
/**
* <p>
* PRECONDITIONS: the values in the Map are either Strings representing fully qualified java class names, or
* java.lang.Class instances.
* </p>
* <p>
* ALGORITHM: Look in the argument map for a value for the argument key. If found, if the value is instanceof String,
* assume the String specifies a fully qualified java class name and obtain the java.lang.Class instance for that String
* using Util.loadClass(). Replace the String instance in the argument map with the Class instance. If the value is
* instanceof Class, proceed. Assert that the value is either instanceof java.lang.Class or java.lang.String.
* </p>
* <p>
* Now that you have a java.lang.class, call its newInstance and return it as the result of this method.
* </p>
*
* @param key Used to look up the value in the <code>Map</code>.
* @param map The <code>Map</code> that will be searched.
* @return The new object instance.
*/
@SuppressWarnings("unchecked")
private <T> T newThing(String key, ViewMemberInstanceFactoryMetadataMap<String, Object> map) {
Object result;
Class<?> clazz;
Object value;
value = map.get(key);
if (value == null) {
return null;
}
assert value instanceof String || value instanceof Class;
if (value instanceof String) {
String cValue = (String) value;
try {
clazz = Util.loadClass(cValue, value);
if (!associate.isDevModeEnabled()) {
map.put(key, clazz);
}
assert clazz != null;
} catch (Exception e) {
throw new FacesException(e.getMessage(), e);
}
} else {
clazz = (Class<?>) value;
}
try {
result = clazz.getDeclaredConstructor().newInstance();
} catch (Throwable t) {
Throwable previousT;
do {
previousT = t;
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "Unable to load class: ", t);
}
} while (null != (t = t.getCause()));
t = previousT;
throw new FacesException(MessageUtils.getExceptionMessageString(MessageUtils.CANT_INSTANTIATE_CLASS_ERROR_MESSAGE_ID, clazz.getName()), t);
}
return (T) result;
}
/*
* This method makes it so that any cc:attribute elements that have a "default" attribute value have those values pushed
* into the composite component attribute map so that programmatic access (as opposed to EL access) will find the
* attribute values.
*
*/
@SuppressWarnings("unchecked")
private void pushDeclaredDefaultValuesToAttributesMap(FacesContext context, BeanInfo componentMetadata, Map<String, Object> attrs, UIComponent component,
ExpressionFactory expressionFactory) {
Collection<String> attributesWithDeclaredDefaultValues = null;
for (PropertyDescriptor propertyDescriptor : componentMetadata.getPropertyDescriptors()) {
Object defaultValue = propertyDescriptor.getValue("default");
if (defaultValue != null) {
String key = propertyDescriptor.getName();
// Ensure this attribute is not a method-signature. method-signature
// declared default values are handled in retargetMethodExpressions.
if (propertyDescriptor.getValue("method-signature") == null || propertyDescriptor.getValue("type") != null) {
if (attributesWithDeclaredDefaultValues == null) {
BeanDescriptor beanDescriptor = componentMetadata.getBeanDescriptor();
attributesWithDeclaredDefaultValues = (Collection<String>) beanDescriptor.getValue(ATTRS_WITH_DECLARED_DEFAULT_VALUES);
if (attributesWithDeclaredDefaultValues == null) {
attributesWithDeclaredDefaultValues = new HashSet<>();
beanDescriptor.setValue(ATTRS_WITH_DECLARED_DEFAULT_VALUES, attributesWithDeclaredDefaultValues);
}
}
attributesWithDeclaredDefaultValues.add(key);
}
}
}
}
/**
* Helper method to convert a value to a type as defined in PropertyDescriptor(s)
*
* @param name
* @param value
* @param propertyDescriptors
* @return value
*/
private Object convertValueToTypeIfNecessary(String name, Object value, PropertyDescriptor[] propertyDescriptors, ExpressionFactory expressionFactory) {
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if (propertyDescriptor.getName().equals(name)) {
value = expressionFactory.coerceToType(value, propertyDescriptor.getPropertyType());
break;
}
}
return value;
}
/**
* <p>
* To enable EL Coercion to use Faces Custom converters, this method will call
* <code>PropertyEditorManager.registerEditor()</code>, passing the <code>ConverterPropertyEditor</code> class for the
* <code>targetClass</code> if the target class is not one of the standard by-type converter target classes.
*
* @param targetClass the target class for which a PropertyEditory may or may not be created
*/
private void addPropertyEditorIfNecessary(Class<?> targetClass) {
if (!registerPropertyEditors) {
return;
}
PropertyEditor editor = findEditor(targetClass);
if (editor != null) {
return;
}
String className = targetClass.getName();
// Don't add a PropertyEditor for the standard by-type converters.
if (targetClass.isPrimitive()) {
return;
}
for (String standardClass : STANDARD_BY_TYPE_CONVERTER_CLASSES) {
if (standardClass.indexOf(className) != -1) {
return;
}
}
Class<?> editorClass = ConverterPropertyEditorFactory.getDefaultInstance().definePropertyEditorClassFor(targetClass);
if (editorClass != null) {
PropertyEditorManager.registerEditor(targetClass, editorClass);
} else {
if (LOGGER.isLoggable(WARNING)) {
LOGGER.warning(MessageFormat.format("definePropertyEditorClassFor({0}) returned null.", targetClass.getName()));
}
}
}
private Converter createConverterBasedOnClass(Class<?> targetClass, Class<?> baseClass) {
Converter returnVal = (Converter) newConverter(targetClass, converterTypeMap, baseClass);
if (returnVal != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName()));
}
return returnVal;
}
// Search for converters registered to interfaces implemented by
// targetClass
Class<?>[] interfaces = targetClass.getInterfaces();
if (interfaces != null) {
for (int i = 0; i < interfaces.length; i++) {
returnVal = createConverterBasedOnClass(interfaces[i], null);
if (returnVal != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName()));
}
return returnVal;
}
}
}
// Search for converters registered to superclasses of targetClass
Class<?> superclass = targetClass.getSuperclass();
if (superclass != null) {
returnVal = createConverterBasedOnClass(superclass, targetClass);
if (returnVal != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName()));
}
return returnVal;
}
}
return returnVal;
}
/**
* <p>
* The same as newThing except that a single argument constructor that accepts a Class is looked for before calling the
* no-arg version.
* </p>
*
* <p>
* PRECONDITIONS: the values in the Map are either Strings representing fully qualified java class names, or
* java.lang.Class instances.
* </p>
* <p>
* ALGORITHM: Look in the argument map for a value for the argument key. If found, if the value is instanceof String,
* assume the String specifies a fully qualified java class name and obtain the java.lang.Class instance for that String
* using Util.loadClass(). Replace the String instance in the argument map with the Class instance. If the value is
* instanceof Class, proceed. Assert that the value is either instanceof java.lang.Class or java.lang.String.
* </p>
* <p>
* Now that you have a java.lang.class, call its newInstance and return it as the result of this method.
* </p>
*
* @param key Used to look up the value in the <code>Map</code>.
* @param map The <code>Map</code> that will be searched.
* @param targetClass the target class for the single argument ctor
* @return The new object instance.
*/
protected Object newConverter(Class<?> key, Map<Class<?>, Object> map, Class<?> targetClass) {
assert key != null && map != null;
Object result = null;
Class<?> clazz;
Object value;
value = map.get(key);
if (value == null) {
return null;
}
assert value instanceof String || value instanceof Class;
if (value instanceof String) {
String cValue = (String) value;
try {
clazz = Util.loadClass(cValue, value);
if (!associate.isDevModeEnabled()) {
map.put(key, clazz);
}
assert clazz != null;
} catch (Exception e) {
throw new FacesException(e.getMessage(), e);
}
} else {
clazz = (Class<?>) value;
}
Constructor ctor = ReflectionUtils.lookupConstructor(clazz, Class.class);
Throwable cause = null;
if (ctor != null) {
try {
result = ctor.newInstance(targetClass);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
cause = e;
}
} else {
try {
result = clazz.getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
cause = e;
}
}
if (null != cause) {
throw new FacesException(MessageUtils.getExceptionMessageString(MessageUtils.CANT_INSTANTIATE_CLASS_ERROR_MESSAGE_ID, clazz.getName()), cause);