-
Notifications
You must be signed in to change notification settings - Fork 488
Rework resolving the default for Context.INITIAL_CONTEXT_FACTORY #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,10 +119,16 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource | |
|
||
public static final String SUN_LDAP_POOLING_FLAG = "com.sun.jndi.ldap.connect.pool"; | ||
|
||
private static final String JDK_142 = "1.4.2"; | ||
|
||
private DirContextAuthenticationStrategy authenticationStrategy = new SimpleDirContextAuthenticationStrategy(); | ||
|
||
public AbstractContextSource() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be moved into For example, in if (this.contextFactory == null) {
try {
this.contextFactory = Class.forName(DEFAULT_CONTEXT_FACTORY);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find " + DEFAULT_CONTEXT_FACTORY + " and no context factory specified");
}
} One thing that's nice about this is that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my original idea, yes. However, there will be a slight behavioral change compared to 2.3.3.
whereas with the change applied
Please let me know if that is acceptable and I will update the PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it alleviate your concern if the PR also changed to run this logic in a private getter? For example, you could add: private Class<?> ensureContextFactoryName() {
if (this.contextFactory != null) {
return this.contextFactory.getName();
}
try {
this.contextFactory = Class.forName(DEFAULT_CONTEXT_FACTORY);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find " + DEFAULT_CONTEXT_FACTORY + " and no context factory specified");
}
return this.contextFactory.getName();
} Then, in ensureContextFactoryName(); And in env.put(Context.INITIAL_CONTEXT_FACTORY, ensureContextFactoryName()); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just initialize the private static final Class<?> DEFAULT_CONTEXT_FACTORY;
static {
try {
DEFAULT_CONTEXT_FACTORY = Class.forName("com.sun.jndi.ldap.LdapCtxFactory");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's problematic since it errors on something that may not be a problem. The class not being present is only a problem if the application does not set a context factory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jzheaux The public class MainClass {
private static final Class<?> DEFAULT_CONTEXT_FACTORY;
static {
try {
DEFAULT_CONTEXT_FACTORY = Class.forName("com.sun.jndi.ldap.LdapCtxFactory");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
System.out.println(DEFAULT_CONTEXT_FACTORY.getName());
}
} Value of the
With my version of change all of the above will continue to work properly and no behavior change will be introduced. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. It's interesting that |
||
try { | ||
this.contextFactory = Class.forName(DEFAULT_CONTEXT_FACTORY); | ||
} catch (ClassNotFoundException e) { | ||
LOG.trace("The default for contextFactory cannot be resolved", e); | ||
} | ||
} | ||
|
||
public DirContext getContext(String principal, String credentials) { | ||
// This method is typically called for authentication purposes, which means that we | ||
// should explicitly disable pooling in case passwords are changed (LDAP-183). | ||
|
@@ -407,7 +413,9 @@ public void afterPropertiesSet() { | |
if (ObjectUtils.isEmpty(urls)) { | ||
throw new IllegalArgumentException("At least one server url must be set"); | ||
} | ||
|
||
if (contextFactory == null) { | ||
throw new IllegalArgumentException("contextFactory must be set"); | ||
} | ||
if (authenticationSource == null) { | ||
LOG.debug("AuthenticationSource not set - " + "using default implementation"); | ||
if (!StringUtils.hasText(userDn)) { | ||
|
@@ -438,7 +446,7 @@ private Hashtable<String, Object> setupAnonymousEnv() { | |
|
||
Hashtable<String, Object> env = new Hashtable<String, Object>(baseEnv); | ||
|
||
env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory != null ? contextFactory.getName() : DEFAULT_CONTEXT_FACTORY); | ||
env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory.getName()); | ||
env.put(Context.PROVIDER_URL, assembleProviderUrlString(urls)); | ||
|
||
if (dirObjectFactory != null) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.