I could notice a strange behaviour of the EventGenerator when Opencard is
used inside an applet runned by the Java Plugin. The events (cardInserted -
cardRemoved) work fine when the applet is first loaded. Then I go to another
page and the applet stops (I call EventGenerator.removeCTListener and
SmartCard.shutdown). Then I go back to the page where the applet is and the
applet starts again (I call SmartCard.start and
EventGenerator.addCTListener), but the events don't work anymore. I am not
notified when a card is inserted or removed. And even the OCF is not aware
of these events (I displayed all OCF debug messages by modifying the
opencard.properties).
Here is a test applet which can demonstrate the problem :
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import opencard.core.service.*;
import opencard.core.event.*;
public class OCFEventTester extends Applet implements CTListener {
public OCFEventTester() {
}
public void start() {
try {
SmartCard.start();
EventGenerator.getGenerator().addCTListener(this);
}
catch (Throwable e) {
e.printStackTrace();
}
}
public void stop() {
try {
EventGenerator.getGenerator().removeCTListener(this);
SmartCard.shutdown();
}
catch (Throwable e) {
e.printStackTrace();
}
}
public void paint(Graphics graphics) {
graphics.setColor(Color.white);
graphics.fillRect(0, 0, getSize().width , getSize().height );
graphics.setColor(Color.black);
graphics.drawString(getText(), 0, getSize().height / 2);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text=text;
repaint();
}
public void cardInserted(CardTerminalEvent event) {
setText("card inserted !");
}
public void cardRemoved(CardTerminalEvent event) {
setText("card removed !");
}
private String text="waiting for events...";
}
So, I tried to find out what the problem was. Apparently, EventGenerator
creates some threads which polls the readers, using the following piece of
code (see EventGenerator.updateTerminals) :
if (t==null) {
t = new Thread(this);
t.setDaemon(true);
t.start();
}
OK, fine. The problem is, when the applet stops, the daemon thread created
by EventGenerator is killed because of the following exception :
java.lang.ThreadDeath
at java.lang.Thread.stop(Thread.java:581)
at java.lang.ThreadGroup.stopOrSuspend(ThreadGroup.java:625)
at java.lang.ThreadGroup.stop(ThreadGroup.java:539)
at sun.awt.AppContext.dispose(AppContext.java:379)
at sun.applet.AppletClassLoader.release(AppletClassLoader.java:450)
at sun.applet.AppletPanel.release(AppletPanel.java:163)
at sun.applet.AppletPanel.sendEvent(AppletPanel.java:260)
at sun.plugin.AppletViewer.onPrivateClose(Unknown source)
at sun.plugin.AppletViewer$1.run(Unknown source)
at java.lang.Thread.run(Thread.java:484)
But the variable t is not set back to null, so the EventGenerator believes
that the thread is still running. Then, the next time that the framework is
started, no new thread is created because of the condition "if (t==null)".
So, I thought that the simplest way to resolve this problem was to add, in
EventGenerator.run, a big try-finally statement to set the t varaible to
null in case the thread dies :
public void run() {
try {
[... the original code of EventGenerator.run ...]
}
finally {
t=null;
}
}
Then, it seems to work. However, I don't know if it is the best solution.
Maybe it would be possible to avoid the ThreadDeath error and solve this
problem in a more elegant manner.
Please, I'd like a feedback on this from the OCF guys. I've already sent a
bug report on this list and I received no feedback. It's quite frustrating.
And actually, I don't even know if the bug reports should be sent here. So,
if I posted on the wrong list, please tell me where is the appropriate
location for that.
Regards,
Dimitri.
---
> Visit the OpenCard web site at http://www.opencard.org/ for more
> information on OpenCard---binaries, source code, documents.
> This list is being archived at http://www.opencard.org/archive/opencard/
! To unsubscribe from the [EMAIL PROTECTED] mailing list send an email
! to
! [EMAIL PROTECTED]
! containing the word
! unsubscribe
! in the body.