import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.controls.LDAPPersistSearchControl;
import com.novell.ldap.events.EventConstant;
import com.novell.ldap.events.LDAPEvent;
import com.novell.ldap.events.LDAPExceptionEvent;
import com.novell.ldap.events.PSearchEventListener;
import com.novell.ldap.events.PsearchEventSource;
import com.novell.ldap.events.SearchReferralEvent;
import com.novell.ldap.events.SearchResultEvent;
public class PersistenceSearchCallback {
static final String QUIT_PROMPT =
"\nMonitoring changes. Enter a 'q' to quit: ";
public void execute(
String ldapHost,
int ldapPort,
String loginDN,
String password,
String searchBase) {
int ldapVersion = LDAPConnection.LDAP_V3;
PsearchEventSource source = null;
SearchEventListener listener = null;
LDAPConnection connection = new LDAPConnection();
try {
connection.connect(ldapHost, ldapPort);
connection.bind(
ldapVersion,
loginDN,
password.getBytes("UTF8"));
source = new PsearchEventSource();
String[] noAttrs = { LDAPConnection.NO_ATTRS };
listener = new SearchEventListener();
source.registerforEvent(connection,
searchBase,
LDAPConnection.SCOPE_SUB,
"(objectClass=*)",
noAttrs,
true,
null,
EventConstant.LDAP_PSEARCH_ANY,
true,
listener);
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
if (connection != null)
try {
connection.disconnect();
} catch (LDAPException e2) {
}
System.exit(1);
} catch (UnsupportedEncodingException e) {
System.out.println("Error: " + e.toString());
}
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
try {
String input;
do {
System.out.print(QUIT_PROMPT);
input = in.readLine();
} while (!(input.startsWith("q") || input.startsWith("Q")));
} catch (IOException e) {
System.out.println(e.getMessage());
}
try {
source.removeListener(listener);
connection.disconnect();
} catch (LDAPException e) {
System.out.println();
System.out.println("Error: " + e.toString());
}
}
public static void main(String[] args) {
if (args.length != 4) {
System.err.println(
"Usage: java PersistenceSearchCallback <host name> <login dn>"
+ " <password> <search base>");
System.err.println(
"Example: java PersistenceSearchCallback Acme.com \"cn=admin,o=Acme\""
+ " secret \"ou=sales,o=Acme\"");
System.exit(0);
}
int ldapPort = LDAPConnection.DEFAULT_PORT;
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
String searchBase = args[3];
PersistenceSearchCallback callback =
new PersistenceSearchCallback();
callback.execute(
ldapHost,
ldapPort,
loginDN,
password,
searchBase);
System.exit(0);
}
private String getChangeTypeString(int changeType) {
String changeTypeString;
switch (changeType) {
case LDAPPersistSearchControl.ADD :
changeTypeString = "ADD";
break;
case LDAPPersistSearchControl.MODIFY :
changeTypeString = "MODIFY";
break;
case LDAPPersistSearchControl.MODDN :
changeTypeString = "MODDN";
break;
case LDAPPersistSearchControl.DELETE :
changeTypeString = "DELETE";
break;
default :
changeTypeString =
"Unknown change type: " + String.valueOf(changeType);
break;
}
return changeTypeString;
}
class SearchEventListener implements PSearchEventListener {
public void searchReferalEvent(SearchReferralEvent referalevent) {
String urls[] = referalevent.getUrls();
System.out.println("\nSearch result references:");
for (int i = 0; i < urls.length; i++)
System.out.println(urls[i]);
System.out.print(QUIT_PROMPT);
}
public void searchResultEvent(SearchResultEvent event) {
int changeType = event.getType();
System.out.println(
"\n\nchange type: " + getChangeTypeString(changeType));
LDAPEntry entry = event.getEntry();
System.out.println("entry: " + entry.getDN());
System.out.print(QUIT_PROMPT);
}
public void ldapEventNotification(LDAPEvent evt) {
System.out.println("ldapEventNotification" + evt);
}
public void ldapExceptionNotification(LDAPExceptionEvent ldapevt) {
System.out.println("ldapEventException" + ldapevt);
ldapevt.getLDAPException().printStackTrace();
}
}
}