I'm trying to write a program to tell me whenever an add or
delete event
occurs. The enclosed code (with the run output following)
works for a
delete event but gives me a ClassCastException on add
events. It seems
that the error thinks I sould be casting my data to
GeneralDSEventData
instead of EntryEventData. When I do that, I don't get the
error, but I
also don't get the information I want, namely the entry
information to
determine which entry was added. Shouldn't this work with
the
EntryEventData class like delete does?
Anyway, any help would be greatly appreciated.
Thanks,
##### Code begins below (modified from EdirEventSample.java
#####
package edu.ukyunit;
* $Novell: EdirEventSample.java,v 1.2 2004/05/14 17:32:24 $
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPExtendedRequest;
import com.novell.ldap.LDAPMessage;
import com.novell.ldap.LDAPResponseQueue;
import com.novell.ldap.events.edir.EdirEventConstant;
import
com.novell.ldap.events.edir.EdirEventIntermediateResponse;
import com.novell.ldap.events.edir.EdirEventSpecifier;
import com.novell.ldap.events.edir.EventResponseData;
import com.novell.ldap.events.edir.MonitorEventRequest;
import com.novell.ldap.events.edir.MonitorEventResponse;
import com.novell.ldap.events.edir.eventdata.EntryEventData;
public class EdirEventListener {
static final String QUIT_PROMPT = "\nMonitoring
changes. Enter a 'q' to
quit: ";
/**
* Check the queue for a response. If a response has
been received,
* print the response information.
*/
static private boolean checkForAChange(LDAPResponseQueue
queue) {
LDAPMessage message;
boolean result = true;
try {
//check if a response has been received so we
don't block
//when calling getResponse()
if (queue.isResponseReceived()) {
message = queue.getResponse();
if (message != null) {
// is the response a search result
reference? This is
an event registration failure
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);
}
// is the response a event response? If
so, lets find
out what kind of event we had.
else if (message instanceof
EdirEventIntermediateResponse) {
System.out.println("\nEdir
Event Occured");
EdirEventIntermediateResponse
eventresponse =
(EdirEventIntermediateResponse) message;
System.out.println("Event
Type=" +
eventresponse.getEventtype());
System.out.println("Event
Result=" +
eventresponse.getEventResult());
//process the eventresponse Data,
depending on the
type of response we have
processEventData(eventresponse.getResponsedata(),
eventresponse.getEventtype());
System.out.print(QUIT_PROMPT);
}
// the message is a Unknown response
else {
System.out.println("UnKnown
Message =" + message);
}
}
}
} catch (LDAPException e) {
System.out.println("Error: " +
e.toString());
result = false;
}
return result;
}
/////////////////////////////////////////////////
// MAIN
/////////////////////////////////////////////////
public static void main(String[] args) {
LDAPResponseQueue queue = null;
LDAPConnection lc = new LDAPConnection();
SSLConnection conn = new SSLConnection();
try {
// connect to the server
lc = conn.Connect();
//Create an Array of EdirEventSpecifier
EdirEventSpecifier specifier[] = new
EdirEventSpecifier[2];
// Register for all ADD and DELETE events
specifier[0] = new
EdirEventSpecifier(EdirEventConstant.EVT_ADD_ENTRY,
EdirEventConstant.EVT_STATUS_ALL);
specifier[1] = new
EdirEventSpecifier(EdirEventConstant.EVT_DELETE_ENTRY,
EdirEventConstant.EVT_STATUS_ALL);
//Create a MonitorEventRequest using the
specifiers.
MonitorEventRequest requestoperation = new
MonitorEventRequest(specifier);
//Create an LDAPExtendedRequest , with above
ldapextendedoperation.
LDAPExtendedRequest request = new
LDAPExtendedRequest(requestoperation, null);
//Send the request to server for asynchronous
access and get the
response queue.
queue = lc.extendedOperation(requestoperation,
null, null);
// use default search constraints
} catch (LDAPException e) {
System.out.println("Error: " +
e.toString());
System.exit(1);
}
////////////////////////////////////////////////////////////
/////
// Persistent search initiated without error,
monitor the results
// looping until the user quits by entering a 'q'
or 'Q'
////////////////////////////////////////////////////////////
/////
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
try { //loop until the user quits by entering 'q'
or 'Q'
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.sleep(10);
}
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (InterruptedException e) {
}
//disconnect from the server before exiting
try {
lc.abandon(queue); //abandon the search
lc.disconnect();
} catch (LDAPException e) {
System.out.println();
System.out.println("Error: " +
e.toString());
}
System.exit(0);
} // end main
/**
* Processes the Event Data depending on the Type.
* param data EventResponseData.
* param type Type of Data.
*/
static private void processEventData(EventResponseData
data, int type) {
switch (type) {
case EdirEventConstant.EVT_ADD_ENTRY :
case EdirEventConstant.EVT_DELETE_ENTRY :
// This is an "Entry Event".
Output the relevant Data.
EntryEventData entryevent = (EntryEventData)
data;
System.out.println("Entry =" +
entryevent.getEntry());
System.out.println("timeStamp="
+
entryevent.getTimeStamp().toString());
break;
default :
// Unknown Event.
System.out.println("Unknown event:
" + type);
break;
}
}
}
##### End Code #####
##### Results printed on the Exlipse Console #####
##### The delete event (Type 2) was done first #####
##### The add event (Type 168) was done second #####
Successful SSL bind with server 128.163.3.103.
Monitoring changes. Enter a 'q' to quit:
Edir Event Occured
Event Type=2
Event Result=0
Entry =cn=Freddy,ou=USERS,o=uky
timeStamp=[TimeStamp
(seconds=1145629731)(replicaNumber=1)(event=1)]
Monitoring changes. Enter a 'q' to quit:
Edir Event Occured
Exception in thread "main"
java.lang.ClassCastException:
com.novell.ldap.events.edir.eventdata.GeneralDSEventData
at
edu.ukyunit.EdirEventListener.processEventData(EdirEventList
ener.java:197)
at
edu.ukyunit.EdirEventListener.checkForAChange(EdirEventListe
ner.java:87)
at
edu.ukyunit.EdirEventListener.main(EdirEventListener.java:16
2)
Event Type=168
Event Result=0
|