public void setSocketFactory(final LDAPSocketFactory socketFactory) {
    this.socketFactory = socketFactory;

    if (socketFactory == null) {
      conn.setSocketFactory(null);
    } else {
      conn.setSocketFactory(new LDAPToJavaSocketFactory(socketFactory));
    }
  }
  public void setConnectTimeout(final int timeout) {
    final LDAPConnectionOptions options = conn.getConnectionOptions();

    if (timeout > 0) {
      options.setConnectTimeoutMillis(1000 * timeout);
    } else {
      options.setConnectTimeoutMillis(0);
    }

    conn.setConnectionOptions(options);
  }
  public LDAPEntry read(
      final String dn, final String[] attrs, final LDAPSearchConstraints constraints)
      throws LDAPException {
    final Filter filter =
        Filter.createORFilter(
            Filter.createPresenceFilter("objectClass"),
            Filter.createEqualityFilter("objectClass", "ldapSubentry"));

    final SearchRequest searchRequest = new SearchRequest(dn, SearchScope.BASE, filter, attrs);
    update(searchRequest, constraints);

    try {
      final SearchResult searchResult = conn.search(searchRequest);
      setResponseControls(searchResult);

      if (searchResult.getEntryCount() != 1) {
        throw new LDAPException(null, LDAPException.NO_RESULTS_RETURNED);
      }

      return new LDAPEntry(searchResult.getSearchEntries().get(0));
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
  public LDAPSearchResults search(
      final String baseDN,
      final int scope,
      final String filter,
      final String[] attributes,
      final boolean typesOnly,
      final LDAPSearchConstraints constraints)
      throws LDAPException {
    final LDAPSearchResults results;
    final LDAPSearchConstraints c = (constraints == null) ? searchConstraints : constraints;
    results = new LDAPSearchResults(c.getTimeLimit());

    try {
      final SearchRequest searchRequest =
          new SearchRequest(results, baseDN, SearchScope.valueOf(scope), filter, attributes);

      searchRequest.setDerefPolicy(DereferencePolicy.valueOf(c.getDereference()));
      searchRequest.setSizeLimit(c.getMaxResults());
      searchRequest.setTimeLimitSeconds(c.getServerTimeLimit());
      searchRequest.setTypesOnly(typesOnly);

      update(searchRequest, constraints);

      conn.asyncSearch(searchRequest);
      return results;
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
 public void abandon(final int id) throws LDAPException {
   try {
     conn.abandon(InternalSDKHelper.createAsyncRequestID(id, conn), getControls(null));
   } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
     debugException(le);
     throw new LDAPException(le);
   }
 }
 public int getConnectTimeout() {
   final int connectTimeoutMillis = conn.getConnectionOptions().getConnectTimeoutMillis();
   if (connectTimeoutMillis > 0) {
     return Math.max(1, (connectTimeoutMillis / 1000));
   } else {
     return 0;
   }
 }
  public void connect(final String host, final int port) throws LDAPException {
    authDN = null;
    authPW = null;
    responseControls = null;

    try {
      conn.connect(host, port);
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      throw new LDAPException(le);
    }
  }
  public void add(final LDAPEntry entry, final LDAPConstraints constraints) throws LDAPException {
    final AddRequest addRequest = new AddRequest(entry.toEntry());
    update(addRequest, constraints);

    try {
      final LDAPResult result = conn.add(addRequest);
      setResponseControls(result);
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
  public void delete(final String dn, final LDAPConstraints constraints) throws LDAPException {
    final DeleteRequest deleteRequest = new DeleteRequest(dn);
    update(deleteRequest, constraints);

    try {
      final LDAPResult result = conn.delete(deleteRequest);
      setResponseControls(result);
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
  public void reconnect() throws LDAPException {
    final String host = getHost();
    final int port = getPort();
    final String dn = authDN;
    final String pw = authPW;

    conn.close();

    if ((dn == null) || (pw == null)) {
      connect(host, port);
    } else {
      connect(host, port, dn, pw);
    }
  }
  public boolean compare(
      final String dn, final LDAPAttribute attribute, final LDAPConstraints constraints)
      throws LDAPException {
    final CompareRequest compareRequest =
        new CompareRequest(dn, attribute.getName(), attribute.getByteValueArray()[0]);
    update(compareRequest, constraints);

    try {
      final CompareResult result = conn.compare(compareRequest);
      setResponseControls(result);
      return result.compareMatched();
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
  public void connect(
      final int version,
      final String host,
      final int port,
      final String dn,
      final String password,
      final LDAPConstraints constraints)
      throws LDAPException {
    connect(host, port);

    try {
      if ((dn != null) && (password != null)) {
        bind(version, dn, password, constraints);
      }
    } catch (LDAPException le) {
      conn.close();
      throw le;
    }
  }
  public void rename(
      final String dn,
      final String newRDN,
      final String newParentDN,
      final boolean deleteOldRDN,
      final LDAPConstraints constraints)
      throws LDAPException {
    final ModifyDNRequest modifyDNRequest =
        new ModifyDNRequest(dn, newRDN, deleteOldRDN, newParentDN);
    update(modifyDNRequest, constraints);

    try {
      final LDAPResult result = conn.modifyDN(modifyDNRequest);
      setResponseControls(result);
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
  public void modify(
      final String dn, final LDAPModification[] mods, final LDAPConstraints constraints)
      throws LDAPException {
    final Modification[] m = new Modification[mods.length];
    for (int i = 0; i < mods.length; i++) {
      m[i] = mods[i].toModification();
    }

    final ModifyRequest modifyRequest = new ModifyRequest(dn, m);
    update(modifyRequest, constraints);

    try {
      final LDAPResult result = conn.modify(modifyRequest);
      setResponseControls(result);
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
  public void bind(
      final int version, final String dn, final String password, final LDAPConstraints constraints)
      throws LDAPException {
    final SimpleBindRequest bindRequest =
        new SimpleBindRequest(dn, password, getControls(constraints));
    authDN = null;
    authPW = null;

    try {
      final BindResult bindResult = conn.bind(bindRequest);
      setResponseControls(bindResult);
      if (bindResult.getResultCode() == ResultCode.SUCCESS) {
        authDN = dn;
        authPW = password;
      }
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
  public LDAPExtendedOperation extendedOperation(
      final LDAPExtendedOperation extendedOperation, final LDAPConstraints constraints)
      throws LDAPException {
    final ExtendedRequest extendedRequest =
        new ExtendedRequest(
            extendedOperation.getID(),
            new ASN1OctetString(extendedOperation.getValue()),
            getControls(constraints));

    try {
      final ExtendedResult result = conn.processExtendedOperation(extendedRequest);
      setResponseControls(result);

      if (result.getResultCode() != ResultCode.SUCCESS) {
        throw new LDAPException(
            result.getDiagnosticMessage(),
            result.getResultCode().intValue(),
            result.getDiagnosticMessage(),
            result.getMatchedDN());
      }

      final byte[] valueBytes;
      final ASN1OctetString value = result.getValue();
      if (value == null) {
        valueBytes = null;
      } else {
        valueBytes = value.getValue();
      }

      return new LDAPExtendedOperation(result.getOID(), valueBytes);
    } catch (com.hwlcn.ldap.ldap.sdk.LDAPException le) {
      debugException(le);
      setResponseControls(le);
      throw new LDAPException(le);
    }
  }
 public boolean isConnected() {
   return conn.isConnected();
 }
  @Override()
  protected void finalize() throws Throwable {
    conn.close();

    super.finalize();
  }
 public void disconnect() throws LDAPException {
   conn.close();
   authDN = null;
   authPW = null;
 }
 public String getHost() {
   return conn.getConnectedAddress();
 }
 public int getPort() {
   return conn.getConnectedPort();
 }