import com.novell.ldap.*;
import com.novell.ldap.controls.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class SearchPersist {
static final String QUIT_PROMPT =
"\nMonitoring changes. Enter a 'q' to quit: ";
public static void main(
String[] args )
{
if (args.length != 4) {
System.err.println(
"Usage: java SearchPersist <host name> <login dn>" +
" <password> <search base>");
System.err.println(
"Example: java SearchPersist Acme.com \"cn=admin,o=Acme\"" +
" secret \"ou=sales,o=Acme\"");
System.exit(0);
}
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
String searchBase = args[3];
LDAPSearchQueue queue = null;
LDAPSearchConstraints constraints;
LDAPPersistSearchControl psCtrl;
LDAPConnection lc = new LDAPConnection();
constraints = new LDAPSearchConstraints();
try {
lc.connect( ldapHost, ldapPort );
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8") );
psCtrl = new LDAPPersistSearchControl(
LDAPPersistSearchControl.ANY,
true,
true,
true);
constraints.setControls( psCtrl );
String[] noAttrs = {LDAPConnection.NO_ATTRS};
queue = lc.search(
searchBase,
LDAPConnection.SCOPE_SUB,
"(objectClass=*)",
noAttrs,
false,
null,
constraints);
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
try { lc.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 {
System.out.print(QUIT_PROMPT);
String input;
while (true) {
if (in.ready()) {
input = in.readLine();
if ( input.startsWith("q") || input.startsWith("Q") )
break;
else
System.out.print(QUIT_PROMPT);
}
if (!checkForAChange(queue))
break;
Thread.currentThread().sleep(10);
}
}
catch (IOException e) {
System.out.println(e.getMessage());
}
catch (InterruptedException e) {
}
try {
lc.abandon(queue);
lc.disconnect();
}
catch( LDAPException e ) {
System.out.println();
System.out.println( "Error: " + e.toString() );
}
System.exit(0);
static private boolean checkForAChange(
LDAPSearchQueue queue)
{
LDAPMessage message;
boolean result = true;
try {
if (queue.isResponseReceived()) {
message = queue.getResponse();
if (message != null) {
if ( message instanceof LDAPSearchResultReference ) {
String urls[] =
((LDAPSearchResultReference)message).getReferrals();
System.out.println("\nSearch result references:");
for ( int i = 0; i < urls.length; i++ )
System.out.println(urls[i]);
System.out.print(QUIT_PROMPT);
}
else if ( message instanceof LDAPSearchResult ) {
LDAPControl[] controls = message.getControls();
for (int i = 0; i < controls.length; i++) {
if (controls[i] instanceof LDAPEntryChangeControl) {
LDAPEntryChangeControl ecCtrl =
(LDAPEntryChangeControl)controls[i];
int changeType = ecCtrl.getChangeType();
System.out.println("\n\nchange type: " +
getChangeTypeString(changeType));
if (changeType ==
LDAPPersistSearchControl.MODDN)
System.out.println("Prev. DN: " +
ecCtrl.getPreviousDN());
if (ecCtrl.getHasChangeNumber())
System.out.println("Change Number: " +
ecCtrl.getChangeNumber());
LDAPEntry entry =
((LDAPSearchResult)message).getEntry();
System.out.println("entry: " + entry.getDN());
System.out.print(QUIT_PROMPT);
}
}
}
else {
LDAPResponse response = (LDAPResponse)message;
int resultCode = response.getResultCode();
if ( resultCode == LDAPException.SUCCESS ) {
System.out.println(
"\nUnexpected success response.");
result = false;
}
else if ( resultCode == LDAPException.REFERRAL ) {
String urls[] =
((LDAPResponse)message).getReferrals();
System.out.println("\n\nReferrals:");
for ( int i = 0; i < urls.length; i++ )
System.out.println(urls[i]);
System.out.print(QUIT_PROMPT);
}
else {
System.out.println("Persistent search failed.");
throw new LDAPException( response.getErrorMessage(),
resultCode,
response.getMatchedDN() );
}
}
}
}
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
result = false;
}
return result;
}
private static 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;
}
}