Archive for the ‘Write that one down!’ Category

Custom Fonts in ColdFusion

April 29, 2009

We have a couple non-standard fonts that we use for some of our our FPU class materials. For our new Coordinator Resource Center we wanted to use these same fonts in a server generated PDF file. However, this was the first time we’ve actually used a non-standard font with ColdFusion. This turned out to be a much more difficult task than it would seem. During this process I ran into a few issues. (These apply to ColdFusion MX 7.02)

Font Family vs Font Face
Within the True Type Font file, there are definitions for the “Font Family” and the “Font Face.” For some reason, ColdFusion would not read the file unless these were set to the exact same string. I used a font editor to modify these. In addition, if the font file contains different a “Windows Name” and “Mac Name” you might want to make those the same as well.

System vs. User Defined File Location
There is a difference in where you place the files. If you happen to be running ColdFusion on a Windows box, you’re better off just installing the fonts as you normally would through the control panel. Developing locally, I didn’t have any problems with that. However, our server runs Linux and I needed to set a custom path to tell ColdFusion where to look for the fonts. This is easily done through the Administrator control panel.

However, unless you have this hot fix installed, it will never read the fonts and never give you an error. The admin tool will show you they are installed, but when you try to generate a PDF using them, it will always fall back to a system font. The documentation on this hot fix gives you no indication that it will fix this problem, and it is not included in any other service pack.  However, this solved the problem for us.

Font Types
Supposedly, ColdFusion will support the following font types:
TTF (True Type Font)
TTC (True Type Collection)
OTF (Open Type Font)
AFM (Adobe Font Metrics)

However, I ran into an OTF font where (no matter what I did) I could not get the PDF to output that font. It would keep falling back to the system font. I ended up using [FontForge] to convert it from OTF to TTF.

How Much Memory Does This Take?

January 21, 2009

Ever wonder how much memory an object takes?

The web has lots of  great theoretical answers to that question.

Theories are great, and they give lots of great insights into how a Java compiler works. But what if things aren’t perfectly clean-cut? There are plenty of reasons to want proof including the following possibilities:

  • Say you use the Spring Framework or some other mechanism where your objects aren’t composed until runtime
  • Maybe you use a library/jar/package/swf/swc/etc, you don’t have access to the underlying code, and you’re trying to weigh its memory footprint
  • Maybe your objects are composed differently in different states
  • Maybe you use a Rapid Application Development language such as  ColdFusion or PHP and want to see how much overhead they bring along
  • Maybe you’re just a hard-core detailed person who wants to prove that the theory really matches reality

We’ve had to troubleshoot ColdFusion apps for memory usage, and we’ve found this approach to be helpful:

  1. Call for garbage collection (if applicable)
  2. Take a snapshot of how much memory is used
  3. Instantiate a whole bunch of copies of the class in question, and stuff them into an array
  4. Call for another garbage collection (if applicable)
  5. Take another snapshot of how much memory is used
  6. Subtract the first memory reading from the second, divide by the number of objects created, and you have a pretty good approximation of how much memory each object takes

As a side-note, the more objects you create in your test, the more your test will drown out background noise from other activity, and the more accurate your reading will be on each object.

After researching this method, we honed it a bit and made some useful discoveries. For example, in ColdFusion 7, each CFC instantiated takes up ~2kB. On top of that, each <cffunction></cffunction> in that cfc takes up an additional ~150 bytes.  So the abc.cfc “class” (found below) with 3 empty methods takes up ~2.3kB. (disclaimer: I’ve heard but not yet personally verified that memory usage improves in CF8 and beyond.)

In our scenario, we had CFCs inheriting a data persistence layer including about 50 methods, our CFCs included about 50 more in addition to member data in the variables scope. Without fully understanding the under-the-hood functionality of ColdFusion 7, we were instantiating 30 CFCs per user at 20kB each (~half a meg) for a few thousand users. Then we wondered why we kept eating through our available memory so quickly.

