Mentawai Web Framework

Script Configuration

DSL stands for (at least here) Dynamic Scripting Languages like Python, Perl, Ruby, Groovy, Beanshell, etc. Mentawai can be extended to support any DSL language for configuration through its abstract class org.mentawai.core.ScriptApplicationManager. Mentawai comes with built-in support for BeanShell through the org.mentawai.core.BshApplicationManager, but support for JRuby, Jython and Groovy may come soon as other people extend ScriptApplicationManager to do so.

The advantages of a DSL may be:

  • Pragramatic approach meaning less verbose and more objective code style.

  • No need to compile the configuration script.

  • Easy auto-reload of the configuration once it is modified in disk.

Before you start converting your old configuration files to a DSL, you should consider that Mentawai also has hot-redeploy of ApplicationManager, in other words, if you change your ApplicationManager.java file, recompile it, and replace the old ApplicationManager.class with the new one, the controller will detect and reload the class automatically with a different classloader.

Below we show an example of how to configure your mentawai application with BeanShell. First you have to tell your application that you want to use the BshApplicationManager and not the good and old ApplicationManager which is loaded by default:

	<servlet>
        <servlet-name>Controller</servlet-name>
        <servlet-class>org.mentawai.core.Controller</servlet-class>
        <init-param>
            <param-name>applicationManager</param-name>
            <param-value>org.mentawai.core.BshApplicationManager</param-value>
        </init-param>  
		<load-on-startup>1</load-on-startup>		
    </servlet>
					

The BshApplicationManager assumes that your ApplicationManager.bsh script file will be in the /WEB-INF/ directory and that it will contain a method definition for config(appMgr, application), where appMgr is a reference to Mentawai's application manager (org.mentawai.core.ApplicationManager) and application is a reference for the application context (org.mentawai.core.ApplicationContext) of your application. Below is an example of an ApplicationManager.bsh:


import org.mentawai.core.*;
import org.mentawai.i18n.*;
import org.mentawai.filter.converter.*;
import calculator.*;

config(appMgr, application) {
        
    appMgr.setDebugMode(true);
    
    LocaleManager.add(new Locale("pt", "BR"));
    LocaleManager.add(new Locale("en", "US"));
    
    appMgr.action("/calc", CalcAction.class)
    
        .on(SUCCESS, fwd("/calc.jsp"))
        .on(ERROR, fwd("/calc.jsp"))
        
        .filter(new CalcValidator())
        .filter(new DoubleConverterFilter("numberOne", "numberTwo"));
        
    appMgr.action("/calc", CalcAction.class, "multiply")
    
        .on(SUCCESS, fwd("/calc.jsp"))
        .on(ERROR, fwd("/calc.jsp"))
        
        .filter(new CalcValidator())
        .filter(new DoubleConverterFilter("numberOne", "numberTwo"));
        
}                
				

You can also extend BshApplicationManager if you want to change the default behaviour like the script name, location and method. Check