inheritance configuration

Ketan Jetty
enthusiasm for technology

inheritance configuration

Inheritance configuration or hierarchy configuration is a configuration file that follows the object oriented concept of inheritance to create a compact and managable configuration file. This is developed to steamline the configuration process within CFTurbine.

Most configuration files are basically text or xml files which can be compromised in an unsecured environment. Also most of the config data is duplicated per environment leading to a messy and bulky configuration files.

Inheritance configuration is a coldfusin cfc with one public method getConfig(), which makes it security safe as this method can only be accessed by the server and a hierarchy JSON configuration string which makes the configuration file compact.

The config data is in JSON, which is easily readable and has 2 distinct areas 'current_env' and 'env'.
current_env:  this part sets the server environment "dev","qa","uat","prod"
env:  this section is for setting the configuration parameters.
   base: this is akin to OOP base/parent class. You can set up config params for all environments here. You must place all environment common params here. eg. dsn
   child: dev/qa/uat/prod/[custom env(s)] are the inherited or child sections, the parent being the 'base' section. The base env is automaticaly available to all child environments.

Override key
You can override the base param by creating a similar key in the child. If you have "dsn"="dsnBase" in the base section, by default all environments have the dsn key available with value "dsnBase", now, if you want to override the value for dev environment create a key "dsn"="dsnDev" in dev section.

Append key
You can append to an existing base key by creating similar key appended with "__" double underscore string. If you have "email"="" in the base section, by default all environments have the email key available with value "", now, if you want to append the value for dev environment create a key "email__"="" in dev section. The dev email key will now have value ","

how to use inheritance configuration / hierarchy configuration
<cffunction name="onApplicationStart" returnType="boolean" output="true">
	<cfset StructClear(application) />
	<cfset application['env'] = createObject("component","config").getConfig() />	
	<cfreturn true />

The following will be generated when you dump the #application# scope.
when "current_env"="dev"                       when "current_env"="qa"

config.cfc :: inheritance configuration / hierarchy configuration
*	'base' is the parent env. and rest are the children of the 'base'
*	'base' env. is duplicated in other env.
*	the 'base' key can be overridden by creating a similar key in the respective env.
	eg. if the key 'dsn' is found in the chil env. the 'base' is overridden
	and the child value is used
* 	use '__' suffix appended to key ("admin_mail__") in env. other than 'base' to
	append to base value in the child env
*	Required keys are:
			- current_env
			- admin_mail
			- domain
			- dsn
			- file_seperator
			- root
			- show_debug
	<cffunction name="getConfigJSON" access="private" returntype="string">
		<cfset var configJSON = '{
					"file_seperator":"#Replace(createObject('java', 'java.lang.System').getProperty('file.separator'),'\','\\','ALL')#",
		}' />
		<cfreturn configJSON />
	<cffunction name="getConfig" access="public" returntype="struct">
		<cfset var config = DeserializeJSON(getConfigJSON()) />
		<cfset var retval = "" />
		<cfloop collection="#config.env#" item="i">
			<cfif i NEQ "base">
				<cfif config.current_env  NEQ i>
					<cfset StructDelete(config.env, i) />
					<cfloop collection="#config.env.base#" item="j">
						<cfif StructKeyExists(config.env["#i#"], "#j#__")>
							<cfset config.env["#i#"]["#j#"] = listAppend(config.env["#i#"]["#j#__"], #config.env["base"][j]#) />
							<cfset StructDelete(config.env["#i#"], "#j#__")>
						<cfif NOT StructKeyExists(config.env["#i#"], j)>
							<cfset config.env["#i#"]["#j#"] = #config.env["base"][j]# />
		<cfset config.env[config.current_env]['env_name'] = config.current_env />
		<cfset StructDelete(config.env, "base") />
		<cfset retval = config.env[config.current_env] />
		<cfreturn retval />


CF Quick Reference

Ginger CMS
the future of cms, a simple, easy and intutive content management system ... more

cf prototyping engine, generates boilerplate code and ... more

Jrun monitor
monitor and timely auto-restart to avoid Jrun hang ... more

Inheritance Config.
uses OOPs inheritance to create configuration file ... more

Real Estate App.
complete real estate application using data from MLS ... more

Search Engine Lite
create your own search engine for your web site ... more