Archive for January 27th, 2008

Creating a Server.cfc

Sunday, January 27th, 2008

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

  1. <cfcomponent hint="Server wide Information" output="false">
  2.  
  3. <!--- PROPERTIES --->
  4.  
  5. <!--- TODO: Set for each server instance --->
  6. <cfset variables.environment = "Development" /> <!--- Development, Test, Staging, Production --->
  7.  
  8.  
  9.  
  10. <!--- INIT --->
  11.  
  12. <cffunction name="init"
  13. hint="Initialise the Server information"
  14. access="public"
  15. returntype="Server"
  16. >
  17.  
  18. <!--- Handle Server Start --->
  19. <cfif Not StructKeyExists(server, "Name") OR Not StructKeyExists(server, "Environment")>
  20. <cflock scope="server" type="exclusive" timeout="10">
  21. <cfif Not StructKeyExists(server, "Name") OR Not StructKeyExists(server, "Environment")>
  22.  
  23. <!--- Set Server Information --->
  24. <cfset server.name = createObject("java", "java.net.InetAddress").getLocalHost().getHostName() />
  25. <cfset server.environment = variables.environment />
  26.  
  27. <!--- call onServerStart --->
  28. <cfset onServerStart() />
  29.  
  30. </cfif>
  31. </cflock>
  32. </cfif>
  33.  
  34. <cfreturn this />
  35. </cffunction>
  36.  
  37.  
  38. <cffunction name="onServerStart"
  39. hint="Initialise the Server information"
  40. access="public"
  41. returntype="void"
  42. >
  43.  
  44. <!--- TODO: Add any server startup code here --->
  45.  
  46. </cffunction>
  47.  
  48. </cfcomponent>

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.

  1. <!--- Run Server.cfc --->
  2. <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.


Copyright © 2005, David Beale

  • Valid XHTML 1.0!
  • Valid CSS
  • Level Triple-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0