Help concerning the interface ObjectChangeListener

F

Florence HENRY

Hello,

I'm trying to use the interface ObjectChangeListener. So I read the
documentation located at
http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/event/ObjectChangeL
istener.html ,
and here is the skeletton of the code that I produced :

public class Simulator implements ObjectChangeListener {
Object myObject;

public Simulator() {
//...
myObject.addNamingListener(this);
}

public void objectChanged(NamingEvent evt) {
System.out.println("NamingEvent " + evt.getSource().toString());
}

public void namingExceptionThrown(NamingExceptionEvent evt) {
System.err.println("NamingExceptionEvent " +
evt.getSource().toString());
}
}

The purpose is to make the object Simulator listening for changes that
happen to the Object myObject (manipulated by another thread).

The obvious problem is that the Object class does not know the
addNamingListener() method. Do I have to implement it ? What is the
right way to perform this ?

Any help would be appreciated.
 
S

Steve W. Jackson

:Hello,
:
:I'm trying to use the interface ObjectChangeListener. So I read the
:documentation located at
:http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/event/ObjectChangeL
:istener.html ,
:and here is the skeletton of the code that I produced :
:
:public class Simulator implements ObjectChangeListener {
: Object myObject;
:
: public Simulator() {
: //...
: myObject.addNamingListener(this);
: }
:
: public void objectChanged(NamingEvent evt) {
: System.out.println("NamingEvent " + evt.getSource().toString());
: }
:
: public void namingExceptionThrown(NamingExceptionEvent evt) {
: System.err.println("NamingExceptionEvent " +
: evt.getSource().toString());
: }
:}
:
:The purpose is to make the object Simulator listening for changes that
:happen to the Object myObject (manipulated by another thread).
:
:The obvious problem is that the Object class does not know the
:addNamingListener() method. Do I have to implement it ? What is the
:right way to perform this ?
:
:Any help would be appreciated.

According to the index included in the API, there's an addNamingListener
method available in the EventContext and EventDirContext interfaces. So
you're not going to be able to use Object there, but must instead use a
class which implements one of those interfaces.

= Steve =
 
F

Florence HENRY

Steve W. Jackson said:
I'm trying to use the interface ObjectChangeListener.
[...]

According to the index included in the API, there's an addNamingListener
method available in the EventContext and EventDirContext interfaces. So
you're not going to be able to use Object there, but must instead use a
class which implements one of those interfaces.

Thanks... But I cannot manage to use the EventContext either. The
javax.naming.event package description says :

An application, for example, can register its interest in changes to
objects in a context as follows:

EventContext src =
(EventContext)(new InitialContext()).lookup("o=wiz,c=us");
src.addNamingListener("ou=users", EventContext.ONELEVEL_SCOPE,
new ChangeHandler());
....
class ChangeHandler implements ObjectChangeListener {
public void objectChanged(NamingEvent evt) {
System.out.println(evt.getNewBinding());
}
public void namingExceptionThrown(NamingExceptionEvent evt) {
System.out.println(evt.getException());
}
}

If I understand well, new InitialContext() creates a new context with an
empty environment. The method lookup is supposed to retreive the named
object "o=wiz,c=us". But as the context is new (thus empty), there is no
object to return... What did I understand wrong ?

Within my code, if I write :

public class Simulator implements ObjectChangeListener {
Object myObject;

public Simulator() {
//...
try {
// create a new context
ctx = (EventContext)(new InitialContext());

// bind the object simul with the name "simulateur"
ctx.bind("simulateur", simul);

// register the current object as a listener of the
// object named by "simulateur"
ctx.addNamingListener("simulateur",
EventContext.OBJECT_SCOPE, this);

} catch (NamingException ne) {
System.err.println(ne.getExplanation());
}
}

public void objectChanged(NamingEvent evt) {
System.out.println("NamingEvent " + evt.getSource().toString());
}

public void namingExceptionThrown(NamingExceptionEvent evt) {
System.err.println("NamingExceptionEvent " +
evt.getSource().toString());
}
}

I get a NamingException, with the following message :

"Need to specify class name in environment or system property, or as an
applet parameter, or in an application resource file:
java.naming.factory.initial"

this exception is produced when doing the new InitialContext(). And I
get the same error with a lookup("whatever")...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top