Extensibility extended - scripting support

Submitted by kobit on Mon, 2009-01-05 15:47.

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.

Trackback URL for this post:

http://www.tigase.org/en/trackback/1482
kobit's picture
Submitted by kobit on Wed, 2009-01-07 13:04.

Thank you for pointing me out to the article. Interesting reading indeed.

I had two problems with running Sala script within the Scala JSR-223 for Java:

  1. One which I described already: it requires variables bindings in a different format than JSR-223 for all other scripting languages: "variable name:variable type". Normally you just provide a variable name for all other languages. This is a minor issue because I could just add one more 'if' and put variables for scala script in a different format. But still some inconvenience.
  2. Another more serious problem was that the script just couldn't use any of the variables I have provided. So basically it worked fine as long as I didn't try to exchange any data. Simple 'println "Hello Workd!"' worked ok. This is actually a serious problem because if the script can't exchange any data between the Java environment and the script it is not very useful and it's application is very limited. This might be just my lack of knowledge and lack of any documentation and examples. It might work but after a few hours spent on searching for JSR-223 support and trying to get the script working on the same level as other languages worked I had to give up.

That said I would love to have Scala support if you can help me with getting this working I can provide you with all my examples, files and code and I am also happy to work with you to adjust the Tigase server to make it working. As long as we can stay within JSR-223.


Submitted by jau (not verified) on Wed, 2009-01-07 08:48.

Scala is superb as a scripting language, still maintaining other features like static typing and performance.

scala-as-a-scripting-language

So what is the problem with with passing data from Tigase to the script? I could take a look at it.

kobit's picture
Submitted by kobit on Mon, 2009-01-05 19:16.

I mentioned Scala in my article. This is a great language and I would love seeing support for it. Unfortunately JSR-223 implementation for Scala is very poor. I have spent whole day trying to make it working without much luck.

It works well as long as you don't need to exchange any data between Tigase (Java) environment and the script environment. The existing JSR-223 support requires providing variable bindings in non-standard way which is sort of acceptable as I would need some special code for the language. This is doable. But then I couldn't manage to make it working with any Tigase specific class. I don't know why, maybe this is my lack of knowledge about Scala.

I read that Scala is as fast as Java (or even better in some places) although I am not sure how fast it would run when executed as a script. If it would run with the same speed as compiled code I could even add support for it in all other places of the server. That would really open completely new possibilities.

Loading packet filters, packet processors, plugins, it could be attached everywhere basically....

On the other hand, as you said this is a language with static typing which might be actually disadvantage for a scripting programming. Actually we could consider Java support as a scripting language as well.....


Submitted by jau (not verified) on Mon, 2009-01-05 19:01.

How about Scala? It is as fast as Java, has static typing with type interference which mean no explicit types and allows to create as short and sweet code as in Ruby, Jython or Groovy.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.