For static configurations (strings such as username, password, database information, smtp account, etc.) you can use a properties file. Mentawai will automatically load a special properties file according to the environment you are running your application. The environments supported are: development (DEV), production (PROD), integration (INT), quality assurance (QA) and tests (TEST). You define the environment by setting an environment variable called ENV. If this variable is not present, the default environment DEV is used. For example, to set this variable on Linux you should run the following command before starting your web container:
export ENV=prod
The properties are loaded from the following directory structure inside your web application:
Note the appManager.properties inside the default directory. This is the base properties file that is always loaded. What happens is you can overwrite any property defined in this default file with a new value defined in the specific environment properties file. You should also use the default properties file to define properties that are common to all environments.
Accessing the properties inside the application manager:
@Override public void init(Context application) { Props props = getProps(); //////////////////////////////////////////// // TURN ON/OFF DEBUG MODE //////////////////////////////////////////// setDebugMode(props.getBoolean("debug_mode")); /////////////////////////////////////////////////// // TURN ON/OFF APP MANAGER AUTO-REDEPLOY FEATURE // OBS: Requires http://www.javarebel.com to work /////////////////////////////////////////////////// setReloadable(props.getBoolean("auto_reload")); ////////////////////////////////////////// // FOR SENDING EMAIL ////////////////////////////////////////// if (!props.getBoolean("email.send_email")) { Email.setSendEmail(false); } else { Email.setDefaultHostName(props.getString("email.host")); Email.setDefaultSslConnection( props.getBoolean("email.ssl") ); Email.setDefaultPort( props.getInt("email.port") ); if (props.getBoolean("email.use_authentication")) { Email.setDefaultAuthentication(props.getString("email.user"), props.getString("email.pass")); } Email.setDefaultFrom(props.getString("email.from_email"), props.getString("email.from_name")); } }
Note that the Props object has many useful methods for you to access its properties.
Checking properties on the view layer:
You can use the special tag propsIf to check a property directly from inside a JSP page. For example, to include a google tracking code if a property is configured to true in the properties file you can do:
<mtw:propsIf test="include_google_analytics" value="true"> <!-- the google analytics code goes here --> </mtw:propsIf>