Tool Class 2 for encapsulating JNDI operations on LDAP Server

Source: Internet
Author: User
Tags ldap
  1. PackageCom. Common. ldapconnection;
  2. ImportJava. util. properties;
  3. ImportJavax. Naming. context;
  4. ImportJavax. Naming. namingexception;
  5. ImportJavax. Naming. Directory. dircontext;
  6. ImportJavax. Naming. Directory. initialdircontext;
  7. ImportOrg. Apache. log4j. Logger;
  8. ImportOrg. Apache. commons. Lang. stringutils;
  9. /**
  10. * Function Description: connects to the LDAP factory and provides methods to initialize the LDAP connection.
  11. * @ Author liaowufeng
  12. * @ Version 1.0
  13. */
  14. Public ClassLdapconnectionfactory {
  15. // Initialize the log processing class
  16. StaticLogger log = logger. getlogger (ldapconnectionfactory.Class. Getname ());
  17. /**
  18. * The constructor is private to prevent instantiation.
  19. */
  20. PrivateLdapconnectionfactory (){
  21. }
  22. /**
  23. * Obtain a connection from the LDAP connection pool
  24. * @ Param poolname string connection pool name
  25. * @ Env connection LDAP connection information
  26. * @ Return dircontext
  27. */
  28. Public StaticDircontext getdircontext (string poolname, ENV env ){
  29. // Obtain the context
  30. Try{
  31. // Log printing indicates entering this method
  32. Log. debug (constants. Warn method + "getdircontext ");
  33. // Initialize the properties object
  34. Properties menv =NewProperties ();
  35. // Use the ldap/Ad authentication method
  36. Menv. Put (context. authoritative, "true ");
  37. // Use the connection pool
  38. Menv. Put ("com. Sun. JNDI. LDAP. Connect. Pool", "true ");
  39. // Set the connection factory for ldap/AD
  40. Menv. Put (context. initial_context_factory, ENV. Factory );
  41. // Set the URL of LDAP/AD
  42. Menv. Put (context. provider_url, ENV. url );
  43. // Set the connection timeout
  44. If(! Stringutils. isempty (Env. Timeout )){
  45. Menv. Put ("com. Sun. JNDI. LDAP. Connect. Timeout", ENV. Timeout );
  46. }
  47. // Set the security mode to simple
  48. Menv. Put (context. security_authentication, "simple ");
  49. // SSL Channel Access
  50. If(ENV! =Null& "SSL". Equals (Env. securityprotocol )){
  51. // Set the access protocol to SSL
  52. Menv. Put (context. security_protocol, ENV. securityprotocol );
  53. // Set the access certificate attribute. If this certificate is not available, you cannot access the Active Directory through SSL.
  54. System. setproperty ("javax.net. SSL. truststore ",
  55. Env. ssltruststore );
  56. }
  57. // Read the LDAP account and password
  58. Menv. Put (context. security_principal, ENV. adminuid );
  59. Menv. Put (context. security_credentials, ENV. adminpwd );
  60. Log. debug (constants. init + "dircontext ");
  61. // Connect to LDAP/ad through Parameters
  62. Dircontext CTX =NewInitialdircontext (menv );
  63. Log. debug ("ldapconnectionfactory:" + constants. init + "dircontext" +
  64. Constants. Success );
  65. ReturnCTX;
  66. }
  67. Catch(Namingexception ex ){
  68. Ex. printstacktrace ();
  69. Log. Error ("ldapconnectionfactory:" + constants. init + "dircontext" +
  70. Constants. Fail );
  71. Log. Error (ex. getmessage ());
  72. Return Null;
  73. }
  74. }
  75. /**
  76. * Close the LDAP connection
  77. * @ Param dircontext the LDAP context instance connected to dircontext
  78. */
  79. Public Static VoidClosedircontext (dircontext ){
  80. Try{
  81. If(Dircontext! =Null)
  82. Dircontext. Close ();
  83. }
  84. Catch(Exception ex ){
  85. Ex. printstacktrace ();
  86. Log. Error ("not close dircontext ");
  87. }
  88. }
  89. /**
  90. * Obtain the connection method of the LDAP server
  91. * @ Param env connection to LDAP connection information
  92. * @ Return dircontext-LDAP server connection
  93. */
  94. Public StaticDircontext getdircontext (ENV env ){
  95. Try{
  96. // Initialize the properties object
  97. Properties menv =NewProperties ();
  98. // Use the ldap/Ad authentication method
  99. Menv. Put (context. authoritative, "true ");
  100. // Set the connection factory for ldap/AD
  101. Menv. Put (context. initial_context_factory, ENV. Factory );
  102. // Set the URL of LDAP/AD
  103. Menv. Put (context. provider_url, ENV. url );
  104. // Set the connection timeout
  105. If(! Stringutils. isempty (Env. Timeout )){
  106. Menv. Put ("com. Sun. JNDI. LDAP. Connect. Timeout", ENV. Timeout );
  107. }
  108. // Set the security mode to simple
  109. Menv. Put (context. security_authentication, "simple ");
  110. // SSL Channel Access
  111. If(ENV! =Null& "SSL". Equals (Env. securityprotocol )){
  112. // Set the access protocol to SSL
  113. Menv. Put (context. security_protocol, ENV. securityprotocol );
  114. // Set the access certificate attribute. If this certificate is not available, you cannot access the Active Directory through SSL.
  115. System. setproperty ("javax.net. SSL. truststore ",
  116. Env. ssltruststore );
  117. }
  118. // Read the LDAP account and password
  119. Menv. Put (context. security_principal, ENV. adminuid );
  120. Menv. Put (context. security_credentials, ENV. adminpwd );
  121. Log. debug (constants. init + "dircontext ");
  122. // Connect to LDAP/ad through Parameters
  123. Dircontext CTX =NewInitialdircontext (menv );
  124. Log. debug ("ldapconnectionfactory:" + constants. init + "dircontext" +
  125. Constants. Success );
  126. ReturnCTX;
  127. }
  128. Catch(Namingexception ex ){
  129. Ex. printstacktrace ();
  130. Log. Error ("ldapconnectionfactory:" + constants. init + "dircontext" +
  131. Constants. Fail );
  132. Log. Error (ex. getmessage ());
  133. Return Null;
  134. }
  135. }
  136. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.