1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.jndi.ldap; 27 28 import java.security.AccessController; 29 import java.security.PrivilegedAction; 30 import java.util.*; 31 import javax.naming.NamingException; 32 import javax.naming.ldap.spi.LdapDnsProvider; 33 import javax.naming.ldap.spi.LdapDnsProviderResult; 34 import sun.security.util.SecurityConstants; 35 36 /** 37 * The {@code LdapDnsProviderService} is responsible for creating and providing 38 * access to the registered {@code LdapDnsProvider}s. The {@link ServiceLoader} 39 * is used to find and register any implementations of {@link LdapDnsProvider}. 40 * 41 * <p> Instances of this class are safe for use by multiple threads. 42 */ 43 final class LdapDnsProviderService { 44 45 private static volatile LdapDnsProviderService service; 46 private static final Object LOCK = new int[0]; 47 private final ServiceLoader<LdapDnsProvider> providers; 48 49 /** 50 * Creates a new instance of LdapDnsProviderService 51 */ 52 private LdapDnsProviderService() { 53 SecurityManager sm = System.getSecurityManager(); 54 if (sm == null) { 55 providers = ServiceLoader.load( 56 LdapDnsProvider.class, 57 ClassLoader.getSystemClassLoader()); 58 } else { 59 final PrivilegedAction<ServiceLoader<LdapDnsProvider>> pa = 60 () -> ServiceLoader.load( 61 LdapDnsProvider.class, 62 ClassLoader.getSystemClassLoader()); 63 64 providers = AccessController.doPrivileged( 65 pa, 66 null, 67 new RuntimePermission("ldapDnsProvider"), 68 SecurityConstants.GET_CLASSLOADER_PERMISSION); 69 } 70 } 71 72 /** 73 * Retrieves the singleton instance of LdapDnsProviderService. 74 */ 75 static LdapDnsProviderService getInstance() { 76 if (service != null) return service; 77 synchronized (LOCK) { 78 if (service != null) return service; 79 service = new LdapDnsProviderService(); 80 } 81 return service; 82 } 83 84 /** 85 * Retrieves result from the first provider that successfully resolves 86 * the endpoints. If no results are found when calling installed 87 * subclasses of {@code LdapDnsProvider} then this method will fall back 88 * to the {@code DefaultLdapDnsProvider}. 89 * 90 * @throws NamingException if the {@code url} in not valid or an error 91 * occurred while performing the lookup. 92 */ 93 LdapDnsProviderResult lookupEndpoints(String url, Hashtable<?,?> env) 94 throws NamingException 95 { 96 LdapDnsProviderResult result = null; 97 Hashtable<?, ?> envCopy = new Hashtable<>(env); 98 synchronized (LOCK) { 99 Iterator<LdapDnsProvider> iterator = providers.iterator(); 100 while (result == null && iterator.hasNext()) { 101 result = iterator.next().lookupEndpoints(url, envCopy) 102 .filter(r -> !r.getEndpoints().isEmpty()) 103 .orElse(null); 104 } 105 } 106 107 if (result == null) { 108 return new DefaultLdapDnsProvider().lookupEndpoints(url, env) 109 .orElse(new LdapDnsProviderResult("", List.of())); 110 } 111 return result; 112 } 113 114 }