(EDIT: Problem is solved - see details at the end)
I want to create a Swing JFrame
with a WindowAdapter
in an OSGi Bundle. When I do this using SwingUtilities.invokeLater
, the WindowAdapter
class is not found. Without invokeLater
it works.
What do I need to do so that WindowAdapter
is found when using invokeLater
?
Is invokeLater
inappropriate in an OSGi environment?
The details:
I start an Apache Felix framework instance with my custom launcher, install the bundle and start it. My bundle's start method looks like this:
public void start(BundleContext arg0) throws Exception {
myFrame = new MyFrame();
myFrame.open();
}
This is the MyFrame class:
public class MyFrame {
JFrame mainFrame;
public void open() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
openImpl();
}
});
// If called like this it works:
// openImpl();
}
public void openImpl() {
mainFrame = new JFrame("Title");
mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
WindowAdapter wa = new WindowAdapter() {
};
mainFrame.addWindowListener(wa);
mainFrame.setSize(800, 600);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
}
This is my manifest for the bundle:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_03-b05 (Oracle Corporation)
Built-By: Rainer Schwarze
Bundle-Name: DummyBdl
Bundle-Description: Dummy Bundle
Bundle-Vendor: admaDIC
Bundle-Version: 0.0.1
Bundle-Activator: dummybdl.Activator
Import-Package: org.osgi.framework, javax.swing
Export-Package: dummybdl.api
Export-Service: dummybdl.Provider
And this is the stack trace which I get:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1432)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:72)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at dummybdl.MyFrame.openImpl(MyFrame.java:24)
at dummybdl.MyFrame$1.run(MyFrame.java:16)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Being an OSGi newbie I tried several things to fix it, but couldn't find a solution so far. So why not make my first question at StackOverflow :-)
EDIT:
After debugging for half an hour it turns out that the problem was entirely mine:
My code stopped the OSGi framework before openImpl
gets called in the EDT.
So the Apache Felix framework marked the BundleWiringImpl
instance (see stack trace) as disposed. When my openImpl
gets called in the EDT, BundleWiringImpl.getClassLoader
returns null
because it is marked as disposed. Eventually this leads to the NPE.
(I should have gone the extra steps of posting the 50 lines of my Felix loader which might have made the error obvious.)
My code stopped the OSGi framework before openImpl
gets called in the EDT.
Without invokeLater
openImpl
is called immediately and before my other code shuts down the OSGi framework. With invokeLater
the call to openImpl
is scheduled for later and before that "later" happens, my code shuts down the OSGi framework.
In that case the Apache Felix framework marked the BundleWiringImpl
instance (see stack trace) as disposed. When my openImpl
gets called in the EDT, BundleWiringImpl.getClassLoader
returns null
because it is marked as disposed. Eventually this leads to the NPE.