API Overview API Index Package Overview Direct link to this page
JDK 1.6
  java.beans.beancontext. BeanContextServicesSupport View Javadoc
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219

/*
 * @(#)BeanContextServicesSupport.java	1.24 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.beans.beancontext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import java.util.TooManyListenersException;

import java.util.Locale;

/**
 * <p>
 * This helper class provides a utility implementation of the
 * java.beans.beancontext.BeanContextServices interface.
 * </p>
 * <p>
 * Since this class directly implements the BeanContextServices interface,
 * the class can, and is intended to be used either by subclassing this
 * implementation, or via delegation of an instance of this class
 * from another through the BeanContextProxy interface.
 * </p>
 *
 * @author Laurence P. G. Cable
 * @version 1.24, 11/17/05
 * @since 1.2
 */

public class BeanContextServicesSupport extends BeanContextSupport
       implements BeanContextServices {

    /**
     * <p>
     * Construct a BeanContextServicesSupport instance
     * </p>
     *
     * @param peer	The peer BeanContext we are supplying an implementation for, if null the this object is its own peer
     * @param lcle	The current Locale for this BeanContext.
     * @param dtime	The initial state, true if in design mode, false if runtime.
     * @param visible	The initial visibility.
     * 
     */

    public BeanContextServicesSupport(BeanContextServices peer, Locale lcle, boolean dTime, boolean visible) {
	super(peer, lcle, dTime, visible);
    }

    /**
     * Create an instance using the specified Locale and design mode.
     *
     * @param peer	The peer BeanContext we are supplying an implementation for, if null the this object is its own peer
     * @param lcle	The current Locale for this BeanContext.
     * @param dtime	The initial state, true if in design mode, false if runtime.
     */

    public BeanContextServicesSupport(BeanContextServices peer, Locale lcle, boolean dtime) {
	this (peer, lcle, dtime, true);
    }

    /**
     * Create an instance using the specified locale
     *
     * @param peer	The peer BeanContext we are supplying an implementation for, if null the this object is its own peer
     * @param lcle	The current Locale for this BeanContext.
     */

    public BeanContextServicesSupport(BeanContextServices peer, Locale lcle) {
	this (peer, lcle, false, true);
    }

    /**
     * Create an instance with a peer
     *
     * @param peer	The peer BeanContext we are supplying an implementation for, if null the this object is its own peer
     */

    public BeanContextServicesSupport(BeanContextServices peer) {
	this (peer, null, false, true);
    }

    /**
     * Create an instance that is not a delegate of another object
     */

    public BeanContextServicesSupport() {
	this (null, null, false, true);
    }

    /**
     * called by BeanContextSupport superclass during construction and 
     * deserialization to initialize subclass transient state.
     *
     * subclasses may envelope this method, but should not override it or
     * call it directly.
     */

    public void initialize() {
	super.initialize();

	services     = new HashMap(serializable + 1);
	bcsListeners = new ArrayList(1);
    }

    /**
     * Gets the <tt>BeanContextServices</tt> associated with this
     * <tt>BeanContextServicesSupport</tt>.
     *
     * @return the instance of <tt>BeanContext</tt> 
     * this object is providing the implementation for.
     */
    public BeanContextServices getBeanContextServicesPeer() { 
	return (BeanContextServices)getBeanContextChildPeer();
    }

    /************************************************************************/

    /*
     * protected nested class containing per child information, an instance
     * of which is associated with each child in the "children" hashtable.
     * subclasses can extend this class to include their own per-child state.
     *
     * Note that this 'value' is serialized with the corresponding child 'key'
     * when the BeanContextSupport is serialized.
     */

    protected class BCSSChild extends BeanContextSupport.BCSChild  {

        private static final long serialVersionUID = -3263851306889194873L;

	/*
	 * private nested class to map serviceClass to Provider and requestors
	 * listeners.
	 */

	class BCSSCServiceClassRef {

	    // create an instance of a service ref

	    BCSSCServiceClassRef(Class sc, BeanContextServiceProvider bcsp, boolean delegated) {
		super();
	
		serviceClass     = sc;

		if (delegated)
		    delegateProvider = bcsp;
		else 
		    serviceProvider  = bcsp;
	    }

	    // add a requestor and assoc listener

	    void addRequestor(Object requestor, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException {
		BeanContextServiceRevokedListener cbcsrl = (BeanContextServiceRevokedListener)requestors.get(requestor);

		if (cbcsrl != null && !cbcsrl.equals(bcsrl))
		    throw new TooManyListenersException();

		requestors.put(requestor, bcsrl);
	    }

	    // remove a requestor

	    void removeRequestor(Object requestor) {
		requestors.remove(requestor);
	    }

	    // check a requestors listener

	    void verifyRequestor(Object requestor, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException {
		BeanContextServiceRevokedListener cbcsrl = (BeanContextServiceRevokedListener)requestors.get(requestor);

		if (cbcsrl != null && !cbcsrl.equals(bcsrl))
		    throw new TooManyListenersException();
	    }

	    void verifyAndMaybeSetProvider(BeanContextServiceProvider bcsp, boolean isDelegated) {
		BeanContextServiceProvider current;

		if (isDelegated) { // the provider is delegated
		    current = delegateProvider;

		    if (current == null || bcsp == null) {
			delegateProvider = bcsp;
			return;
		    }
		} else { // the provider is registered with this BCS
		    current = serviceProvider;

		    if (current == null || bcsp == null) {
			serviceProvider = bcsp;
			return;
		    }
		}

		if (!current.equals(bcsp))
		    throw new UnsupportedOperationException("existing service reference obtained from different BeanContextServiceProvider not supported");

	    }

	    Iterator cloneOfEntries() {
		return ((HashMap)requestors.clone()).entrySet().iterator();
	    }

	    Iterator entries() { return requestors.entrySet().iterator(); }

	    boolean isEmpty() { return requestors.isEmpty(); }

	    Class getServiceClass() { return serviceClass; }

	    BeanContextServiceProvider getServiceProvider() {
		return serviceProvider;
	    }

	    BeanContextServiceProvider getDelegateProvider() {
		return delegateProvider;
	    }

	    boolean isDelegated() { return delegateProvider != null; }

 	    void addRef(boolean delegated) { 
		if (delegated) {
		    delegateRefs++;
		} else {
		    serviceRefs++;
		}
	    }


	    void releaseRef(boolean delegated) { 
                if (delegated) {
                    if (--delegateRefs == 0) {
			delegateProvider = null;
		    }
                } else {
                    if (--serviceRefs  <= 0) {
			serviceProvider = null;
		    }
		}
            }    

	    int getRefs() { return serviceRefs + delegateRefs; }

	    int getDelegateRefs() { return delegateRefs; }

	    int getServiceRefs() { return serviceRefs; }

	    /*
	     * fields
	     */

	    Class				serviceClass;

	    BeanContextServiceProvider		serviceProvider;
	    int 				serviceRefs;

	    BeanContextServiceProvider		delegateProvider; // proxy
	    int					delegateRefs;

	    HashMap				requestors = new HashMap(1);
	}

     	/*
	 * per service reference info ...
	 */

	class BCSSCServiceRef {
	    BCSSCServiceRef(BCSSCServiceClassRef scref, boolean isDelegated) {
		serviceClassRef = scref;
		delegated       = isDelegated;
	    }

	    void addRef()  { refCnt++;        }
	    int  release() { return --refCnt; }

	    BCSSCServiceClassRef getServiceClassRef() { return serviceClassRef; }

	    boolean isDelegated() { return delegated; }

	    /*
    	     * fields
	     */

	    BCSSCServiceClassRef serviceClassRef;
	    int			 refCnt	   = 1;
	    boolean		 delegated = false;
	}

	BCSSChild(Object bcc, Object peer) { super(bcc, peer); }

        // note usage of service per requestor, per service

	synchronized void usingService(Object requestor, Object service, Class serviceClass, BeanContextServiceProvider bcsp, boolean isDelegated, BeanContextServiceRevokedListener bcsrl)  throws TooManyListenersException, UnsupportedOperationException {
	
	    // first, process mapping from serviceClass to requestor(s)

	    BCSSCServiceClassRef serviceClassRef = null;

	    if (serviceClasses == null) 
		serviceClasses = new HashMap(1);
	    else
	        serviceClassRef = (BCSSCServiceClassRef)serviceClasses.get(serviceClass);

	    if (serviceClassRef == null) { // new service being used ...
	    	serviceClassRef = new BCSSCServiceClassRef(serviceClass, bcsp, isDelegated);
		serviceClasses.put(serviceClass, serviceClassRef);

	    } else { // existing service ...
		serviceClassRef.verifyAndMaybeSetProvider(bcsp, isDelegated); // throws
		serviceClassRef.verifyRequestor(requestor, bcsrl); // throws
	    }

	    serviceClassRef.addRequestor(requestor, bcsrl);
	    serviceClassRef.addRef(isDelegated);

	    // now handle mapping from requestor to service(s)

	    BCSSCServiceRef serviceRef = null;
	    Map		    services   = null;

	    if (serviceRequestors == null) {
		serviceRequestors = new HashMap(1);
	    } else {
		services = (Map)serviceRequestors.get(requestor);
	    }

	    if (services == null) {
		services = new HashMap(1);

		serviceRequestors.put(requestor, services);
	    } else 
		serviceRef = (BCSSCServiceRef)services.get(service);

	    if (serviceRef == null) {
		serviceRef = new BCSSCServiceRef(serviceClassRef, isDelegated);

		services.put(service, serviceRef);
	    } else {
		serviceRef.addRef();
	    }
	}

	// release a service reference

	synchronized void releaseService(Object requestor, Object service) {
	    if (serviceRequestors == null) return;

	    Map services = (Map)serviceRequestors.get(requestor);
	
	    if (services == null) return; // oops its not there anymore!

	    BCSSCServiceRef serviceRef = (BCSSCServiceRef)services.get(service);

	    if (serviceRef == null) return; // oops its not there anymore!
	    
	    BCSSCServiceClassRef serviceClassRef = serviceRef.getServiceClassRef();
	    boolean		       isDelegated = serviceRef.isDelegated();
	    BeanContextServiceProvider bcsp        = isDelegated ? serviceClassRef.getDelegateProvider() : serviceClassRef.getServiceProvider();

	    bcsp.releaseService(BeanContextServicesSupport.this.getBeanContextServicesPeer(), requestor, service);

	    serviceClassRef.releaseRef(isDelegated);
	    serviceClassRef.removeRequestor(requestor);

	    if (serviceRef.release() == 0) {

		services.remove(service);

		if (services.isEmpty()) {
		    serviceRequestors.remove(requestor);
		    serviceClassRef.removeRequestor(requestor);
		}

		if (serviceRequestors.isEmpty()) {
		    serviceRequestors = null;
		}

		if (serviceClassRef.isEmpty()) {
		    serviceClasses.remove(serviceClassRef.getServiceClass());
		}

		if (serviceClasses.isEmpty())
		    serviceClasses = null;
	    }
	}

	// revoke a service

	synchronized void revokeService(Class serviceClass, boolean isDelegated, boolean revokeNow) {
	    if (serviceClasses == null) return;

	    BCSSCServiceClassRef serviceClassRef = (BCSSCServiceClassRef)serviceClasses.get(serviceClass);

	    if (serviceClassRef == null) return;

	    Iterator i = serviceClassRef.cloneOfEntries();

	    BeanContextServiceRevokedEvent bcsre       = new BeanContextServiceRevokedEvent(BeanContextServicesSupport.this.getBeanContextServicesPeer(), serviceClass, revokeNow);
	    boolean			   noMoreRefs  = false;

	    while (i.hasNext() && serviceRequestors != null) {
		Map.Entry	    		  entry    = (Map.Entry)i.next();
		BeanContextServiceRevokedListener listener = (BeanContextServiceRevokedListener)entry.getValue();

		if (revokeNow) {
		    Object  requestor = entry.getKey();
		    Map     services  = (Map)serviceRequestors.get(requestor);

		    if (services != null) {
			Iterator i1 = services.entrySet().iterator();

			while (i1.hasNext()) {
			    Map.Entry       tmp        = (Map.Entry)i1.next();

			    BCSSCServiceRef serviceRef = (BCSSCServiceRef)tmp.getValue();
			    if (serviceRef.getServiceClassRef().equals(serviceClassRef) && isDelegated == serviceRef.isDelegated()) {	
				i1.remove();
			    }
			}

			if (noMoreRefs = services.isEmpty()) {
			    serviceRequestors.remove(requestor);
			}
		    }

		    if (noMoreRefs) serviceClassRef.removeRequestor(requestor);
		}

		listener.serviceRevoked(bcsre);
	    }

	    if (revokeNow && serviceClasses != null) {
		if (serviceClassRef.isEmpty())
		    serviceClasses.remove(serviceClass);

	        if (serviceClasses.isEmpty())
		    serviceClasses = null;
	    }

	    if (serviceRequestors != null && serviceRequestors.isEmpty())
		serviceRequestors = null;
	}

	// release all references for this child since it has been unnested.

	void cleanupReferences() {

	    if (serviceRequestors == null) return;

	    Iterator requestors = serviceRequestors.entrySet().iterator();

	    while(requestors.hasNext()) {
		Map.Entry 	     tmp       = (Map.Entry)requestors.next();
		Object 	             requestor = tmp.getKey();
		Iterator 	     services  = ((Map)tmp.getValue()).entrySet().iterator();

		requestors.remove();

		while (services.hasNext()) {
		    Map.Entry       entry   = (Map.Entry)services.next();
		    Object 	    service = entry.getKey();
		    BCSSCServiceRef sref    = (BCSSCServiceRef)entry.getValue();

		    BCSSCServiceClassRef       scref = sref.getServiceClassRef();
		
		    BeanContextServiceProvider bcsp  = sref.isDelegated() ? scref.getDelegateProvider() : scref.getServiceProvider();

		    scref.removeRequestor(requestor);
		    services.remove();

		    while (sref.release() >= 0) {
			bcsp.releaseService(BeanContextServicesSupport.this.getBeanContextServicesPeer(), requestor, service);
		    }
		}
	    }

	    serviceRequestors = null;
	    serviceClasses    = null;
	}

        void revokeAllDelegatedServicesNow() {
	    if (serviceClasses == null) return;

	    Iterator serviceClassRefs  = 
		new HashSet(serviceClasses.values()).iterator();

	    while (serviceClassRefs.hasNext()) {
	        BCSSCServiceClassRef serviceClassRef = (BCSSCServiceClassRef)serviceClassRefs.next();

	    	if (!serviceClassRef.isDelegated()) continue;

	        Iterator i = serviceClassRef.cloneOfEntries();
	    	BeanContextServiceRevokedEvent bcsre       = new BeanContextServiceRevokedEvent(BeanContextServicesSupport.this.getBeanContextServicesPeer(), serviceClassRef.getServiceClass(), true);
	    	boolean			       noMoreRefs  = false;

	    	while (i.hasNext()) {
		    Map.Entry	    		      entry     = (Map.Entry)i.next();
		    BeanContextServiceRevokedListener listener  = (BeanContextServiceRevokedListener)entry.getValue();

		    Object  			      requestor = entry.getKey();
		    Map     			      services  = (Map)serviceRequestors.get(requestor);

		    if (services != null) {
			Iterator i1 = services.entrySet().iterator();

			while (i1.hasNext()) {
			    Map.Entry       tmp        = (Map.Entry)i1.next();

			    BCSSCServiceRef serviceRef = (BCSSCServiceRef)tmp.getValue();
			    if (serviceRef.getServiceClassRef().equals(serviceClassRef) && serviceRef.isDelegated()) {	
				i1.remove();
			    }
			}

			if (noMoreRefs = services.isEmpty()) {
			    serviceRequestors.remove(requestor);
			}
		    }

		    if (noMoreRefs) serviceClassRef.removeRequestor(requestor);

		    listener.serviceRevoked(bcsre);

		    if (serviceClassRef.isEmpty())
		        serviceClasses.remove(serviceClassRef.getServiceClass());
	    	}
	    }

	    if (serviceClasses.isEmpty()) serviceClasses = null;

	    if (serviceRequestors != null && serviceRequestors.isEmpty())
		serviceRequestors = null;
        }

	/*
 	 * fields 
	 */

	private transient HashMap	serviceClasses;	   
	private transient HashMap	serviceRequestors;
    }

    /**
     * <p>
     * Subclasses can override this method to insert their own subclass
     * of Child without having to override add() or the other Collection
     * methods that add children to the set.
     * </p>
     *
     * @param targetChild the child to create the Child on behalf of
     * @param peer        the peer if the targetChild and peer are related by BeanContextProxy
     */

    protected BCSChild createBCSChild(Object targetChild, Object peer) {
	return new BCSSChild(targetChild, peer);
    }

    /************************************************************************/

	/**
	 * subclasses may subclass this nested class to add behaviors for
	 * each BeanContextServicesProvider.
	 */

	protected static class BCSSServiceProvider implements Serializable {

	    BCSSServiceProvider(Class sc, BeanContextServiceProvider bcsp) {
		super();
	
		serviceProvider = bcsp;
	    }

	    protected BeanContextServiceProvider getServiceProvider() {
		return serviceProvider;
	    }

	    /*
   	     * fields
	     */

	    protected BeanContextServiceProvider serviceProvider;
	}

    	/**
	 * subclasses can override this method to create new subclasses of
	 * BCSSServiceProvider without having to overrride addService() in
	 * order to instantiate.
	 */

	protected BCSSServiceProvider createBCSSServiceProvider(Class sc, BeanContextServiceProvider bcsp) {
	    return new BCSSServiceProvider(sc, bcsp);
	}

    /************************************************************************/

    /**
     * add a BeanContextServicesListener
     *
     * @throw new NullPointerException
     */

    public void addBeanContextServicesListener(BeanContextServicesListener bcsl) {
	if (bcsl == null) throw new NullPointerException("bcsl");

	synchronized(bcsListeners) {
	    if (bcsListeners.contains(bcsl))
		return;
	    else
	        bcsListeners.add(bcsl);
	}
    }

    /**
     * remove a BeanContextServicesListener
     */

    public void removeBeanContextServicesListener(BeanContextServicesListener bcsl) {
	if (bcsl == null) throw new NullPointerException("bcsl");

	synchronized(bcsListeners) {
	    if (!bcsListeners.contains(bcsl))
		return;
	    else
	        bcsListeners.remove(bcsl);
	}
    }

    /**
     * add a service
     */

    public boolean addService(Class serviceClass, BeanContextServiceProvider bcsp) {
	return addService(serviceClass, bcsp, true);
    }

    /**
     * add a service
     */

    protected boolean addService(Class serviceClass, BeanContextServiceProvider bcsp, boolean fireEvent) {

	if (serviceClass == null) throw new NullPointerException("serviceClass");
	if (bcsp         == null) throw new NullPointerException("bcsp");

	synchronized(BeanContext.globalHierarchyLock) {
            if (services.containsKey(serviceClass))
    	        return false;
    	    else {
    	        services.put(serviceClass,  createBCSSServiceProvider(serviceClass, bcsp));
    
    	        if (bcsp instanceof Serializable) serializable++;
    
    	        if (!fireEvent) return true;
    
    
    	        BeanContextServiceAvailableEvent bcssae = new BeanContextServiceAvailableEvent(getBeanContextServicesPeer(), serviceClass);
    
    	        fireServiceAdded(bcssae);
    
    	        synchronized(children) {
    	            Iterator i = children.keySet().iterator();
    
    	            while (i.hasNext()) {
    	                Object c = i.next();
    
    	                if (c instanceof BeanContextServices) {
    		            ((BeanContextServicesListener)c).serviceAvailable(bcssae);
    	                }
    	            }
    	        }
    
    	        return true;
    	    }
	}
    }

    /**
     * remove a service
     */

    public void revokeService(Class serviceClass, BeanContextServiceProvider bcsp, boolean revokeCurrentServicesNow) {

	if (serviceClass == null) throw new NullPointerException("serviceClass");
	if (bcsp         == null) throw new NullPointerException("bcsp");

	synchronized(BeanContext.globalHierarchyLock) {
	    if (!services.containsKey(serviceClass)) return;

	    BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass);
	
	    if (!bcsssp.getServiceProvider().equals(bcsp)) 
	        throw new IllegalArgumentException("service provider mismatch");

	    services.remove(serviceClass);

            if (bcsp instanceof Serializable) serializable--;

	    Iterator i = bcsChildren(); // get the BCSChild values.

	    while (i.hasNext()) {
	        ((BCSSChild)i.next()).revokeService(serviceClass, false, revokeCurrentServicesNow);
	    }

	    fireServiceRevoked(serviceClass, revokeCurrentServicesNow);
	}
    }

    /**
     * has a service, which may be delegated
     */

    public synchronized boolean hasService(Class serviceClass) {
	if (serviceClass == null) throw new NullPointerException("serviceClass");

	synchronized(BeanContext.globalHierarchyLock) {
	    if (services.containsKey(serviceClass)) return true;

	    BeanContextServices bcs = null;

	    try {
	        bcs = (BeanContextServices)getBeanContext();
	    } catch (ClassCastException cce) {
	        return false;
	    }

	    return bcs == null ? false : bcs.hasService(serviceClass);
	}
    }

    /************************************************************************/

    /*
     * a nested subclass used to represent a proxy for serviceClasses delegated
     * to an enclosing BeanContext.
     */

    protected class BCSSProxyServiceProvider implements BeanContextServiceProvider, BeanContextServiceRevokedListener {
	
        BCSSProxyServiceProvider(BeanContextServices bcs) {
	    super();
		
	    nestingCtxt = bcs;
	}

	public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector) {
	    Object service = null;

	    try {
	 	service = nestingCtxt.getService(bcs, requestor, serviceClass, serviceSelector, this);
	    } catch (TooManyListenersException tmle) {
		return null;
	    }
	
	    return service;
	}

	public void releaseService(BeanContextServices bcs, Object requestor, Object service) {
	    nestingCtxt.releaseService(bcs, requestor, service);
	}

	public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) {
	    return nestingCtxt.getCurrentServiceSelectors(serviceClass);
	}

	public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) {
	    Iterator i = bcsChildren(); // get the BCSChild values.

	    while (i.hasNext()) {
	        ((BCSSChild)i.next()).revokeService(bcsre.getServiceClass(), true, bcsre.isCurrentServiceInvalidNow());
	    }
	}

	/*
	 * fields
	 */

	private BeanContextServices nestingCtxt;
    }

    /************************************************************************/

    /**
     * obtain a service which may be delegated
     */

     public Object getService(BeanContextChild child, Object requestor, Class serviceClass, Object serviceSelector, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException {
	if (child        == null) throw new NullPointerException("child");
	if (serviceClass == null) throw new NullPointerException("serviceClass");
	if (requestor    == null) throw new NullPointerException("requestor");
	if (bcsrl        == null) throw new NullPointerException("bcsrl");

	Object              service = null;
	BCSSChild           bcsc;
	BeanContextServices bcssp   = getBeanContextServicesPeer();

	synchronized(BeanContext.globalHierarchyLock) {
	    synchronized(children) { bcsc = (BCSSChild)children.get(child); }

	    if (bcsc == null) throw new IllegalArgumentException("not a child of this context"); // not a child ...

	    BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass);

	    if (bcsssp != null) {
	        BeanContextServiceProvider bcsp = bcsssp.getServiceProvider();
	        service = bcsp.getService(bcssp, requestor, serviceClass, serviceSelector);
	        if (service != null) { // do bookkeeping ...
		    try {
	                bcsc.usingService(requestor, service, serviceClass, bcsp, false, bcsrl);
		    } catch (TooManyListenersException tmle) {
		        bcsp.releaseService(bcssp, requestor, service);
		        throw tmle;
		    } catch (UnsupportedOperationException uope) {
		        bcsp.releaseService(bcssp, requestor, service);
		        throw uope; // unchecked rt exception
		    }

		    return service;
	        }
	    }


	    if (proxy != null) {

	        // try to delegate ...

	        service = proxy.getService(bcssp, requestor, serviceClass, serviceSelector);
	
	        if (service != null) { // do bookkeeping ...
		    try {
		        bcsc.usingService(requestor, service, serviceClass, proxy, true, bcsrl);
		    } catch (TooManyListenersException tmle) {
		        proxy.releaseService(bcssp, requestor, service);
		        throw tmle;
		    } catch (UnsupportedOperationException uope) {
		        proxy.releaseService(bcssp, requestor, service);
		        throw uope; // unchecked rt exception
		    }

		    return service;
	        }
	    }
	}

       	return null;
    }

    /**
     * release a service
     */

    public void releaseService(BeanContextChild child, Object requestor, Object service) {
	if (child     == null) throw new NullPointerException("child");
	if (requestor == null) throw new NullPointerException("requestor");
	if (service   == null) throw new NullPointerException("service");

	BCSSChild bcsc;

	synchronized(BeanContext.globalHierarchyLock) {
	        synchronized(children) { bcsc = (BCSSChild)children.get(child); }

	        if (bcsc != null)
		    bcsc.releaseService(requestor, service);
		else
		   throw new IllegalArgumentException("child actual is not a child of this BeanContext");
        }
    }

    /**
     * @return an iterator for all the currently registered service classes.
     */

    public Iterator getCurrentServiceClasses() {
	return new BCSIterator(services.keySet().iterator());
    }

    /**
     * @return an iterator for all the currently available service selectors
     * (if any) available for the specified service.
     */

    public Iterator getCurrentServiceSelectors(Class serviceClass) {
	
	BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass);
	
	return bcsssp != null ? new BCSIterator(bcsssp.getServiceProvider().getCurrentServiceSelectors(getBeanContextServicesPeer(), serviceClass)) : null;
    }

    /**
     * BeanContextServicesListener callback, propagates event to all 
     * currently registered listeners and BeanContextServices children,
     * if this BeanContextService does not already implement this service
     * itself.
     *
     * subclasses may override or envelope this method to implement their
     * own propagation semantics.
     */

     public void serviceAvailable(BeanContextServiceAvailableEvent bcssae) {
	synchronized(BeanContext.globalHierarchyLock) {
	    if (services.containsKey(bcssae.getServiceClass())) return;

	    fireServiceAdded(bcssae);

	    Iterator i;

	    synchronized(children) {
	        i = children.keySet().iterator();
	    }

	    while (i.hasNext()) {
	        Object c = i.next();

	        if (c instanceof BeanContextServices) {
	            ((BeanContextServicesListener)c).serviceAvailable(bcssae);
	        }
	    }
	}
     }

    /**
     * BeanContextServicesListener callback, propagates event to all 
     * currently registered listeners and BeanContextServices children,
     * if this BeanContextService does not already implement this service
     * itself.
     *
     * subclasses may override or envelope this method to implement their
     * own propagation semantics.
     */

    public void serviceRevoked(BeanContextServiceRevokedEvent bcssre) {
	synchronized(BeanContext.globalHierarchyLock) {
	    if (services.containsKey(bcssre.getServiceClass())) return;

	    fireServiceRevoked(bcssre);

	    Iterator i;

	    synchronized(children) {
	        i = children.keySet().iterator();
	    }

	    while (i.hasNext()) {
	        Object c = i.next();

	        if (c instanceof BeanContextServices) {
	            ((BeanContextServicesListener)c).serviceRevoked(bcssre);
	        }
	    }
	}
    }

    /**
     * Gets the <tt>BeanContextServicesListener</tt> (if any) of the specified
     * child.
     *
     * @param child the specified child
     * @return the BeanContextServicesListener (if any) of the specified child
     */
    protected static final BeanContextServicesListener getChildBeanContextServicesListener(Object child) {
	try {
	    return (BeanContextServicesListener)child;
	} catch (ClassCastException cce) {
	    return null;
	}
    }

    /**
     * called from superclass child removal operations after a child
     * has been successfully removed. called with child synchronized.
     *
     * This subclass uses this hook to immediately revoke any services
     * being used by this child if it is a BeanContextChild.
     *
     * subclasses may envelope this method in order to implement their
     * own child removal side-effects.
     */

    protected void childJustRemovedHook(Object child, BCSChild bcsc) {
	BCSSChild bcssc = (BCSSChild)bcsc;

	bcssc.cleanupReferences();
    }

    /**
     * called from setBeanContext to notify a BeanContextChild
     * to release resources obtained from the nesting BeanContext.
     * 
     * This method revokes any services obtained from its parent.
     * 
     * subclasses may envelope this method to implement their own semantics.
     */

    protected synchronized void releaseBeanContextResources() {
	Object[] bcssc;

	super.releaseBeanContextResources();

	synchronized(children) {
	    if (children.isEmpty()) return;

	    bcssc = children.values().toArray();
	}


	for (int i = 0; i < bcssc.length; i++) {
	    ((BCSSChild)bcssc[i]).revokeAllDelegatedServicesNow();
	}

	proxy = null;
    }

    /**
     * called from setBeanContext to notify a BeanContextChild
     * to allocate resources obtained from the nesting BeanContext.
     * 
     * subclasses may envelope this method to implement their own semantics.
     */

    protected synchronized void initializeBeanContextResources() {
	super.initializeBeanContextResources();

	BeanContext nbc = getBeanContext();

	if (nbc == null) return;

	try {
	    BeanContextServices bcs = (BeanContextServices)nbc;

	    proxy = new BCSSProxyServiceProvider(bcs);
	} catch (ClassCastException cce) {
	    // do nothing ...
	}
    }

    /**
     * Fires a <tt>BeanContextServiceEvent</tt> notifying of a new service.
     */
    protected final void fireServiceAdded(Class serviceClass) {
	BeanContextServiceAvailableEvent bcssae = new BeanContextServiceAvailableEvent(getBeanContextServicesPeer(), serviceClass); 

	fireServiceAdded(bcssae);
    }

    /**
     * Fires a <tt>BeanContextServiceAvailableEvent</tt> indicating that a new
     * service has become available.
     *
     * @param bcssae the <tt>BeanContextServiceAvailableEvent</tt>
     */
    protected final void fireServiceAdded(BeanContextServiceAvailableEvent bcssae) {
	Object[]		         copy;

	synchronized (bcsListeners) { copy = bcsListeners.toArray(); }

	for (int i = 0; i < copy.length; i++) {
	    ((BeanContextServicesListener)copy[i]).serviceAvailable(bcssae);
	}
    }

    /**
     * Fires a <tt>BeanContextServiceEvent</tt> notifying of a service being revoked.
     *
     * @param bcsre the <tt>BeanContextServiceRevokedEvent</tt>
     */
    protected final void fireServiceRevoked(BeanContextServiceRevokedEvent bcsre) {
	Object[]		         copy;

	synchronized (bcsListeners) { copy = bcsListeners.toArray(); }

	for (int i = 0; i < copy.length; i++) {
	    ((BeanContextServiceRevokedListener)copy[i]).serviceRevoked(bcsre);
	}
    }

    /**
     * Fires a <tt>BeanContextServiceRevokedEvent</tt> 
     * indicating that a particular service is 
     * no longer available.
     */
    protected final void fireServiceRevoked(Class serviceClass, boolean revokeNow) {
	Object[]		       copy;
	BeanContextServiceRevokedEvent bcsre = new BeanContextServiceRevokedEvent(getBeanContextServicesPeer(), serviceClass, revokeNow); 

	synchronized (bcsListeners) { copy = bcsListeners.toArray(); }

	for (int i = 0; i < copy.length; i++) {
	    ((BeanContextServicesListener)copy[i]).serviceRevoked(bcsre);
	}
   }

    /**
     * called from BeanContextSupport writeObject before it serializes the
     * children ...
     *
     * This class will serialize any Serializable BeanContextServiceProviders
     * herein.
     *
     * subclasses may envelope this method to insert their own serialization
     * processing that has to occur prior to serialization of the children
     */

    protected synchronized void bcsPreSerializationHook(ObjectOutputStream oos) throws IOException {

	oos.writeInt(serializable);

	if (serializable <= 0) return;

	int count = 0;

	Iterator i = services.entrySet().iterator();

	while (i.hasNext() && count < serializable) {
	    Map.Entry 		entry = (Map.Entry)i.next();
	    BCSSServiceProvider bcsp  = null;

	     try {
		bcsp = (BCSSServiceProvider)entry.getValue();
	     } catch (ClassCastException cce) {
		continue;
	     }

	     if (bcsp.getServiceProvider() instanceof Serializable) {
		oos.writeObject(entry.getKey());
		oos.writeObject(bcsp);
		count++;
	     }
	}

	if (count != serializable) 
	    throw new IOException("wrote different number of service providers than expected");
    }

    /**
     * called from BeanContextSupport readObject before it deserializes the
     * children ...
     *
     * This class will deserialize any Serializable BeanContextServiceProviders
     * serialized earlier thus making them available to the children when they
     * deserialized.
     *
     * subclasses may envelope this method to insert their own serialization
     * processing that has to occur prior to serialization of the children
     */

    protected synchronized void bcsPreDeserializationHook(ObjectInputStream ois) throws IOException, ClassNotFoundException {

	serializable = ois.readInt();

	int count = serializable;

	while (count > 0) {
	    services.put(ois.readObject(), ois.readObject());
	    count--;
	} 
    }
 
    /**
     * serialize the instance
     */

    private synchronized void writeObject(ObjectOutputStream oos) throws IOException {
	oos.defaultWriteObject();

	serialize(oos, (Collection)bcsListeners);
    }

    /**
     * deserialize the instance
     */

    private synchronized void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {

	ois.defaultReadObject();

	deserialize(ois, (Collection)bcsListeners);
    }

    
    /*
     * fields
     */

    /**
     * all accesses to the <code> protected transient HashMap services </code>
     * field should be synchronized on that object
     */
    protected transient HashMap  		 services;

    /**
     * The number of instances of a serializable <tt>BeanContextServceProvider</tt>.
     */
    protected transient int 	 		 serializable = 0;


    /**
     * Delegate for the <tt>BeanContextServiceProvider</tt>.
     */
    protected transient BCSSProxyServiceProvider proxy;


    /**
     * List of <tt>BeanContextServicesListener</tt> objects.
     */
    protected transient ArrayList		 bcsListeners;
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar