appConstants Mach-II plugin
This plugin allows you to have a different set of application constants for each server running your Mach-II application. This is really useful if you are using version control and each developer is running the application on their local machine.
If you’re looking for a PHP version of this plugin, you can get one from Dave Spurr’s blog.
Note: this plugin requires version 1.0.10 of the Mach-II core files (although it can be modified to work with 1.0.9).
Place appConstants.cfc in the MachII plugins folder. Place this in the Mach-II configuration file:
<plugin name="appConstants" type="MachII.plugins.appConstants" />
or
<plugin name="appConstants" type="MachII.plugins.appConstants"> <parameters> <parameter name="constantsFile" value="path_to_constants_file" /> </parameters> </plugin>
The constantsFile optional parameter should be a path to your constants XML file relative to your application root (set in the properties section of your configuration file). It defaults to “/config/constants.xml”.
The constants XML file should follow the following format:
<?xml version="1.0" encoding="utf-8"?> <constants> <common> <constant name="constant_name_1" value="constant_value_1" /> <constant name="constant_name_2" value="constant_value_2" /> ... <property name="property_name_1" value="property_value_1" /> ... </common> <server name="your_dev_server_name"> <constant name="constant_name_1" value="constant_value_1" /> <constant name="constant_name_2" value="constant_value_2" /> ... <property name="property_name_1" value="property_value_1" /> ... </server> <server name="your_testing_server_name"> <constant name="constant_name_1" value="constant_value_1" /> <constant name="constant_name_2" value="constant_value_2" /> ... <property name="property_name_1" value="property_value_1" /> ... </server> <server name="your_live_server_name"> <constant name="constant_name_1" value="constant_value_1" /> <constant name="constant_name_2" value="constant_value_2" /> ... <property name="property_name_1" value="property_value_1" /> ... </server> </constants>
You will have access to the constants set up in the server section of the constants file that corresponds to the server currently running your application as well as all constants in the common block (common constant values are overwritten by server block values).
You can also specify a comma separated list of server names for a single server section (useful if several domains point to the same application).
All constants are placed in a structure named appConstants that you can access as a Mach-II property.
You can also set Mach-II properties with the property tag. These properties will overwrite any existing Mach-II properties.
Note: You can use this plugin for a single server setup. Place all your constants in the common block and don’t bother with any server blocks.

October 11th, 2006 at 4:17 pm
Kai: you would have to pass the appConstants structure into the gateway/facade object when you create it so that it is available as a property of the object.
I’m assuming you are creating the facade inside the Mach-II framework, in which case you can pass appConstants to it’s init function. If you aren’t then I can’t see how you can access it.
October 11th, 2006 at 3:53 pm
Awesome plugin. I am wondering how people are accessing the appConstants in a Gateway or a Facade. For example, I have a facade that exposes methods to Flex. Flex then accesses the compoent via RemoteObject and is not aware of the Mach-ii Framework.
I can’t place the “variables.appConstants = getAppManager().getPropertyManager().getProperty(’appConstants’);” code into the init function but how do I access the constants then?
Any ideas?
Thanks,
Kai
January 12th, 2006 at 3:34 pm
Martin:
Thanks for the quick response. You are right, the server context is different in the new environment and needs to be reflected in the xml file.
I really have been using this to just hold my DSN information which is the same across all server contexts so this detail slipped past me.
Thanks again for the quick response.
Dan Wilson
January 12th, 2006 at 3:27 pm
Dan Wilson: you usually get this error when the plugin cannot find a section for the environment’s server name. It should be better handled and your fix is probably the easiest way of fixing it. I’ll have a look and update my plugin.
However, in most cases, when you use this plugin, you should have a section for each server context. Do you not need the constants in the third environment?
January 12th, 2006 at 3:21 pm
Hi, I ran into an issue using this plugin. Before I go into it, let me say I had it working in two separate environments without a hitch. When I deployed the app in another context, thats where I ran into problems.
The specific error related to “Can’t use java.object.string as an array”
The error was on this line:
for(i=1;i lte arrayLen(commonConstants);i=i+1) arrayPrepend( constants, commonConstants[i] );
After digging through the code, I noticed that ‘constants’ was defined as a local variable and initialized like this:
var constants = 0;
To fix this I changed the variable initialization to:
var constants = arrayNew(1);
This seemed to fix it and the app ran fine after that.
I don’t know how good of a fix this is since I don’t fully understand the plug-in. If anyone can offer guidance, I would appreciate it.
Dan Wilson
September 27th, 2005 at 10:25 pm
Sami: I modified the appConstants plugin to support property tags in the constants.xml file. The property tag allows you to set Mach-II properties without having to group them in the appConstants structure. I think this method is more flexible than your suggestion.
September 8th, 2005 at 6:43 pm
Added to constants:
<constant name=”applicationRoot” value=”/intranet” />
September 8th, 2005 at 6:42 pm
<!– <property name=”applicationRoot” value=”/intranet” /> –>
September 8th, 2005 at 6:42 pm
Didnt show in comments:
Commented out in Mach II Xml File:
September 8th, 2005 at 6:41 pm
Martin,
I’d like to suggest an enhancement. As we know, sometimes the property applicationRoot is different on different servers. But since we are trying to extract that and use this plugin, I commented out the following.
–>
In the constants file, I added:
In the appConstants.cfc, I added some code to add that property:
// Loop over constant node array
for( i = 1; i LTE arrayLen( constants ); i = i + 1 ) {
// Insert each constant in the constants array
structInsert( variables.appConstants, constants[i].xmlAttributes.name, constants[i].xmlAttributes.value );
//NEW CODE
if( constants[i].xmlAttributes.name EQ “applicationRoot” ){
setProperty( “applicationRoot”, constants[i].xmlAttributes.value );
}
//END NEW CODE
}
So now it works. applicationRoot is free from the Mach II XML File.
Just one issue, in the Mach II XML File, I have to:
It references /intranet, because until its loaded, there is no /config… Any ideas how to bypass this?
Sami