The task is to establish a connection to an Active Directory or Lightweight Directory Access Protocol server.

/* NetRexx */
options replace format comments java crossref symbols binary
 
import org.apache.directory.ldap.client.api.LdapConnection
import org.apache.directory.ldap.client.api.LdapNetworkConnection
import org.apache.directory.shared.ldap.model.exception.LdapException
import org.slf4j.Logger
import org.slf4j.LoggerFactory
 
class RDirectoryLDAP public
 
  properties constant
    log_ = LoggerFactory.getLogger(RDirectoryLDAP.class)
 
  properties private static
    connection = LdapConnection null
 
  method main(args = String[]) public static
    ldapHostName = String "localhost"
    ldapPort = int 10389
 
    if log_.isInfoEnabled() then log_.info("LDAP Connection to" ldapHostName "on port" ldapPort)
    connection = LdapNetworkConnection(ldapHostName, ldapPort)
 
    do
      if log_.isTraceEnabled() then log_.trace("LDAP bind")
      connection.bind()
 
      if log_.isTraceEnabled() then log_.trace("LDAP unbind")
      connection.unBind()
    catch lex = LdapException
      log_.error("LDAP Error", Throwable lex)
    catch iox = IOException
      log_.error("I/O Error", Throwable iox)
    finally
      do
      if connection \= null then connection.close()
      catch iox = IOException
        log_.error("I/O Error on connection.close()", Throwable iox)
      end
    end
 
    return
 