|
|
Config.cfc allows application and session variables in a ColdFusion application to be set from an XML file.
Config.cfc is designed to allow you to store all of your application and session variables in an xml file,
eliminating the need to edit Application.cfm. This is an issue if you are developing locally but
deploying your application on a remote server.
My goal in writing config.cfc was to keep it as simple and easy to use as possible.
To use config.cfc put config.xml.cfm in the root of your application and edit it to fit your needs.
Config.cfc can be anywhere but it always looks for config.xml.cfm in the root.
config.xml.cfm is an XML file, the cfm extension is so it cannot be read remotely in a browser.
If you're using Application.cfm, in Application.cfm put the following lines of code AFTER the cfapplication tag:
<cfparam name='url.appreload' default=FALSE />
<!--- use double-checked locking to prevent race conditions --->
<cfif not structKeyExists( application, 'appInitialized' ) or url.appReload >
<cflock name='appInitBlock' type='exclusive' timeout='10'>
<cfif not structKeyExists( application, 'appInitialized' ) or url.appReload >
<cfset createObject('component','config').init().SetVariables() />
</cfif>
</cflock>
</cfif>
If you are using Application.cfc , that would go in onRequestStart().
To force a reload , simply add appreload to the URL ie 'index.cfm?appreload=true'.
I would suggest making your remote config.xml.cfm read-only so you don't overwrite it accidentally.
Config.cfc uses double-checked locking to prevent race conditions. For more information about
double-checked locking see Joe Rineharts explanation at
http://www.firemoss.com/blog/index.cfm?mode=entry&entry=AA5D506E-3048-55C9-43FA875EB499C53D
This code was influenced by the following:
Peter Bell: The Benefits of XML Config Files (and why I don't use them)
http://www.pbell.com/index.cfm/2006/10/4/The-Benefits-of-XML-Config-Files-and-why-I-dont-use-them
Ray Camden: ColdFusion 101: Config Files A-Go-Go Part 2: XML Files
http://ray.camdenfamily.com/index.cfm/2005/9/2/ColdFusion-101-Config-Files-AGoGo-Part-2-XML-Files
Hal Helms: Creating Configuration Files
http://coldfusion.sys-con.com/read/49182.htm
Joe Rinehart: Best Practice: Configuring CF Applications with XML
http://www.firemoss.com/blog/index.cfm?mode=entry&entry=67E5ADDF-E081-0478-471D4AB4B664CE9A
Special thanks to Nic Tunney (www.nictunney.com) for his suggestions and code review.
Questions? Comments? email me at jimcollins@gmail.com
Config.cfc is designed to allow you to store all of your application and session variables in an xml file,
eliminating the need to edit Application.cfm. This is an issue if you are developing locally but
deploying your application on a remote server.
My goal in writing config.cfc was to keep it as simple and easy to use as possible.
To use config.cfc put config.xml.cfm in the root of your application and edit it to fit your needs.
Config.cfc can be anywhere but it always looks for config.xml.cfm in the root.
config.xml.cfm is an XML file, the cfm extension is so it cannot be read remotely in a browser.
If you're using Application.cfm, in Application.cfm put the following lines of code AFTER the cfapplication tag:
<cfparam name='url.appreload' default=FALSE />
<!--- use double-checked locking to prevent race conditions --->
<cfif not structKeyExists( application, 'appInitialized' ) or url.appReload >
<cflock name='appInitBlock' type='exclusive' timeout='10'>
<cfif not structKeyExists( application, 'appInitialized' ) or url.appReload >
<cfset createObject('component','config').init().SetVariables() />
</cfif>
</cflock>
</cfif>
If you are using Application.cfc , that would go in onRequestStart().
To force a reload , simply add appreload to the URL ie 'index.cfm?appreload=true'.
I would suggest making your remote config.xml.cfm read-only so you don't overwrite it accidentally.
Config.cfc uses double-checked locking to prevent race conditions. For more information about
double-checked locking see Joe Rineharts explanation at
http://www.firemoss.com/blog/index.cfm?mode=entry&entry=AA5D506E-3048-55C9-43FA875EB499C53D
This code was influenced by the following:
Peter Bell: The Benefits of XML Config Files (and why I don't use them)
http://www.pbell.com/index.cfm/2006/10/4/The-Benefits-of-XML-Config-Files-and-why-I-dont-use-them
Ray Camden: ColdFusion 101: Config Files A-Go-Go Part 2: XML Files
http://ray.camdenfamily.com/index.cfm/2005/9/2/ColdFusion-101-Config-Files-AGoGo-Part-2-XML-Files
Hal Helms: Creating Configuration Files
http://coldfusion.sys-con.com/read/49182.htm
Joe Rinehart: Best Practice: Configuring CF Applications with XML
http://www.firemoss.com/blog/index.cfm?mode=entry&entry=67E5ADDF-E081-0478-471D4AB4B664CE9A
Special thanks to Nic Tunney (www.nictunney.com) for his suggestions and code review.
Questions? Comments? email me at jimcollins@gmail.com
