ColdFusion, WordPress, Flash & other web things


appConstants Mach-II plugin

File name:
appConstants.cfc
Version:
2.3
Last updated:
27 September 2005
Requirements:
Mach-II 1.0.10, CFMX 6.1

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.

Download appConstants.cfc

21 comments

Pages: « 3 2 [1] Show All

  1. #10: Martin Says:

    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.

  2. #9: Rob M Says:

    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.

  3. #8: Kim G Says:

    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).

  4. #7: Kim G Says:

    anyone getting this error when trying to use this plugin? Object of type class java.lang.String cannot be used as an array

  5. #6: Martin Says:

    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.

  6. #5: Joe Baarsch Says:

    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?

  7. #4: Christopher Town Says:

    You don’t need to set the event argument for the views in version 1.0.10 since the getProperty() method is now available.

  8. #3: Martin Says:

    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.

  9. #2: Scott Barnes Says:

    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.

  10. #1: Scott Barnes Says:

    heh, I’ve only just started to use Mach II and I was after such a solution. Very cool and easy to use.

Pages: « 3 2 [1] Show All