After making this discovery we spent a few days re-factoring some code hoping to use resources better. We limited the inheritance of the persistence layer to where it was necessary. We re-factored the code to use ColdFusion queries rather than CFCs (queries wound up being MUCH lighter than similar CFCs). When we launched the changes our application used about 40% less memory.

As hinted earlier, this algorithm can also be used to analyze memory usage by basic Java components. One question I’ve dabbled with is how much memory java code takes. I may cover that subject in another post someday, but for now I’ll leave that as an exercise for the reader. In the meantime, here’s a ColdFusion script that implements the algorithm described above.  Enjoy!

<!---this part executes in a cfm--->

<cfset runtime = CreateObject("java","java.lang.Runtime").getRuntime() />
<cfset myArray = arrayNew(1) />
<cfset intNumberOfObjectsCreatedPerLoop = 10 />
<cfset intTotalNumberOfObjectsCreated = 0 />
<cfset intX = 0 />
<cfset intBytesBeforeTest = 0 />
<cfset intBytesAfterTest = 0 />
<cfset intBytesCreated = 0 />
<cfset intBytesIn1MB = 1048576 />
<cfset memoryThresholdInMB = 50/>
<cfset obj = "" />

<cfset runtime.gc() />
<cfset intBytesBeforeTest = runtime.totalMemory() - runtime.freeMemory() />
<cfloop condition="intBytesCreated lt (intBytesIn1MB * memoryThresholdInMB)">
<cfset intTotalNumberOfObjectsCreated = intTotalNumberOfObjectsCreated +
		intNumberOfObjectsCreatedPerLoop />
	<cfloop from="1" to="#intNumberOfObjectsCreatedPerLoop#" index="intX">
		<cfset obj = CreateObject("component", "abc") />
		<cfset arrayAppend(myArray, obj) />
	</cfloop>
	<cfset intBytesAfterTest = runtime.totalMemory() - runtime.freeMemory() />
	<cfset intBytesCreated = intBytesAfterTest - intBytesBeforeTest />
</cfloop>
<cfoutput>
<cfset runtime.gc() />
#intBytesCreated / 1024# kilobytes of objects created.<br>
Each CFC takes approximately :
#(intBytesAfterTest - intBytesBeforeTest) / intTotalNumberOfObjectsCreated / 1024#kB
</cfoutput>
<!--- this part resides in a separate file (abc.cfc) in the same directory --->
<cfcomponent name="abc">
	<cffunction name="a" output="false"><!--- Really important code here. ---></cffunction>
	<cffunction name="b" output="false"><!--- Really important code here. ---></cffunction>
	<cffunction name="c" output="false"><!--- Really important code here. ---></cffunction>
</cfcomponent>

More ColdFusion Testing Woes With Java Objects

November 24, 2008

Once again, my team is currently working on a Flex-based mapping application and I’m working on the back-end using ColdFusion to grab the data from our SQL server, package it in a Java based data transfer object (DTO), and send it to the requesting Flex app.

Another minor glitch I ran into while trying to test the ColdFusion method calls is with the SerializeJSON function built into ColdFusion 8. Because of the way BlazeDS works, in order to send objects from the Flex app back to ColdFusion we have to convert the object to a JSON string, send it, and then rebuild the object in ColdFusion from the JSON string. My teammate explains the process in his blog entry about the tech stack “Flex, ColdFusion, Java, and BlazeDS: with JSON?

The issue is simple. I want to test methods in the ColdFusion service we are writing in ColdFusion before I try to call them from the Flex app. It should be easier to debug that way. So I create the object and use SerializeJSON to make the string to pass to the ColdFusion service method. But I get errors saying such and such property is not found when the service method tries to rebuild the object.

Here’s a simple example that demonstrates the issue

