1 /* |
1 /* |
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
35 |
35 |
36 /** |
36 /** |
37 * The {@code LdapDnsProviderService} is responsible for creating and providing |
37 * The {@code LdapDnsProviderService} is responsible for creating and providing |
38 * access to the registered {@code LdapDnsProvider}s. The {@link ServiceLoader} |
38 * access to the registered {@code LdapDnsProvider}s. The {@link ServiceLoader} |
39 * is used to find and register any implementations of {@link LdapDnsProvider}. |
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. |
40 */ |
42 */ |
41 final class LdapDnsProviderService { |
43 final class LdapDnsProviderService { |
42 |
44 |
43 private static volatile LdapDnsProviderService service; |
45 private static volatile LdapDnsProviderService service; |
44 private static final Object LOCK = new int[0]; |
46 private static final Object LOCK = new int[0]; |
66 SecurityConstants.GET_CLASSLOADER_PERMISSION); |
68 SecurityConstants.GET_CLASSLOADER_PERMISSION); |
67 } |
69 } |
68 } |
70 } |
69 |
71 |
70 /** |
72 /** |
71 * Retrieve the singleton static instance of LdapDnsProviderService. |
73 * Retrieves the singleton instance of LdapDnsProviderService. |
72 */ |
74 */ |
73 static LdapDnsProviderService getInstance() { |
75 static LdapDnsProviderService getInstance() { |
74 if (service != null) return service; |
76 if (service != null) return service; |
75 synchronized(LOCK) { |
77 synchronized (LOCK) { |
76 if (service != null) return service; |
78 if (service != null) return service; |
77 service = new LdapDnsProviderService(); |
79 service = new LdapDnsProviderService(); |
78 } |
80 } |
79 return service; |
81 return service; |
80 } |
82 } |
81 |
83 |
82 /** |
84 /** |
83 * Retrieve result from the first provider that successfully resolves |
85 * Retrieves result from the first provider that successfully resolves |
84 * the endpoints. If no results are found when calling installed |
86 * the endpoints. If no results are found when calling installed |
85 * subclasses of {@code LdapDnsProvider} then this method will fall back |
87 * subclasses of {@code LdapDnsProvider} then this method will fall back |
86 * to the {@code DefaultLdapDnsProvider}. |
88 * to the {@code DefaultLdapDnsProvider}. |
87 * |
89 * |
88 * @throws NamingException if the {@code url} in not valid or an error |
90 * @throws NamingException if the {@code url} in not valid or an error |
89 * occurred while performing the lookup. |
91 * occurred while performing the lookup. |
90 */ |
92 */ |
91 LdapDnsProviderResult lookupEndpoints(String url, Hashtable<?,?> env) |
93 LdapDnsProviderResult lookupEndpoints(String url, Hashtable<?,?> env) |
92 throws NamingException |
94 throws NamingException |
93 { |
95 { |
94 Iterator<LdapDnsProvider> iterator = providers.iterator(); |
96 LdapDnsProviderResult result = null; |
95 Hashtable<?, ?> envCopy = new Hashtable<>(env); |
97 Hashtable<?, ?> envCopy = new Hashtable<>(env); |
96 LdapDnsProviderResult result = null; |
98 synchronized (LOCK) { |
97 |
99 Iterator<LdapDnsProvider> iterator = providers.iterator(); |
98 while (result == null && iterator.hasNext()) { |
100 while (result == null && iterator.hasNext()) { |
99 result = iterator.next().lookupEndpoints(url, envCopy) |
101 result = iterator.next().lookupEndpoints(url, envCopy) |
100 .filter(r -> r.getEndpoints().size() > 0) |
102 .filter(r -> !r.getEndpoints().isEmpty()) |
101 .orElse(null); |
103 .orElse(null); |
|
104 } |
102 } |
105 } |
103 |
106 |
104 if (result == null) { |
107 if (result == null) { |
105 return new DefaultLdapDnsProvider().lookupEndpoints(url, env) |
108 return new DefaultLdapDnsProvider().lookupEndpoints(url, env) |
106 .orElse(new LdapDnsProviderResult("", List.of())); |
109 .orElse(new LdapDnsProviderResult("", List.of())); |