Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.naming/share/classes/com/sun/jndi/ldap/LdapDnsProviderService.java
41161 views
1
/*
2
* Copyright (c) 2018, 2021, 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
* <p> Instances of this class are safe for use by multiple threads.
42
*/
43
final class LdapDnsProviderService {
44
45
private static volatile LdapDnsProviderService service;
46
private static final Object LOCK = new int[0];
47
private final ServiceLoader<LdapDnsProvider> providers;
48
49
/**
50
* Creates a new instance of LdapDnsProviderService
51
*/
52
@SuppressWarnings("removal")
53
private LdapDnsProviderService() {
54
SecurityManager sm = System.getSecurityManager();
55
if (sm == null) {
56
providers = ServiceLoader.load(
57
LdapDnsProvider.class,
58
ClassLoader.getSystemClassLoader());
59
} else {
60
final PrivilegedAction<ServiceLoader<LdapDnsProvider>> pa =
61
() -> ServiceLoader.load(
62
LdapDnsProvider.class,
63
ClassLoader.getSystemClassLoader());
64
65
providers = AccessController.doPrivileged(
66
pa,
67
null,
68
new RuntimePermission("ldapDnsProvider"),
69
SecurityConstants.GET_CLASSLOADER_PERMISSION);
70
}
71
}
72
73
/**
74
* Retrieves the singleton instance of LdapDnsProviderService.
75
*/
76
static LdapDnsProviderService getInstance() {
77
if (service != null) return service;
78
synchronized (LOCK) {
79
if (service != null) return service;
80
service = new LdapDnsProviderService();
81
}
82
return service;
83
}
84
85
/**
86
* Retrieves result from the first provider that successfully resolves
87
* the endpoints. If no results are found when calling installed
88
* subclasses of {@code LdapDnsProvider} then this method will fall back
89
* to the {@code DefaultLdapDnsProvider}.
90
*
91
* @throws NamingException if the {@code url} is not valid or an error
92
* occurred while performing the lookup.
93
*/
94
LdapDnsProviderResult lookupEndpoints(String url, Hashtable<?,?> env)
95
throws NamingException
96
{
97
LdapDnsProviderResult result = null;
98
Hashtable<?, ?> envCopy = new Hashtable<>(env);
99
synchronized (LOCK) {
100
Iterator<LdapDnsProvider> iterator = providers.iterator();
101
while (result == null && iterator.hasNext()) {
102
result = iterator.next().lookupEndpoints(url, envCopy)
103
.filter(r -> !r.getEndpoints().isEmpty())
104
.orElse(null);
105
}
106
}
107
108
if (result == null) {
109
return new DefaultLdapDnsProvider().lookupEndpoints(url, env)
110
.orElse(new LdapDnsProviderResult("", List.of()));
111
}
112
return result;
113
}
114
}
115
116