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