Custom Event Gateways
Preface
Here you will find a short introduction into writing your own Event Gateway type.
Since you can write these in pure cfml (and Java when you want it), it is really simple to do.
There are 2 to 3 files you need to create:
- the Gateway cfc
- the Gateway Driver cfc
- A listener cfc
The Gateway CFC
This is the file which contains the action you want your gateway to do.
Also, it is the file which is instantiated by Lucee when the gateway starts.
You can take the following files as an example:
- {Lucee-install}/lib/lucee-server/context/gateway/lucee/extension/gateway/DirectoryWatcher.cfc
- {Lucee-install}/lib/lucee-server/context/gateway/lucee/extension/gateway/MailWatcher.cfc
The example code shown underneath is a modified version of the DirectoryWatcher.cfc, which, at time of writing, is in line for reviewing at the Lucee team.
By default, you need to have the following functions:
- An init function, which receives the necessary config data.
- A start function, which continues to run while variables.state="running".
- A stop and restart function
- A getState function, which returns the current state of the gateway instance (running,stopping,stopped)
- A sendMessage function, which will be called when the CFML sendGatewayMessage function is used.
The following is all the code you need:
<cfcomponent output="no">
<cfset variables.logFileName="DirectoryWatcher" />
<cfset variables.state="stopped" />
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"init"</span> <span class="nv">access</span><span class="o">=</span><span class="s2">"public"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"void"</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"id"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"true"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"string"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"config"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"true"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"struct"</span> <span class="k">default</span><span class="o">=</span><span class="s2">"</span><span class="s-Interp">#structNew()#</span><span class="s2">"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"listener"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"true"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"component"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="k">var</span> <span class="nv">cfcatch</span><span class="o">=</span><span class="s2">""</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cftry></span>
<span class="nb"><cfset</span> <span class="nv">variables.id</span><span class="o">=</span><span class="nv">id</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="nv">variables.config</span><span class="o">=</span><span class="nv">config</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="nv">variables.listener</span><span class="o">=</span><span class="nv">listener</span> <span class="o">/</span><span class="nb">></span>
<span class="nt"><cflog</span> <span class="na">text=</span><span class="s">"init"</span> <span class="na">type=</span><span class="s">"information"</span> <span class="na">file=</span><span class="s">"#variables.logFileName#"</span> <span class="nt">/></span>
<span class="nb"><cfcatch></span>
<span class="nb"><cfset</span> <span class="nf">_handleError</span><span class="p">(</span><span class="nv">cfcatch</span><span class="p">,</span> <span class="s2">"init"</span><span class="p">)</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cfcatch></span>
<span class="nb"></cftry></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"start"</span> <span class="nv">access</span><span class="o">=</span><span class="s2">"public"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"void"</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="k">var</span> <span class="nv">sleepStep</span><span class="o">=</span><span class="nf">iif</span><span class="p">(</span><span class="nv">variables.config.interval</span> <span class="o">lt</span> <span class="m">500</span><span class="p">,</span> <span class="s1">'variables.config.interval'</span><span class="p">,</span> <span class="nf">de</span><span class="p">(</span><span class="m">500</span><span class="p">))</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="k">var</span> <span class="nv">i</span><span class="o">=-</span><span class="m">1</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="k">var</span> <span class="nv">cfcatch</span><span class="o">=</span><span class="s2">""</span> <span class="o">/</span><span class="nb">></span>
<span class="err"><</span>--- when restart() is called, we enter this loop until the previous execution has ended. --->
<span class="nt"><cfwhile</span> <span class="na">variables</span><span class="err">.</span><span class="na">state</span> <span class="na">EQ</span> <span class="err">"</span><span class="na">stopping</span><span class="err">"</span><span class="nt">></span>
<span class="nb"><cfset</span> <span class="nf">sleep</span><span class="p">(</span><span class="m">10</span><span class="p">)</span><span class="nb">></span>
<span class="nt"></cfwhile></span>
<span class="nb"><cfset</span> <span class="nv">variables.state</span><span class="o">=</span><span class="s2">"running"</span> <span class="o">/</span><span class="nb">></span>
<span class="nt"><cflog</span> <span class="na">text=</span><span class="s">"start"</span> <span class="na">type=</span><span class="s">"information"</span> <span class="na">file=</span><span class="s">"#variables.logFileName#"</span><span class="nt">></span>
<span class="nt"><cfwhile</span> <span class="na">variables</span><span class="err">.</span><span class="na">state</span> <span class="na">EQ</span> <span class="err">"</span><span class="na">running</span><span class="err">"</span><span class="nt">></span>
<span class="nb"><cftry></span>
<span class="err"><</span>--- YOUR GATEWAY ACTIONS HERE --->
<span class="nb"><cfcatch></span>
<span class="nb"><cfset</span> <span class="nf">_handleError</span><span class="p">(</span><span class="nv">cfcatch</span><span class="p">,</span> <span class="s2">"start"</span><span class="p">)</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cfcatch></span>
<span class="nb"></cftry></span>
<span class="err"><</span>--- sleep until the next run, but cut it into half seconds, so we can stop the gateway easily --->
<span class="nb"><cfloop</span> <span class="nv">from</span><span class="o">=</span><span class="s2">"</span><span class="s-Interp">#sleepStep#</span><span class="s2">"</span> <span class="nv">to</span><span class="o">=</span><span class="s2">"</span><span class="s-Interp">#variables.config.interval#</span><span class="s2">"</span> <span class="nv">step</span><span class="o">=</span><span class="s2">"</span><span class="s-Interp">#sleepStep#</span><span class="s2">"</span> <span class="nv">index</span><span class="o">=</span><span class="s2">"i"</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="nf">sleep</span><span class="p">(</span><span class="nv">sleepStep</span><span class="p">)</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfif</span> <span class="nv">variables.state</span> <span class="nv">neq</span> <span class="s2">"running"</span><span class="nb">></span>
<span class="nb"><cfbreak</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cfif></span>
<span class="nb"></cfloop></span>
<span class="err"><</span>--- some extra sleeping if the requested timeout is not yet completely done --->
<span class="nb"><cfif</span> <span class="nv">variables.config.interval</span> <span class="o">mod</span> <span class="nv">sleepStep</span> <span class="o">and</span> <span class="nv">variables.state</span> <span class="o">eq</span> <span class="s2">"running"</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="nf">sleep</span><span class="p">((</span><span class="nv">variables.config.interval</span> <span class="o">mod</span> <span class="nv">sleepStep</span><span class="p">))</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cfif></span>
<span class="nt"></cfwhile></span>
<span class="nb"><cfset</span> <span class="nv">variables.state</span><span class="o">=</span><span class="s2">"stopped"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"stop"</span> <span class="nv">access</span><span class="o">=</span><span class="s2">"public"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"void"</span><span class="nb">></span>
<span class="nt"><cflog</span> <span class="na">text=</span><span class="s">"stop"</span> <span class="na">type=</span><span class="s">"information"</span> <span class="na">file=</span><span class="s">"#variables.logFileName#"</span><span class="nt">></span>
<span class="nb"><cfset</span> <span class="nv">variables.state</span><span class="o">=</span><span class="s2">"stopping"</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"restart"</span> <span class="nv">access</span><span class="o">=</span><span class="s2">"public"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"void"</span><span class="nb">></span>
<span class="nb"><cfif</span> <span class="nv">variables.state</span> <span class="o">EQ</span> <span class="s2">"running"</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="nf">stop</span><span class="p">()</span><span class="nb">></span>
<span class="nb"></cfif></span>
<span class="nb"><cfset</span> <span class="nf">start</span><span class="p">()</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"getState"</span> <span class="nv">access</span><span class="o">=</span><span class="s2">"public"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"string"</span><span class="nb">></span>
<span class="nb"><cfreturn</span> <span class="nv">variables.state</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"sendMessage"</span> <span class="nv">access</span><span class="o">=</span><span class="s2">"public"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"string"</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"data"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"false"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"struct"</span><span class="nb">></span>
<span class="nb"><cfreturn</span> <span class="s2">"ERROR: sendMessage not supported"</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"_handleError"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"void"</span> <span class="nv">access</span><span class="o">=</span><span class="s2">"private"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"catchData"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"yes"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"functionName"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"string"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"no"</span> <span class="k">default</span><span class="o">=</span><span class="s2">"unknown"</span> <span class="o">/</span><span class="nb">></span>
<span class="nt"><cflog</span> <span class="na">text=</span><span class="s">"Function #arguments.functionName#: #arguments.catchData.message# #arguments.catchData.detail#"</span> <span class="na">type=</span><span class="s">"error"</span> <span class="na">file=</span><span class="s">"#variables.logFileName#"</span> <span class="nt">/></span>
<span class="nb"></cffunction></span>
</cfcomponent>
I guess you noticed the comment "YOUR GATEWAY ACTIONS HERE"? That's where you add the real functionality.
The Gateway Driver
The driver is used to configure and define your Gateway.
With it, you define the form fields in the Lucee admin settings page for your gateway, and it makes sure that your gateway is listed as an available Gateway.
The Gateway Driver is a CFC file, which must be added into the directory {Lucee-install}/lib/lucee-server/context/admin/gdriver/
Gateway driver Functions
-
getClass():string. Returns the Java class name. If the gateway is Java based, then the Java class has to implement the interface "org.opencfml.eventgateway.Gateway". If it is not Java based, then this method must return an empty string or void.
-
getCFCPath():string. Returns the cfc path, when the gateway is cfc based, If it is not cfc based, then this method must return an empty string or void.
-
getLabel():string. The label (friendly name) of the gateway.
-
getDescription():string The description of the gateway
-
onBeforeUpdate(string cfcPath, string startupMode, struct custom):void. This method is invoked before the settings entered in the form are saved. This method can be used to validate the entered data.
-
onBeforeError(cfcatch):void. Invoked before an error is thrown. Can be used to throw your own error, and/or do logging.
-
getListenerCfcMode():string. Can be one of the following:
- "none": no listener gets defined
- "required": defining a listener is required
-
getListenerPath():string The default location of a listener cfc. (only used when the listenerCfcMode is not "none")
Example Gateway driver
<cfcomponent extends="Gateway" output="no">
<--- The form fields which will be shown when adding a gateway instance via the Lucee admin --->
<--- argument names (see file Gateway.cfc): displayName, name, defaultValue, required, description, type, values --->
<cfset variables.fields = array(
field( "Path to file", "filepath", "", true, "The file you want to check the size for", "text"),
field( "Minimum file size", "minimalsize", "", true, "The minimum size of the file, in Bytes, before the Listener CFC is called", "text"),
field( "Interval (ms)", "interval", "60000", true, "The interval between checks, in milliseconds", "text"),
field( "CFC Listener Function name", "listenerFunction", "onChange", true, "Called when the file reaches the minimum file size", "text")
) />
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"getClass"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"string"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span><span class="nb">></span>
<span class="nb"><cfreturn</span> <span class="s2">""</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"getCFCPath"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"string"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span><span class="nb">></span>
<span class="nb"><cfreturn</span> <span class="s2">"filesizechecker.FileSizeWatcher"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"getLabel"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"string"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span><span class="nb">></span>
<span class="nb"><cfreturn</span> <span class="s2">"Filesize Watcher"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"getDescription"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"string"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span><span class="nb">></span>
<span class="nb"><cfreturn</span> <span class="s2">"Watches the filesize of a certain file"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"onBeforeUpdate"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"void"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"false"</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"cfcPath"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"true"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"string"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"startupMode"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"true"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"string"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"custom"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"true"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"struct"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="k">var</span> <span class="nv">errors</span><span class="o">=</span><span class="p">[</span> <span class="p">]</span> <span class="o">/</span><span class="nb">></span>
<span class="err"><</span>--- does gven file exist? --->
<span class="nb"><cfif</span> <span class="o">not</span> <span class="nf">fileExists</span><span class="p">(</span><span class="nv">arguments.custom.filepath</span><span class="p">)</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="nf">arrayAppend</span><span class="p">(</span><span class="nv">errors</span><span class="p">,</span> <span class="s2">"The file [</span><span class="s-Interp">#arguments.custom.filepath#</span><span class="s2">] does not exist"</span><span class="p">)</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cfif></span>
<span class="err"><</span>--- interval --->
<span class="nb"><cfif</span> <span class="o">not</span> <span class="nf">IsNumeric</span><span class="p">(</span><span class="nv">custom.interval</span><span class="p">)</span> <span class="o">or</span> <span class="nv">custom.interval</span> <span class="o">LT</span> <span class="m">1</span> <span class="o">or</span> <span class="nf">int</span><span class="p">(</span><span class="nv">custom.interval</span><span class="p">)</span> <span class="nv">neq</span> <span class="nv">custom.interval</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="nf">arrayAppend</span><span class="p">(</span><span class="nv">errors</span><span class="p">,</span> <span class="s2">"The interval [</span><span class="s-Interp">#custom.interval#</span><span class="s2">] must be a numeric value greater than 0"</span><span class="p">)</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cfif></span>
<span class="err"><</span>--- minimalsize --->
<span class="nb"><cfif</span> <span class="o">not</span> <span class="nf">IsNumeric</span><span class="p">(</span><span class="nv">custom.minimalsize</span><span class="p">)</span> <span class="o">or</span> <span class="nv">custom.minimalsize</span> <span class="o">LT</span> <span class="m">1</span> <span class="o">or</span> <span class="nf">int</span><span class="p">(</span><span class="nv">custom.minimalsize</span><span class="p">)</span> <span class="nv">neq</span> <span class="nv">custom.minimalsize</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="nf">arrayAppend</span><span class="p">(</span><span class="nv">errors</span><span class="p">,</span> <span class="s2">"The Minimum file size [</span><span class="s-Interp">#custom.minimalsize#</span><span class="s2">] must be a numeric value greater than 0"</span><span class="p">)</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cfif></span>
<span class="nb"><cfif</span> <span class="nf">arrayLen</span><span class="p">(</span><span class="nv">errors</span><span class="p">)</span><span class="nb">></span>
<span class="nb"><cfthrow</span> <span class="nv">message</span><span class="o">=</span><span class="s2">"The following error(s) occurred while validating your input: <ul</span><span class="nb">></span><span class="nt"><li></span>#arrayToList(errors, '<span class="nt"></li><li></span>')#<span class="nt"></li></ul></span>" />
<span class="nb"></cfif></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"getListenerCfcMode"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"string"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span> <span class="nv">hint</span><span class="o">=</span><span class="s2">"Returns either 'none' or 'required'"</span><span class="nb">></span>
<span class="nb"><cfreturn</span> <span class="s2">"required"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"getListenerPath"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"string"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span> <span class="nv">hint</span><span class="o">=</span><span class="s2">"Returns the path to the default Listener cfc"</span><span class="nb">></span>
<span class="nb"><cfreturn</span> <span class="s2">"filesizechecker.FileBackuper"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cffunction></span>
</cfcomponent>
Also see the file Gateway.cfc, which extends the functionality of this example (
The Listener CFC
Most gateways need a Listener CFC to respond to events occurring in the Gateway instance. For example, if the Mail watcher finds new email in the mailbox, then it needs to do something with that event; it calls a method (function) of the Listener CFC. The path to your listener CFC must be given as an argument when you create or update a gateway instance. The contents of the CFC is completely up to you, as long as it has a public function that can be called by the Gateway.
Example
Let's say our Gateway type to create is a "filesize checker", which checks a file for a minimum filesize. If the file's size has the minimum filesize, then we will call the listener CFC. First, we'll create the listener CFC:
<cfcomponent output="no">
<cffunction name="onBigFilesize" access="public" output="no" returntype="void">
<cfargument name="filepath" required="true" type="string" />
<cfargument name="size" required="true" type="numeric" />
<span class="err"><</span>--- create a non-existing zipfile path--->
<span class="nb"><cfset</span> <span class="k">var</span> <span class="nv">zipFileName</span><span class="o">=</span><span class="nv">a</span> <span class="nv">rguments.filepath</span> <span class="o">&</span> <span class="s2">".zip"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="k">var</span> <span class="nv">nr</span><span class="o">=</span><span class="m">1</span> <span class="o">/</span><span class="nb">></span>
<span class="nt"><cfwhile</span> <span class="na">fileExists</span><span class="err">(</span><span class="na">zipFileName</span><span class="err">)</span><span class="nt">></span>
<span class="nb"><cfset</span> <span class="nv">zipFileName</span><span class="o">=</span><span class="nv">a</span> <span class="nv">rguments.filepath</span> <span class="o">&</span> <span class="s2">".</span><span class="s-Interp">#nr#</span><span class="s2">.zip"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfset</span> <span class="o">++</span><span class="nv">nr</span> <span class="o">/</span><span class="nb">></span>
<span class="nt"></cfwhile></span>
<span class="nb"><cftry></span>
<span class="err"><</span>--- zip the file --->
<span class="nb"><cfzip</span> <span class="nv">action</span><span class="o">=</span><span class="s2">"zip"</span> <span class="nv">source</span><span class="o">=</span><span class="s2">"</span><span class="s-Interp">#arguments.filepath#</span><span class="s2">"</span> <span class="nv">file</span><span class="o">=</span><span class="s2">"</span><span class="s-Interp">#zipFileName#</span><span class="s2">"</span> <span class="o">/</span><span class="nb">></span>
<span class="err"><</span>--- log the zip action --->
<span class="nt"><cflog</span> <span class="na">text=</span><span class="s">"Backed up #arguments.filepath# to #zipFileName#"</span> <span class="na">type=</span><span class="s">"information"</span> <span class="na">file=</span><span class="s">"#variables.logFileName#"</span> <span class="nt">/></span>
<span class="err"><</span>--- now delete the file --->
<span class="nb"><cffile</span> <span class="nv">action</span><span class="o">=</span><span class="s2">"delete"</span> <span class="nv">file</span><span class="o">=</span><span class="s2">"</span><span class="s-Interp">#arguments.filepath#</span><span class="s2">"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfcatch></span>
<span class="nb"><cfset</span> <span class="nf">_handleError</span><span class="p">(</span><span class="nv">cfcatch</span><span class="p">,</span> <span class="s2">"onBigFilesize"</span><span class="p">)</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"></cfcatch></span>
<span class="nb"></cftry></span>
<span class="nb"></cffunction></span>
<span class="nb"><cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"_handleError"</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">"void"</span> <span class="nv">access</span><span class="o">=</span><span class="s2">"private"</span> <span class="nv">output</span><span class="o">=</span><span class="s2">"no"</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"catchData"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"yes"</span> <span class="o">/</span><span class="nb">></span>
<span class="nb"><cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">"functionName"</span> <span class="nv">type</span><span class="o">=</span><span class="s2">"string"</span> <span class="nv">required</span><span class="o">=</span><span class="s2">"no"</span> <span class="k">default</span><span class="o">=</span><span class="s2">"unknown"</span> <span class="o">/</span><span class="nb">></span>
<span class="nt"><cflog</span> <span class="na">text=</span><span class="s">"Function #arguments.functionName#: #arguments.catchData.message# #arguments.catchData.detail#"</span> <span class="na">type=</span><span class="s">"error"</span> <span class="na">file=</span><span class="s">"#variables.logFileName#"</span> <span class="nt">/></span>
<span class="nb"></cffunction></span>
</cfcomponent>
We will save the file as {Lucee-install}/lib/lucee-server/context/gateway/filesizechecker/FileBackuper.cfc
Now we will add the "file size check" functionality into our gateway cfc. We'll replace "YOUR GATEWAY ACTIONS HERE" with the following:
<cfset var qFile = "" />
<--- get the file's size by using cfdirectory --->
<cfdirectory action="list" directory="#getDirectoryFromPath(variables.config.filepath)#"
filter="#getFileFromPath(variables.config.filepath)#" name="qFile" />
<--- call the listener CFC if the file size meets the minimum requirement --->
<cfif qFile.recordcount and qFile.size gte variables.config.minimalsize>
<cfset variables.listener[variables.config.listenerFunction](
qFile.directory & server.separator.file & qFile.name
, qFile.size
) />
</cfif>
We will save the file as {Lucee-install}/lib/lucee-server/context/gateway/filesizechecker/FileSizeWatcher.cfc
Btw: the variables.listener and variables.config variables did not just come falling from the sky; instead, it was saved to the variables scope in the init() function.
Lastly, we need to create the Gateway driver. We can use the Gateway driver code which was shown before, and then save it as {Lucee-install}/lib/lucee-server/context/admin/gdriver/FileSizeWatcher.cfc
Now we are almost good to go! We do need to restart Lucee to have it pick up the new Gateway driver. So just go to the server admin, click on the menu-item "Restart", and then hit the "Restart Lucee" button.
We can add an instance of our new Gateway type now! You can do it by using cfadmin like this:
<cfadmin action="updateGatewayEntry" type="server" password="server-admin-password"
startupMode="automatic"
id="zipLargeLogFiles"
class=""
cfcpath="filesizechecker.FileSizeWatcher"
listenerCfcPath="filesizechecker.FileBackuper"
custom='#{
filepath = "C:/mysite/logs/failedlogins.log"
, listenerFunction = "onBigFilesize"
, minimalsize = 100000
, interval = 60000
}#'
readOnly=false
/>
Interval: time in milliseconds to wait between each check Minimalsize: the minimum filesize in bytes
After executing the cfadmin code, or going through the admin screens, you should now have an instance of your own Event Gateway type running!
When creating a Socket gateway or an Instant messaging gateway, you will need to do a bit more coding, but hopefully this instruction helped you out!