- qpscanner/fusebox5/Application.cfc
- v0.7.4
- 18 KB
- 386
1<cfcomponent output="false">
2 <cfprocessingdirective suppresswhitespace="true">
3<!---
4Copyright 2006-2007 TeraTech, Inc. http://teratech.com/
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17--->
18
19 <!--- code that must execute at the very start of every single request --->
20 <!--- FB5: allow "" default - FB41 required this variable: --->
21 <cfparam name="variables.FUSEBOX_APPLICATION_PATH" default="" />
22 <!--- FB5: application key - FB41 always uses 'fusebox': --->
23 <cfparam name="variables.FUSEBOX_APPLICATION_KEY" default="fusebox" />
24 <!--- FB51: allow application to be included from other directories: --->
25 <cfparam name="variables.FUSEBOX_CALLER_PATH" default="#replace(getDirectoryFromPath(getBaseTemplatePath()),"\","/","all")#" />
26 <!--- FB55: easy way to override fusebox.xml parameters programmatically: --->
27 <cfparam name="variables.FUSEBOX_PARAMETERS" default="#structNew()#" />
28
29 <cfparam name="variables.attributes" default="#structNew()#" />
30 <cfset structAppend(attributes,URL,true) />
31 <cfset structAppend(attributes,form,true) />
32
33 <!--- FB5: uses request.__fusebox for internal tracking of compiler / runtime operations: --->
34 <cfset request.__fusebox = structNew() />
35
36 <!--- FB55: bleeding variables scope back and forth between fusebox5.cfm and this CFC for backward compatibility --->
37 <cfset variables.exposed = structNew() />
38
39 <!--- FB55: scaffolder integration --->
40 <cfif structKeyExists(attributes,"scaffolding.go")>
41 <!--- if we're not already executing the scaffolder, branch to it --->
42 <cfif findNoCase("/scaffolder/",CGI.SCRIPT_NAME) eq 0>
43 <cftry>
44 <cfinclude template="/scaffolder/manager.cfm" />
45 <cfcatch type="missinginclude">
46 <cfif structKeyExists(attributes,"scaffolding.debug")>
47 <cfrethrow />
48 </cfif>
49 <cfthrow type="fusebox.noScaffolder" message="Scaffolder not found" detail="You requested the scaffolder but /scaffolder/index.cfm does not exist." />
50 </cfcatch>
51 </cftry>
52 </cfif>
53 </cfif>
54
55 <cffunction name="bleed" returntype="any" access="public" output="false">
56 <cfargument name="outerVariables" type="any" required="true" />
57
58 <cfset variables.exposed = arguments.outerVariables />
59 <!--- expose known variables: --->
60 <cfset variables.exposed.attributes = variables.attributes />
61
62 <cfreturn this />
63
64 </cffunction>
65
66 <cffunction name="onApplicationStart" output="false">
67
68 <!--- FB5: myFusebox is an object but has FB41-compatible public properties --->
69 <cfset variables.myFusebox = createObject("component","myFusebox").init(variables.FUSEBOX_APPLICATION_KEY,variables.attributes,variables) />
70 <!--- expose known variables: --->
71 <cfset variables.exposed.myFusebox = variables.myFusebox />
72
73 <!--- FB55: guarantee XFA struct exists --->
74 <cfparam name="variables.xfa" default="#structNew()#" />
75 <!--- FB51: ticket 164: add OO synonym for attributes scope --->
76 <cfparam name="variables.event" default="#createObject('component','fuseboxEvent').init(attributes,xfa,myFusebox)#" />
77 <!--- expose known variables: --->
78 <cfset variables.exposed.xfa = variables.xfa />
79 <cfset variables.exposed.event = variables.event />
80
81 <cfset loadFusebox() />
82
83 </cffunction>
84
85 <cffunction name="onSessionStart" output="false">
86 </cffunction>
87
88 <cffunction name="onRequestStart" output="false">
89 <cfargument name="targetPage" type="string" required="true" />
90
91 <cfset var doCompile = true />
92
93 <!--- ensure CFC / Web Service / Flex Remoting calls are not intercepted --->
94 <cfif right(arguments.targetPage,4) is ".cfc">
95 <cfset doCompile = false />
96 <cfset structDelete(variables,"onRequest") />
97 <cfset structDelete(this,"onRequest") />
98 </cfif>
99
100 <!--- onApplicationStart() may create this (on first request) --->
101 <cfif not structKeyExists(variables,"myFusebox")>
102 <!--- FB5: myFusebox is an object but has FB41-compatible public properties --->
103 <cfset variables.myFusebox = createObject("component","myFusebox").init(variables.FUSEBOX_APPLICATION_KEY,variables.attributes,variables) />
104 <!--- expose known variables: --->
105 <cfset variables.exposed.myFusebox = variables.myFusebox />
106 </cfif>
107
108 <!--- FB55: guarantee XFA struct exists --->
109 <cfparam name="variables.xfa" default="#structNew()#" />
110 <!--- FB51: ticket 164: add OO synonym for attributes scope --->
111 <cfparam name="variables.event" default="#createObject('component','fuseboxEvent').init(variables.attributes,variables.xfa,variables.myFusebox)#" />
112 <!--- expose known variables: --->
113 <cfset variables.exposed.xfa = variables.xfa />
114 <cfset variables.exposed.event = variables.event />
115
116 <cfif variables.myFusebox.parameters.load>
117 <cflock name="#application.ApplicationName#_fusebox_#variables.FUSEBOX_APPLICATION_KEY#" type="exclusive" timeout="300">
118 <cfif variables.myFusebox.parameters.load>
119 <cfset loadFusebox() />
120 <cfelse>
121 <!--- _fba should *not* be exposed --->
122 <cfset variables._fba = application[variables.FUSEBOX_APPLICATION_KEY] />
123 <!--- fix attributes precedence --->
124 <cfif _fba.precedenceFormOrURL is "URL">
125 <cfset structAppend(variables.attributes,URL,true) />
126 </cfif>
127 <!--- set the default fuseaction if necessary --->
128 <cfif not structKeyExists(variables.attributes,_fba.fuseactionVariable) or trim(variables.attributes[_fba.fuseactionVariable]) is "">
129 <cfset variables.attributes[_fba.fuseactionVariable] = _fba.defaultFuseaction />
130 </cfif>
131 <cfset variables.attributes[_fba.fuseactionVariable] = trim(variables.attributes[_fba.fuseactionVariable]) />
132 <cfset variables.attributes.fuseaction = variables.attributes[_fba.fuseactionVariable] />
133 </cfif>
134 </cflock>
135 <cfelse>
136 <cfset variables._fba = application[variables.FUSEBOX_APPLICATION_KEY] />
137 <!--- fix attributes precedence --->
138 <cfif _fba.precedenceFormOrURL is "URL">
139 <cfset structAppend(variables.attributes,URL,true) />
140 </cfif>
141 <!--- set the default fuseaction if necessary --->
142 <cfif not structKeyExists(variables.attributes,_fba.fuseactionVariable) or trim(variables.attributes[_fba.fuseactionVariable]) is "">
143 <cfset variables.attributes[_fba.fuseactionVariable] = _fba.defaultFuseaction />
144 </cfif>
145 <cfset variables.attributes[_fba.fuseactionVariable] = trim(variables.attributes[_fba.fuseactionVariable]) />
146 <cfset variables.attributes.fuseaction = variables.attributes[_fba.fuseactionVariable] />
147 </cfif>
148
149 <!---
150 Fusebox 4.1 did not set attributes.fuseaction or default the fuseaction variable until
151 *after* fusebox.init.cfm had run. This made it hard for fusebox.init.cfm to do URL
152 rewriting. For Fusebox 5, we default the fuseaction variable and set attributes.fuseaction
153 before fusebox.init.cfm so it can rely on attributes.fuseaction and rewrite that. However,
154 in order to maintain backward compatibility, we need to allow fusebox.init.cfm to set
155 attributes[_fba.fuseactionVariable] and still have that reflected in attributes.fuseaction
156 and for that to actually be the request that gets processed.
157 --->
158 <cfif _fba.debug>
159 <cfset variables.myFusebox.trace("Fusebox","Including fusebox.init.cfm") />
160 </cfif>
161 <cftry>
162 <!--- _fba_ttr_fav and _ba_attr_fa should *not* be exposed --->
163 <cfset _fba_attr_fav = variables.attributes[_fba.fuseactionVariable] />
164 <cfset _fba_attr_fa = variables.attributes.fuseaction />
165 <cfinclude template="#_fba.getCoreToAppRootPath()#fusebox.init.cfm" />
166 <cfif variables.attributes.fuseaction is not _fba_attr_fa>
167 <cfif variables.attributes.fuseaction is not variables.attributes[_fba.fuseactionVariable]>
168 <cfif variables.attributes[_fba.fuseactionVariable] is not _fba_attr_fav>
169 <!--- inconsistent modification of both variables?!? --->
170 <cfthrow type="fusebox.inconsistentFuseaction"
171 message="Inconsistent fuseaction variables"
172 detail="Both attributes.fuseaction and attributes[{fusebox}.fuseactionVariable] changed in fusebox.init.cfm so Fusebox doesn't know what to do with the values!" />
173 <cfelse>
174 <!--- ok, only attributes.fuseaction changed --->
175 <cfset variables.attributes[_fba.fuseactionVariable] = variables.attributes.fuseaction />
176 </cfif>
177 <cfelse>
178 <!--- ok, they were both changed and they match --->
179 </cfif>
180 <cfelse>
181 <!--- attributes.fuseaction did not change --->
182 <cfif variables.attributes[_fba.fuseactionVariable] is not _fba_attr_fav>
183 <!--- make attributes.fuseaction match the other changed variable --->
184 <cfset variables.attributes.fuseaction = variables.attributes[_fba.fuseactionVariable] />
185 <cfelse>
186 <!--- ok, neither variable changed --->
187 </cfif>
188 </cfif>
189 <cfcatch type="missinginclude" />
190 </cftry>
191 <cfif doCompile>
192 <!---
193 must special case development-circuit-load mode since it causes circuits to reload during
194 the compile (post-load) phase and therefore must be exclusive
195 --->
196 <cfif _fba.debug>
197 <cfset variables.myFusebox.trace("Fusebox","Compiling requested fuseaction '#variables.attributes.fuseaction#'") />
198 </cfif>
199 <!--- _parsedFileData should *not* be exposed --->
200 <cfif _fba.mode is "development-circuit-load">
201 <cflock name="#application.ApplicationName#_fusebox_#variables.FUSEBOX_APPLICATION_KEY#" type="exclusive" timeout="300">
202 <cfset variables._parsedFileData = _fba.compileRequest(attributes.fuseaction,myFusebox) />
203 </cflock>
204 <cfelse>
205 <cflock name="#application.ApplicationName#_fusebox_#variables.FUSEBOX_APPLICATION_KEY#" type="readonly" timeout="300">
206 <cfset variables._parsedFileData = _fba.compileRequest(attributes.fuseaction,myFusebox) />
207 </cflock>
208 </cfif>
209 </cfif>
210
211 </cffunction>
212
213 <!--- excuse the formatting here - it is done to completely suppress whitespace --->
214 <cffunction name="onRequest"><cfargument
215 name="targetPage" type="string" required="true" /><cfsetting
216 enablecfoutputonly="true">
217
218 <cfif variables.myFusebox.parameters.execute>
219 <cfif _fba.debug>
220 <cfset myFusebox.trace("Fusebox","Including parsed file for '#variables.attributes.fuseaction#'") />
221 </cfif>
222 <cftry>
223 <!---
224 readonly lock protects against including the parsed file while
225 another threading is writing it...
226 --->
227 <cflock name="#_parsedFileData.lockName#" type="readonly" timeout="30">
228 <cfinclude template="#_parsedFileData.parsedFile#" />
229 </cflock>
230 <cfcatch type="missinginclude">
231 <cfif right(cfcatch.missingFileName, len(_parsedFileData.parsedName)) is _parsedFileData.parsedName>
232 <cfthrow type="fusebox.missingParsedFile"
233 message="Parsed File or Directory not found."
234 detail="Attempting to execute the parsed file '#_parsedFileData.parsedName#' threw an error. This can occur if the parsed file does not exist in the parsed directory or if the parsed directory itself is missing." />
235 <cfelse>
236 <cfrethrow />
237 </cfif>
238 </cfcatch>
239 </cftry>
240 </cfif>
241
242 <cfsetting enablecfoutputonly="false">
243 </cffunction>
244
245 <cffunction name="onRequestEnd" output="true">
246 <cfargument name="targetPage" type="string" required="true" />
247
248 <cfif structKeyExists(variables,"myFusebox")>
249 <cfset variables.myFusebox.trace("Fusebox","Request completed") />
250 </cfif>
251 <cfif isDefined("_fba.debug") and _fba.debug and structKeyExists(variables,"myFusebox") and right(arguments.targetPage,4) is not ".cfc">
252 <cfoutput>#variables.myFusebox.renderTrace()#</cfoutput>
253 </cfif>
254
255 </cffunction>
256
257 <cffunction name="onSessionEnd" output="false">
258 </cffunction>
259
260 <cffunction name="onApplicationEnd" output="false">
261 </cffunction>
262
263 <cffunction name="onError">
264 <cfargument name="exception" />
265
266 <cfset var stack = 0 />
267 <cfset var prefix = "Raised at " />
268
269 <!--- top-level exception is always event name / expression for Application.cfc (but not fusebox5.cfm) --->
270 <cfset var caughtException = arguments.exception />
271
272 <cfif structKeyExists(caughtException,"rootcause")>
273 <cfset caughtException = caughtException.rootcause />
274 </cfif>
275
276 <cfif listFirst(caughtException.type,".") is "fusebox">
277 <cfif isDefined("_fba.debug") and _fba.debug and structKeyExists(variables,"myFusebox")>
278 <cfset variables.myFusebox.trace("Fusebox","Caught Fusebox exception '#caughtException.type#'") />
279 <cfif structKeyExists(caughtException,"tagcontext")>
280 <cfloop index="stack" from="1" to="#arrayLen(caughtException.tagContext)#">
281 <cfset variables.myFusebox.trace("Fusebox",prefix &
282 caughtException.tagContext[stack].template & ":" &
283 caughtException.tagContext[stack].line) />
284 <cfset prefix = "Called from " />
285 </cfloop>
286 </cfif>
287 </cfif>
288 <cfif not isDefined("_fba.errortemplatesPath") or (
289 structKeyExists(variables,"attributes") and structKeyExists(variables,"myFusebox") and
290 not _fba.handleFuseboxException(caughtException,variables.attributes,variables.myFusebox,variables.FUSEBOX_APPLICATION_KEY)
291 )>
292 <cfif isDefined("_fba.debug") and _fba.debug and structKeyExists(variables,"myFusebox")>
293 <cfoutput>#variables.myFusebox.renderTrace()#</cfoutput>
294 </cfif>
295 <cfthrow object="#caughtException#" />
296 </cfif>
297 <cfelse>
298 <cfif isDefined("_fba.debug") and _fba.debug and structKeyExists(variables,"myFusebox")>
299 <cfset variables.myFusebox.trace("Fusebox","Request failed with exception '#caughtException.type#' (#caughtException.message#)") />
300 <cfif structKeyExists(caughtException,"tagcontext")>
301 <cfloop index="stack" from="1" to="#arrayLen(caughtException.tagContext)#">
302 <cfset variables.myFusebox.trace("Fusebox",prefix &
303 caughtException.tagContext[stack].template & ":" &
304 caughtException.tagContext[stack].line) />
305 <cfset prefix = "Called from " />
306 </cfloop>
307 </cfif>
308 <cfoutput>#variables.myFusebox.renderTrace()#</cfoutput>
309 </cfif>
310 <cfthrow object="#caughtException#" />
311 </cfif>
312
313 <!--- if we hit an error before starting the request, prevent the request from running --->
314 <cfset myFusebox.parameters.execute = false />
315
316 </cffunction>
317
318 <cffunction name="override" returntype="void" access="public" output="false">
319 <cfargument name="name" type="string" required="true" />
320 <cfargument name="value" type="any" required="true" />
321 <cfargument name="useThisScope" type="boolean" default="false" />
322
323 <cfif arguments.useThisScope>
324 <cfset this[arguments.name] = arguments.value />
325 <cfelse>
326 <cfset variables[arguments.name] = arguments.value />
327 </cfif>
328
329 </cffunction>
330
331 <cffunction name="onFuseboxApplicationStart">
332 </cffunction>
333
334 <cffunction name="loadFusebox" access="private" output="false">
335 <!--- ticket 232: extend request timeout value on framework load --->
336 <cfsetting requesttimeout="600" />
337 <cfif not structKeyExists(application,variables.FUSEBOX_APPLICATION_KEY) or variables.myFusebox.parameters.userProvidedLoadParameter>
338 <!--- can't be conditional: we don't know the state of the debug flag yet --->
339 <cfset variables.myFusebox.trace("Fusebox","Creating Fusebox application object") />
340 <cfset variables._fba = createObject("component","fuseboxApplication") />
341 <cfset application[variables.FUSEBOX_APPLICATION_KEY] = _fba.init(variables.FUSEBOX_APPLICATION_KEY,variables.FUSEBOX_APPLICATION_PATH,variables.myFusebox,variables.FUSEBOX_CALLER_PATH,variables.FUSEBOX_PARAMETERS) />
342 <cfelse>
343 <!--- can't be conditional: we don't know the state of the debug flag yet --->
344 <cfset variables.myFusebox.trace("Fusebox","Reloading Fusebox application object") />
345 <cfset variables._fba = application[variables.FUSEBOX_APPLICATION_KEY] />
346 <!--- it exists and the load is implicit, not explicit (via user) so just reload XML --->
347 <cfset _fba.reload(variables.FUSEBOX_APPLICATION_KEY,variables.FUSEBOX_APPLICATION_PATH,variables.myFusebox,variables.FUSEBOX_PARAMETERS) />
348 </cfif>
349 <!--- fix attributes precedence --->
350 <cfif _fba.precedenceFormOrURL is "URL">
351 <cfset structAppend(variables.attributes,URL,true) />
352 </cfif>
353 <!--- set the default fuseaction if necessary --->
354 <cfif not structKeyExists(variables.attributes,_fba.fuseactionVariable) or variables.attributes[_fba.fuseactionVariable] is "">
355 <cfset variables.attributes[_fba.fuseactionVariable] = _fba.defaultFuseaction />
356 </cfif>
357 <!--- set this up for fusebox.appinit.cfm --->
358 <cfset variables.attributes.fuseaction = variables.attributes[_fba.fuseactionVariable] />
359 <!--- flag this as the first request for the application --->
360 <cfset variables.myFusebox.applicationStart = true />
361 <!--- force parse after reload for consistency in development modes --->
362 <cfif _fba.mode is not "production" or variables.myFusebox.parameters.userProvidedLoadParameter>
363 <cfset variables.myFusebox.parameters.parse = true />
364 </cfif>
365 <!--- need all of the above set before we attempt any compiles! --->
366 <cfif variables.myFusebox.parameters.parseall>
367 <cfset _fba.compileAll(variables.myFusebox) />
368 </cfif>
369 <!--- FB55: template method to allow no-XML application initialization --->
370 <cfif _fba.debug>
371 <cfset variables.myFusebox.trace("Fusebox","Executing onFuseboxApplicationStart()") />
372 </cfif>
373 <cfset onFuseboxApplicationStart() />
374 <!--- FB5: new appinit include file --->
375 <cfif _fba.debug>
376 <cfset variables.myFusebox.trace("Fusebox","Including fusebox.appinit.cfm") />
377 </cfif>
378 <cftry>
379 <cfinclude template="#_fba.getCoreToAppRootPath()#fusebox.appinit.cfm" />
380 <cfcatch type="missinginclude" />
381 </cftry>
382 <!--- ticket 269 ensure there is no double reload at CF startup --->
383 <cfset variables.myFusebox.parameters.load = false />
384 </cffunction>
385
386 </cfprocessingdirective>
387</cfcomponent>