Tomcat + LDAP connection as Global Resource












0















I have Tomcat 7 + many webapps (webservices). All webapps needed search in LDAP directory. I configured Resource in conf/context.xml with any parameters and <resource-ref> in conf/web.xml.
In my webapps i have my LdapContextFactory class with com.sun.jndi.ldap.connect.pool definition.
Everything is ok, but ... when LDAP server is restarted, connection is lost and I don't now how configure automatic reconnecting (when possible, something like JDBC)
Thank's for help and sorry for my English



context.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



public class LdapContextFactory implements 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;
}









share|improve this question



























    0















    I have Tomcat 7 + many webapps (webservices). All webapps needed search in LDAP directory. I configured Resource in conf/context.xml with any parameters and <resource-ref> in conf/web.xml.
    In my webapps i have my LdapContextFactory class with com.sun.jndi.ldap.connect.pool definition.
    Everything is ok, but ... when LDAP server is restarted, connection is lost and I don't now how configure automatic reconnecting (when possible, something like JDBC)
    Thank's for help and sorry for my English



    context.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



    public class LdapContextFactory implements 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;
    }









    share|improve this question

























      0












      0








      0








      I have Tomcat 7 + many webapps (webservices). All webapps needed search in LDAP directory. I configured Resource in conf/context.xml with any parameters and <resource-ref> in conf/web.xml.
      In my webapps i have my LdapContextFactory class with com.sun.jndi.ldap.connect.pool definition.
      Everything is ok, but ... when LDAP server is restarted, connection is lost and I don't now how configure automatic reconnecting (when possible, something like JDBC)
      Thank's for help and sorry for my English



      context.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



      public class LdapContextFactory implements 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;
      }









      share|improve this question














      I have Tomcat 7 + many webapps (webservices). All webapps needed search in LDAP directory. I configured Resource in conf/context.xml with any parameters and <resource-ref> in conf/web.xml.
      In my webapps i have my LdapContextFactory class with com.sun.jndi.ldap.connect.pool definition.
      Everything is ok, but ... when LDAP server is restarted, connection is lost and I don't now how configure automatic reconnecting (when possible, something like JDBC)
      Thank's for help and sorry for my English



      context.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



      public class LdapContextFactory implements 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;
      }






      java ldap tomcat7






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 14:31









      IgorIgor

      513




      513
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395249%2ftomcat-ldap-connection-as-global-resource%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395249%2ftomcat-ldap-connection-as-global-resource%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?