mach-ii

Package: MachII
Bootstrapper for Application.cfc integration

<!--- License: Copyright 2008 GreatBizTools, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Copyright: GreatBizTools, LLC Author: Peter J. Farrell (peter@mach-ii.com) $Id: mach-ii.cfc 1327 2009-02-09 07:34:44Z peterfarrell $ Created version: 1.1.1 Updated version: 1.6.0 Notes: - Compatible only with Adobe ColdFusion MX 7.0+ or NewAtlanta BlueDragon 7+. - Call loadFramework in your onApplicationStart() event. - Call handleRequest in your onRequestStart() or onRequest() events. N.B. Do not implement the handleRequest() in onRequest() application event if you want to utilitze any CFCs that implement AJAX requests, web services, Flash Remoting or event gateway requests. ColdFusion MX will not execute these types of requests if you implement the handleRequest() method in the onRequest() application event. --->

Method Summary
public string getAppKey()

Returns a clean AppKey.

public AppManager getAppManager()

Get the Mach-II AppManager. Not available until loadFramework has been called.

public any getProperty(string propertyName, [any defaultValue=""])

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.

public void handleRequest()

Handles a Mach-II request. Recommend to call in onRequestStart() event.

public boolean isPropertyDefined(string propertyName)

Checks if property name is defined in the properties. Not available until loadFramework() has been called.

public void loadFramework()

Loads the framework. Only call in onApplicationStart() event.

public boolean onApplicationStart()

Run on the application start event. Override to provide customize functionality.

public void onRequestStart(string targetPage)

Handles Mach-II requests. Output must be set to true. Override to provide custom functionality.

public void onSessionEnd(struct sessionScope, struct applicationScope)

Handles on session end event if sessions are enabled for this application.

public void onSessionStart()

Handles on session start event if sessions are enabled for this application.

public void setProperty(string propertyName, any propertyValue)

Sets the property value by name. Not available until loadFramework() has been called.

public boolean shouldReloadConfig()

Returns if the config should be dynamically reloaded.

Method Detail
getAppKey

public string getAppKey( )

Returns a clean AppKey.

Parameters:

Code:

	<cffunction name="getAppKey" access="public" returntype="string" output="false"
		hint="Returns a clean AppKey.">
		<cfreturn REReplace(MACHII_APP_KEY, "[[:punct:]|[:cntrl:]]", "", "all") />
	</cffunction> 

getAppManager

public AppManager getAppManager( )

Get the Mach-II AppManager. Not available until loadFramework has been called.

Parameters:

Code:

	<cffunction name="getAppManager" access="public" returntype="MachII.framework.AppManager" output="false"
		hint="Get the Mach-II AppManager. Not available until loadFramework has been called.">
		<cfreturn application[getAppKey()].appLoader.getAppManager() />
	</cffunction> 

getProperty

public any getProperty( string propertyName, [any defaultValue=""] )

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.

Parameters:
string propertyName
[any defaultValue=""]

Code:

	<cffunction name="getProperty" access="public" returntype="any" output="false"
		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.">
		<cfargument name="propertyName" type="string" required="true" />
		<cfargument name="defaultValue" type="any" required="false" default="" />
		<cfreturn getAppManager().getPropertyManager().getProperty(arguments.propertyName, arguments.defaultValue) />
	</cffunction> 

handleRequest

public void handleRequest( )

Handles a Mach-II request. Recommend to call in onRequestStart() event.

Parameters:

Code:

	<cffunction name="handleRequest" access="public" returntype="void" output="true"
		hint="Handles a Mach-II request. Recommend to call in onRequestStart() event.">

		<cfset var appKey = getAppKey() />
		
		
		<cfif StructKeyExists(request,"MachIIConfigMode")>
			<cfset MACHII_CONFIG_MODE = request.MachIIConfigMode />
		</cfif>
		
		
		<cfif NOT IsDefined("application.#appKey#.appLoader") OR NOT IsObject(application[appKey].appLoader)>
			<cflock name="application_#appKey#_reload" type="exclusive" timeout="#MACHII_ONLOAD_REQUEST_TIMEOUT#">
				<cfif NOT IsDefined("application.#appKey#.appLoader") OR NOT IsObject(application[appKey].appLoader)>
					<cfset loadFramework() />
				</cfif>
			</cflock>
		</cfif>

		
		<cfif MACHII_CONFIG_MODE EQ -1>
			
		<cfelseif MACHII_CONFIG_MODE EQ 1 AND NOT StructKeyExists(request, "MachIIReload")>
			<cflock name="application_#appKey#_reload" type="exclusive" timeout="#MACHII_ONLOAD_REQUEST_TIMEOUT#">
				<cfsetting requestTimeout="#MACHII_ONLOAD_REQUEST_TIMEOUT#" />
				<cfset application[appKey].appLoader.reloadConfig(MACHII_VALIDATE_XML) />
			</cflock>
		<cfelseif MACHII_CONFIG_MODE EQ 0 AND application[appKey].appLoader.shouldReloadBaseConfig()>
			<cflock name="application_#appKey#_reload" type="exclusive" timeout="#MACHII_ONLOAD_REQUEST_TIMEOUT#">
				<cfsetting requestTimeout="#MACHII_ONLOAD_REQUEST_TIMEOUT#" />
				<cfset application[appKey].appLoader.reloadConfig(MACHII_VALIDATE_XML) />
			</cflock>
		<cfelseif MACHII_CONFIG_MODE EQ 0 AND application[appKey].appLoader.shouldReloadModuleConfig()>
			<cflock name="application_#appKey#_reload" type="exclusive" timeout="#MACHII_ONLOAD_REQUEST_TIMEOUT#">
				<cfsetting requestTimeout="#MACHII_ONLOAD_REQUEST_TIMEOUT#" />
				<cfset application[appKey].appLoader.reloadModuleConfig(MACHII_VALIDATE_XML) />
			</cflock>
		</cfif>

		
		<cfset 	getAppManager().getRequestHandler().handleRequest() />
	</cffunction> 

