Creating a Simple Gateway Application
In Chapter 29, "Extending ColdFusion with Java," we created several examples for a photo album application. One of the examples we created looped over the contents of a directory and made thumbnails for all the images found in that directory. That's pretty cool, but it requires a user to interact with the application through a Web browser. What if your boss now asks you to allow users to FTP-upload files of photos, and wants the application to detect that a file has been uploaded and create an associated directory of thumbnails! Normally you couldn't do this with ColdFusion because you're not interacting with the server directly through HTTPbut with ColdFusion MX 7 and the DirectoryWatcher event gateway, it's a snap.For this example, the DirectoryWatcher event gateway (supplied with CFMX7) will create a simple responder application. The DirectoryWatcher gateway sends events to a listener CFC when a file is created, deleted, or modified in a directory you tell the gateway to watch. It runs checks the directory at an interval specified in a configuration file that you edit, and when the interval has passed, checks for changes since last time. If it finds any changes, DirectoryWatcher sends a message to a listener CFC, which can perform the unzipping and creation of thumbnails.First you need to configure the gateway configuration file, found in:
For this example we'll assume that your ColdFusion root is on drive C, so a literal example of the file path would look like this:
coldfusion_root\gateway\config\directory-watcher.cfg
Open this file and edit the very first attribute, directory, and have it point to where you will FTP your zipped files. For this example, the directory is called gallery and the path is as follows:
C:\CFusionMX7\gateway\config\directory-watcher.cfg
Chapter 29) to create the thumbnails. For this example we are assuming that the DirectoryWatcher.cfc is being placed in the same directory as the imagemanipulator.cfc which is webroot\JavaInteg\ and the directory gallery is beneath that. Lastly, we'll log the event. The code to do all this is in Listing 31.1.
directory=C:/Inetpub/wwwroot/JavaInteg/imageio/resize/gallery/
Listing 31.1. DirectoryWatcher.cfcSimple CFC that creates thumbnails of new images
[View full width]
Most of the code here is pretty straightforward. It has been repurposed from Chapter 29, and you'll see some things that are specific to working with event gateways. The first is that the listener CFC expects a struct called CFEvent, which is a Java object that is mapped to a ColdFusion struct. The CFEvent object contains a variety of information, including a GatewayID, OriginatorId, GatewayType, CFCPath, CFCMethod, CFCTimeout, and Data. Figure 31.2 shows an example of what the CFEvent message might look like in our example. Table 31.2 describes each node in the structure.Listing 31.1, the CFEvent message is used only to log the DirectoryWatcher method that was used, data.type, as well as the file and file path, data.filename. In a more-complex application, you may want your CFC to take actions based on specific information from the CFEvent message.Now to get our code to actually work, we need to do two other things. The first is to go into the ColdFusion Administrator and create a mapping for the DirectoryWatcher CFC; otherwise, the event gateway will not know where to look. The second thing we need to do is create an instance of the event gateway.
<cfcomponent>
<cffunction name="onAdd" output="no">
<cfargument name="CFEvent" type="struct">
<!--- get event data --->
<cfset data=CFEvent.data>
<!--- watcher will ignore outgoing messages --->
<!--- Location of images --->
<cfset GalleryFolder = ExpandPath("gallery")>
<!--- Get list of images --->
<cftry>
<cfdirectory action="list" name="GetImages" directory="#GalleryFolder#"
filter="*.jpg">
<!--- Loop over images --->
<cfloop query="GetImages">
<!--- Proposed location of thumbnail --->
<cfset ThumbPath = ExpandPath("gallery/thumbs/#Name#")>
<!--- If the thumbnail does not exist --->
<cfif not FileExists(ThumbPath)>
<!--- Invoke our image-resizing function --->
<cfinvoke component="JavaInteg.imageio.resize.ImageManipulator"
method="createResizedImage" sourcePath="#GalleryFolder#/#Name#"
destPath="#ThumbPath#" newWidth="100">
<!--- log a message --->
<cflog file="watch" text="a file was #data.type# and the name was #data.filename#">
</cfif>
</cfloop>
<cfcatch type="Any">
<cflog file="watch" text="A exception, #CFCATCH.TYPE#, was thrown in
DirectoryWatcher.CFC, the error message is #cfcatch.message#">
</cfcatch>
</cftry>
</cffunction>
</cfcomponent>
Creating an Event Gateway Instance
Before you can use the example in Listing 31.1, you must create an instance of the event gateway.First go to the ColdFusion Administrator and select Event Gateways > Gateway Instances. You'll see something like Figure 31.3.
Figure 31.3. The Event Gateways > Gateway Instances configuration screen in the ColdFusion Administrator.
[View full size image]