//suppose you have a Java class like so
public class BoundingBox extends DataTransferObject
	implements Serializable
{
	static final long serialVersionUID = -4519523511904622352L;
	//Properties
	private LatLngOnly upperLeftLatLng;
	private LatLngOnly lowerRightLatLng;

	//methods
	public BoundingBox() {}

	public BoundingBox(LatLngOnly upperLeft, LatLngOnly lowerRight)
	{
		this.upperLeftLatLng = upperLeft;
		this.lowerRightLatLng = lowerRight;
	}	

	…
	//various methods including getters and setters
	…
}


<cfscript>
bnd = CreateObject("java", "com.lampo.mapping.vo.BoundingBox").init();
bnd.upperLeftLatLng =
       CreateObject("java", "com.lampo.mapping.vo.LatLngOnly").init(1.23,3.45);
bnd.lowerRightLatLng =
       CreateObject("java", "com.lampo.mapping.vo.LatLngOnly").init(5.67,7.89);
</cfscript>

<cfset str = SerializeJSON(bnd) />
<cfset objBnd =
       CreateObject("java","com.lampo.mapping.vo.BoundingBox").makeFromJSON(str) />

Annoyingly it turns out SerializeJSON is changing some of the property names so they no longer match. While it is true ColdFusion is not case-sensitive, Java is and so capitalizing the first character of the property names causes havoc. If the property is a reference to an object, errors are thrown. If the property is a primitive, the values are not set correctly.

The only solution I have is to alter the JSON string and hard code it as an argument for the method I’m testing. You can do this manually or with some regular expression magic.

<cfset str = SerializeJSON(bnd) />

<cfset pos = REFind('\"[A-Z]',str) />
<cfloop condition="pos NEQ 0">
     <cfset str = Replace(str,Mid(str,pos,2),'"#LCase(Mid(str,pos+1,1))#','all') />
     <cfset pos = REFind('\"[A-Z]',str) />
</cfloop>

<cfset objBnd =
       CreateObject("java","com.lampo.mapping.vo.BoundingBox").makeFromJSON(str) />

ColdFusion Testing Woes With Java Objects

October 7, 2008

My team is currently working on a Flex-based mapping application. I’m working on the backend using ColdFusion to grab the data from our SQL server, package it in a Java based data transfer object (DTO), and send it to the requesting Flex app. My teammate wrote a great blog entry about the tech stack entitled “Flex, ColdFusion, Java, and BlazeDS: with JSON?“.

One minor glitch showed up while trying to test the ColdFusion method calls. I wrote a simple ColdFusion template to call the methods in the ColdFusion service component. All I wanted to do was simply check the methods were working before I tried calling them from the Flex application. I called the function and used the <cfdump> tag to see what was in the object:

...
<cfset obj = CreateObject("component",
      "components.lampo.mapping.service.MapToolWebServiceImpl") />
<cfset myobj = obj.getPostalCodePolygon(objUser, "37128") />
<cfdump var="#myobj#">
...

Sadly all the <cfdump> tag showed was the names of the methods and properties of the object. I.e. it did not show the property values!

I did a little bit of research and was unable to locate an elegant solution. One possibility would be to simply add a method to each DTO that dumps the current state of the object. But we have over a dozen DTOs and I didn’t really want to pollute them with a debugging method. So I wrote a simple function to display the property values of any object:

<cffunction name="dumpJObj" output="false" returntype="struct" access="public">
       <cfargument name="obj" type="any" required="yes" />
       <cfargument name="depth" type="numeric" required="no" default="1" />

       <cfset var stcRet = StructNew() />
       <cfset var ary = StructKeyArray(obj) />
       <cfloop index="i" from="1" to="#ArrayLen(ary)#">
              <cfif IsDefined("obj.#ary[i]#")>
                     <cfset value = Evaluate("obj.#ary[i]#") />
                     <cfif IsObject(value) AND depth GT 0>
                           <cfset stcRet[ary[i]] = dumpJObj(value, depth - 1) />
                     <cfelseif IsObject(value)>
                           <cfset stcRet[ary[i]] = "depth limit reached" />
                     <cfelse>
                           <cfset stcRet[ary[i]] = Evaluate("obj.#ary[i]#") />
                     </cfif>
              <cfelse>
                     <cfset stcRet[ary[i]] = "" />
             </cfif>
       </cfloop>
      <cfreturn stcRet />
