1. <!--- This is a simple code share example using the mach-ii.cfc --->
  2. <!---
  3. License:
  4. Copyright 2006 Mach-II Corporation
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. Copyright: Mach-II Corporation
  14. Author: Peter J. Farrell (pjf@maestropublishing.com)
  15. $Id: mach-ii.cfc 4824 2006-11-15 07:40:00Z pfarrell $
  16. Created version: 1.1.1
  17. Updated version: 1.1.1
  18. Acknowledgement:
  19. Thank you to eCivis, Inc. for being the driving force behind
  20. the development of this component.
  21. Notes:
  22. - Compatible only with ColdFusion MX 7.0 and above.
  23. - Call loadFramework in your onApplicationStart() event.
  24. - Call handleRequest in your onRequestStart() or onRequest() events.
  25. N.B
  26. Do not implement the handleRequest() in onRequest() application event if you to
  27. utilitze any CFCs that implement AJAX requests, web services, Flash
  28. Remoting or event gateway requests.
  29. ColdFusion MX will not execute these types of requests if you implement
  30. the handleRequest() method in the onRequest() application event.
  31. --->
  32. <cfcomponent
  33. displayname="mach-ii"
  34. output="false"
  35. hint="Base component for Application.cfc integration">
  36. <!---
  37. PROPERTIES - DEFAULTS
  38. --->
  39. <!--- Set the path to the application's mach-ii.xml file. Default to ./config/mach-ii.xml. --->
  40. <cfparam name="MACHII_CONFIG_PATH" type="string" default="#ExpandPath('./config/mach-ii.xml')#" />
  41. <!--- Set the configuration mode (when to reload): -1=never, 0=dynamic, 1=always --->
  42. <cfparam name="MACHII_CONFIG_MODE" type="numeric" default="0" />
  43. <!--- Set the app key for sub-applications within a single cf-application. Default to the folder name. --->
  44. <cfparam name="MACHII_APP_KEY" type="string" default="#GetFileFromPath(ExpandPath('.'))#" />
  45. <!--- Whether or not to validate the configuration XML before parsing. Default to false. --->
  46. <cfparam name="MACHII_VALIDATE_XML" type="boolean" default="false" />
  47. <!--- Set the path to the Mach-II's DTD file. Default to /MachII/mach-ii_1_1.dtd. --->
  48. <cfparam name="MACHII_DTD_PATH" type="string" default="#ExpandPath('/MachII/mach-ii_1_1_1.dtd')#" />
  49. <!--- Set the version number of Mach-II --->
  50. <cfset MACHII_VERSION = "1.1.1.8" />
  51. <!---
  52. PUBLIC FUNCTIONS
  53. --->
  54. <cffunction name="loadFramework" access="public" returntype="void" output="false"
  55. hint="Loads the framework. Only call in onApplicationStart() event.">
  56. <!--- Create the AppLoader. No locking requires if called during the onApplicationStart() event. --->
  57. <cfset application[MACHII_APP_KEY] = StructNew() />
  58. <cfset application[MACHII_APP_KEY].appLoader = CreateObject('component', 'MachII.framework.AppLoader').init(MACHII_CONFIG_PATH, MACHII_DTD_PATH, MACHII_VALIDATE_XML, MACHII_VERSION) />
  59. <cfset request.MachIIReload = FALSE />
  60. </cffunction>
  61. <cffunction name="handleRequest" access="public" returntype="void" output="true"
  62. hint="Handles a Mach-II request. Recommend to call in onRequestStart() event.">
  63. <!--- Default is request.MachIIConfigMode if it is defined temporarily override the config mode --->
  64. <cfif StructKeyExists(request,"MachIIConfigMode")>
  65. <cfset MACHII_CONFIG_MODE = request.MachIIConfigMode />
  66. </cfif>
  67. <!--- Create the AppLoader if necessary. Double check required for proper multi-threading. --->
  68. <cfif NOT StructKeyExists(application, MACHII_APP_KEY)>
  69. <cflock name="application_#MACHII_APP_KEY#_reload" type="exclusive" timeout="120">
  70. <cfif NOT StructKeyExists(application, MACHII_APP_KEY)>
  71. <cfset loadFramework() />
  72. </cfif>
  73. </cflock>
  74. </cfif>
  75. <!--- Reload the configuration if necessary --->
  76. <cfif MACHII_CONFIG_MODE EQ 1 AND NOT StructKeyExists(request, "MachIIReload")>
  77. <cflock name="application_#MACHII_APP_KEY#_reload" type="exclusive" timeout="120">
  78. <cfset application[MACHII_APP_KEY].appLoader.reloadConfig(MACHII_VALIDATE_XML, MACHII_VERSION) />
  79. </cflock>
  80. <cfelseif MACHII_CONFIG_MODE EQ 0 AND application[MACHII_APP_KEY].appLoader.shouldReloadConfig()>
  81. <cflock name="application_#MACHII_APP_KEY#_reload" type="exclusive" timeout="120">
  82. <cfset application[MACHII_APP_KEY].appLoader.reloadConfig(MACHII_VALIDATE_XML, MACHII_VERSION) />
  83. </cflock>
  84. <cfelseif MACHII_CONFIG_MODE EQ -1>
  85. <!--- Do not reload config. --->
  86. </cfif>
  87. <!--- Handle the request --->
  88. <cfset application[MACHII_APP_KEY].appLoader.getAppManager().getRequestHandler().handleRequest() />
  89. </cffunction>
  90. <!---
  91. ACCESSORS - MACHII INTEGRATION
  92. --->
  93. <cffunction name="setProperty" access="public" returntype="void" output="false"
  94. hint="Sets the property value by name. Not available until loadFramework() has been called.">
  95. <cfargument name="propertyName" type="string" required="true" />
  96. <cfargument name="propertyValue" type="any" required="true" />
  97. <cfset getAppManager().getPropertyManager().setProperty(arguments.propertyName, arguments.propertyValue) /> />
  98. </cffunction>
  99. <cffunction name="getProperty" access="public" returntype="any" output="false"
  100. hint="Returns the property value by name. If the property is not defined, and a default value is passed, it will be returned. If the property and a default value are both not defined then an exception is thrown. Not available until loadFramework() has been called.">
  101. <cfargument name="propertyName" type="string" required="true" />
  102. <cfargument name="defaultValue" type="any" required="false" default="" />
  103. <cfreturn getAppManager().getPropertyManager().getProperty(arguments.propertyName, arguments.defaultValue) />
  104. </cffunction>
  105. <cffunction name="isPropertyDefined" access="public" returntype="boolean" output="false"
  106. hint="Checks if property name is defined in the properties. Not available until loadFramework() has been called.">
  107. <cfargument name="propertyName" type="string" required="true"/>
  108. <cfreturn getAppManager().getPropertyManager().isPropertyDefined(arguments.propertyName) />
  109. </cffunction>
  110. <cffunction name="getAppManager" access="public" returntype="MachII.framework.AppManager" output="false"
  111. hint="Get the Mach-II AppManager. Not available until loadFramework has been called.">
  112. <cfreturn application[MACHII_APP_KEY].appLoader.getAppManager() />
  113. </cffunction>
  114. <cffunction name="shouldReloadConfig" access="public" returntype="boolean" output="false"
  115. hint="Returns if the config should be dynamically reloaded.">
  116. <cfreturn application[MACHII_APP_KEY].appLoader.shouldReloadConfig() />
  117. </cffunction>
  118. </cfcomponent>