Get a Service object. Needs a provider object, but will create a URLName if needed. It attempts to instantiate the correct class. @param provider which provider to use @param url which URLName to use (can be null) @param type the service type (class) @exception NoSuchProviderException thrown when
(Provider provider, URLName url, Class<T> type)
| 786 | * Service. |
| 787 | */ |
| 788 | private <T extends Service> T getService(Provider provider, URLName url, |
| 789 | Class<T> type) |
| 790 | throws NoSuchProviderException { |
| 791 | // need a provider and url |
| 792 | if (provider == null) { |
| 793 | throw new NoSuchProviderException("null"); |
| 794 | } |
| 795 | |
| 796 | // create a url if needed |
| 797 | if (url == null) { |
| 798 | url = new URLName(provider.getProtocol(), null, -1, |
| 799 | null, null, null); |
| 800 | } |
| 801 | |
| 802 | Object service = null; |
| 803 | |
| 804 | // get the ClassLoader associated with the Authenticator |
| 805 | ClassLoader cl; |
| 806 | if (authenticator != null) |
| 807 | cl = authenticator.getClass().getClassLoader(); |
| 808 | else |
| 809 | cl = this.getClass().getClassLoader(); |
| 810 | |
| 811 | // now load the class |
| 812 | Class<?> serviceClass = null; |
| 813 | try { |
| 814 | // First try the "application's" class loader. |
| 815 | ClassLoader ccl = getContextClassLoader(); |
| 816 | if (ccl != null) |
| 817 | try { |
| 818 | serviceClass = |
| 819 | Class.forName(provider.getClassName(), false, ccl); |
| 820 | } catch (ClassNotFoundException ex) { |
| 821 | // ignore it |
| 822 | } |
| 823 | if (serviceClass == null || !type.isAssignableFrom(serviceClass)) |
| 824 | serviceClass = |
| 825 | Class.forName(provider.getClassName(), false, cl); |
| 826 | |
| 827 | if (!type.isAssignableFrom(serviceClass)) |
| 828 | throw new ClassCastException( |
| 829 | type.getName() + " " + serviceClass.getName()); |
| 830 | } catch (Exception ex1) { |
| 831 | // That didn't work, now try the "system" class loader. |
| 832 | // (Need both of these because JDK 1.1 class loaders |
| 833 | // may not delegate to their parent class loader.) |
| 834 | try { |
| 835 | serviceClass = Class.forName(provider.getClassName()); |
| 836 | if (!type.isAssignableFrom(serviceClass)) |
| 837 | throw new ClassCastException( |
| 838 | type.getName() + " " + serviceClass.getName()); |
| 839 | } catch (Exception ex) { |
| 840 | // Nothing worked, give up. |
| 841 | logger.log(Level.FINE, "Exception loading provider", ex); |
| 842 | throw new NoSuchProviderException(provider.getProtocol()); |
| 843 | } |
| 844 | } |
| 845 |
no test coverage detected