Tag Archives: coldfusion

ColdFusion CacheLocker

Pay Per Use Locker

Pay Per Use Locker

I ran into a problem the other day where I needed a way to temporarily store data between page requests. Typically I’m able to stash this sort of thing in the session scope, but these requests originated from different sources and I prefer to avoid (de)serializing when I can.

Instead I set up something akin to one of those pay-per-use lockers. You stick your items in the bin, drop in a few quarters, and take the newly unlocked key. Later you come back and use that key to retrieve your items. Your key is now ‘locked’ back into the starting position and the cycle begins anew.

Like one of those pay-per-use lockers you just stash your data, save they key, use the key, trash the data.

Simple as pie.

This isn’t the sort of thing that comes up often, but should it arise I’ve got just the tool for the job!

Example Usage:

// initialize the locker
application.cacheLocker = CreateObject("component","cacheLocker").init();

// store some arbitrary data
key = application.cacheLocker.store([1,2,3,4]);

// then retrieve the data using the saved key
// throws a CacheException if the key doesn't 'fit'
arbitraryNumbers = application.cacheLocker.retrieve(key);

The data is destroyed after being retrieved; it’s a one time only locker.

There are two important things to keep in mind when using this utility:

  1. There’s currently no mechanism for cleaning out lockers, so if you’re not regularly retrieving your data then this thing is just going to grow, and grow, and grow.
  2. The locker is not stored in any sort of persistent memory. If ColdFusion goes down, then the lockers are destroyed.

Enjoy!

Generating Mock Images in ColdFusion

Dummy Image!

Dummy Image!

My co-worker Brian passed an interesting lifehacker post along about dummyimage.com.

It’s a cool site that lets you pass in a width and a height to generate an image which could come in really handy for quickly mocking up web pages.

I can’t believe I never thought of doing something like this!

ColdFusion is a great language for doing this sort of thing, so it was trivial to whip something up quickly.

I was originally writing a file to disk because I didn’t realize you could stream an image variable to the browser, but my other co-worker Jim came to the rescue with the cfimage “writeToBrowser” action!

<cfset params = listToArray(cgi.query_string,"x") />

<cfif arrayLen(params) lt 2>
	<cfthrow message="Input should be like ?[w]x[h]"/>
</cfif>

<cfset width  = params[1] />
<cfset height = params[2] />
<cfset color  = "gray" />

<cfif not (isNumeric(width) and isNumeric(height))/>
	<cfthrow message="Width/Height should be numeric ?100x100">
</cfif>

<cfset image = ImageNew("", width, height,"rgb", color) />
<cfset ImageDrawText(image, "#width# x #height#", 0, 10)>
<cfimage source="#image#" action="writeToBrowser"/>

I’d love to link an example to you, but my hosting plan doesn’t support _cf_image_

ColdFusion FileSweeper

I’m tired writing scripts to clean up temporary files.

It’s a nigh trivial task. The chance for error is small, but the consequences can be dire!

DIRE!

DIRE!

I wrote a little utility that makes this a little easier. It simply deletes all the files in a folder (recursive or no) that are older than a specified number of seconds.

Example Usage:

// no init needed, but the function's there if you like
fileSweeper = CreateObject("component", "fileSweeper");   

// all arguments are actually defaulted to the values shown
// so you needn't pass any, fancy eh?
fileSweeper.deleteOldFiles(
    folder   = ExpandPath("/temp"),
    seconds  = 600,
    filter   = "*.*",
    recurse = "no",
    lockname = "deleteFiles",
    timeout  = 60,
    logging  = true
);

Download it!

ColdFusion serializeJSON Problem

I ran into a little problem with the CF8 serializeJSON function. The function doesn’t properly escape quotes in struct keys, which results in invalid JSON being generated.

For Example: (all code is in cfscript)

// Struct Key with quotes in it
heightCounts["6'0"""] = 5;

// Serialize function runs alright...
serialized = serializeJSON(heightCounts);

