Software%20Architecture%20for%20ColdFusion%20Developers - PowerPoint PPT Presentation

About This Presentation
Title:

Software%20Architecture%20for%20ColdFusion%20Developers

Description:

Understand the Application framework in ColdFusion MX 7 and how it differs from ... Once you have a database and application design, and the directory structure is ... – PowerPoint PPT presentation

Number of Views:128
Avg rating:3.0/5.0
Slides: 33
Provided by: simonh1
Category:

less

Transcript and Presenter's Notes

Title: Software%20Architecture%20for%20ColdFusion%20Developers


1
Software Architecture for ColdFusion Developers
Unit 4 Application Events and Global Variables
2
Unit Objectives
  • At the end of this unit, students will
  • Understand the Application framework in
    ColdFusion MX 7 and how it differs from that of
    prior versions
  • Be comfortable defining applications and
    application event handlers in Application.cfc
  • Be able to describe the various global variable
    scopes and their uses

3
Unit Overview
  • Application Framework Overview
  • Application.cfc
  • Global Variable Scopes
  • Application Events

4
Application Framework Overview
  • Once you have a database and application design,
    and the directory structure is in place, use the
    application framework to
  • Define application settings
  • Handle application events
  • Create and/or manipulate global variables
  • application variables
  • session variables
  • request variables

5
Application Framework Overview contd
  • Before ColdFusion MX 7
  • Application.cfm ran before each request
  • OnRequestEnd.cfm ran after each request
  • When a page is requested, ColdFusion
  • looked in the local directory for Application.cfm
  • If no Application.cfm is found it traverses up
    the directory structure to the root drive,
    looking for one on each pass
  • The first time Application.cfm is found, it is
    processed before the page being requested and CF
    stops looking
  • If an Application.cfm was found, after the
    requested page runs, CF looks in the same
    directory as Application.cfm for a file named
    OnRequestEnd.cfm and, if found, runs it

6
Application Framework Overview contd
  • In ColdFusion MX 7
  • ColdFusion still looks in the current directory
    and then traverses to the root, but now it first
    looks for Application.cfc
  • If no Application.cfc is found, it looks in that
    directory for Application.cfm
  • If Application.cfc is found, ColdFusion creates
    an instance of the component and uses its data
    and methods
  • If no Application.cfc is found but
    Application.cfm is found, ColdFusion behaves as
    it did in prior versions (including
    Application.cfm, then the requested page, then
    OnRequestEnd.cfm if one exists in that directory)

7
Application.cfc What is it?
  • Application.cfc is a ColdFusion Component!
  • This component contains
  • Application settings definitions
  • Method definitions to handle application events
  • Any other code thats valid inside a CFC

8
Defining Application Settings
  • Prior to ColdFusion MX 7 application settings
    were defined with the ltCFAPPLICATIONgt tag
  • ltcfapplication
  • name"myApp"
  • applicationtimeout"createTimeSpan(2,0,0,0)"
  • sessionmanagement"true"
  • sessiontimeout"createTimeSpan(0,0,20,0)"gt

9
Defining Application Settings contd
  • In CFMX 7 these settings retain the same names as
    the ltCFAPPLICATIONgt attributes but are defined in
    the Application.cfc this scope outside of any
    methods

ltcfcomponentgt ltcfscriptgt this.name
"myApp" this.applicationtimeout
createTimeSpan(2,0,0,0) this.sessionmanagement
true this.sessiontimeout createTimeSpan(0,0,
20,0) lt/cfscriptgt lt/cfcomponentgt
10
Global Variable Scopes
  • Use the application scope for data values and
    component instances that rarely change and are
    shared by all users
  • Use the session scope for data values and
    component instances that must persist or rarely
    change and are user (session) specific
  • Use request scope for data values and component
    instances that change frequently or have no need
    to persist

11
Application Events
  • Application Starts for the first time
  • User sends first request session starts
  • Request begins
  • Request is processed
  • Request ends
  • Eventually, the session expires
  • Eventually, the application expires due to
    inactivity or a server restart
  • During any of the above, errors can occur in
    pages!

12
Application Event Handlers
  • A special function name is used for a method to
    handle each of the application events
  • onApplicationStart()
  • onSessionStart()
  • onRequestStart()
  • onRequest()
  • onRequestEnd()
  • onSessionEnd()
  • onApplicationEnd()
  • onError()
  • These methods are thread safe - locking is not
    necessary!

