1 7 8 package java.beans.beancontext; 9 10 import java.util.ArrayList ; 11 import java.util.Collection ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Map.Entry; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 23 import java.util.TooManyListenersException ; 24 25 import java.util.Locale ; 26 27 43 44 public class BeanContextServicesSupport extends BeanContextSupport 45 implements BeanContextServices { 46 47 58 59 public BeanContextServicesSupport(BeanContextServices peer, Locale lcle, boolean dTime, boolean visible) { 60 super(peer, lcle, dTime, visible); 61 } 62 63 70 71 public BeanContextServicesSupport(BeanContextServices peer, Locale lcle, boolean dtime) { 72 this (peer, lcle, dtime, true); 73 } 74 75 81 82 public BeanContextServicesSupport(BeanContextServices peer, Locale lcle) { 83 this (peer, lcle, false, true); 84 } 85 86 91 92 public BeanContextServicesSupport(BeanContextServices peer) { 93 this (peer, null, false, true); 94 } 95 96 99 100 public BeanContextServicesSupport() { 101 this (null, null, false, true); 102 } 103 104 111 112 public void initialize() { 113 super.initialize(); 114 115 services = new HashMap (serializable + 1); 116 bcsListeners = new ArrayList (1); 117 } 118 119 126 public BeanContextServices getBeanContextServicesPeer() { 127 return (BeanContextServices )getBeanContextChildPeer(); 128 } 129 130 131 132 140 141 protected class BCSSChild extends BeanContextSupport.BCSChild { 142 143 private static final long serialVersionUID = -3263851306889194873L; 144 145 149 150 class BCSSCServiceClassRef { 151 152 154 BCSSCServiceClassRef(Class sc, BeanContextServiceProvider bcsp, boolean delegated) { 155 super(); 156 157 serviceClass = sc; 158 159 if (delegated) 160 delegateProvider = bcsp; 161 else 162 serviceProvider = bcsp; 163 } 164 165 167 void addRequestor(Object requestor, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException { 168 BeanContextServiceRevokedListener cbcsrl = (BeanContextServiceRevokedListener )requestors.get(requestor); 169 170 if (cbcsrl != null && !cbcsrl.equals(bcsrl)) 171 throw new TooManyListenersException (); 172 173 requestors.put(requestor, bcsrl); 174 } 175 176 178 void removeRequestor(Object requestor) { 179 requestors.remove(requestor); 180 } 181 182 184 void verifyRequestor(Object requestor, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException { 185 BeanContextServiceRevokedListener cbcsrl = (BeanContextServiceRevokedListener )requestors.get(requestor); 186 187 if (cbcsrl != null && !cbcsrl.equals(bcsrl)) 188 throw new TooManyListenersException (); 189 } 190 191 void verifyAndMaybeSetProvider(BeanContextServiceProvider bcsp, boolean isDelegated) { 192 BeanContextServiceProvider current; 193 194 if (isDelegated) { current = delegateProvider; 196 197 if (current == null || bcsp == null) { 198 delegateProvider = bcsp; 199 return; 200 } 201 } else { current = serviceProvider; 203 204 if (current == null || bcsp == null) { 205 serviceProvider = bcsp; 206 return; 207 } 208 } 209 210 if (!current.equals(bcsp)) 211 throw new UnsupportedOperationException ("existing service reference obtained from different BeanContextServiceProvider not supported"); 212 213 } 214 215 Iterator cloneOfEntries() { 216 return ((HashMap )requestors.clone()).entrySet().iterator(); 217 } 218 219 Iterator entries() { return requestors.entrySet().iterator(); } 220 221 boolean isEmpty() { return requestors.isEmpty(); } 222 223 Class getServiceClass() { return serviceClass; } 224 225 BeanContextServiceProvider getServiceProvider() { 226 return serviceProvider; 227 } 228 229 BeanContextServiceProvider getDelegateProvider() { 230 return delegateProvider; 231 } 232 233 boolean isDelegated() { return delegateProvider != null; } 234 235 void addRef(boolean delegated) { 236 if (delegated) { 237 delegateRefs++; 238 } else { 239 serviceRefs++; 240 } 241 } 242 243 244 void releaseRef(boolean delegated) { 245 if (delegated) { 246 if (--delegateRefs == 0) { 247 delegateProvider = null; 248 } 249 } else { 250 if (--serviceRefs <= 0) { 251 serviceProvider = null; 252 } 253 } 254 } 255 256 int getRefs() { return serviceRefs + delegateRefs; } 257 258 int getDelegateRefs() { return delegateRefs; } 259 260 int getServiceRefs() { return serviceRefs; } 261 262 265 266 Class serviceClass; 267 268 BeanContextServiceProvider serviceProvider; 269 int serviceRefs; 270 271 BeanContextServiceProvider delegateProvider; int delegateRefs; 273 274 HashMap requestors = new HashMap (1); 275 } 276 277 280 281 class BCSSCServiceRef { 282 BCSSCServiceRef(BCSSCServiceClassRef scref, boolean isDelegated) { 283 serviceClassRef = scref; 284 delegated = isDelegated; 285 } 286 287 void addRef() { refCnt++; } 288 int release() { return --refCnt; } 289 290 BCSSCServiceClassRef getServiceClassRef() { return serviceClassRef; } 291 292 boolean isDelegated() { return delegated; } 293 294 297 298 BCSSCServiceClassRef serviceClassRef; 299 int refCnt = 1; 300 boolean delegated = false; 301 } 302 303 BCSSChild(Object bcc, Object peer) { super(bcc, peer); } 304 305 307 synchronized void usingService(Object requestor, Object service, Class serviceClass, BeanContextServiceProvider bcsp, boolean isDelegated, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException , UnsupportedOperationException { 308 309 311 BCSSCServiceClassRef serviceClassRef = null; 312 313 if (serviceClasses == null) 314 serviceClasses = new HashMap (1); 315 else 316 serviceClassRef = (BCSSCServiceClassRef)serviceClasses.get(serviceClass); 317 318 if (serviceClassRef == null) { serviceClassRef = new BCSSCServiceClassRef(serviceClass, bcsp, isDelegated); 320 serviceClasses.put(serviceClass, serviceClassRef); 321 322 } else { serviceClassRef.verifyAndMaybeSetProvider(bcsp, isDelegated); serviceClassRef.verifyRequestor(requestor, bcsrl); } 326 327 serviceClassRef.addRequestor(requestor, bcsrl); 328 serviceClassRef.addRef(isDelegated); 329 330 332 BCSSCServiceRef serviceRef = null; 333 Map services = null; 334 335 if (serviceRequestors == null) { 336 serviceRequestors = new HashMap (1); 337 } else { 338 services = (Map )serviceRequestors.get(requestor); 339 } 340 341 if (services == null) { 342 services = new HashMap (1); 343 344 serviceRequestors.put(requestor, services); 345 } else 346 serviceRef = (BCSSCServiceRef)services.get(service); 347 348 if (serviceRef == null) { 349 serviceRef = new BCSSCServiceRef(serviceClassRef, isDelegated); 350 351 services.put(service, serviceRef); 352 } else { 353 serviceRef.addRef(); 354 } 355 } 356 357 359 synchronized void releaseService(Object requestor, Object service) { 360 if (serviceRequestors == null) return; 361 362 Map services = (Map )serviceRequestors.get(requestor); 363 364 if (services == null) return; 366 BCSSCServiceRef serviceRef = (BCSSCServiceRef)services.get(service); 367 368 if (serviceRef == null) return; 370 BCSSCServiceClassRef serviceClassRef = serviceRef.getServiceClassRef(); 371 boolean isDelegated = serviceRef.isDelegated(); 372 BeanContextServiceProvider bcsp = isDelegated ? serviceClassRef.getDelegateProvider() : serviceClassRef.getServiceProvider(); 373 374 bcsp.releaseService(BeanContextServicesSupport.this.getBeanContextServicesPeer(), requestor, service); 375 376 serviceClassRef.releaseRef(isDelegated); 377 serviceClassRef.removeRequestor(requestor); 378 379 if (serviceRef.release() == 0) { 380 381 services.remove(service); 382 383 if (services.isEmpty()) { 384 serviceRequestors.remove(requestor); 385 serviceClassRef.removeRequestor(requestor); 386 } 387 388 if (serviceRequestors.isEmpty()) { 389 serviceRequestors = null; 390 } 391 392 if (serviceClassRef.isEmpty()) { 393 serviceClasses.remove(serviceClassRef.getServiceClass()); 394 } 395 396 if (serviceClasses.isEmpty()) 397 serviceClasses = null; 398 } 399 } 400 401 403 synchronized void revokeService(Class serviceClass, boolean isDelegated, boolean revokeNow) { 404 if (serviceClasses == null) return; 405 406 BCSSCServiceClassRef serviceClassRef = (BCSSCServiceClassRef)serviceClasses.get(serviceClass); 407 408 if (serviceClassRef == null) return; 409 410 Iterator i = serviceClassRef.cloneOfEntries(); 411 412 BeanContextServiceRevokedEvent bcsre = new BeanContextServiceRevokedEvent (BeanContextServicesSupport.this.getBeanContextServicesPeer(), serviceClass, revokeNow); 413 boolean noMoreRefs = false; 414 415 while (i.hasNext() && serviceRequestors != null) { 416 Map.Entry entry = (Map.Entry )i.next(); 417 BeanContextServiceRevokedListener listener = (BeanContextServiceRevokedListener )entry.getValue(); 418 419 if (revokeNow) { 420 Object requestor = entry.getKey(); 421 Map services = (Map )serviceRequestors.get(requestor); 422 423 if (services != null) { 424 Iterator i1 = services.entrySet().iterator(); 425 426 while (i1.hasNext()) { 427 Map.Entry tmp = (Map.Entry )i1.next(); 428 429 BCSSCServiceRef serviceRef = (BCSSCServiceRef)tmp.getValue(); 430 if (serviceRef.getServiceClassRef().equals(serviceClassRef) && isDelegated == serviceRef.isDelegated()) { 431 i1.remove(); 432 } 433 } 434 435 if (noMoreRefs = services.isEmpty()) { 436 serviceRequestors.remove(requestor); 437 } 438 } 439 440 if (noMoreRefs) serviceClassRef.removeRequestor(requestor); 441 } 442 443 listener.serviceRevoked(bcsre); 444 } 445 446 if (revokeNow && serviceClasses != null) { 447 if (serviceClassRef.isEmpty()) 448 serviceClasses.remove(serviceClass); 449 450 if (serviceClasses.isEmpty()) 451 serviceClasses = null; 452 } 453 454 if (serviceRequestors != null && serviceRequestors.isEmpty()) 455 serviceRequestors = null; 456 } 457 458 460 void cleanupReferences() { 461 462 if (serviceRequestors == null) return; 463 464 Iterator requestors = serviceRequestors.entrySet().iterator(); 465 466 while(requestors.hasNext()) { 467 Map.Entry tmp = (Map.Entry )requestors.next(); 468 Object requestor = tmp.getKey(); 469 Iterator services = ((Map )tmp.getValue()).entrySet().iterator(); 470 471 requestors.remove(); 472 473 while (services.hasNext()) { 474 Map.Entry entry = (Map.Entry )services.next(); 475 Object service = entry.getKey(); 476 BCSSCServiceRef sref = (BCSSCServiceRef)entry.getValue(); 477 478 BCSSCServiceClassRef scref = sref.getServiceClassRef(); 479 480 BeanContextServiceProvider bcsp = sref.isDelegated() ? scref.getDelegateProvider() : scref.getServiceProvider(); 481 482 scref.removeRequestor(requestor); 483 services.remove(); 484 485 while (sref.release() >= 0) { 486 bcsp.releaseService(BeanContextServicesSupport.this.getBeanContextServicesPeer(), requestor, service); 487 } 488 } 489 } 490 491 serviceRequestors = null; 492 serviceClasses = null; 493 } 494 495 void revokeAllDelegatedServicesNow() { 496 if (serviceClasses == null) return; 497 498 Iterator serviceClassRefs = 499 new HashSet (serviceClasses.values()).iterator(); 500 501 while (serviceClassRefs.hasNext()) { 502 BCSSCServiceClassRef serviceClassRef = (BCSSCServiceClassRef)serviceClassRefs.next(); 503 504 if (!serviceClassRef.isDelegated()) continue; 505 506 Iterator i = serviceClassRef.cloneOfEntries(); 507 BeanContextServiceRevokedEvent bcsre = new BeanContextServiceRevokedEvent (BeanContextServicesSupport.this.getBeanContextServicesPeer(), serviceClassRef.getServiceClass(), true); 508 boolean noMoreRefs = false; 509 510 while (i.hasNext()) { 511 Map.Entry entry = (Map.Entry )i.next(); 512 BeanContextServiceRevokedListener listener = (BeanContextServiceRevokedListener )entry.getValue(); 513 514 Object requestor = entry.getKey(); 515 Map services = (Map )serviceRequestors.get(requestor); 516 517 if (services != null) { 518 Iterator i1 = services.entrySet().iterator(); 519 520 while (i1.hasNext()) { 521 Map.Entry tmp = (Map.Entry )i1.next(); 522 523 BCSSCServiceRef serviceRef = (BCSSCServiceRef)tmp.getValue(); 524 if (serviceRef.getServiceClassRef().equals(serviceClassRef) && serviceRef.isDelegated()) { 525 i1.remove(); 526 } 527 } 528 529 if (noMoreRefs = services.isEmpty()) { 530 serviceRequestors.remove(requestor); 531 } 532 } 533 534 if (noMoreRefs) serviceClassRef.removeRequestor(requestor); 535 536 listener.serviceRevoked(bcsre); 537 538 if (serviceClassRef.isEmpty()) 539 serviceClasses.remove(serviceClassRef.getServiceClass()); 540 } 541 } 542 543 if (serviceClasses.isEmpty()) serviceClasses = null; 544 545 if (serviceRequestors != null && serviceRequestors.isEmpty()) 546 serviceRequestors = null; 547 } 548 549 552 553 private transient HashMap serviceClasses; 554 private transient HashMap serviceRequestors; 555 } 556 557 567 568 protected BCSChild createBCSChild(Object targetChild, Object peer) { 569 return new BCSSChild(targetChild, peer); 570 } 571 572 573 574 578 579 protected static class BCSSServiceProvider implements Serializable { 580 581 BCSSServiceProvider(Class sc, BeanContextServiceProvider bcsp) { 582 super(); 583 584 serviceProvider = bcsp; 585 } 586 587 protected BeanContextServiceProvider getServiceProvider() { 588 return serviceProvider; 589 } 590 591 594 595 protected BeanContextServiceProvider serviceProvider; 596 } 597 598 603 604 protected BCSSServiceProvider createBCSSServiceProvider(Class sc, BeanContextServiceProvider bcsp) { 605 return new BCSSServiceProvider(sc, bcsp); 606 } 607 608 609 610 615 616 public void addBeanContextServicesListener(BeanContextServicesListener bcsl) { 617 if (bcsl == null) throw new NullPointerException ("bcsl"); 618 619 synchronized(bcsListeners) { 620 if (bcsListeners.contains(bcsl)) 621 return; 622 else 623 bcsListeners.add(bcsl); 624 } 625 } 626 627 630 631 public void removeBeanContextServicesListener(BeanContextServicesListener bcsl) { 632 if (bcsl == null) throw new NullPointerException ("bcsl"); 633 634 synchronized(bcsListeners) { 635 if (!bcsListeners.contains(bcsl)) 636 return; 637 else 638 bcsListeners.remove(bcsl); 639 } 640 } 641 642 645 646 public boolean addService(Class serviceClass, BeanContextServiceProvider bcsp) { 647 return addService(serviceClass, bcsp, true); 648 } 649 650 653 654 protected boolean addService(Class serviceClass, BeanContextServiceProvider bcsp, boolean fireEvent) { 655 656 if (serviceClass == null) throw new NullPointerException ("serviceClass"); 657 if (bcsp == null) throw new NullPointerException ("bcsp"); 658 659 synchronized(BeanContext.globalHierarchyLock) { 660 if (services.containsKey(serviceClass)) 661 return false; 662 else { 663 services.put(serviceClass, createBCSSServiceProvider(serviceClass, bcsp)); 664 665 if (bcsp instanceof Serializable ) serializable++; 666 667 if (!fireEvent) return true; 668 669 670 BeanContextServiceAvailableEvent bcssae = new BeanContextServiceAvailableEvent (getBeanContextServicesPeer(), serviceClass); 671 672 fireServiceAdded(bcssae); 673 674 synchronized(children) { 675 Iterator i = children.keySet().iterator(); 676 677 while (i.hasNext()) { 678 Object c = i.next(); 679 680 if (c instanceof BeanContextServices ) { 681 ((BeanContextServicesListener )c).serviceAvailable(bcssae); 682 } 683 } 684 } 685 686 return true; 687 } 688 } 689 } 690 691 694 695 public void revokeService(Class serviceClass, BeanContextServiceProvider bcsp, boolean revokeCurrentServicesNow) { 696 697 if (serviceClass == null) throw new NullPointerException ("serviceClass"); 698 if (bcsp == null) throw new NullPointerException ("bcsp"); 699 700 synchronized(BeanContext.globalHierarchyLock) { 701 if (!services.containsKey(serviceClass)) return; 702 703 BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass); 704 705 if (!bcsssp.getServiceProvider().equals(bcsp)) 706 throw new IllegalArgumentException ("service provider mismatch"); 707 708 services.remove(serviceClass); 709 710 if (bcsp instanceof Serializable ) serializable--; 711 712 Iterator i = bcsChildren(); 714 while (i.hasNext()) { 715 ((BCSSChild)i.next()).revokeService(serviceClass, false, revokeCurrentServicesNow); 716 } 717 718 fireServiceRevoked(serviceClass, revokeCurrentServicesNow); 719 } 720 } 721 722 725 726 public synchronized boolean hasService(Class serviceClass) { 727 if (serviceClass == null) throw new NullPointerException ("serviceClass"); 728 729 synchronized(BeanContext.globalHierarchyLock) { 730 if (services.containsKey(serviceClass)) return true; 731 732 BeanContextServices bcs = null; 733 734 try { 735 bcs = (BeanContextServices )getBeanContext(); 736 } catch (ClassCastException cce) { 737 return false; 738 } 739 740 return bcs == null ? false : bcs.hasService(serviceClass); 741 } 742 } 743 744 745 746 750 751 protected class BCSSProxyServiceProvider implements BeanContextServiceProvider , BeanContextServiceRevokedListener { 752 753 BCSSProxyServiceProvider(BeanContextServices bcs) { 754 super(); 755 756 nestingCtxt = bcs; 757 } 758 759 public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector) { 760 Object service = null; 761 762 try { 763 service = nestingCtxt.getService(bcs, requestor, serviceClass, serviceSelector, this); 764 } catch (TooManyListenersException tmle) { 765 return null; 766 } 767 768 return service; 769 } 770 771 public void releaseService(BeanContextServices bcs, Object requestor, Object service) { 772 nestingCtxt.releaseService(bcs, requestor, service); 773 } 774 775 public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) { 776 return nestingCtxt.getCurrentServiceSelectors(serviceClass); 777 } 778 779 public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) { 780 Iterator i = bcsChildren(); 782 while (i.hasNext()) { 783 ((BCSSChild)i.next()).revokeService(bcsre.getServiceClass(), true, bcsre.isCurrentServiceInvalidNow()); 784 } 785 } 786 787 790 791 private BeanContextServices nestingCtxt; 792 } 793 794 795 796 799 800 public Object getService(BeanContextChild child, Object requestor, Class serviceClass, Object serviceSelector, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException { 801 if (child == null) throw new NullPointerException ("child"); 802 if (serviceClass == null) throw new NullPointerException ("serviceClass"); 803 if (requestor == null) throw new NullPointerException ("requestor"); 804 if (bcsrl == null) throw new NullPointerException ("bcsrl"); 805 806 Object service = null; 807 BCSSChild bcsc; 808 BeanContextServices bcssp = getBeanContextServicesPeer(); 809 810 synchronized(BeanContext.globalHierarchyLock) { 811 synchronized(children) { bcsc = (BCSSChild)children.get(child); } 812 813 if (bcsc == null) throw new IllegalArgumentException ("not a child of this context"); 815 BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass); 816 817 if (bcsssp != null) { 818 BeanContextServiceProvider bcsp = bcsssp.getServiceProvider(); 819 service = bcsp.getService(bcssp, requestor, serviceClass, serviceSelector); 820 if (service != null) { try { 822 bcsc.usingService(requestor, service, serviceClass, bcsp, false, bcsrl); 823 } catch (TooManyListenersException tmle) { 824 bcsp.releaseService(bcssp, requestor, service); 825 throw tmle; 826 } catch (UnsupportedOperationException uope) { 827 bcsp.releaseService(bcssp, requestor, service); 828 throw uope; } 830 831 return service; 832 } 833 } 834 835 836 if (proxy != null) { 837 838 840 service = proxy.getService(bcssp, requestor, serviceClass, serviceSelector); 841 842 if (service != null) { try { 844 bcsc.usingService(requestor, service, serviceClass, proxy, true, bcsrl); 845 } catch (TooManyListenersException tmle) { 846 proxy.releaseService(bcssp, requestor, service); 847 throw tmle; 848 } catch (UnsupportedOperationException uope) { 849 proxy.releaseService(bcssp, requestor, service); 850 throw uope; } 852 853 return service; 854 } 855 } 856 } 857 858 return null; 859 } 860 861 864 865 public void releaseService(BeanContextChild child, Object requestor, Object service) { 866 if (child == null) throw new NullPointerException ("child"); 867 if (requestor == null) throw new NullPointerException ("requestor"); 868 if (service == null) throw new NullPointerException ("service"); 869 870 BCSSChild bcsc; 871 872 synchronized(BeanContext.globalHierarchyLock) { 873 synchronized(children) { bcsc = (BCSSChild)children.get(child); } 874 875 if (bcsc != null) 876 bcsc.releaseService(requestor, service); 877 else 878 throw new IllegalArgumentException ("child actual is not a child of this BeanContext"); 879 } 880 } 881 882 885 886 public Iterator getCurrentServiceClasses() { 887 return new BCSIterator(services.keySet().iterator()); 888 } 889 890 894 895 public Iterator getCurrentServiceSelectors(Class serviceClass) { 896 897 BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass); 898 899 return bcsssp != null ? new BCSIterator(bcsssp.getServiceProvider().getCurrentServiceSelectors(getBeanContextServicesPeer(), serviceClass)) : null; 900 } 901 902 911 912 public void serviceAvailable(BeanContextServiceAvailableEvent bcssae) { 913 synchronized(BeanContext.globalHierarchyLock) { 914 if (services.containsKey(bcssae.getServiceClass())) return; 915 916 fireServiceAdded(bcssae); 917 918 Iterator i; 919 920 synchronized(children) { 921 i = children.keySet().iterator(); 922 } 923 924 while (i.hasNext()) { 925 Object c = i.next(); 926 927 if (c instanceof BeanContextServices ) { 928 ((BeanContextServicesListener )c).serviceAvailable(bcssae); 929 } 930 } 931 } 932 } 933 934 943 944 public void serviceRevoked(BeanContextServiceRevokedEvent bcssre) { 945 synchronized(BeanContext.globalHierarchyLock) { 946 if (services.containsKey(bcssre.getServiceClass())) return; 947 948 fireServiceRevoked(bcssre); 949 950 Iterator i; 951 952 synchronized(children) { 953 i = children.keySet().iterator(); 954 } 955 956 while (i.hasNext()) { 957 Object c = i.next(); 958 959 if (c instanceof BeanContextServices ) { 960 ((BeanContextServicesListener )c).serviceRevoked(bcssre); 961 } 962 } 963 } 964 } 965 966 973 protected static final BeanContextServicesListener getChildBeanContextServicesListener(Object child) { 974 try { 975 return (BeanContextServicesListener )child; 976 } catch (ClassCastException cce) { 977 return null; 978 } 979 } 980 981 991 992 protected void childJustRemovedHook(Object child, BCSChild bcsc) { 993 BCSSChild bcssc = (BCSSChild)bcsc; 994 995 bcssc.cleanupReferences(); 996 } 997 998 1006 1007 protected synchronized void releaseBeanContextResources() { 1008 Object [] bcssc; 1009 1010 super.releaseBeanContextResources(); 1011 1012 synchronized(children) { 1013 if (children.isEmpty()) return; 1014 1015 bcssc = children.values().toArray(); 1016 } 1017 1018 1019 for (int i = 0; i < bcssc.length; i++) { 1020 ((BCSSChild)bcssc[i]).revokeAllDelegatedServicesNow(); 1021 } 1022 1023 proxy = null; 1024 } 1025 1026 1032 1033 protected synchronized void initializeBeanContextResources() { 1034 super.initializeBeanContextResources(); 1035 1036 BeanContext nbc = getBeanContext(); 1037 1038 if (nbc == null) return; 1039 1040 try { 1041 BeanContextServices bcs = (BeanContextServices )nbc; 1042 1043 proxy = new BCSSProxyServiceProvider(bcs); 1044 } catch (ClassCastException cce) { 1045 } 1047 } 1048 1049 1052 protected final void fireServiceAdded(Class serviceClass) { 1053 BeanContextServiceAvailableEvent bcssae = new BeanContextServiceAvailableEvent (getBeanContextServicesPeer(), serviceClass); 1054 1055 fireServiceAdded(bcssae); 1056 } 1057 1058 1064 protected final void fireServiceAdded(BeanContextServiceAvailableEvent bcssae) { 1065 Object [] copy; 1066 1067 synchronized (bcsListeners) { copy = bcsListeners.toArray(); } 1068 1069 for (int i = 0; i < copy.length; i++) { 1070 ((BeanContextServicesListener )copy[i]).serviceAvailable(bcssae); 1071 } 1072 } 1073 1074 1079 protected final void fireServiceRevoked(BeanContextServiceRevokedEvent bcsre) { 1080 Object [] copy; 1081 1082 synchronized (bcsListeners) { copy = bcsListeners.toArray(); } 1083 1084 for (int i = 0; i < copy.length; i++) { 1085 ((BeanContextServiceRevokedListener )copy[i]).serviceRevoked(bcsre); 1086 } 1087 } 1088 1089 1094 protected final void fireServiceRevoked(Class serviceClass, boolean revokeNow) { 1095 Object [] copy; 1096 BeanContextServiceRevokedEvent bcsre = new BeanContextServiceRevokedEvent (getBeanContextServicesPeer(), serviceClass, revokeNow); 1097 1098 synchronized (bcsListeners) { copy = bcsListeners.toArray(); } 1099 1100 for (int i = 0; i < copy.length; i++) { 1101 ((BeanContextServicesListener )copy[i]).serviceRevoked(bcsre); 1102 } 1103 } 1104 1105 1115 1116 protected synchronized void bcsPreSerializationHook(ObjectOutputStream oos) throws IOException { 1117 1118 oos.writeInt(serializable); 1119 1120 if (serializable <= 0) return; 1121 1122 int count = 0; 1123 1124 Iterator i = services.entrySet().iterator(); 1125 1126 while (i.hasNext() && count < serializable) { 1127 Map.Entry entry = (Map.Entry )i.next(); 1128 BCSSServiceProvider bcsp = null; 1129 1130 try { 1131 bcsp = (BCSSServiceProvider)entry.getValue(); 1132 } catch (ClassCastException cce) { 1133 continue; 1134 } 1135 1136 if (bcsp.getServiceProvider() instanceof Serializable ) { 1137 oos.writeObject(entry.getKey()); 1138 oos.writeObject(bcsp); 1139 count++; 1140 } 1141 } 1142 1143 if (count != serializable) 1144 throw new IOException ("wrote different number of service providers than expected"); 1145 } 1146 1147 1158 1159 protected synchronized void bcsPreDeserializationHook(ObjectInputStream ois) throws IOException , ClassNotFoundException { 1160 1161 serializable = ois.readInt(); 1162 1163 int count = serializable; 1164 1165 while (count > 0) { 1166 services.put(ois.readObject(), ois.readObject()); 1167 count--; 1168 } 1169 } 1170 1171 1174 1175 private synchronized void writeObject(ObjectOutputStream oos) throws IOException { 1176 oos.defaultWriteObject(); 1177 1178 serialize(oos, (Collection )bcsListeners); 1179 } 1180 1181 1184 1185 private synchronized void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException { 1186 1187 ois.defaultReadObject(); 1188 1189 deserialize(ois, (Collection )bcsListeners); 1190 } 1191 1192 1193 1196 1197 1201 protected transient HashMap services; 1202 1203 1206 protected transient int serializable = 0; 1207 1208 1209 1212 protected transient BCSSProxyServiceProvider proxy; 1213 1214 1215 1218 protected transient ArrayList bcsListeners; 1219} 1220 | Popular Tags |