Path: blob/master/src/java.naming/share/classes/javax/naming/ldap/spi/LdapDnsProvider.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 javax.naming.ldap.spi;2627import javax.naming.Context;28import javax.naming.NamingException;29import java.util.Map;30import java.util.Optional;3132/**33* Service-provider class for DNS lookups when performing LDAP operations.34*35* <p> An LDAP DNS provider is a concrete subclass of this class that36* has a zero-argument constructor. LDAP DNS providers are located using the37* ServiceLoader facility, as specified by38* {@linkplain javax.naming.directory.InitialDirContext InitialDirectContext}.39*40* The41* {@link java.util.ServiceLoader ServiceLoader} is used to create and register42* implementations of {@code LdapDnsProvider}.43*44* <p> An LDAP DNS provider can be used in environments where the default45* DNS resolution mechanism is not sufficient to accurately pinpoint the46* correct LDAP servers needed to perform LDAP operations. For example, in an47* environment containing a mix of {@code ldap} and {@code ldaps} servers48* you may want the {@linkplain javax.naming.ldap.LdapContext LdapContext}49* to query {@code ldaps} servers only.50*51* @since 1252*/53public abstract class LdapDnsProvider {5455// The {@code RuntimePermission("ldapDnsProvider")} is56// necessary to subclass and instantiate the {@code LdapDnsProvider} class.57private static final RuntimePermission DNSPROVIDER_PERMISSION =58new RuntimePermission("ldapDnsProvider");5960/**61* Creates a new instance of {@code LdapDnsProvider}.62*63* @throws SecurityException if a security manager is present and its64* {@code checkPermission} method doesn't allow65* the {@code RuntimePermission("ldapDnsProvider")}.66*/67protected LdapDnsProvider() {68this(checkPermission());69}7071private LdapDnsProvider(Void unused) {72// nothing to do.73}7475private static Void checkPermission() {76@SuppressWarnings("removal")77final SecurityManager sm = System.getSecurityManager();78if (sm != null) {79sm.checkPermission(DNSPROVIDER_PERMISSION);80}81return null;82}8384/**85* Lookup the endpoints and domain name for the given {@link Context}86* {@link Context#PROVIDER_URL provider URL} and environment. The resolved87* endpoints and domain name are returned as an88* {@link LdapDnsProviderResult}.89*90* <p> An endpoint is a {@code String} representation of an LDAP URL which91* points to an LDAP server to be used for LDAP operations. The syntax of92* an LDAP URL is defined by <a href="http://www.ietf.org/rfc/rfc2255.txt">93* <i>RFC 2255: The LDAP URL Format</i></a>.94*95* @param url The {@link Context} {@link Context#PROVIDER_URL provider URL}96* @param env The {@link Context} environment.97*98* @return an {@link LdapDnsProviderResult} or empty {@code Optional}99* if the lookup fails.100*101* @throws NamingException if the {@code url} is not valid or an error102* occurred while performing the lookup.103* @throws NullPointerException if either {@code url} or {@code env} are104* {@code null}.105*/106public abstract Optional<LdapDnsProviderResult> lookupEndpoints(107String url, Map<?,?> env) throws NamingException;108}109110111