powered by Jive Software

Problem with XIFF Extensions

There is a problem (it seems to me) with the way extensions work in XIFF. The problem is that every extension has to have a different namespace.

I can understand why the combination of namespace and the root element name needs to be unique, but it seems a little silly to have to create a new namespace for every message. Why not use the combination of namespace and element name?

Why is this a problem? In my case, I have wrapped the Room class in my own custom ChatRoom class. When a message arrives in the chat room, I check for any extensions in a particular namespace and if they are found I know the message isn’t a normal chat message, but rather one of my custom message types. In this case, I do not need to dispatch a GROUP_MESSAGE event … instead I generate a custom event, set the data field to the extension class, and dispatch this custom event. The code handling the event can then look at the element name to decide what to do.

I want the ChatRoom class to remain ignorant of what all of the extensions are because I don’t want to have to change the ChatRoom each and every time I create a new extension. I want to be able to check whether the message contains ANY one of my extensions by doing a lookup(mycustomns), but I cannot do that because you must currently register all extensions with a unique namespace … this means ChatRoom has to know every single namespace for each extension and every time I add a new one, I have to change ChatRoom.

I wrote all the code such that all my extensions share the same namespace, but when I started testing I discovered that when you register an extension, the ExtensionClassRegistry stores it only by namespace … so if multiple extensions share a namespace, only the last class actually remains in the registry since each new register() call just overwrites the previous one. In trying to avoid changing ExtensionClassRegistry (which is used in other core code), I followed the pattern I noted in some of the built-in extensions … i.e. add an anchor like #myextension to the namespace. But that puts me in the situation I said I didn’t want … now ChatRoom has to know about every extension in order to check for the presence of them in an incoming message … because it has to call getExtensionsByNS() over and over, once with each of the many namespaces.

My belief that multiple extensions could share the same namespace was reinforced by the fact the getExtensionsByNS() returns an Array.

Comments?

Well, I’m still not convinced namespaces should be used in the way they are here, but I did manage to implement a workaround. Basically, I created a separate registry of my own and in the enable() method of each of my extensions I added a call to MyRegistry.register() in addition to the existing call to ExtensionClassRegistry.register(). Then in my ChatRoom class I check each new group or private message for any of the extensions (namespaces) that are registered in MyRegistry. It is a little less efficient than what I wanted to do since I have to iterate over a list of namespaces and compare each one individually, but the list of namespaces should never be longer than a few dozen so it probably won’t make any significant difference in performance.

I’d still be interested in hearing if there is some reason it works the way it does. Or if someone agrees with me, I could use the moral support. It often feels like I am on a deserted island in these forums. Is anyone out there?