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.

August 8th, 2005 at 12:11 pm
Rob,
I have updated the appConstants plugin (version 2.2). The config file now accepts a common block for default constant values. If you write a constants.xml file with a common block but no server blocks it will behave in the way you described. I added this change ages ago but forgot to upload it to my website. I think this solution is slightly better than having a single server block as you also get a way of setting default values for all servers.
August 8th, 2005 at 6:54 am
I just added your plugin to my webmail app, thanks. I did make one modification which others may find useful. I added a check that looks at the array length of servers and, if the array length is one, it assumes that the single set of constants is valid for all servers. Just email me if you would like the modified file.
February 11th, 2005 at 9:41 pm
The above error is caused by not having a matching server name entry in your constants.xml file. For instance, if you move your app to a new server and don’t have an entry in your constants.xml that matches the host name you’re using in your browser to access that server, you’ll get that very unhelpful Java error (see above comment).
February 8th, 2005 at 7:34 pm
anyone getting this error when trying to use this plugin? Object of type class java.lang.String cannot be used as an array
November 9th, 2004 at 7:48 am
Joe: This was my initial plan but I decided to group all constants in a structure to avoid name conflicts with existing Mach-II properties.
November 8th, 2004 at 6:50 pm
Question, why wouldn’t we add the constants directly into the propertyManager rather than nesting the constants in a structure? This would allow pre-existing mach-ii applications to use this cool plugin without code rewrite. Or am I missing an implementation detail?
October 26th, 2004 at 1:27 pm
You don’t need to set the event argument for the views in version 1.0.10 since the getProperty() method is now available.
October 22nd, 2004 at 6:54 pm
Scott, thanks for pointing this out. I have just uploaded a new version of the plugin. Lists of servers are now handled in a much nicer way and the code is nicely commented. Let me know what you think.
October 22nd, 2004 at 5:27 am
Dude, latest fix needs to be updated ie: constants = xmlSearch( defFileXML, “/constants/mode[contains(@name,':#serverName#:')]/constant” ); should be: constants = xmlSearch( defFileXML, “/constants/server[contains(@name,':#serverName#:')]/constant” ); As you no longer have the LIVE/DEV thing happening with the new :Server: version.
October 6th, 2004 at 11:17 pm
heh, I’ve only just started to use Mach II and I was after such a solution. Very cool and easy to use.