13
Events Prior to ColdFusion MX 7
  • Prior to ColdFusion MX 7
  • Application.cfm mimics onRequestStart()
  • onRequestEnd.cfm mimics onRequestEnd()
  • Conditional logic in Application.cfm mimics
    onSessionStart()
  • CFERROR in Application.cfm mimics onError()
  • Accessing the servlet response buffer in
    onRequestEnd.cfm kind of mimics onRequest()
  • There is no good way to mimic onSessionEnd()!!

14
Application Events onApplicationStart()
  • Code that is run only when an application
    initializes is contained inside a function named
    onApplicationStart
  • ReturnType Boolean (optional if no returntype is
    specified in the function definition)
  • Arguments None
  • Application doesnt start if false is returned or
    error thrown
  • Popular Uses
  • Set global variables and cached queries in the
    application scope
  • Contact Administrator (Email/SMS)?

15
onApplicationStart() Code Sample
  • ltcffunction name"onApplicationStart"
    returntype"boolean"gt
  • ltcfscriptgt
  • var done false
  • // set some application scope variables
  • application.dsn "myDSN"
  • application.dbUser "myDBUser"
  • application.dbPass "myDBPass"
  • done true
  • lt/cfscriptgt
  • ltcfreturn donegt
  • lt/cffunctiongt

16
Application Events onApplicationEnd()
  • Code that is run only when an application expires
    (due to activity timeout or elegant server
    shutdown) is contained inside a function named
    onApplicationEnd
  • ReturnType void
  • Arguments ApplicationScope (struct)
  • This method cannot access session or request
    scopes and can only access application scope as
    an argument server scope is accessible
  • Popular Uses
  • Clean-up application data and/or processes
  • Contact Administrator (Email/SMS)?

17
onApplicationEnd() Code Sample
  • ltcffunction name"onApplicationEnd"
    returntype"void"gt
  • ltcfargument name"appScope" type"struct"
    required"true"gt
  • ltcfscriptgt
  • var logMsg "Application shut down with the
    following keys in the application scope "
    structKeyList(arguments.appScope)
  • lt/cfscriptgt
  • ltcflog file"appEvents" text"logMsg"
    application"true"gt
  • ltcfmail to"webmaster_at_site.com"
    from"webmaster_at_site.com" subject"Application
    shut down"gt
  • logMsg
  • lt/cfmailgt
  • ltcfreturngt
  • lt/cffunctiongt

18
Session Events onSessionStart()
  • Code that is run the first time a user makes a
    request is contained inside a function named
    onSessionStart
  • ReturnType None
  • Arguments None
  • Popular Uses
  • Initialize session variables
  • Authentication and other user security-related
    code
  • Write to log file or database

19
onSessionStart() Code Sample
  • ltcffunction name"onSessionStart"
    returntype"void"gt
  • ltcfscriptgt
  • session.objUser createObject("component",
    "user")
  • lt/cfscriptgt
  • ltcfreturngt
  • lt/cffunctiongt

20
Session Events onSessionEnd()
  • Code that is run when a session ends just prior
    to the session being removed from memory (due to
    activity timeout or closing the browser if using
    J2EE sessions) is contained inside a function
    named onSessionEnd
  • ReturnType void
  • Arguments SessionScope (struct),
    ApplicationScope (struct)
  • Application and session scopes only accessible as
    arguments, server scope accessible, request scope
    unavailable
  • Popular Uses
  • Clean-up session data and/or processes
  • Write to log file or database

21
onSessionEnd() Code Sample
  • ltcffunction name"onSessionEnd"
    returntype"void"gt
  • ltcfargument name"sessionScope" type"struct"
    required"true"gt
  • ltcfargument name"appScope" type"struct"
    required"true"gt
  • ltcfscriptgt
  • var logMsg "Session ID " arguments.sessionSc
    ope.jSessionID "ended"
  • lt/cfscriptgt
  • ltcflog file"sessionEvents" text"logMsg"
    application"true"gt
  • ltcfreturngt
  • lt/cffunctiongt

22
Request Events onRequestStart()
  • Code that is run when a request begins (just
    prior to the requested page being executed) is
    contained inside a function named
    onRequestStart
  • ReturnType Boolean (optional if no returntype is
    specified in the function definition)
  • Arguments targetPage (string)
  • Request isnt processed if false is returned or
    error thrown
  • Popular Uses
  • Any business logic that needs to be performed on
    every request, such as user authorization or
    setting request specific variables

