У меня есть Tomcat 7 + много веб-приложений (веб-сервисов).Все веб-приложения нуждались в поиске в каталоге LDAP.Я настроил Resource
в conf/context.xml
с любыми параметрами и <resource-ref>
в conf/web.xml
.В моих веб-приложениях у меня есть класс LdapContextFactory
с определением com.sun.jndi.ldap.connect.pool
.Все в порядке, но ... когда сервер LDAP перезапускается, соединение теряется, и я не могу сейчас настроить автоматическое переподключение (когда это возможно, что-то вроде JDBC). Спасибо за помощь и извините за мой английский
контекст..xml
<Resource name="ldap/LdapResource" auth="Container"
type="javax.naming.ldap.LdapContext"
factory="cz.XXX.ws.test.LdapContextFactory"
singleton="false"
java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
java.naming.provider.url="ldaps://ldap1.mydomain.cz:636"
java.naming.security.authentication="simple"
java.naming.security.protocol="ssl"
java.naming.security.principal="cn=XXXXX, ou=AAAAA,o=BBBB"
java.naming.security.credentials="YYYYYYYY" />
web.xml
<resource-ref>
<description>LDAP Connection common</description>
<res-ref-name>ldap/LdapResource</res-ref-name>
<res-type>javax.naming.ldap.DirContext</res-type>
<res-auth>Container</res-auth>
</resource-ref>
LdapContextFactory.java
открытый класс LdapContextFactory реализует ObjectFactory {
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
Hashtable<Object, Object> env = new Hashtable<Object, Object>();
Reference reference = (Reference) obj;
Enumeration<RefAddr> references = reference.getAll();
while (references.hasMoreElements()) {
RefAddr address = references.nextElement();
String type = address.getType();
String content = (String) address.getContent();
switch (type) {
case Context.INITIAL_CONTEXT_FACTORY:
env.put(Context.INITIAL_CONTEXT_FACTORY, content);
break;
case Context.PROVIDER_URL:
env.put(Context.PROVIDER_URL, content);
break;
case Context.SECURITY_AUTHENTICATION:
env.put(Context.SECURITY_AUTHENTICATION, content);
break;
case Context.SECURITY_PROTOCOL:
env.put(Context.SECURITY_PROTOCOL, content);
break;
case Context.SECURITY_PRINCIPAL:
env.put(Context.SECURITY_PRINCIPAL, content);
break;
case Context.SECURITY_CREDENTIALS:
env.put(Context.SECURITY_CREDENTIALS, content);
break;
default:
break;
}
}
env.put("com.sun.jndi.ldap.connect.pool", "true");
try {
_manageTrustStores();
} catch (Exception ex) {
throw new Exception(StatusEnum.LDAP_ERROR);
}
DirContext context = new InitialDirContext(env);
return context;
}