Creating a Server.cfc
Sunday, January 27th, 2008This is technique I have been using for a while to handle environmental (development, test, production etc) configuration settings. It mimics the Application.cfc added in ColdFusion 7, creating a Server.cfc shown below.
<cfcomponent hint="Server wide Information" output="false"><!--- PROPERTIES ---><!--- TODO: Set for each server instance ---><cfset variables.environment = "Development" /> <!--- Development, Test, Staging, Production ---><!--- INIT ---><cffunction name="init"
hint="Initialise the Server information"access="public"returntype="Server"><!--- Handle Server Start ---><cfif Not StructKeyExists(server, "Name") OR Not StructKeyExists(server, "Environment")><cflock scope="server" type="exclusive" timeout="10"><cfif Not StructKeyExists(server, "Name") OR Not StructKeyExists(server, "Environment")><!--- Set Server Information ---><cfset server.name = createObject("java", "java.net.InetAddress").getLocalHost().getHostName() /><cfset server.environment = variables.environment /><!--- call onServerStart ---><cfset onServerStart() /></cfif></cflock></cfif><cfreturn this /></cffunction><cffunction name="onServerStart"
hint="Initialise the Server information"access="public"returntype="void"><!--- TODO: Add any server startup code here ---></cffunction></cfcomponent>- Download this code: server.cfc
The Server.cfc is copied to the approot/webroot of the server and the variables.environment property set the the server type e.g. "Development". Then in each Application deployed to the server, the following code is added to the begining of the onApplicationStart method in Application.cfc.
<!--- Run Server.cfc ---><cfset CreateObject("component", "approot.Server").init() />
Environmental configuration changes within each application can then be made by referencing the server.environment and server.name properties of the server scope.
