Skip to content

Commit 0f30b05

Browse files
Ihor Herasymenkojzheaux
Ihor Herasymenko
authored andcommitted
Rework Context.INITIAL_CONTEXT_FACTORY Default
With this approach in place, one will be able to specify a different implementation using the setContextFactory method should `com.sun.jndi.ldap.LdapCtxFactory` get removed one day (since this class is not exported by the `java.naming` modules in JDK 9+ and is an implementation detail). Closes gh-579
1 parent cef377b commit 0f30b05

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2020 the original author or authors.
2+
* Copyright 2005-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -123,6 +123,14 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource
123123

124124
private DirContextAuthenticationStrategy authenticationStrategy = new SimpleDirContextAuthenticationStrategy();
125125

126+
public AbstractContextSource() {
127+
try {
128+
contextFactory = Class.forName(DEFAULT_CONTEXT_FACTORY);
129+
} catch (ClassNotFoundException e) {
130+
LOG.trace("The default for contextFactory cannot be resolved", e);
131+
}
132+
}
133+
126134
public DirContext getContext(String principal, String credentials) {
127135
// This method is typically called for authentication purposes, which means that we
128136
// should explicitly disable pooling in case passwords are changed (LDAP-183).
@@ -407,7 +415,9 @@ public void afterPropertiesSet() {
407415
if (ObjectUtils.isEmpty(urls)) {
408416
throw new IllegalArgumentException("At least one server url must be set");
409417
}
410-
418+
if (contextFactory == null) {
419+
throw new IllegalArgumentException("contextFactory must be set");
420+
}
411421
if (authenticationSource == null) {
412422
LOG.debug("AuthenticationSource not set - " + "using default implementation");
413423
if (!StringUtils.hasText(userDn)) {
@@ -438,7 +448,7 @@ private Hashtable<String, Object> setupAnonymousEnv() {
438448

439449
Hashtable<String, Object> env = new Hashtable<String, Object>(baseEnv);
440450

441-
env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory != null ? contextFactory.getName() : DEFAULT_CONTEXT_FACTORY);
451+
env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory.getName());
442452
env.put(Context.PROVIDER_URL, assembleProviderUrlString(urls));
443453

444454
if (dirObjectFactory != null) {

0 commit comments

Comments
 (0)