// But the JSON is invalid. The quotes are unescaped! 
writeOutput(serialized);
// => {"6'0"":5.0}

// As you might expect, deserializing throws an error
deserializeJSON(serialized);

I certainly don’t like the idea of having quotes in struct keys, but that’s beside the point. I filed a bug report, but I also wrote a little function to jsStringFormat my struct keys. Problem solved!

function cleanKeys(dirtyData) {
	var cleanData = structNew();
	var cleanKey = "";
	var i = "";

	if(!isStruct(dirtyData)) {
		return dirtyData;
	}

	for(i in dirtyData) {
		cleanKey = jsStringFormat(i);
		cleanData[cleanKey] = cleanKeys(dirtyData[i]);
	}

	return cleanData;
}

You just pass in your quote-fully keyed struct, and get a clean one back:

// Same example as above
heightCounts["6'0"""] = 5;

// This time we sanitize the struct keys
jsSafeHeightCounts = cleanKeys(heightCounts);

// The serializeJSON call still runs without error
serialized = serializeJSON(jsSafeHeightCounts);

// But this time the output is correct!
writeOutput(serialized);
// => {"6'0"":5.0}

// And the deserialize works as expected
deserializeJSON(serialized);

Ta Da!

Merging Netflix Accounts with ColdFusion and JavaScript

I wrote a little script using ColdFusion and JavaScript to merge two NetFlix accounts. It’s ugly and cheesy, but it works so I thought I’d share.

Here’s how you do it:

  1. Register for a developer key.
  2. Grab the RSS feed url of the queue you want to merge FROM. This can be obtained by logging into the FROM account and clicking on the “RSS” link in the footer.
  3. Log in to the account that you’d like to merge TO.
  4. Hit this script in the same browser you logged into the TO account with, and make sure you allow pop-ups!
<!--- enter your feed here! --->
<cfset feed = "http://rss.netflix.com/QueueRSS?id=P4806480653914107620156446228102116" />
<!--- enter your consumer key here! --->
<cfset key  = "your-consumer-key-here" />
<cfset href = "http://widgets.netflix.com/addToQueue.jsp?output=json&devKey=#key#&queue_type=disc&movie_id=http://api.netflix.com/catalog/movie/" />

<cffeed action="read" name="queue" source="#feed#" />

<cfset movies = queue.item />
<cfset idList = "" />

<cfloop array="#movies#" index="i">
	<cfset idList = ListAppend(idList,ListLast(i.guid.value,"/")) />
</cfloop>

