-
Notifications
You must be signed in to change notification settings - Fork 53
/
BaseHK2JAXBBean.java
executable file
·1384 lines (1146 loc) · 50 KB
/
BaseHK2JAXBBean.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) 2014, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.hk2.xml.jaxb.internal;
import java.beans.PropertyChangeEvent;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import jakarta.xml.bind.annotation.XmlTransient;
import javax.xml.namespace.QName;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.Customizer;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase;
import org.glassfish.hk2.configuration.hub.api.WriteableType;
import org.glassfish.hk2.utilities.general.GeneralUtilities;
import org.glassfish.hk2.utilities.reflection.ClassReflectionHelper;
import org.glassfish.hk2.utilities.reflection.Logger;
import org.glassfish.hk2.utilities.reflection.ReflectionHelper;
import org.glassfish.hk2.xml.api.XmlHk2ConfigurationBean;
import org.glassfish.hk2.xml.api.XmlRootHandle;
import org.glassfish.hk2.xml.api.XmlService;
import org.glassfish.hk2.xml.internal.ChildType;
import org.glassfish.hk2.xml.internal.DynamicChangeInfo;
import org.glassfish.hk2.xml.internal.ModelImpl;
import org.glassfish.hk2.xml.internal.ModelPropertyType;
import org.glassfish.hk2.xml.internal.NamespaceBeanLikeMapImpl;
import org.glassfish.hk2.xml.internal.ParentedModel;
import org.glassfish.hk2.xml.internal.QNameUtilities;
import org.glassfish.hk2.xml.internal.Utilities;
import org.glassfish.hk2.xml.internal.XmlDynamicChange;
import org.glassfish.hk2.xml.internal.XmlRootHandleImpl;
/**
* @author jwells
*
*/
@XmlTransient
public abstract class BaseHK2JAXBBean implements XmlHk2ConfigurationBean, Serializable {
private static final long serialVersionUID = 8149986319033910297L;
private final static boolean DEBUG_GETS_AND_SETS = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return Boolean.parseBoolean(
System.getProperty("org.jvnet.hk2.properties.xmlservice.jaxb.getsandsets", "false"));
}
});
private final static String EMPTY = "";
public final static char XML_PATH_SEPARATOR = '/';
private final ReentrantLock lock = new ReentrantLock();
/**
* All fields, including child lists and direct children for beans
* with no namespace specified (##default)
*/
private final NamespaceBeanLikeMap nBeanLikeMap = new NamespaceBeanLikeMapImpl();
/**
* All children whose type has an identifier. First key is the xml parameter name, second
* key is the identifier of the specific child. Used in lookup operations. Works
* as a cache, may not be completely accurate and must be flushed on remove
* operations
*/
private final Map<QName, Map<String, BaseHK2JAXBBean>> keyedChildrenCache = new ConcurrentHashMap<QName, Map<String, BaseHK2JAXBBean>>();
/** The model for this, including lists of all children property names */
// private UnparentedNode model;
/** The parent of this instance, or null if this is a root (or has not been fully initialized yet) */
private XmlHk2ConfigurationBean parent;
/** My own namespace, which is determined either by my parent or by my root value */
private String selfNamespace;
/** My own XmlTag, which is determined either by my parent or by my root value */
private String selfXmlTag;
/** The full instance name this takes, with names from keyed children or ids from unkeyed multi children */
private String instanceName;
/** The value of my key field, if I have one */
private String keyValue;
/** The global classReflectionHelper, which minimizes reflection */
private ClassReflectionHelper classReflectionHelper;
/** My own full xmlPath from root */
private String xmlPath = EMPTY;
/**
* This object contains the tree locks
* Once this has been set then all other fields should have been set,
* and at that point this object is ready for its life as an
* in-memory node in a tree hierarchy
*/
private volatile transient DynamicChangeInfo<?> changeControl;
/**
* The root of this bean, or null if there is no root
* for this bean
*/
private volatile transient XmlRootHandleImpl<?> root;
/**
* If true this bean has been given to the user to
* add/remove or call getters or setters. This can
* happen in two ways, the first being via being parsed
* from an XML file, the other is via dynamic creation
*/
private boolean active = false;
/**
* The descriptor that this bean is advertised with
*/
private transient ActiveDescriptor<?> selfDescriptor;
/**
* The cost to add this bean to a tree, which
* is 1 plus the add cost of all children. This
* value is transient and is only calculated under
* locks and may not be accurate after intended use.
* The add cost and the remove cost are the same
*/
private transient int addCost = -1;
/**
* A map from the namespace prefix to the namespace URI
*/
private final Map<String, String> prefixToNamespaceMap = new HashMap<String, String>();
/**
* A map from the namespace URI to the prefix
*/
private final Map<String, String> namespaceToPrefixMap = new HashMap<String, String>();
/**
* For JAXB and Serialization
*/
public BaseHK2JAXBBean() {
}
@Override
public void _setProperty(String propName, Object propValue) {
_setProperty(XmlService.DEFAULT_NAMESPACE, propName, propValue);
}
@Override
public void _setProperty(String propNamespace, String propName, Object propValue) {
_setProperty(propNamespace, propName, propValue, true);
}
public void _setProperty(String propNamespace, String propName, Object propValue, boolean changeInHub) {
_setProperty(propNamespace, propName, propValue, changeInHub, false);
}
public void _setProperty(QName qName, Object propValue) {
String namespace = QNameUtilities.getNamespace(qName);
String propName = qName.getLocalPart();
_setProperty(namespace, propName, propValue);
}
public void _setProperty(QName qName, Object propValue, boolean changeInHub) {
String namespace = QNameUtilities.getNamespace(qName);
String propName = qName.getLocalPart();
_setProperty(namespace, propName, propValue, changeInHub);
}
@SuppressWarnings("unchecked")
public void _setProperty(String propNamespace, String propName, Object propValue, boolean changeInHub, boolean rawSet) {
if (propNamespace == null || propName == null) throw new IllegalArgumentException(
"properyName or propertyNamespace may not be null");
if (DEBUG_GETS_AND_SETS) {
// Hidden behind static because of potential expensive toString costs
Logger.getLogger().debug("XmlService setting property " + propName + " to " + propValue + " in " + this + " rawSet=" + rawSet);
}
if (propValue != null && (propValue instanceof List)) {
// All lists are unmodifiable and ArrayLists
if (propValue instanceof ArrayList) {
propValue = Collections.unmodifiableList((ArrayList<Object>) propValue);
}
else {
propValue = Collections.unmodifiableList(new ArrayList<Object>((List<Object>) propValue));
}
}
if (changeControl == null) {
if (active) {
lock.lock();
try {
nBeanLikeMap.setValue(propNamespace, propName, propValue);
} finally {
lock.unlock();
}
}
else {
nBeanLikeMap.setValue(propNamespace, propName, propValue);
}
}
else {
boolean doAdd = false;
boolean doRemove = false;
boolean doModify = false;
boolean doDirectReplace = false;
Object currentValue = null;
String directCurrentKey = null;
String directNewKey = null;
if (!rawSet) {
changeControl.getReadLock().lock();
try {
currentValue = nBeanLikeMap.getValue(propNamespace, propName);
// If both null this goes, or if they are somehow exactly the same
if (currentValue == propValue) return;
ModelImpl model = _getModel();
ParentedModel childModel = model.getChild(propNamespace, propName);
if (childModel != null) {
if (ChildType.DIRECT.equals(childModel.getChildType())) {
if (currentValue == null && propValue != null) {
// This is an add
doAdd = true;
QName keyQName = childModel.getChildModel().getKeyProperty();
String childkeyNamespace = keyQName == null ? null : keyQName.getNamespaceURI();
String childKeyProperty = keyQName == null ? null : keyQName.getLocalPart();
if (childKeyProperty != null) {
directNewKey = (String) ((BaseHK2JAXBBean) propValue)._getProperty(childkeyNamespace, childKeyProperty);
}
}
else if (currentValue != null && propValue == null) {
// This is a remove
doRemove = true;
}
else {
// Both beans are different
QName keyQName = childModel.getChildModel().getKeyProperty();
String childkeyNamespace = keyQName == null ? null : keyQName.getNamespaceURI();
String childKeyProperty = keyQName == null ? null : keyQName.getLocalPart();
if (childKeyProperty != null) {
directCurrentKey = (String) ((BaseHK2JAXBBean) currentValue)._getProperty(childkeyNamespace, childKeyProperty);
directNewKey = (String) ((BaseHK2JAXBBean) propValue)._getProperty(childkeyNamespace, childKeyProperty);
if (GeneralUtilities.safeEquals(directCurrentKey, directNewKey)) {
doModify = true;
}
else {
doDirectReplace = true;
}
}
else {
doModify = true;
}
}
}
else {
// Direct modification of a child
doModify = true;
}
}
}
finally {
changeControl.getReadLock().unlock();
}
}
if (doAdd) {
_doAdd(propNamespace, propName, propValue, directNewKey, -1);
return;
}
if (doRemove) {
_doRemove(propNamespace, propName, null, -1, currentValue);
return;
}
if (doModify) {
_doModify(propNamespace, propName, currentValue, propValue);
return;
}
if (doDirectReplace) {
changeControl.getWriteLock().lock();
try {
boolean success = false;
changeControl.startOrContinueChange(this);
try {
_doRemove(propNamespace, propName, directCurrentKey, -1, currentValue, false);
_doAdd(propNamespace, propName, propValue, directNewKey, -1, true);
success = true;
}
finally {
changeControl.endOrDeferChange(success);
}
}
finally {
changeControl.getWriteLock().unlock();
}
return;
}
QName keyQName = _getModel().getKeyProperty();
String keyPropertyNamespace = keyQName == null ? null : QNameUtilities.getNamespace(keyQName);
String keyProperty = keyQName == null ? null : keyQName.getLocalPart();
if (keyProperty != null && propName.equals(keyProperty) && (keyValue != null) &&
keyPropertyNamespace != null && propNamespace.equals(keyPropertyNamespace)) {
throw new IllegalArgumentException("The key property of a bean (" + keyProperty + ") may not be changed from " +
keyValue + " to " + propValue);
}
changeControl.getWriteLock().lock();
try {
boolean success = false;
changeControl.startOrContinueChange(this);
try {
if (!rawSet) {
Object oValue = nBeanLikeMap.getValue(propNamespace, propName);
Utilities.invokeVetoableChangeListeners(changeControl, this,
oValue, propValue, propName, classReflectionHelper);
}
if (changeInHub) {
changeInHubDirect(propNamespace, propName, propValue);
}
nBeanLikeMap.backup();
nBeanLikeMap.setValue(propNamespace, propName, propValue);
success = true;
}
finally {
changeControl.endOrDeferChange(success);
}
}
finally {
changeControl.getWriteLock().unlock();
}
}
}
public void _setProperty(String propNamespace, String propName, byte propValue) {
if (propName == null) throw new IllegalArgumentException("properyName may not be null");
_setProperty(propNamespace, propName, (Byte) propValue);
}
public void _setProperty(String propNamespace, String propName, boolean propValue) {
if (propName == null) throw new IllegalArgumentException("properyName may not be null");
_setProperty(propNamespace, propName, (Boolean) propValue);
}
public void _setProperty(String propNamespace, String propName, char propValue) {
if (propName == null) throw new IllegalArgumentException("properyName may not be null");
_setProperty(propNamespace, propName, (Character) propValue);
}
public void _setProperty(String propNamespace, String propName, short propValue) {
if (propName == null) throw new IllegalArgumentException("properyName may not be null");
_setProperty(propNamespace, propName, (Short) propValue);
}
public void _setProperty(String propNamespace, String propName, int propValue) {
if (propName == null) throw new IllegalArgumentException("properyName may not be null");
_setProperty(propNamespace, propName, (Integer) propValue);
}
public void _setProperty(String propNamespace, String propName, float propValue) {
if (propName == null) throw new IllegalArgumentException("properyName may not be null");
_setProperty(propNamespace, propName, (Float) propValue);
}
public void _setProperty(String propNamespace, String propName, long propValue) {
if (propName == null) throw new IllegalArgumentException("properyName may not be null");
_setProperty(propNamespace, propName, (Long) propValue);
}
public void _setProperty(String propNamespace, String propName, double propValue) {
if (propName == null) throw new IllegalArgumentException("properyName may not be null");
_setProperty(propNamespace, propName, (Double) propValue);
}
private Object _getProperty(String propNamespace, String propName, Class<?> expectedClass) {
return _getProperty(propNamespace, propName, expectedClass, null);
}
private Object _getProperty(String propNamespace, String propName, Class<?> expectedClass, ParentedModel parentNode) {
if (propNamespace == null) throw new IllegalArgumentException("propNamespace must not be null");
boolean isSet;
Object retVal;
boolean doDefaulting = active ? true : false;
if (changeControl == null) {
if (active) {
lock.lock();
try {
isSet = nBeanLikeMap.isSet(propNamespace, propName);
retVal = nBeanLikeMap.getValue(propNamespace, propName);
} finally {
lock.unlock();
}
}
else {
isSet = nBeanLikeMap.isSet(propNamespace, propName);
retVal = nBeanLikeMap.getValue(propNamespace, propName);
}
}
else {
changeControl.getReadLock().lock();
try {
doDefaulting = true;
isSet = nBeanLikeMap.isSet(propNamespace, propName);
retVal = nBeanLikeMap.getValue(propNamespace, propName);
}
finally {
changeControl.getReadLock().unlock();
}
}
if (doDefaulting && (retVal == null) && !isSet) {
if (expectedClass != null) {
retVal = Utilities.getDefaultValue(_getModel().getDefaultChildValue(propNamespace, propName), expectedClass, prefixToNamespaceMap);
}
else if (parentNode != null) {
switch (parentNode.getChildType()) {
case LIST:
retVal = Collections.EMPTY_LIST;
break;
case ARRAY:
Class<?> cType = parentNode.getChildModel().getOriginalInterfaceAsClass();
retVal = Array.newInstance(cType, 0);
break;
case DIRECT:
default:
break;
}
}
}
if (DEBUG_GETS_AND_SETS) {
// Hidden behind static because of potential expensive toString costs
Logger.getLogger().debug("XmlService getting property " + propName + "=" + retVal + " in " + this);
}
return retVal;
}
public Object _getProperty(QName qName) {
String propNamespace = QNameUtilities.getNamespace(qName);
String propName = qName.getLocalPart();
return _getProperty(propNamespace, propName);
}
/**
* Called by proxy
*
* @param propName Property of child or non-child element or attribute
* @return Value
*/
@Override
public Object _getProperty(String propNamespace, String propName) {
ModelImpl model = _getModel();
ModelPropertyType mpt = model.getModelPropertyType(propNamespace, propName);
switch(mpt) {
case FLAT_PROPERTY:
return _getProperty(propNamespace, propName, model.getNonChildType(propNamespace, propName));
case TREE_ROOT:
ParentedModel parent = model.getChild(propNamespace, propName);
return _getProperty(parent.getChildXmlNamespace(), parent.getChildXmlTag(), null, parent);
case UNKNOWN:
default:
throw new AssertionError("Unknown type " + mpt + " for " + propName + " in " + this);
}
}
@Override
public Object _getProperty(String propName) {
return _getProperty(XmlService.DEFAULT_NAMESPACE, propName);
}
/**
* Called by proxy
*
* @param propName
* @return
*/
public boolean _getPropertyZ(String propNamespace, String propName) {
return (Boolean) _getProperty(propNamespace, propName, boolean.class);
}
/**
* Called by proxy
*
* @param propName
* @return
*/
public byte _getPropertyB(String propNamespace, String propName) {
return (Byte) _getProperty(propNamespace, propName, byte.class);
}
/**
* Called by proxy
*
* @param propName
* @return
*/
public char _getPropertyC(String propNamespace, String propName) {
return (Character) _getProperty(propNamespace, propName, char.class);
}
/**
* Called by proxy
*
* @param propName
* @return
*/
public short _getPropertyS(String propNamespace, String propName) {
return (Short) _getProperty(propNamespace, propName, short.class);
}
/**
* Called by proxy
*
* @param propName
* @return
*/
public int _getPropertyI(String propNamespace, String propName) {
return (Integer) _getProperty(propNamespace, propName, int.class);
}
/**
* Called by proxy
*
* @param propName
* @return
*/
public float _getPropertyF(String propNamespace, String propName) {
return (Float) _getProperty(propNamespace, propName, float.class);
}
/**
* Called by proxy
*
* @param propName
* @return
*/
public long _getPropertyJ(String propNamespace, String propName) {
return (Long) _getProperty(propNamespace, propName, long.class);
}
/**
* Called by proxy
*
* @param propName
* @return
*/
public double _getPropertyD(String propNamespace, String propName) {
return (Double) _getProperty(propNamespace, propName, double.class);
}
@SuppressWarnings("unchecked")
private Object internalLookup(String propNamespace, String propName, String keyValue) {
QName propertyQName = QNameUtilities.createQName(propNamespace, propName);
// First look in the cache
Object retVal = null;
Map<String, BaseHK2JAXBBean> byName;
byName = keyedChildrenCache.get(propertyQName);
if (byName != null) {
retVal = byName.get(keyValue);
}
if (retVal != null) {
// Found it in cache!
return retVal;
}
// Now do it the hard way
Object prop = _getProperty(propNamespace, propName);
if (prop == null) return null; // Just not found
if (prop instanceof List) {
for (BaseHK2JAXBBean child : (List<BaseHK2JAXBBean>) prop) {
if (GeneralUtilities.safeEquals(keyValue, child._getKeyValue())) {
// Add it to the cache
if (byName == null) {
byName = new ConcurrentHashMap<String, BaseHK2JAXBBean>();
keyedChildrenCache.put(propertyQName, byName);
}
byName.put(keyValue, child);
// and return
return child;
}
}
}
else if (prop.getClass().isArray()) {
for (Object childRaw : (Object[]) prop) {
BaseHK2JAXBBean child = (BaseHK2JAXBBean) childRaw;
if (GeneralUtilities.safeEquals(keyValue, child._getKeyValue())) {
// Add it to the cache
if (byName == null) {
byName = new ConcurrentHashMap<String, BaseHK2JAXBBean>();
keyedChildrenCache.put(propertyQName, byName);
}
byName.put(keyValue, child);
// and return
return child;
}
}
}
// Just not found
return null;
}
@Override
public Object _lookupChild(String propName, String keyValue) {
return _lookupChild(XmlService.DEFAULT_NAMESPACE, propName, keyValue);
}
@Override
public Object _lookupChild(String propNamespace, String propName, String keyValue) {
if (changeControl == null) {
return internalLookup(propNamespace, propName, keyValue);
}
changeControl.getReadLock().lock();
try {
return internalLookup(propNamespace, propName, keyValue);
}
finally {
changeControl.getReadLock().unlock();
}
}
public Object _doAdd(String propNamespace, String childProperty, Object rawChild, String childKey, int index) {
return _doAdd(propNamespace, childProperty, rawChild, childKey, index, true);
}
public Object _doAdd(String propNamespace, String childProperty, Object rawChild, String childKey, int index, boolean changeList) {
if (changeControl == null) {
return Utilities.internalAdd(this, propNamespace, childProperty, rawChild, childKey, index, null, XmlDynamicChange.EMPTY, new LinkedList<ActiveDescriptor<?>>(), changeList);
}
changeControl.getWriteLock().lock();
try {
Object oldValue = nBeanLikeMap.getValue(propNamespace, childProperty);
LinkedList<ActiveDescriptor<?>> addedServices = new LinkedList<ActiveDescriptor<?>>();
Object retVal;
boolean success = false;
XmlDynamicChange change = changeControl.startOrContinueChange(this);
try {
retVal = Utilities.internalAdd(this, propNamespace, childProperty, rawChild, childKey, index, changeControl, change, addedServices, changeList);
Object newValue = nBeanLikeMap.getValue(propNamespace, childProperty);
Utilities.invokeVetoableChangeListeners(changeControl, this,
oldValue, newValue, childProperty, classReflectionHelper);
success = true;
}
finally {
changeControl.endOrDeferChange(success);
}
ServiceLocator locator = changeControl.getServiceLocator();
for (ActiveDescriptor<?> added : addedServices) {
// Ensures that any defaulting listeners are run
locator.getServiceHandle(added).getService();
}
return retVal;
}
finally {
changeControl.getWriteLock().unlock();
}
}
private void _doModify(String propNamespace, String propName, Object currentValue, Object newValue) {
if (root == null) {
throw new IllegalStateException("A direct set will only work on a rooted bean");
}
changeControl.getWriteLock().lock();
try {
boolean success = false;
XmlDynamicChange change = changeControl.startOrContinueChange(this);
try {
Utilities.internalModifyChild(this, propNamespace, propName, currentValue, newValue, root, changeControl, change);
success = true;
}
finally {
changeControl.endOrDeferChange(success);
}
}
finally {
changeControl.getWriteLock().unlock();
}
}
public Object _invokeCustomizedMethod(String methodName, Class<?>[] params, Object[] values) {
if (DEBUG_GETS_AND_SETS) {
// Hidden behind static because of potential expensive toString costs
Logger.getLogger().debug("XmlService invoking customized method " + methodName +
" with params " + Arrays.toString(params) + " adn values " + Arrays.toString(values));
}
Class<?> tClass = getClass();
Customizer customizer = tClass.getAnnotation(Customizer.class);
if (customizer == null) {
throw new RuntimeException("Method " + methodName + " was called on class " + tClass.getName() +
" with no customizer, failing");
}
Class<?> cClassArray[] = customizer.value();
String cNameArray[] = customizer.name();
if (cNameArray.length > 0 && cClassArray.length != cNameArray.length) {
throw new RuntimeException("The @Customizer annotation must have the value and name arrays be of equal size. " +
"The class array is of size " + cClassArray.length + " while the name array is of size " + cNameArray.length +
" for class " + tClass.getName());
}
LinkedList<Throwable> errors = new LinkedList<Throwable>();
for (int lcv = 0; lcv < cClassArray.length; lcv++) {
Class<?> cClass = cClassArray[lcv];
String cName = (cNameArray.length == 0) ? null : cNameArray[lcv] ;
Object cService = null;
if (cName == null || "".equals(cName)) {
cService = changeControl.getServiceLocator().getService(cClass);
}
else {
cService = changeControl.getServiceLocator().getService(cClass, cName);
}
if (cService == null) {
if (customizer.failWhenMethodNotFound()) {
errors.add(new RuntimeException("Method " + methodName + " was called on class " + tClass.getName() +
" but service " + cClass.getName() + " with name " + cName + " was not found"));
}
continue;
}
ModelImpl model = _getModel();
Class<?> topInterface = (model == null) ? null : model.getOriginalInterfaceAsClass() ;
Method cMethod = Utilities.findSuitableCustomizerMethod(cClass, methodName, params, topInterface);
if (cMethod == null) {
if (customizer.failWhenMethodNotFound()) {
errors.add(new RuntimeException("No customizer method with name " + methodName + " was found on customizer " + cClass.getName() +
" with parameters " + Arrays.toString(params) + " for bean " + tClass.getName()));
}
continue;
}
boolean useAlt = false;
if (cMethod.getParameterTypes().length == (params.length + 1)) useAlt = true;
if (useAlt) {
Object altValues[] = new Object[values.length + 1];
altValues[0] = this;
for (int lcv2 = 0; lcv2 < values.length; lcv2++) {
altValues[lcv2 + 1] = values[lcv2];
}
values = altValues;
}
try {
return ReflectionHelper.invoke(cService, cMethod, values, false);
}
catch (RuntimeException re) {
throw re;
}
catch (Throwable e) {
throw new RuntimeException(e);
}
}
if (errors.isEmpty()) {
return null;
}
throw new MultiException(errors);
}
public int _invokeCustomizedMethodI(String methodName, Class<?>[] params, Object[] values) {
return ((Integer) _invokeCustomizedMethod(methodName, params, values)).intValue();
}
public long _invokeCustomizedMethodJ(String methodName, Class<?>[] params, Object[] values) {
return ((Long) _invokeCustomizedMethod(methodName, params, values)).longValue();
}
public boolean _invokeCustomizedMethodZ(String methodName, Class<?>[] params, Object[] values) {
return ((Boolean) _invokeCustomizedMethod(methodName, params, values)).booleanValue();
}
public byte _invokeCustomizedMethodB(String methodName, Class<?>[] params, Object[] values) {
return ((Byte) _invokeCustomizedMethod(methodName, params, values)).byteValue();
}
public char _invokeCustomizedMethodC(String methodName, Class<?>[] params, Object[] values) {
return ((Character) _invokeCustomizedMethod(methodName, params, values)).charValue();
}
public short _invokeCustomizedMethodS(String methodName, Class<?>[] params, Object[] values) {
return ((Short) _invokeCustomizedMethod(methodName, params, values)).shortValue();
}
public float _invokeCustomizedMethodF(String methodName, Class<?>[] params, Object[] values) {
return ((Float) _invokeCustomizedMethod(methodName, params, values)).floatValue();
}
public double _invokeCustomizedMethodD(String methodName, Class<?>[] params, Object[] values) {
return ((Double) _invokeCustomizedMethod(methodName, params, values)).doubleValue();
}
public Object _doRemove(String propNamespace, String childProperty, String childKey, int index, Object child) {
return _doRemove(propNamespace, childProperty, childKey, index, child, true);
}
public Object _doRemove(String propNamespace, String childProperty, String childKey, int index, Object child, boolean changeList) {
QName childPropQName = QNameUtilities.createQName(propNamespace, childProperty);
if (changeControl == null) {
Object retVal = Utilities.internalRemove(this, propNamespace, childProperty, childKey, index, child, null, XmlDynamicChange.EMPTY, changeList);
if (retVal != null) {
keyedChildrenCache.remove(childPropQName);
}
return retVal;
}
changeControl.getWriteLock().lock();
try {
XmlDynamicChange xmlDynamicChange = changeControl.startOrContinueChange(this);
Object retVal;
boolean success = false;
try {
Object oldVal = nBeanLikeMap.getValue(propNamespace, childProperty);
retVal = Utilities.internalRemove(this, propNamespace, childProperty, childKey, index, child, changeControl, xmlDynamicChange, changeList);
Object newVal = nBeanLikeMap.getValue(propNamespace, childProperty);
Utilities.invokeVetoableChangeListeners(changeControl, this,
oldVal, newVal, childProperty, classReflectionHelper);
success = true;
}
finally {
changeControl.endOrDeferChange(success);
}
if (retVal != null) {
keyedChildrenCache.remove(childPropQName);
}
return retVal;
}
finally {
changeControl.getWriteLock().unlock();
}
}
public boolean _doRemoveZ(String propNamespace, String childProperty, String childKey, int index, Object child) {
Object retVal = _doRemove(propNamespace, childProperty, childKey, index, child);
return (retVal != null);
}
public boolean _hasProperty(String propNamespace, String propName) {
if (changeControl == null) {
if (active) {
lock.lock();
try {
return nBeanLikeMap.isSet(propNamespace, propName);
} finally {
lock.unlock();
}
}
return nBeanLikeMap.isSet(propNamespace, propName);
}
changeControl.getReadLock().lock();
try {
return nBeanLikeMap.isSet(propNamespace, propName);
}
finally {
changeControl.getReadLock().unlock();
}
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.api.XmlHk2ConfigurationBean#getBeanLikeMap()
*/
@Override
public Map<String, Object> _getBeanLikeMap() {
if (changeControl == null) {
if (active) {
lock.lock();
try {
return Collections.unmodifiableMap(nBeanLikeMap.getBeanLikeMap(namespaceToPrefixMap));
} finally {
lock.unlock();
}
}
return Collections.unmodifiableMap(nBeanLikeMap.getBeanLikeMap(namespaceToPrefixMap));
}
changeControl.getReadLock().lock();
try {
return Collections.unmodifiableMap(nBeanLikeMap.getBeanLikeMap(namespaceToPrefixMap));
}
finally {
changeControl.getReadLock().unlock();
}
}
public Map<QName, Object> _getQNameMap() {
if (changeControl == null) {
if (active) {
lock.lock();
try {
return Collections.unmodifiableMap(nBeanLikeMap.getQNameMap());
} finally {
lock.unlock();
}
}