Path: blob/master/src/java.naming/share/classes/com/sun/jndi/ldap/LdapDnsProviderService.java
41161 views
/*1* Copyright (c) 2018, 2021, 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 javax.naming.ldap.spi.LdapDnsProvider;32import javax.naming.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*/51@SuppressWarnings("removal")52private LdapDnsProviderService() {53SecurityManager sm = System.getSecurityManager();54if (sm == null) {55providers = ServiceLoader.load(56LdapDnsProvider.class,57ClassLoader.getSystemClassLoader());58} else {59final PrivilegedAction<ServiceLoader<LdapDnsProvider>> pa =60() -> ServiceLoader.load(61LdapDnsProvider.class,62ClassLoader.getSystemClassLoader());6364providers = AccessController.doPrivileged(65pa,66null,67new RuntimePermission("ldapDnsProvider"),68SecurityConstants.GET_CLASSLOADER_PERMISSION);69}70}7172/**73* Retrieves the singleton instance of LdapDnsProviderService.74*/75static LdapDnsProviderService getInstance() {76if (service != null) return service;77synchronized (LOCK) {78if (service != null) return service;79service = new LdapDnsProviderService();80}81return service;82}8384/**85* Retrieves result from the first provider that successfully resolves86* the endpoints. If no results are found when calling installed87* subclasses of {@code LdapDnsProvider} then this method will fall back88* to the {@code DefaultLdapDnsProvider}.89*90* @throws NamingException if the {@code url} is not valid or an error91* occurred while performing the lookup.92*/93LdapDnsProviderResult lookupEndpoints(String url, Hashtable<?,?> env)94throws NamingException95{96LdapDnsProviderResult result = null;97Hashtable<?, ?> envCopy = new Hashtable<>(env);98synchronized (LOCK) {99Iterator<LdapDnsProvider> iterator = providers.iterator();100while (result == null && iterator.hasNext()) {101result = iterator.next().lookupEndpoints(url, envCopy)102.filter(r -> !r.getEndpoints().isEmpty())103.orElse(null);104}105}106107if (result == null) {108return new DefaultLdapDnsProvider().lookupEndpoints(url, env)109.orElse(new LdapDnsProviderResult("", List.of()));110}111return result;112}113}114115116