Archive for the ‘Gateways’ Category

Directory Watcher Gateway for file management logging

Sunday, April 4th, 2010

I wanted to play with gateways a little bit in Coldfusion 9 this weekend, but wanted to build something at least slightly practical.  What I ended up with is  a gateway that keeps up with file additions, changes, and deletions for a given project and logs those changes to a file.  This was surprisingly easy to accomplish with the directory-watcher gateway that ships with Coldfusion.  All in all this is a tool I may implement for development purposes just to have a running log of file changes that occur within a given project over the given amount of time.

So let’s get this ball rolling by creating a couple projects under the ‘wwwroot’ folder in our Coldfusion9 directory.  If you are still using Coldfusion 8 I don’t see any reason why this won’t work for you as well.  I haven’t tested it though.  For me, I named the first project ‘my_site’ and the second ‘my_othersite’.  Now set up two projects in Coldfusion Builder that respectively points to these two directories.

Now we’ll create a set of directories in each of these projects.  Under the project you should create a directory called ‘gateways’.  Under that directory create another directory called ‘cfcs’ and another one called ‘config’.  Note that this is how that I’ve chosen to do this.  You can set up your file structure however you want.  The main thing to note is that you will need the configuration file for the directory-watcher gateway in your project.  This way when you create your instance of the gateway in the Coldfusion Administrator you can point it to this specific project.  You will then be able to set a new configuration file in another project (which we’ll do) allowing you to watch multiple directories at once.  Inside of the ‘cfcs’ directory you can create a component called ‘directory_watcher.cfc’, and under the ‘config’ directory create a file called ‘directory-watcher.cfg’.  So your file structure should look like the following.

File Structure

File Structure

Now we’ll set up the directory-watcher.cfg file.  Under your ‘ColdFusion9′ directory navigate to ‘gateway/config’ and open the ‘directory-watcher.cfg’ file.  Copy the contents to your file in the my_site project.  Note: you could also just copy the entire file to your ‘config’ directory if you like.  The contents will look like the following.

directory-watcher.cfg

#
# DirectoryWatcherGateway configuration file
#

# The directory you want to watch.  If you are entering a Windows path
# either use forward slashes (C:/mydir) or escape the back slashes (C:\\mydir).
directory=

# Should we watch the directory and all subdirectories too
# Default is no.  Set to 'yes' to do the recursion.
recurse=no

# The interval between checks, in miliseconds
# Default is 60 seconds
interval=60000

# The comma separated list of extensions to match.
# Default is * - all files
extensions=*

# CFC Function for file Change events
# Default is onChange, set to nothing if you don't want to see these events
changeFunction=onChange

# CFC Function for file Add events
# Default is onAdd, set to nothing if you don't want to see these events
addFunction=onAdd

# CFC Function for file Delete events
# Default is onDelete, set to nothing if you don't want to see these events
deleteFunction=onDelete

We’re going to make just a couple changes to this file.  First and foremost we need to add the directory for the gateway to watch.  This is the my_site directory we created under the ‘wwwroot’ directory.  I’m on a mac so I would write ‘/Applications/ColdFusion9/wwwroot/my_site’ after the ‘directory=’ at the beginning of the file.  Next we need to set ‘recurse=no’ to ‘recurse=yes’.  This will allow our gateway to watch this directory and all directories beneath it.  Lastly, just so we can see our changes more quickly we can set the interval to ‘10000′ milliseconds.  By default it runs every minute.  So our new file will look like:

(more…)