</cffunction>

I wrote the function specifically for the Java based DTOs we are passing. My first thought was to write it using Java reflection but I was able to write it completely in ColdFusion. So I guess it should work for ColdFusion objects as well.

My first version did not handle nested objects so in the next version I added in some recursion. This opened the possibility of an infinite loop if the class has itself as a nested object. I simply added a depth count to prevent this from happening. The depth defaults to 1 so the function will display the values of the top level object and the values of the objects it contains.

To make the function available where I needed it, I made it a part of the service class.

...
<cfset obj = CreateObject("component",
      "components.lampo.mapping.service.MapToolWebServiceImpl") />
<cfset myobj = obj.getPostalCodePolygon(objUser, "37128") />
<cfdump var="#dumpJObj(myobj)#">
...

Subversion Branch Management

August 26, 2008

If you’re just starting out with SVN, chances are you use the “just-throw-everything-on-trunk” method which will eventually lead to some, “Who broke staging again!?!?!” moments. I was introduced to the world of SVN branching via a large code rewrite effecting… pretty much our entire codebase.

Through that experience, I realized the benefits (and the hardships) of maintaining your own branch of Trunk. One of the things I love about our culture here is that we “kill the sacred cow” meaning we don’t do things just “because we’ve always done it that way.” With that in mind, we decided to have each developer setup their own project branch instead of just committing to Trunk. The benefits have been huge. We now have a Trunk the entire team can rely on. When rolling out new code, that confidence goes a long way and avoids quite a few rabbit holes trying to determine if your code changes are the problem or if someone else is half-way through a project and their code is the culprit.

After learning things the hard way, I’ve come to really appreciate Chapter 4 of the book Version Control with Subversion. If you’re not using branches and you have more than a couple people on your team, you should be using branches. If you are using branches, this chapter is a must read. It will take about an hour or so to get through, but it will save you a lot of trouble down the road.

Unless you’re using SVN version 1.5, I recommend using a wiki page or excel file to keep track of your revision numbers. This can be really helpful as you start rolling code out to your development servers, merging with release and updating your live servers. Knowing what you’ve merged and what’s been updated on which server really saves time, especially during ongoing development while rolling out bug fixes to existing versions. Below are a couple examples using Confluence and Excel:

Take it from someone who has been there, done that, got the t-shirt… branch management can be great if you do it correctly and keep track of your revision numbers during the process.

This isn’t a perfect process, but it’s the best one we’ve come up with.  If you’ve found a better way, leave a comment and start the conversation.

float: center

July 9, 2008

As a CSS enthusiast, one of the questions I have heard a lot over the past few years has been, “How do I float center?” To that, I have always replied simply, “you don’t,” and then I delve into what my colleague is actually trying to do. (Really, when was the last time you wanted content to flow around both sides of a block?)

Today my answer has changed.

Here are three block-display elements.
There are no explicit widths set.
Siblings have the same effective width and are centered.

Alternatives

First off, let’s not use a cannon to kill a mosquito. If you need to center a single line of text (styling not withstanding), just set text-align: center on the containing block. If you really need to center a block, set its left and right margins to identical values. If your block has an explicit width, set the left and right margins to auto.

What if you do not have (or desire) explicit margins or an explicit width? What if you want several sibling blocks to have the same width? I’m glad you asked…

The simple, three-step process

Consider the following HTML fragment in this example.

<div class="rowWrapper">
	<div class="row">Here are three block-display elements.</div>
	<div class="row">There are no explicit widths set.</div>
	<div class="row">Siblings have the same effective width and are centered.</div>
</div> 

For the sake of the example, I’ll give the wrapper a red border and the rows a blue border.

Here are three block-display elements.
There are no explicit widths set.
Siblings have the same effective width and are centered.

