[Date Prev][Date Next] [Chronological] [Thread] [Top]

Fw: Looking for java code to connect to LDAP



might not be the best of implementation but here ya go.....for the benefit of the rest
 
----- Original Message -----
From: Sivasakthi
Sent: Thursday, November 04, 2004 9:58 AM
Subject: Looking for java code to connect to LDAP

my program basically reads from an LDIF file and adds into the openldap
 
Here is the relevant code that u've inquired for..........if u have other solutions appreciate if you could share it with me too.
 
i have to call the DOS CMD program and run the openldap command.
sample:-
public int mSendToOpenLDAP(String cmd) {
    dcOpenLDAPAStream errorGobbler;
    dcOpenLDAPAStream outputGobbler;
      Runtime rt = Runtime.getRuntime();
      Process proc = rt.exec(cmd);
      // any error message?
      errorGobbler = new
          dcOpenLDAPAStream(proc.getErrorStream(), "ERR");
      // any output?
      outputGobbler = new
          dcOpenLDAPAStream(proc.getInputStream(), "OUT");
      // kick them off
      errorGobbler.start();
      outputGobbler.start();
      // any error???
      if (proc.waitFor() > 0) {
        vecError = errorGobbler.sTV;
      }

      return (proc.waitFor());

    }
the string cmd goes something like this : c:\openldap\ldapadd -x -D <DN> -w <password> -f <file>
 
dcOpenLDAPAStream looks like this:-
import java.io.*;
import java.util.*;
 
public class dcOpenLDAPAStream
    extends Thread {
  InputStream iStream;
  String StreamType;
  OutputStream oStream;
  Vector sTV = new Vector();
 
  dcOpenLDAPAStream(InputStream iStreamInput, String sStreamType) {
    this(iStreamInput, sStreamType, null);
  }
 
  dcOpenLDAPAStream(InputStream iStreamInput, String sStreamType,
                    OutputStream redirect) {
    this.iStream = iStreamInput;
    this.StreamType = sStreamType;
    this.oStream = redirect;
  }
 
  public void run() {
    try {
      PrintWriter pw = null;
      if (oStream != null)
        pw = new PrintWriter(oStream);
 
      InputStreamReader isr = new InputStreamReader(iStream);
      BufferedReader br = new BufferedReader(isr);
      String line = null;
      while ( (line = br.readLine()) != null) {
        if (pw != null) {
          pw.println(line);
        }
        if (StreamType.equalsIgnoreCase("ERR"))
          sTV.add(new String(line));
          // System.out.println("[PROCESS] " + StreamType + ">" + line);
 
      }
      if (pw != null)
        pw.flush();
    }
    catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}