ClassLoader issues with room extensions
Posted: 08 Oct 2011, 17:14
Hello,
I am developing my first extension, which I have designed to include a Zone extension and various extensions for the Room level that implement different logic for different types of Rooms.
Essentially I want my Spring context with DB connections/ws clients/remote credentials and the like to live on my Zone level extension, and then have a different Room extension for each type of game, that can navigate up to the Zone level to get at the single application context. SFS2X api provides for this without doing anything tricky:
This seemed like a clean way to separate my logic and make an extensible server that can have additional types of games added via new room-level extensions. But there's a funny catch when I run the above code:
The behavior that makes sense to me is to use the same ClassLoader that the zone level extension used, because after all, how else can one call createRoom without having written a zone-level extension?
I am proposing a way to specify that the same ClassLoader should be used; or changing default behavior if using separate ClassLoaders for the same extension almost never makes sense--I think the only time it would is with singletons.
Best regards,
Harlan
I am developing my first extension, which I have designed to include a Zone extension and various extensions for the Room level that implement different logic for different types of Rooms.
Essentially I want my Spring context with DB connections/ws clients/remote credentials and the like to live on my Zone level extension, and then have a different Room extension for each type of game, that can navigate up to the Zone level to get at the single application context. SFS2X api provides for this without doing anything tricky:
Code: Select all
// from within room-level request handler
MyZoneExt zoneExt = (MyZoneExt)getParentExtension().getParentRoom().getZone().getExtension();
zoneExt.getSomeResource();
(no, that's not a typo!). The problem is that the two versions of the same class were loaded in different ClassLoader instances... my guess is that when we pass the extension directory into CreateExtensionSettings, it creates a new full extension class loader.Exception: java.lang.ClassCastException
Message: net.example.mygame.sfs.MyZoneExt cannot be cast to net.example.mygame.sfs.MyZoneExt
The behavior that makes sense to me is to use the same ClassLoader that the zone level extension used, because after all, how else can one call createRoom without having written a zone-level extension?
I am proposing a way to specify that the same ClassLoader should be used; or changing default behavior if using separate ClassLoaders for the same extension almost never makes sense--I think the only time it would is with singletons.
Best regards,
Harlan