<cfoutput>
	<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"/>
	<--- Pop up a new window, swap the href every second. Told ya it was cheesy! --->
	<script>
		var popUp = window.open('http://google.com');
		var idList = [#idList#];

		new PeriodicalExecuter(
			function(pe) {
				if (idList.length == 0) {
					pe.stop();
				} else {
					popUp.location.href = "#href#" + idList.pop();
				}
			},
			1
		);
	</script>

</cfoutput>

Note: I don’t know if this allowed by the EULA, or if there even is a EULA…so, use at your own risk!

When is a Struct not a Struct?

I’ve run into an annoying problem trying to get some older java code working under ColdFusion 8. The problem method accepts two arguments, java.util.Hashmap and java.util.Vector, but the signature isn’t matching.

Here’s the set up:

<cfset classpath = [
  CreateObject( "java","java.net.URL" ).init( "file:#cfusion#" ),
  CreateObject( "java","java.net.URL" ).init( "file:#testfile#" )
] />

<cfset loader  = CreateObject( "java","java.net.URLClassLoader" ).Init(
  classpath
) />
<cfset example = loader.LoadClass( "ProblemExample" ).NewInstance() />

<cfset cfarray  = ArrayNew(1) />
<cfset cfstruct = StructNew() />

<cfdump var="#example#" />

My sample method looks good…

public void originalMethod(java.util.Hashtable h,java.util.Vector v){}

The dump looks greatdump

But I’m getting a “method was not found” error

I did a bit of googling, and found a great post about the differences between CF7 and CF8 structs and arrays. Perfect…right?

I wrote a little test method to accept a coldfusion.util.FastHashtable:

public void takeFastHashtable(coldfusion.util.FastHashtable fh){}
method was not found"

I wrote a little script, to see what I was dealing with:

<cfoutput>
  #cfstruct.getClass().getName()# == #example.inspect(cfstruct)#
  => #cfstruct.getClass().getName() EQ example.inspect(cfstruct)#
</cfoutput>

using

public String inspect( Object o ) {
  return o.getClass().getName();
}

And my output was as expected:
coldfusion.runtime.Struct == coldfusion.runtime.Struct => YES

So I wrote a up a simple test method for coldfusion.runtime.Struct:

public void testStruct(coldfusion.runtime.Struct s){}
<cfset example.testStruct(s) />

You guessed it:

"method was not found"

Okay….let’s try casting….

public coldfusion.runtime.Struct castAsStruct(Object o) {
  return (coldfusion.runtime.Struct) o;
}

This time the error’s a little different…

coldfusion.runtime.Struct cannot be cast to coldfusion.runtime.Struct
W T F

Granted, I’m trying to use underlying undocumented code, and I’m no Java expert, but there’s something here that’s not adding up. Is this a bug? Am I missing something stupid? At this point I’ll be moving on to alternative solutions, but any help would be still greatly appreciated.

You can download some sample code I’ve been using, just make sure to compile with the appropriate classpath for your environment.

Here’s my command

javac ProblemExample.java -classpath /opt/coldfusion8/lib/cfusion.jar

Implicit Getters and Setters in ColdFusion

Thanks to CF8’s new onMissingMethod method, it’s trivial to implement implicit getters and setters. As easy as it is, I couldn’t google up any code. Since it’s not the kind of thing I’d rather search for than write myself, I thought I’d go ahead and do you the favor and post it here.

I realize there’s a nasty stigma attached and I don’t disagree that it’s bad practice, but it does come in handy when building a proof of concepts or programming exploratoraly.

So here you go, irregardless of whether or not it’s bad practice:

<cfcomponent name="BaseObject">

	<cffunction name="Init" output="no">
		<cfargument name="instance" default="#StructNew()#"/>

		<cfset SetInstance( arguments.instance )/>

		<cfreturn this/>
	</cffunction>

	<cffunction name="OnMissingMethod" output="no">
		<cfargument name="missingmethodname"      required="yes" />
		<cfargument name="missingmethodarguments" />

		<cfset var prefix = Left( arguments.missingmethodname, 3 ) />
		<cfset var suffix = Mid( arguments.missingmethodname,
			4,
			Len( arguments.missingmethodname )
		) />

		<cfif ( CompareNoCase( prefix, "get" ) eq 0 ) and Has( suffix ) >
			<cfreturn Get( suffix )/>
		</cfif>
		<cfif CompareNoCase( prefix, "set" ) eq 0>
			<cfreturn Set( suffix, arguments.missingmethodarguments.1 )/>
		</cfif>

		<cfthrow message="Method #arguments.missingmethodname# not found" />
	</cffunction>

	<!--- private on down --->

	<cffunction name="GetInstance" output="no" access="private">
		<cfreturn variables.instance/>
	</cffunction>

	<cffunction name="Get" output="no" access="private">
		<cfargument name="field" required="yes"/>

		<cfset var instance = GetInstance()/>

		<cfreturn instance[ arguments.field ]/>
	</cffunction>

	<cffunction name="Has" output="no" access="private">
		<cfargument name="field" required="yes"/>

		<cfreturn StructKeyExists( GetInstance(), arguments.field )/>
	</cffunction>

	<cffunction name="Set" output="no" access="private">
		<cfargument name="field" required="yes"/>
		<cfargument name="value" required="yes"/>

		<cfset var instance = GetInstance()/>
		<cfset instance[ arguments.field ] = arguments.value/>

		<cfreturn Get( arguments.field )/>
	</cffunction>

	<cffunction name="SetInstance" output="no" access="private">
		<cfargument name="instance" required="yes">

		<cfset variables.instance = arguments.instance/>

		<cfreturn GetInstance()/>
	</cffunction>

</cfcomponent>

Download it and the MxUnit Tests!

MXUnit and Me

mxunit1If you’ve been keeping up with my blog over the last couple months, then you already know that I’ve been experimenting with test driven development. I love the work flow, the way it makes me view my code, and the peace of mind…I just don’t know that it will pay off in my work environment. Hence the experimentation.

Most of my TDD dabbling up to this point has been done in Ruby. I’ve finally gotten around to messing around with some of the major ColdFusion testing frameworks: cfunit, cfcUnit and finally MXUnit.

After doing small projects with each of them, I’ve finally decided to settle down with MXUnit. It’s got decent docs, a nice work around for private methods, I dig the web interface, the eclipse plug-in is great and creating my own stubs for the generator was a snap!

In short: Two thumbs up!

Here’s my test. I’m still really new to this sort of thing, so I’d love feedback should you feel so inclined:


	

		function Setup() {
			this.cfc = CreateObject( "component" , "BaseObject" );
			this.cfc = this.cfc.init();
	
			makePublic( this.cfc, "SetInstance" );
			makePublic( this.cfc, "Set" );
			makePublic( this.cfc, "Has" );
			makePublic( this.cfc, "Get" );
			makePublic( this.cfc, "OnMissingMethod" );
		}
	
		function TestSetInstance() {
			var value = "a";
			AssertEquals( value, this.cfc.SetInstance( value ) );
	
			value = StructNew();
			AssertEquals( value, this.cfc.SetInstance( value ) );
		}
	
		function TestGetInstance() {
			var value = "a";
	
			this.cfc.SetInstance( value );
			AssertEquals( value, this.cfc.GetInstance() );
	
			value = "b";
			this.cfc.SetInstance( value );
			AssertEquals( value, this.cfc.GetInstance() );
		}
	
		function Testinit() {
			var value = "a";
	
			this.cfc = this.cfc.init( value );
			AssertEquals( value, this.cfc.GetInstance() );
	
			this.cfc = this.cfc.init();
			AssertEquals( StructNew(), this.cfc.GetInstance() );
		}
	
		function TestSet() {
			var field = "a";
			var value = "1";
	
			AssertEquals( value, this.cfc.Set( field, value ) );
	
			field = "w";
			value = "2";
	
			AssertEquals( value, this.cfc.Set( field, value ) );
		}
	
		function TestHas() {
			var field = "a";
			var value = "1";
	
			AssertEquals( false, this.cfc.Has( field ) );
	
			this.cfc.Set( field, value );
			AssertEquals( true,  this.cfc.Has( field ) );
	
			field = "w";
			value = "2";
	
			AssertEquals( false, this.cfc.Has( field ) );
		}
	
		function TestGet() {
			var field = "a";
			var value = "1";
	
			this.cfc.Set( field, value );
			AssertEquals( value, this.cfc.Get( field ) );
	
			field = "w";
			value = "2";
	
			this.cfc.Set( field, value );
			AssertEquals( value, this.cfc.Get( field ) );
		}
	
		function OnMissingMethod() {
			var value = "a";
	
			AssertEquals( value, this.cfc.SetSomething( value ) );
			AssertEquals( value, this.cfc.GetSomething() );
		}

	

Click to download the component I’m testing and the file above. Now.

ColdFusion Button Maker

button maker

I started working on a button maker using ColdFusion’s built in image functions. The ultimate goal is to create graphical buttons using an simple and quick interface. Steps being as follows:

1. Select (or create a new) configuration consisting of background images, and fonts
2. Type in some text and drag (if necessary) the text to the desired location.
3. Export!

For now all I’ve done is set up the base cfc’s for the image stretching and text writing. Next up is the interface. You can check out the results from a test script, or just download the code.