Entries for month: May 2008

How to access underlying Java regular expression functionality in ColdFusion to get around "RE" function limitations

Coldfusion No Comments »

Currently in ColdFusion there are functions that easily provide regular expression functionality. These function preceed with "RE" in front of the function name (eg REFind). For those unfamiliar to regular expressions, these functions allow you to provide regular expression patterns against a provided string to help find, replace, etc complex strings.

 

Now I would think for most of us ColdFusion programmers we don't look any further than to use these provided functions, BUT there does come a time when you could hit a limitation on how ColdFusion interacts with the regular expression engine (eg. cannot do negative/positive look behinds). To get around this you need to work with the underlying engine itself provided in Java.

 

To do this you only need to do the following adjustments when running against regular expression functions:

ColdFusion example:
REReplace(strString,"for\s?\(","","all")

Underlying Java example:
strString.ReplaceAll("for\s?\(","")

 

It's pretty much that simple. You don't need to do a cfobject to the library or any other setup to use this feature as the string object in ColdFusion already has access to such functionality when it is initially created.

 

Below is a link to the matcher java class reference, which contains other possible underlying methods. I say "possible" because I have not tested them myself yet.
http://java.sun.com/docs/books/tutorial/essential/regex/

 

NOTE: I have tested this type of approach in a ColdFusion 6.1 environment and it works.

Clearing template cache automatically through the ColdFusion gateway .... varScoper bundle NOW included

Coldfusion 10 Comments »

A bit back Brian Szoszorek showed a wonderful example on how to use Cold Fusion's gateway functionality to help streamline the use of the Trusted Cache. This article was brought to my attention by a fellow co-worker (Mike Schierberl) and after reviewing the article in detail I was stunned on how powerful this was and how easy it was to implement. So I decided to tweak it here and there to see what else it could do.

Here are the tweaks:

  • Incorporated Mike Schierberl's varScoper as an optional feature. If you update or add a cfc it will run a varScope check against it and log it into a VarScoper.log file within cold fusions log folder. Auto-watch it threw your favorite file watching tool (Example: Logwatcher for cfEclipse) and you have a realtime varScoper running telling you if there are any unscoped vars within the file you just added/updated. For varScoper fans I see this as being very nice!
  • Made the clear caching method to be just a little smarter (ability to just removing specific files from the cache rather than clearing the entire cache itself)
  • New method "setEventGateway" that will create the needed cfg file and gateway entries within Cold Fusion Administrator. Very Cool.. Very Powerful.. and Very Easy!

You can download the code here.

NOTES:
There seems to be a few bugs within the adminapi interface for clearing the cache. Currently when you attempt to remove a file from the cache after a file has been removed you receive an error stating it cannot remove it from the cache since the file does not exist on the server. To fix this we just clear the entire cache when a file is removed. Another bug is when you delete, then add the file back into the cache. For some reason it does not pick it up. To fix this we do clear cache call on the file being added. I'm not sure why this works, but it does. I will be posting this to Adobe to see what's up.

Here are the steps on how to implement:

1)       Create a new directory that the component will live in and place the component in there

a.       Example: C:\inetpub\wwwroot\templateListener

2)    Set the cfpassword variable to reflect your cfpassword. This needs to be hardcoded within the component in order for the gateway to work properly.

3)    Access and run the setEventGateway method provided within the component and provide it the directory you want to watch

a.       Example: #createObject("component", "templateListener").setEventGateway(directoryToWatch="c:/inetpub/wwwroot")#

4)       Turn on Template Caching within Cold Fusion Administrator by (CF Admin -> Server Settings -> Caching -> Trusted Cache)

5)       Confirm that the Gateway Instance is running by (CF Admin -> Event Gateways -> Gateway Instances)

6)       If you want to turn off the varScoper check then you can turn it off by adjusting the "runVarScoper" flag at the top of the component to false

Powered by Mango Blog. Design and Icons by N.Design Studio
RSS Feeds