Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/ldap/LdapDnsProviderService.java
38924 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.ldap;2627import java.security.AccessController;28import java.security.PrivilegedAction;29import java.util.*;30import javax.naming.NamingException;31import com.sun.jndi.ldap.spi.LdapDnsProvider;32import com.sun.jndi.ldap.spi.LdapDnsProviderResult;33import sun.security.util.SecurityConstants;3435/**36* The {@code LdapDnsProviderService} is responsible for creating and providing37* access to the registered {@code LdapDnsProvider}s. The {@link ServiceLoader}38* is used to find and register any implementations of {@link LdapDnsProvider}.39*40* <p> Instances of this class are safe for use by multiple threads.41*/42final class LdapDnsProviderService {4344private static volatile LdapDnsProviderService service;45private static final Object LOCK = new int[0];46private final ServiceLoader<LdapDnsProvider> providers;4748/**49* Creates a new instance of LdapDnsProviderService50*/51private LdapDnsProviderService() {52SecurityManager sm = System.getSecurityManager();53if (sm == null) {54providers = ServiceLoader.load(55LdapDnsProvider.class,56ClassLoader.getSystemClassLoader());57} else {58final PrivilegedAction<ServiceLoader<LdapDnsProvider>> pa =59() -> ServiceLoader.load(60LdapDnsProvider.class,61ClassLoader.getSystemClassLoader());6263providers = AccessController.doPrivileged(64pa,65null,66new RuntimePermission("ldapDnsProvider"),67SecurityConstants.GET_CLASSLOADER_PERMISSION);68}69}7071/**72* Retrieves the singleton instance of LdapDnsProviderService.73*/74static LdapDnsProviderService getInstance() {75if (service != null) return service;76synchronized (LOCK) {77if (service != null) return service;78service = new LdapDnsProviderService();79}80return service;81}8283/**84* Retrieves result from the first provider that successfully resolves85* the endpoints. If no results are found when calling installed86* subclasses of {@code LdapDnsProvider} then this method will fall back87* to the {@code DefaultLdapDnsProvider}.88*89* @throws NamingException if the {@code url} in not valid or an error90* occurred while performing the lookup.91*/92LdapDnsProviderResult lookupEndpoints(String url, Hashtable<?,?> env)93throws NamingException94{95LdapDnsProviderResult result = null;96Hashtable<?, ?> envCopy = new Hashtable<>(env);97synchronized (LOCK) {98Iterator<LdapDnsProvider> iterator = providers.iterator();99while (result == null && iterator.hasNext()) {100result = iterator.next().lookupEndpoints(url, envCopy)101.filter(r -> !r.getEndpoints().isEmpty())102.orElse(null);103}104}105106if (result == null) {107return new DefaultLdapDnsProvider().lookupEndpoints(url, env)108.orElse(new LdapDnsProviderResult("", Collections.emptyList()));109}110return result;111}112113}114115116