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