Archive for the ‘ColdFusion’ Category

Coldfusion Soundex Algorithm Component

July 18th, 2008

Although this has been done a million times I took a little time (wasting time mostly) to cook up a simple ColdFusion component that implements a basic soundex algorithm (see http://www.creativyst.com/Doc/Articles/SoundEx1/SoundEx1.htm for more info on the reference I used). The component provides a simple interface to add “dictionary” items, and compare subject text against the dictionary items within a specific threshold for potential soundex matches.
Read the rest of this entry »

Tags: ,
Posted in ColdFusion, Development | Comments (0)

ColdFusion Race Conditions?

July 2nd, 2008

One of the things I’ve been struggling with at work lately is related to bizzare ColdFusion errors. Essentially what I am seeing is a page that normally works just suddenly throw an error stating that a specific variable cannot be accessed as an object because it is of type java.lang.String. The problem with this is that it is in fact a CFC instance, and works great for the most part. Then suddenly it will stop working, and logging off the software, and back in, is the only solution. This, of course, is problematic.

Read the rest of this entry »

Tags: ,
Posted in ColdFusion, Development | Comments (0)

No Expand? Sweet!

February 25th, 2008

It’s funny the little things you miss as you use a programming language. Although not considered the longest amount of time, I have been using ColdFusion for over 4 years now. I’ve worked with CFC’s, frameworks, performance tuning, reflection, advanced Java techniques, and more, but somehow seemed to missed one of the most simple, yet useful little attributes (at least useful to me now).

Very frequently I find myself dumping out debug data to evaluate how the results of a given set of code is coming along. Good old <CFDUMP> is my friend in plenty of cases. How in the world, then, did I miss the expand=”no” attribute? OMG! Whenever I’m dumping out large CFC structures, that puppy is my FRIEND! lol.

This, in my opinion, is one of the beauties of programming. Always learning something new.

Tags: , ,
Posted in ColdFusion, Development | Comments (0)

On ColdFusion 7 Arrays

November 12th, 2007

At Ben Nadel’s Blog, which I enjoy reading simply because he has a similar passion to me (digging into how and why it works under the hood), he talked about calling Java functions in ColdFusion that require an actual value array instead of a vector, which is what ColdFusion’s arrays are based on. He has an example, and then goes to show how ColdFusion 8 made that task much easier.

Only one thing I saw about it that struck me as odd was that he was manually copying the values from the ColdFusion “array” (vector) to the Java array. Didn’t think that would be necessary, so I looked it up. Not much different than Mr. Nadel’s code, but a few lines shorter and if I had to guess, the Java method to copy the data to the new array is likely more efficient than a manual copy.

  1. <!————————————————-
  2.    ColdFusion array, which is actually a Vector.
  3. ————————————————–>
  4. <cfset<!– Traffic Statistics –>  <!– End Traffic Statistics –> arrValue = ArrayNew(1)>
  5.  
  6. <cfset arrValue[1] = "What’s up hot stuff?" />
  7. <cfset arrvalue[2] = "You come here a lot?" />
  8. <cfset arrValue[3] = "You from out of town?" />
  9. <cfset arrValue[4] = "Want a little of this?" />
  10. <cfset arrValue[5] = "How ’bout a little of that?" />
  11.  
  12. <!—————————————————————–
  13.    Need to get the class of a string, and create our java array.
  14. ——————————————————————>
  15. <cfset stringClass = createObject("java", "java.lang.String").getClass()>
  16. <cfset arrJavaValue = createObject("java", "java.lang.reflect.Array").newInstance(stringClass, arrayLen(arrValue))>
  17.  
  18. <cfset arrValue.copyInto(arrJavaValue)>
  19.  
  20. <cfdump var="#arrValue#">
  21.  
  22. <cfoutput>
  23.  
  24. <p>
  25.    #arrValue.GetClass().GetName()#<br />
  26.    #arrValue.GetClass().GetSuperClass().GetName()#<br />
  27.    #arrValue.GetClass().GetSuperClass().GetSuperClass().GetName()#<br />
  28.    #arrValue.GetClass().GetSuperClass().GetSuperClass().GetSuperClass().GetName()#<br />
  29.    #arrValue.GetClass().GetSuperClass().GetSuperClass().GetSuperClass().GetSuperClass().GetName()#<br />
  30. </p>
  31.  
  32. <h4>
  33.    Java String Array Class Hierarchy
  34. </h4>
  35.  
  36. <p>
  37.    #arrJavaValue.GetClass().GetName()#<br />
  38. </p>
  39.  
  40. </cfoutput>

Tags: ,
Posted in ColdFusion, Development | Comments (0)

Barbecue revisited.

October 12th, 2007

I made a post recently about using Barbecue barcode software in ColdFusion. In my example I instantiated the CFC object and called the getCode128 function as the source of an image tag. With a bit of experimentation you will find that although this produces an image… that’s ALL it produces. Why? Cause we essentially hijack the content type of ColdFusion’s output buffer and set it to image/jpeg. It removes all other content from the buffer, and only sends over the image. The fix?

The component

  1. <cfcomponent>
  2.  
  3.    <cffunction name="getCode128" returntype="any" access="remote" output="true">
  4.       <cfargument name="data" type="string" required="true" />
  5.  
  6.       <cfsetting showdebugoutput="false">
  7.  
  8.       <cfscript>
  9.  
  10.          outStream = createObject("java", "java.io.ByteArrayOutputStream").init();
  11.  
  12.          //———————————————————–
  13.          // Create the barcode, and draw it to a buffered image class.
  14.          //———————————————————–
  15.          barcode = createObject("java", "net.sourceforge.barbecue.BarcodeFactory").createCode128("#arguments.data#");
  16.          bufferedImage = createObject("java", "net.sourceforge.barbecue.BarcodeImageHandler").writeJPEG(barcode, outStream);
  17.  
  18.       </cfscript>
  19.  
  20.       <!— Return the information as streaming bytes of type image/jpeg —>
  21.       <cfset getPageContext().getOut().clearBuffer()><cfcontent type="image/jpeg" variable="#outStream.toByteArray()#"><cfreturn>
  22.    </cffunction>
  23.  
  24. </cfcomponent>

Usage:

  1. <cfoutput>
  2.  
  3. <img src="barcode.cfc?method=getCode128&amp;data=My First Barcode" />
  4.  
  5. </cfoutput>

So, as you can see in this case I am calling the CFC method remotely in the image source attribute forcing a separate HTTP request. This also required me to make the access to the getCode128 function to remote. Hope it helps! Cheers!

Tags: ,
Posted in ColdFusion, Development | Comments (3)

Using Barbecue Barcode in ColdFusion

October 3rd, 2007

I was approached today by a coworker about integrating the open source Java-based barcode library, Barbecue, into ColdFusion. He wanted to use this library to prototype a project concept for adding barcode support to certain parts of our application. A little bit of play and here’s how it works.

The first thing you will need to do is extract the JAR file from the download ZIP file (the file I used was barbecue-1.5-beta1.jar). Put this file into your {ColdFusionRoot}\runtime\lib (for single server installs). You will need to restart the ColdFusion service for CF to pick that up.

Now lets do some code. First lets create a component called barcode.cfc. And in there we will have a function called getCode128, which will return a Code 128 barcode that dynamically switches between character sets to give the smallest possible encoding. It will look like so.

  1. <cfcomponent>
  2.  
  3.    <cffunction name="getCode128" returntype="any" access="public" output="true">
  4.       <cfargument name="data" type="string" required="true" />
  5.  
  6.       <cfsetting showdebugoutput="false">
  7.  
  8.       <cfscript>
  9.  
  10.          outStream = createObject("java", "java.io.ByteArrayOutputStream").init();
  11.  
  12.          //———————————————————–
  13.          // Create the barcode, and draw it to a buffered image class.
  14.          //———————————————————–
  15.          barcode = createObject("java", "net.sourceforge.barbecue.BarcodeFactory").createCode128("#arguments.data#");
  16.          bufferedImage = createObject("java", "net.sourceforge.barbecue.BarcodeImageHandler").writeJPEG(barcode, outStream);
  17.  
  18.       </cfscript>
  19.  
  20.       <!— Return the information as streaming bytes of type image/jpeg —>
  21.       <cfset getPageContext().getOut().clearBuffer()><cfcontent type="image/jpeg" variable="#outStream.toByteArray()#"><cfreturn>
  22.    </cffunction>
  23.  
  24. </cfcomponent>

The code is pretty well documented so it should be pretty clear what we are doing here. Now, to use it we will simply create an instance of our new component, and call the getCode128 function as the source to an image tag in HTML. Like so.

  1. <cfoutput>
  2.  
  3. <cfset o = createObject("component", "barcode")>
  4. <img src="#o.getCode128(’My First Barcode’)#" />
  5.  
  6. </cfoutput>

So there you have it. Give it a go! It was fun!

Tags: ,
Posted in ColdFusion, Development | Comments (3)

Using TreeMap in ColdFusion

April 12th, 2007

Have you ever found the need to maintain a structured list of items? Of course! Have you ever needed to make sure those items stay in natural order? Maybe. If you answered yes then you need the Java TreeMap class.

Let us say we are going to store a structure of employee names and their titles associated to them. In this example we will keep just such a list, insert them in random order, and you will see the output result comes out in alphabetical (natural) order. The order is maintained based on the KEY, by the way. So, without further ado, here is an example.

  1. <script>
  2.  
  3. treeMap = createObject("java", "java.util.TreeMap").init();
  4.  
  5. writeOutput("<strong>Inserting the following employees in this order:</strong> Dave, Adam, Kyle, James.<br />");
  6.  
  7. treeMap.put("Donny", "President");
  8. treeMap.put("Adam", "Engineer");
  9. treeMap.put("Kujo", "Director");
  10. treeMap.put("Johnny", "Support");
  11.  
  12. writeOutput("<strong>Now show the contents of this tree in key order.</strong><br />");
  13.  
  14. iter = treeMap.keySet().iterator();
  15. while (iter.hasNext())
  16. {
  17.    key = iter.next();
  18.    writeOutput("Key: #key# Value: #treeMap.get(key)#<br />");
  19. }
  20.  
  21. </script>

In this example we instantiate the TreeMap class first. We then insert the employees Donny, Adam, Kujo, and Johnny, making the value of these keys their job titles. We then get an iterator for the keyset (an array of the TreeMap keys) and loop while we still have keys, displaying the key name, and its value. Here is what the output looks like.

Inserting the following employees in this order: Donny, Adam, Kujo, Johnny.

Now show the contents of this tree in key order.
Key: Adam Value: Engineer
Key: Donny Value: President
Key: Johnny Value: Support
Key: Kujo Value: Director

Tags: ,
Posted in ColdFusion, Development | Comments (0)

Reflection in ColdFusion Part 2

March 28th, 2007

Yesterday I was working on using Java reflection to create function pointers in ColdFusion to native ColdFusion methods. I was able to get this to work on any function with only String parameters, but not integer. Well, I found out why.

The integer data type in Java is a primitive data type. There IS a class representation but it still maps down to the primitive data type. This posed a problem trying to point to a ColdFusion function such as FindNoCase. Why? Cause it takes two strings and one integer argument, and the java.lang.Integer class wasn’t doing the trick.

Turns out that the primitive wrapper classes have a property called TYPE that return an instance of the primitive type. THIS was exactly what I needed. So, here is an example of creating a function pointer to FindNoCase.

  1. <cfset stringFunc = createObject("java", "coldfusion.runtime.StringFunc")>
  2. <cfset stringClass = createObject("java", "java.lang.String")>
  3. <cfset integerClass = createObject("java", "java.lang.Integer")>
  4.  
  5. <!— Setup the argument types —>
  6. <cfset argumentTypeArray[1] = stringClass.getClass()>
  7. <cfset argumentTypeArray[2] = stringClass.getClass()>
  8. <cfset argumentTypeArray[3] = integerClass.TYPE>
  9.  
  10. <cfset method = stringFunc.getClass().getMethod("FindNoCase", argumentTypeArray)>
  11.  
  12. <!— Call the function. We must pass arguments as an array of objects. —>
  13. <cfset searchWhat = "This is a test.">
  14. <cfset searchFor = "is">
  15. <cfset startPos = 0>
  16.  
  17. <cfset argumentArray = arrayNew(1)>
  18. <cfset argumentArray[1] = searchFor>
  19. <cfset argumentArray[2] = searchWhat>
  20. <cfset argumentArray[3] = javaCast("int", startPos)>
  21.  
  22. <!— Invoke the function, and show some results —>
  23. <cfset result = method.invoke(stringFunc, argumentArray)>
  24.  
  25. <cfoutput>
  26.  
  27. <fieldset>
  28. <legend>FindNoCase Example</legend>
  29. Search What: #searchWhat#<br />
  30. Search For: #searchFor#<br />
  31. Start At: #startPos#<br />
  32.  
  33. result = #result#
  34. </fieldset>
  35.  
  36. </cfoutput>

And voila! It works!

Tags: , ,
Posted in ColdFusion, Development | Comments (0)