1. Float the wrapper

.rowWrapper {
	border: 1px solid red;
	float: left;
}

Wrap your blocks and float the wrapper. Because your blocks are in normal flow, they will each have the same width by expanding to fill their parent block. Because the parent block is floated, it will have the minimum width necessary to contain the child blocks. This results in the following:

Here are three block-display elements.
There are no explicit widths set.
Siblings have the same effective width and are centered.

2. Position the wrapper

.rowWrapper {
	border: 1px solid red;
	float: left;
	position: relative;
	left: 50%;
}

This centers the left edge of the wrapper, and thus your soon-to-be-centered, now equal-width blocks.

Here are three block-display elements.
There are no explicit widths set.
Siblings have the same effective width and are centered.

3. Position the ‘rows’

.row {
	border: 1px solid blue;
	position: relative;
	left: -50%;
}

This puts the center of the equal-width blocks at the left edge of the wrapper.

Here are three block-display elements.
There are no explicit widths set.
Siblings have the same effective width and are centered.

Naturally, you’ll want to remove any visible signs of the wrapper.

Good times with the cfhtmlhead tag

July 1, 2008

So for my first post, I’m going with something simple. For all you die hard ColdFusion gurus this is likely routine. But for anyone fairly new to ColdFusion, being aware of the cfhtmlhead tag is pretty handy. This is especially true if you are doing maintenance on a fairly substantial ColdFusion site … like me.

In most (probably very close to all) of the sections of DaveRamsey.com I help maintain, pages are generated by at least three ColdFusion templates. The header and footer templates create left and right navigation bars, a banner across the top, and a footer. An additional template(s) generate the main content for the page. The fun part is the html header is defined in the header template. The html body is opened in the header and closed in the footer. So if you want to do something ‘silly’ like write a JavaScript function to do something when the page loads or define an internal style sheet, you have something of a quandary. You don’t really want to change the header template because that would affect multiple pages, but the correct place for the JavaScript function and internal style sheet is the html header block.

Enter the cfhtmlhead tag. This tag simply takes a string for an argument and inserts the string into the page’s header block. So a solution to the problem described above would be to create a string with your JavaScriptcode and internal style sheet in it. The cfsavecontent tag is an easy way to do this. Then use the variable from the cfsavecontent in the cfhtmlhead tag to put it in the header block.

Something like so:

<cfoutput>
<cfsavecontent variable="strMyHeaderStuff">

<!--- page specific external style sheets --->
<link type="text/css" rel="stylesheet" href=" ... ">

<!--- page specific JavaScript libraries --->
<script type='text/javascript' language='javascript' src=' ... '></script>

<!--- internal style sheets --->
<style type="text/css">
...
</style>

<!--- custom JavaScipt functions for page --->
<script type="text/javascript" language="javascript">
...
</script>

</cfsavecontent>
</cfoutput>
<cfhtmlhead text="#strMyHeaderStuff#" />

ColdFusion 7 Memory Leak

June 20, 2008

In working on Financial Peace University Online last year, I became aware of a really ugly memory leak that had developed in the app (thankfully it was still in development, not in production). It took a few days to track down, but when I found it, it proved to have some implications that applied to all our ColdFusion web apps.

First I’ll describe the environment:

I discovered that for each singleton instance of a cfc that the ColdSpring factories provided, a little memory was leaked and never garbage collected. I simplified things to remove Mach-II, ColdSpring, and eventually everything except the simplest form I could think of. A cfm would create an instance of a cfc that had a method in it that would return a reference to itself. Then the cfm would go through 100 iterations of calling that method and place the result in an array. So I would have an array with 100 references to the same object, and still, the memory leak was present.

Finally I noticed that on my cfc method, I hadn’t specified output=”false”, though I had specified it on the cfcomponent tag. Adding output=”false” to the method that returned a reference to the cfc cured the memory leak. It seems bizarre, but if a memory leak was obvious, it wouldn’t have made it into the release version of ColdFusion.