src/java.naming/share/classes/com/sun/jndi/ldap/LdapDnsProviderService.java
author lmesnik
Tue, 02 Apr 2019 17:11:04 -0700
changeset 54389 772f62a13376
parent 52493 a609d549992a
child 58126 1def54255e93
permissions -rw-r--r--
8221437: assert(java_lang_invoke_ResolvedMethodName::vmtarget(resolved_method()) == m()) failed: Should not change after link resolution Reviewed-by: coleenp, sspitsyn
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
/*
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
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}.
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    40
 */
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    41
final class LdapDnsProviderService {
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
    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
    44
    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
    45
    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
    46
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    47
    /**
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    48
     * 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
    49
     */
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    50
    private LdapDnsProviderService() {
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    51
        SecurityManager sm = System.getSecurityManager();
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    52
        if (sm == null) {
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    53
            providers = ServiceLoader.load(
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    54
                    LdapDnsProvider.class,
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    55
                    ClassLoader.getSystemClassLoader());
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    56
        } else {
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    57
            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
    58
                    () -> ServiceLoader.load(
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    59
                            LdapDnsProvider.class,
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    60
                            ClassLoader.getSystemClassLoader());
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    61
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    62
            providers = AccessController.doPrivileged(
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    63
                pa,
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    64
                null,
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    65
                new RuntimePermission("ldapDnsProvider"),
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    66
                SecurityConstants.GET_CLASSLOADER_PERMISSION);
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    67
        }
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    68
    }
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
     * Retrieve the singleton static instance of LdapDnsProviderService.
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    72
     */
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    73
    static LdapDnsProviderService getInstance() {
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    74
        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
    75
        synchronized(LOCK) {
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;
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    77
            service = new LdapDnsProviderService();
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    78
        }
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    79
        return service;
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
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
     * Retrieve result from the first provider that successfully resolves
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    84
     * 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
    85
     * 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
    86
     * to the {@code DefaultLdapDnsProvider}.
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    87
     *
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    88
     * @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
    89
     *                         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
    90
     */
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    91
    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
    92
        throws NamingException
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    93
    {
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    94
        Iterator<LdapDnsProvider> iterator = providers.iterator();
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    95
        Hashtable<?, ?> envCopy = new Hashtable<>(env);
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    96
        LdapDnsProviderResult result = null;
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    97
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    98
        while (result == null && iterator.hasNext()) {
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
    99
            result = iterator.next().lookupEndpoints(url, envCopy)
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
   100
                    .filter(r -> r.getEndpoints().size() > 0)
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
   101
                    .orElse(null);
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
   102
        }
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
   103
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
   104
        if (result == null) {
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
   105
            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
   106
                .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
   107
        }
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
   108
        return result;
a609d549992a 8160768: Add capability to custom resolve host/domain names within the default JNDI LDAP provider
robm
parents:
diff changeset
   109
    }
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
}