1 7 8 package javax.imageio.spi; 9 10 import java.io.File ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.NoSuchElementException ; 17 import java.util.Set ; 18 import sun.misc.Service; 19 20 82 public class ServiceRegistry { 83 84 private Map categoryMap = new HashMap (); 86 87 98 public ServiceRegistry(Iterator <Class <?>> categories) { 99 if (categories == null) { 100 throw new IllegalArgumentException ("categories == null!"); 101 } 102 while (categories.hasNext()) { 103 Class category = (Class )categories.next(); 104 SubRegistry reg = new SubRegistry(this, category); 105 categoryMap.put(category, reg); 106 } 107 } 108 109 118 152 public static <T> Iterator <T> lookupProviders(Class <T> providerClass, 153 ClassLoader loader) 154 { 155 if (providerClass == null) { 156 throw new IllegalArgumentException ("providerClass == null!"); 157 } 158 return Service.providers(providerClass, loader); 159 } 160 161 183 public static <T> Iterator <T> lookupProviders(Class <T> providerClass) { 184 if (providerClass == null) { 185 throw new IllegalArgumentException ("providerClass == null!"); 186 } 187 return Service.providers(providerClass); 188 } 189 190 198 public Iterator <Class <?>> getCategories() { 199 Set keySet = categoryMap.keySet(); 200 return keySet.iterator(); 201 } 202 203 207 private Iterator getSubRegistries(Object provider) { 208 List l = new ArrayList (); 209 Iterator iter = categoryMap.keySet().iterator(); 210 while (iter.hasNext()) { 211 Class c = (Class )iter.next(); 212 if (c.isAssignableFrom(provider.getClass())) { 213 l.add((SubRegistry)categoryMap.get(c)); 214 } 215 } 216 return l.iterator(); 217 } 218 219 244 public <T> boolean registerServiceProvider(T provider, 245 Class <T> category) { 246 if (provider == null) { 247 throw new IllegalArgumentException ("provider == null!"); 248 } 249 SubRegistry reg = (SubRegistry)categoryMap.get(category); 250 if (reg == null) { 251 throw new IllegalArgumentException ("category unknown!"); 252 } 253 if (!category.isAssignableFrom(provider.getClass())) { 254 throw new ClassCastException (); 255 } 256 257 return reg.registerServiceProvider(provider); 258 } 259 260 278 public void registerServiceProvider(Object provider) { 279 if (provider == null) { 280 throw new IllegalArgumentException ("provider == null!"); 281 } 282 Iterator regs = getSubRegistries(provider); 283 while (regs.hasNext()) { 284 SubRegistry reg = (SubRegistry)regs.next(); 285 reg.registerServiceProvider(provider); 286 } 287 } 288 289 309 public void registerServiceProviders(Iterator <?> providers) { 310 if (providers == null) { 311 throw new IllegalArgumentException ("provider == null!"); 312 } 313 while (providers.hasNext()) { 314 registerServiceProvider(providers.next()); 315 } 316 } 317 318 346 public <T> boolean deregisterServiceProvider(T provider, 347 Class <T> category) { 348 if (provider == null) { 349 throw new IllegalArgumentException ("provider == null!"); 350 } 351 SubRegistry reg = (SubRegistry)categoryMap.get(category); 352 if (reg == null) { 353 throw new IllegalArgumentException ("category unknown!"); 354 } 355 if (!category.isAssignableFrom(provider.getClass())) { 356 throw new ClassCastException (); 357 } 358 return reg.deregisterServiceProvider(provider); 359 } 360 361 370 public void deregisterServiceProvider(Object provider) { 371 if (provider == null) { 372 throw new IllegalArgumentException ("provider == null!"); 373 } 374 Iterator regs = getSubRegistries(provider); 375 while (regs.hasNext()) { 376 SubRegistry reg = (SubRegistry)regs.next(); 377 reg.deregisterServiceProvider(provider); 378 } 379 } 380 381 393 public boolean contains(Object provider) { 394 if (provider == null) { 395 throw new IllegalArgumentException ("provider == null!"); 396 } 397 Iterator regs = getSubRegistries(provider); 398 while (regs.hasNext()) { 399 SubRegistry reg = (SubRegistry)regs.next(); 400 if (reg.contains(provider)) { 401 return true; 402 } 403 } 404 405 return false; 406 } 407 408 428 public <T> Iterator <T> getServiceProviders(Class <T> category, 429 boolean useOrdering) { 430 SubRegistry reg = (SubRegistry)categoryMap.get(category); 431 if (reg == null) { 432 throw new IllegalArgumentException ("category unknown!"); 433 } 434 return reg.getServiceProviders(useOrdering); 435 } 436 437 447 public interface Filter { 448 449 458 boolean filter(Object provider); 459 } 460 461 483 public <T> Iterator <T> getServiceProviders(Class <T> category, 484 Filter filter, 485 boolean useOrdering) { 486 SubRegistry reg = (SubRegistry)categoryMap.get(category); 487 if (reg == null) { 488 throw new IllegalArgumentException ("category unknown!"); 489 } 490 Iterator iter = getServiceProviders(category, useOrdering); 491 return new FilterIterator(iter, filter); 492 } 493 494 511 public <T> T getServiceProviderByClass(Class <T> providerClass) { 512 if (providerClass == null) { 513 throw new IllegalArgumentException ("providerClass == null!"); 514 } 515 Iterator iter = categoryMap.keySet().iterator(); 516 while (iter.hasNext()) { 517 Class c = (Class )iter.next(); 518 if (c.isAssignableFrom(providerClass)) { 519 SubRegistry reg = (SubRegistry)categoryMap.get(c); 520 T provider = reg.getServiceProviderByClass(providerClass); 521 if (provider != null) { 522 return provider; 523 } 524 } 525 } 526 return null; 527 } 528 529 556 public <T> boolean setOrdering(Class <T> category, 557 T firstProvider, 558 T secondProvider) { 559 if (firstProvider == null || secondProvider == null) { 560 throw new IllegalArgumentException ("provider is null!"); 561 } 562 if (firstProvider == secondProvider) { 563 throw new IllegalArgumentException ("providers are the same!"); 564 } 565 SubRegistry reg = (SubRegistry)categoryMap.get(category); 566 if (reg == null) { 567 throw new IllegalArgumentException ("category unknown!"); 568 } 569 if (reg.contains(firstProvider) && 570 reg.contains(secondProvider)) { 571 return reg.setOrdering(firstProvider, secondProvider); 572 } 573 return false; 574 } 575 576 601 public <T> boolean unsetOrdering(Class <T> category, 602 T firstProvider, 603 T secondProvider) { 604 if (firstProvider == null || secondProvider == null) { 605 throw new IllegalArgumentException ("provider is null!"); 606 } 607 if (firstProvider == secondProvider) { 608 throw new IllegalArgumentException ("providers are the same!"); 609 } 610 SubRegistry reg = (SubRegistry)categoryMap.get(category); 611 if (reg == null) { 612 throw new IllegalArgumentException ("category unknown!"); 613 } 614 if (reg.contains(firstProvider) && 615 reg.contains(secondProvider)) { 616 return reg.unsetOrdering(firstProvider, secondProvider); 617 } 618 return false; 619 } 620 621 630 public void deregisterAll(Class <?> category) { 631 SubRegistry reg = (SubRegistry)categoryMap.get(category); 632 if (reg == null) { 633 throw new IllegalArgumentException ("category unknown!"); 634 } 635 reg.clear(); 636 } 637 638 642 public void deregisterAll() { 643 Iterator iter = categoryMap.values().iterator(); 644 while (iter.hasNext()) { 645 SubRegistry reg = (SubRegistry)iter.next(); 646 reg.clear(); 647 } 648 } 649 650 659 public void finalize() throws Throwable { 660 deregisterAll(); 661 super.finalize(); 662 } 663 } 664 665 666 670 class SubRegistry { 671 672 ServiceRegistry registry; 673 674 Class category; 675 676 PartiallyOrderedSet poset = new PartiallyOrderedSet (); 678 679 Map <Class <?>,Object > map = new HashMap (); 681 682 public SubRegistry(ServiceRegistry registry, Class category) { 683 this.registry = registry; 684 this.category = category; 685 } 686 687 public boolean registerServiceProvider(Object provider) { 688 Object oprovider = map.get(provider.getClass()); 689 boolean present = oprovider != null; 690 691 if (present) { 692 deregisterServiceProvider(oprovider); 693 } 694 map.put(provider.getClass(), provider); 695 poset.add(provider); 696 if (provider instanceof RegisterableService ) { 697 RegisterableService rs = (RegisterableService )provider; 698 rs.onRegistration(registry, category); 699 } 700 701 return !present; 702 } 703 704 709 public boolean deregisterServiceProvider(Object provider) { 710 Object oprovider = map.get(provider.getClass()); 711 712 if (provider == oprovider) { 713 map.remove(provider.getClass()); 714 poset.remove(provider); 715 if (provider instanceof RegisterableService ) { 716 RegisterableService rs = (RegisterableService )provider; 717 rs.onDeregistration(registry, category); 718 } 719 720 return true; 721 } 722 return false; 723 } 724 725 public boolean contains(Object provider) { 726 Object oprovider = map.get(provider.getClass()); 727 return oprovider == provider; 728 } 729 730 public boolean setOrdering(Object firstProvider, 731 Object secondProvider) { 732 return poset.setOrdering(firstProvider, secondProvider); 733 } 734 735 public boolean unsetOrdering(Object firstProvider, 736 Object secondProvider) { 737 return poset.unsetOrdering(firstProvider, secondProvider); 738 } 739 740 public Iterator getServiceProviders(boolean useOrdering) { 741 if (useOrdering) { 742 return poset.iterator(); 743 } else { 744 return map.values().iterator(); 745 } 746 } 747 748 public <T> T getServiceProviderByClass(Class <T> providerClass) { 749 return (T)map.get(providerClass); 750 } 751 752 public void clear() { 753 Iterator iter = map.values().iterator(); 754 while (iter.hasNext()) { 755 Object provider = iter.next(); 756 iter.remove(); 757 758 if (provider instanceof RegisterableService ) { 759 RegisterableService rs = (RegisterableService )provider; 760 rs.onDeregistration(registry, category); 761 } 762 } 763 poset.clear(); 764 } 765 766 public void finalize() { 767 clear(); 768 } 769 } 770 771 772 776 class FilterIterator<T> implements Iterator <T> { 777 778 private Iterator <T> iter; 779 private ServiceRegistry.Filter filter; 780 781 private T next = null; 782 783 public FilterIterator(Iterator <T> iter, 784 ServiceRegistry.Filter filter) { 785 this.iter = iter; 786 this.filter = filter; 787 advance(); 788 } 789 790 private void advance() { 791 while (iter.hasNext()) { 792 T elt = iter.next(); 793 if (filter.filter(elt)) { 794 next = elt; 795 return; 796 } 797 } 798 799 next = null; 800 } 801 802 public boolean hasNext() { 803 return next != null; 804 } 805 806 public T next() { 807 if (next == null) { 808 throw new NoSuchElementException (); 809 } 810 T o = next; 811 advance(); 812 return o; 813 } 814 815 public void remove() { 816 throw new UnsupportedOperationException (); 817 } 818 } 819 | Popular Tags |