Ticket #345: Listener.cfc

File Listener.cfc, 3.1 kB (added by da_cameron, 17 years ago)

Updated Listener.cfc file

Line 
1<!---
2LICENSE INFORMATION:
3
4Copyright 2007, Joe Rinehart
5 
6Licensed under the Apache License, Version 2.0 (the "License"); you may not
7use this file except in compliance with the License.
8
9You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12       
13Unless required by applicable law or agreed to in writing, software distributed
14under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
15CONDITIONS OF ANY KIND, either express or implied. See the License for the
16specific language governing permissions and limitations under the License.
17
18VERSION INFORMATION:
19
20This file is part of Model-Glue Model-Glue: ColdFusion (2.0.304).
21
22The version number in parenthesis is in the format versionNumber.subversion.revisionNumber.
23--->
24
25
26<cfcomponent displayname="Listener" hint="I define a listener for a message broadcast." output="false">
27
28<cffunction name="init" returntype="ModelGlue.unity.listener.Listener" access="public" output="false">
29        <cfargument name="target" type="any" required="true" />
30        <cfargument name="method" type="string" required="true" />
31        <cfargument name="async" type="boolean" required="true" />
32       
33        <cfset variables._target = arguments.target />
34        <cfset variables._method = arguments.method />
35        <cfset variables._async = arguments.async />
36        <cfreturn this />
37</cffunction>
38
39<cffunction name="invokeListener" returntype="void" access="public" output="false">
40        <cfargument name="messageName" />
41        <cfargument name="parameters" />
42
43        <cfset var eventContext = parameters.event />
44        <cfset var future = "" />
45        <cfset var async = "" />
46       
47       
48        <cfif structKeyExists(variables._target, variables._method)>
49                <cfif not variables._async>
50            <cfset eventContext.addTraceStatement("Message-Listener", "Invoking function ""#variables._method#"" in the controller named ""#variables._target.getName()#""" , "&lt;message-listener message=""#arguments.messageName#"" function=""#variables._method#"" /&gt;", "OK") />
51                        <cfinvoke component="#variables._target#" method="#variables._method#" argumentcollection="#arguments.parameters#" />
52                <cfelse>
53            <cfset eventContext.addTraceStatement("Message-Listener", "Asynchronously invoking function ""#variables._method#""", "", "OK") />
54            <cfset eventContext.getEventRequest().setInvokedAsyncListeners(true) />
55                        <cfset future = createObject("component","Concurrency.FutureTask").init(task=variables._target, method=variables._method, event=eventContext)>
56                        <cfset async = createObject("component", "ModelGlue.unity.async.AsyncRequest").init(future, eventContext) />
57                        <cfset eventContext.getModelGlue().addAsyncRequest(arguments.messagename, async) />
58                        <cfset future.run()>
59                </cfif>
60        <cfelse>
61                <cfset eventContext.addTraceStatement("Listener Function Undefined", "The function ""<strong>#variables._method#</strong>"" can't be found in the controller #getMetaData(variables._target).name# (#getMetaData(variables._target).path#).  A common cause of this is mispelling the function name in the &lt;message-listener&gt; tag.", "&lt;message-listener message=""#arguments.messageName#"" function=""#variables._method#"" /&gt;", "WARNING") />
62        </cfif>
63</cffunction>
64
65</cfcomponent>