Project

General

Profile

Extensibility extended - scripting support

Avatar?id=6023&size=32x32

Artur Hefczyc TigaseTeam
Added over 4 years ago

The Tigase server is very extensible through the well defined API. You can easily attach your code to any part of the server and load it at the startup time. It is quite simple to connect the Tigase to any database, add own components, plugins, packets filters, resource monitors and so on.

There are, however at least two disadvantages of this kind of extensibility. You have to write Java code and the code can not be reloaded at run-time. So basically you have to restart the server to apply your changes.

Installing Groovy script

Installing Groovy script

What about a code in a scripting language which could be reloaded/added/removed at runtime without affecting normal work of the server? Which scripting language? Ideally any, ideally your preferred language. How? Of course using ad-hoc commands.

This is possible now. You can create your scripts in almost any language and load the script at runtime to the server to do some work for you. The Tigase server supports scripting through JSR-223 API in Java6. Maybe it doesn't make sense to use some of the supported languages like AWK for example in the Tigase server. Nevertheless I have learned already that users' use cases go far beyond my expectations. Therefore there are virtually no restrictions on the language you choose. Just put all required JAR files in the Tigase libs/ directory and the language is available to you.

List of supported scripting languages

List of supported scripting languages

There is also nothing stopping you from adding support for many scripting languages at the same time and load different scripts in different languages. However you must be aware that each scripting language significantly increases memory requirements for the Tigase server. The best way is therefore to stick to one or at most two.

Even though I don't force to use any particular language it is good to know that some languages are supported better than others through the JSR-223 API. This is a problem especially if you want to exchange different types of data between the Tigase core code and the script environment. I have done some basic tests over three major scripting languages for the JVM: Groovy, JRuby and Jython. I've got them all working and exchanging data between the Tigase and the script. Other tempting alternatives with JSR-223 support are: Scala, JavaScript, Jaskell, JavaFX. The only one which didn't work at all is JavaFX. The problem is either with the language itself or with my lack of knowledge. Scala works as well but it exchanges (binds) data in non-standard way therefore it can not be easily plugged.

Groovy script ask user for input using ad-hoc command and data form

Groovy script ask user for input using ad-hoc command and data form

The best language to pick is Groovy. First of all all it's types are compatible with Java types so exchanging data is easy and painless and it also has the smallest memory requirements for the scripting engine. So if you need a language which will always work and which allow you to do almost everything you want inside the Tigase server this is the best choice. And this is a good scripting language too.

The next one which seems to integrate very well is JRuby. It can simply return a value of any type which is compatible with Java (Tigase) type. Jython works too but there are problems with returning values. Jython can not return an object from the main script (as far as I know) therefore the only way for exchanging data us through variables binding. And this has its limitations too.

Groovy script displays script results via ad-hoc command

Groovy script displays script results via ad-hoc command

Anyway, I am not an expert in any of those languages so I invite you for experimenting. You might achieve much better results than I did.

Of course there are security issues with this feature therefore loading scripts is restricted to the service administrators only. The scripting code has also performance and resources consumption issues so even though in theory it is possible to allow attaching scripts to any place in the Tigase server I have limited it to only administrator commands.

As all such stuff in the Tigase server you can load a new script, change it or remove using ad-hoc commands. Also the loaded script can interact with the user using ad-hoc commands or it can do some background job without interaction with the user. How is this can be useful? The script has an access to all the server main resources - database, user repository, authentication repository, user sessions, user connections, administrator commands, service discovery and so on. In practice the script can perform any administration task - managing user accounts, user roster data, disconnecting users, altering user roster and so on.

Basically what you really need is JAR files for you scripting language and an XMPP (Jabber) client with a good service discovery and ad-hoc command support. Psi would be a good choice although there are also other which I can recommend like Coccinella for example.

List of loaded scripts

List of loaded scripts

I am going also to add some other hooks to other parts of the server to allow even deeper integration. In all cases however the performance and stability is the priority. Therefore the script will not be able to attach to data processing hook. This kind of code must be written in Java. The script however might alter for example the data processing or filtering parameters.