isPropertyDefined

public boolean isPropertyDefined( string propertyName )

Checks if property name is defined in the properties. Not available until loadFramework() has been called.

Parameters:
string propertyName

Code:

	<cffunction name="isPropertyDefined" access="public" returntype="boolean" output="false"
		hint="Checks if property name is defined in the properties. Not available until loadFramework() has been called.">
		<cfargument name="propertyName" type="string" required="true"/>
		<cfreturn getAppManager().getPropertyManager().isPropertyDefined(arguments.propertyName) />
	</cffunction> 

loadFramework

public void loadFramework( )

Loads the framework. Only call in onApplicationStart() event.

Parameters:

Code:

	<cffunction name="loadFramework" access="public" returntype="void" output="false"
		hint="Loads the framework. Only call in onApplicationStart() event.">
		
		<cfset var appKey = getAppKey() />
		
		
		<cfsetting requestTimeout="#MACHII_ONLOAD_REQUEST_TIMEOUT#" />
		
		
		<cfset application[appKey] = StructNew() />
		<cfset application[appKey].appLoader = CreateObject("component", "MachII.framework.AppLoader").init(MACHII_CONFIG_PATH, MACHII_DTD_PATH, AppKey, MACHII_VALIDATE_XML) />
		<cfset request.MachIIReload = FALSE />
	</cffunction> 

onApplicationStart

public boolean onApplicationStart( )

Run on the application start event. Override to provide customize functionality.

Parameters:

Code:

	<cffunction name="onApplicationStart" access="public" returntype="boolean" output="false"
		hint="Run on the application start event. Override to provide customize functionality.">
		
		<cfset LoadFramework() />
		
		<cfreturn TRUE />
	</cffunction> 

onRequestStart

public void onRequestStart( string targetPage )

Handles Mach-II requests. Output must be set to true. Override to provide custom functionality.

Parameters:
string targetPage

Code:

	<cffunction name="onRequestStart" access="public" returntype="void" output="true"
		hint="Handles Mach-II requests. Output must be set to true. Override to provide custom functionality.">
		<cfargument name="targetPage" type="string" required="true" />

		
		<cfif FindNoCase("index.cfm", arguments.targetPage)>
			<cfset handleRequest() />
		</cfif>
	</cffunction> 

onSessionEnd

public void onSessionEnd( struct sessionScope, struct applicationScope )

Handles on session end event if sessions are enabled for this application.

Parameters:
struct sessionScope
struct applicationScope

Code:

	<cffunction name="onSessionEnd" access="public" returntype="void" output="false"
		hint="Handles on session end event if sessions are enabled for this application.">
		<cfargument name="sessionScope" type="struct" required="true" />
		<cfargument name="applicationScope" type="struct" required="true" />
			
		<cfset arguments.applicationScope[getAppKey()].appLoader.getAppManager().onSessionEnd(arguments.sessionScope) />
	</cffunction> 

onSessionStart

public void onSessionStart( )

Handles on session start event if sessions are enabled for this application.

Parameters:

Code:

	<cffunction name="onSessionStart" access="public" returntype="void" output="false"
		hint="Handles on session start event if sessions are enabled for this application.">
		<cfset getAppManager().onSessionStart() />
	</cffunction> 

setProperty

public void setProperty( string propertyName, any propertyValue )

Sets the property value by name. Not available until loadFramework() has been called.

Parameters:
string propertyName
any propertyValue

Code:

	<cffunction name="setProperty" access="public" returntype="void" output="false"
		hint="Sets the property value by name. Not available until loadFramework() has been called.">
		<cfargument name="propertyName" type="string" required="true" />
		<cfargument name="propertyValue" type="any" required="true" />
		<cfset getAppManager().getPropertyManager().setProperty(arguments.propertyName, arguments.propertyValue) />
	</cffunction> 

shouldReloadConfig

public boolean shouldReloadConfig( )

Returns if the config should be dynamically reloaded.

Parameters:

Code:

	<cffunction name="shouldReloadConfig" access="public" returntype="boolean" output="false"
		hint="Returns if the config should be dynamically reloaded.">
		<cfreturn application[getAppKey()].appLoader.shouldReloadConfig() />
	</cffunction>