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.LDAPException;
import com.novell.ldap.LDAPMessage;
import com.novell.ldap.events.LDAPEvent;
import com.novell.ldap.events.LDAPEventListener;
import com.novell.ldap.events.LDAPExceptionEvent;
import com.novell.ldap.events.edir.EdirEventConstant;
import com.novell.ldap.events.edir.EdirEventIntermediateResponse;
import com.novell.ldap.events.edir.EdirEventSource;
import com.novell.ldap.events.edir.EdirEventSpecifier;
import com.novell.ldap.events.edir.EventResponseData;
import com.novell.ldap.events.edir.MonitorEventResponse;
import com.novell.ldap.events.edir.eventdata.ValueEventData;
public class EdirEventsCallback {
static final String QUIT_PROMPT =
"\nMonitoring changes. Enter a 'q' to quit: ";
public void execute(
String ldapHost,
int ldapPort,
String loginDN,
String password) {
int ldapVersion = LDAPConnection.LDAP_V3;
EdirEventSource source = null;
EdirLDAPEventListener listener = null;
LDAPConnection connection = new LDAPConnection();
try {
connection.connect(ldapHost, ldapPort);
connection.bind(
ldapVersion,
loginDN,
password.getBytes("UTF8"));
source = new EdirEventSource();
EdirEventSpecifier specifier[] = new EdirEventSpecifier[2];
listener = new EdirLDAPEventListener();
specifier[0] =
new EdirEventSpecifier(EdirEventConstant.EVT_ADD_VALUE,
EdirEventConstant.EVT_STATUS_ALL);
specifier[1] =
new EdirEventSpecifier(
EdirEventConstant.EVT_ADD_ENTRY,
EdirEventConstant.EVT_STATUS_ALL);
source.registerforEvent(specifier, connection, listener);
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
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 != 3) {
System.err.println(
"Usage: java EdirEventsCallback <host name> <login dn>"
+ " <password> ");
System.err.println(
"Example: java EdirEventsCallback Acme.com \"cn=admin,o=Acme\""
+ " secret ");
System.exit(0);
}
int ldapPort = LDAPConnection.DEFAULT_PORT;
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
EdirEventsCallback callback = new EdirEventsCallback();
callback.execute(ldapHost, ldapPort, loginDN, password);
System.exit(0);
}
class EdirLDAPEventListener implements LDAPEventListener {
public void ldapEventNotification(LDAPEvent evt) {
System.out.println("Edir Event Occured");
LDAPMessage message = evt.getContainedEventInformation();
if (message instanceof EdirEventIntermediateResponse) {
System.out.println("Edir Event Occured");
EdirEventIntermediateResponse eventresponse =
(EdirEventIntermediateResponse) message;
System.out.println(
"Event Type=" + eventresponse.getEventtype());
System.out.println(
"Event Result=" + eventresponse.getEventResult());
processEventData(
eventresponse.getResponsedata(),
eventresponse.getEventtype());
System.out.print(QUIT_PROMPT);
}
else {
System.out.println("UnKnown Message =" + message);
}
}
public void ldapExceptionNotification(LDAPExceptionEvent ldapevt) {
System.out.println("ldapEventException" + ldapevt);
ldapevt.getLDAPException().printStackTrace();
LDAPMessage message = ldapevt.getContainedEventInformation();
if (message instanceof MonitorEventResponse) {
MonitorEventResponse eventerrorresponse =
(MonitorEventResponse) message;
System.out.println(
"\nError in Registration ResultCode="
+ eventerrorresponse.getResultCode());
EdirEventSpecifier specifiers[] =
eventerrorresponse.getSpecifierList();
for (int i = 0; i < specifiers.length; i++) {
System.out.println(
"Specifier: EventClassification="
+ specifiers[i].getEventclassification()
+ "EventType="
+ specifiers[i].getEventtype());
}
System.exit(-1);
}
}
}
static private void processEventData(
EventResponseData data,
int type) {
switch (type) {
case EdirEventConstant.EVT_ADD_VALUE :
ValueEventData valueevent = (ValueEventData) data;
System.out.println("Entry =" + valueevent.getEntry());
System.out.println(
"attribute =" + valueevent.getAttribute());
System.out.println("Class id =" + valueevent.getClassid());
System.out.println("Data=" + valueevent.getData());
System.out.println(
"prepetratorDN=" + valueevent.getPrepetratorDN());
System.out.println("Syntax=" + valueevent.getSyntax());
System.out.println(
"timeStamp=" + valueevent.getTimeStamp());
break;
default :
break;
}
}
}