23
onRequestStart() Code Sample
  • ltcffunction name"onRequestStart"
    returntype"boolean"gt
  • ltcfargument name"target" type"string"
    required"true"gt
  • ltcfscriptgt
  • var allow session.objUser.allowAccess(argument
    s.target)
  • lt/cfscriptgt
  • ltcfreturn allowgt
  • lt/cffunctiongt

24
Request Events onRequestEnd()
  • Code that is run after a request is processed but
    before the output is returned to the client is
    contained inside a function named onRequestEnd
  • ReturnType void
  • Arguments TargetPage (string)
  • Popular Uses
  • Any business logic that needs to be performed
    after every request, such as performing data
    clean-up or performance metrics analysis of some
    sort

25
onRequestEnd() Code Sample
  • ltcffunction name"onRequestEnd"
    returntype"void"gt
  • ltcfargument name"target" type"string"
    required"true"gt
  • lt!--- not recommended - just an example ---gt
  • ltcfinclude template"footer.cfm"gt
  • ltcfreturngt
  • lt/cffunctiongt

26
Request Events onRequest()
  • Code that must intercept a request is contained
    inside a function named onRequest
  • ReturnType void
  • Arguments TargetPage (string)
  • Using this method intercepts the requested page
    and makes the CFC the request i.e. you must
    manually include any resource to send back to the
    client.
  • Using this method makes the variables scope of
    any requested page thats included visible to
    other Application.cfc methods
  • Do not use this method if there are CFCs accessed
    via SOAP or remoting that are effected by
    Application.cfc
  • Popular Uses
  • In order to intercept and/or manipulate the
    output stream (the output generated by the
    requested page) generated by a requested resource
    for example, you could append/prepend text,
    replace specific characters or words, etc. by
    including the resource requested in a
    ltCFSAVECONTENTgt and could manipulate that content
    before returning it to the client

27
onRequest() Code Sample
  • ltcffunction name"onRequest" returntype"void"gt
  • ltcfargument name"target" type"string"
    required"true"gt
  • ltcfscriptgtvar retText ""lt/cfscriptgt
  • ltcfsavecontent variable"retText"gtltcfinclude
    template"arguments.target"gtlt/cfsavecontentgt
  • ltcfscriptgt
  • retText replaceNoCase(retText, "Simon", "King
    Simon", "All")
  • writeOutput(retText)
  • lt/cfscriptgt
  • ltcfreturngt
  • lt/cffunctiongt

28
Error Events onError()
  • Code that is run whenever an unhandled exception
    occurs is contained inside a function named
    onError
  • ReturnType void
  • Arguments Exception (struct), EventName (string)
  • Event name argument holds name of Application.cfc
    event that threw exception is an empty string
    if onRequest() isnt used and the error wasnt
    thrown by an event method
  • Overrides any ltCFERRORgt or CFAdmin error settings
  • Cannot display anything to users if the error
    occurred in onApplicationEnd() or onSessionEnd()
  • If onError() throws an exception, its handled by
    ltCFERRORgt tags in the Application.cfc
    initializatoin block and by CFAdmin error pages
  • Popular Uses
  • Error handling

29
onError() Code Sample
  • ltcffunction name"onError" returntype"void"gt
  • ltcfargument name"objErr" type"struct"
    required"true"gt
  • ltcfargument name"guiltyEvent" type"string"
    required"true"gt
  • ltcfif trim(arguments.guiltyEvent) is ""gt
  • ltcfinclude template"error_template.cfm"gt
  • ltcfelsegt
  • ltcflog file"eventErrs" text"An error occured
    in the following event arguments.guiltyEvent"gt
  • lt/cfifgt
  • ltcfreturngt
  • lt/cffunctiongt

30
Other Information
  • You can add other methods to Application.cfc
  • Application.cfc can extend other components
  • You can manually call Application.cfc methods
    one great use for this is manually resetting a
    session
  • If you manually call an event the scope is not
    locked

31
Summary
  • The first code you write in an application should
    be to define the application, handle events, and
    set global variables
  • Application.cfc is a ColdFusion Component with 8
    reserved method names for handling events
  • Prior to CFMX 7, Application.cfm was used to
    implement a more limited application framework
  • Application settings are stored in the
    Application.cfc this scope
  • Global variables are defined in application
    events, as is any code that must execute when an
    application event occurs

32
QA
  • Questions?
Write a Comment
User Comments (0)
About PowerShow.com