ColdFusion Job with AboutWeb

AboutWeb is currently hiring Jr-Mid level ColdFusion developers in the Baltimore area for an immediate need. If you would like to build your skillset with web technologies, here is an awesome opportunity! The right developers will have roughly 2 years experience with ColdFusion (or 1+ in ColdFusion MX) and exposure to CFCs and custom tags. If you are interested in joining the AboutWeb team and working with Nic Tunney, send him your resume.

We are also looking for senior J2EE and CF developers in both the DC and Baltimore metro areas.

CFObjective ColdFusion Performance Tuning Slides

Here are the slides from my presentation at CFObjective. I was going to post them sooner, but got slammed when I got back to work.

[Download Slides]

There are no Pointers in ColdFusion

We were discussing references in ColdFusion today at work and there was some confusion over what the term means. References are not pointers.

Reference != Pointer

The code below shows an example of how references work in ColdFusion/Java.

<cfset var1.data=1>

This creates a CF structure and creates a data key with a value of 1. A new object is created on the Java heap which var1 references.

<cfset var2=var1>

Creates a new variable and sets it to var1. Unlike a pointer in other languages var2 is not pointing to var1, instead var2 is a reference to the same object on the heap as var1.

<cfoutput>#var2.data#<br></cfoutput>

Since var2 references the same object as var1, you can access the structure created through var1.

<cfset var2.data=2>
<cfoutput>#var1.data#</cfoutput>

If you change the value using var2, the data is also changed for var1. They are both referencing the same object.

<cfset var1="Test">
<cfoutput>#var1#<br>#var2.data#</cfoutput>

This creates a new String Object on the Java heap and changes var1's reference to this new object, var2 is unaffected and continues to reference to the structure object.

I hope this clears up some of the confusion about ColdFusion references.

Adding Dynamic Properties and Methods to ColdFusion CFCs

ColdFusion CFCs are dynamic, really dynamic. You can add new methods and properties to objects at run time. Take for instance this object:

<cfcomponent displayname="Empty" output="false">
</cfcomponent>

It's an empty object that has no properties or methods, but I can add properties and methods to it dynamically.

<cfset obj=createObject("component","empty") />
<cfscript>   
   function test(data){
      return data;
   }   
   
   obj.myvar=2;
   obj.myfun=test;
</cfscript>

<cfoutput>
#obj.myvar#
#obj.myfun("Some Text")#
</cfoutput>

I can even add a dynamic method that uses a property I create dynamically.

<cfset obj=createObject("component","empty") />
<cfscript>   
   function times2(){
      return this.myvar*2;
   }   
   
   obj.myvar=2;
   obj.times2=times2;
</cfscript>

<cfoutput>
#obj.times2()#
</cfoutput>

You could do some really interesting things with this. You could also do some really bad things with this that would ensure your job security for years to come.

Dueling Object Factories

Phill Nacelli recently published an object factory on his blog. This started a conversation with Nic Tunney and myself about Object Factories. We each took a different path in creating our own version of an Object Factory:

Phill’s Object Factory

Nic’s Object Factory

The goals for my object factory are:

  • Simplify creation of objects: If the object factory isn’t making it easier to create objects in the rest of your code then something is wrong.
  • Easy Configuration: This later became no configuration.
  • Reusable: You should never have to edit the code in the cfc itself.
  • Support singletons

Initializing the Factory
The factory takes the base cfc path as a required argument. This path is used to create objects and saves you from worrying about object paths in the rest of your code. The factory initialization also takes an unlimited number of key value pairs that will be used to initialize other objects. For instance many objects need a DSN for initialization either as a simple String or as DSN object. If the object factory is initialized with a DSN then the DSN object will automatically be passed into all object that have DSN as an argument. Currently this matching is based on name and not type, so following a standard naming convention in your init methods is required to use this factory. Here’s a sample of initializing the object factory with a DSN:

<cfset factory=createObject("component","#basepath#objectFactory").init(basePath,"DSN",DSN) />

Initialization Parameters
The getObject method retrieves an initialized object from the factory. You simply pass in the name off the object you wish to get. The factory introspecs the object using the getMetaData function and will automatically add in initialization arguments. Using the factory created above you could request an object that required the DSN without specifying it.

<cfcomponent displayname="MyObject" hint=" MyObject " output="false">
   <cfset variables.DSN="" />

   <cffunction name="init" displayname="Init" hint="Init" access="public" output="false">   
      <cfargument name="dsn" displayname="DSN" hint="Datasource" required="true">
      <cfset variables.DSN=arguments.DSN />   
      <cfreturn this />
   </cffunction>
</cfcomponent>

Code to get the object from the factory:
<cfset obj=factory.getObject("MyObject")>
The factory sees that the object has DNS as an argument and automatically adds it.

Additional Arguments
Objects may need additional arguments that aren’t auto populated. The getObject method takes the object name along with an unlimited number of key values pairs. The key value pairs are added to the argument collection for the object init. Arguments are automatically numbered according to the order in which they were passed to the method. Since we pass the object name as the first argument to getObject additional arguments are named numerically starting with 2.

<cfloop index="i" from="2" to="#ArrayLen(StructKeyArray(arguments))#" step="2">
   <cfset objectParams[arguments[i]]=arguments[i+1] />
</cfloop>

Singletons
The factory supports singletons. Following my no configuration goal I wanted objects to be able to indicate they were singletons to the factory. This is accomplished by adding this.singleton=true to an object. This creates a public singleton property that the object factory can check. The factory checks to see if a singleton has already been created in the singleton cache, if it is in the cache the existing object is returned.

<cfif structKeyExists(variables.singletons,arguments.objectType)>
   <cfreturn variables.singletons[arguments.objectType] />
</cfif>
Once a new object is created it is checked or a singleton property. If it exists and is true the object is added to the singleton cache. This will only happen the first time an object is initialized. If it was already in the cache the existing object would have been returned in the previous code.
<cfif StructKeyExists(objRet,"singleton") and objRet.singleton>
   <cfset structInsert(variables.singletons,arguments.objectType,objRet,true) />
</cfif>

Over all I accomplished my goals. The object factory can be dropped into an application and supports all the functionality I wanted without needing any type of configuration or editing of the object factory code itself. It doesn’t have all the features of some other factories, but covers the functionality you need on a regular basis.

Download Object Factory and Sample Code

ColdFusion 7.0.2 Updater Doesn't Include Latest Database Drivers

I was working on a server for a client that was having some database issues. They had installed the 7.0.2 updater, but the database drivers were older. I updated to the 3.5 JDBC drivers which fixed the database problem they were having.

Later I found an Adobe Tech Note which suggests updating the drivers after installing the updater.

Error in Report Builder when you Upgrade to 7.0.2

I don't use the report builder very often, but I ran into an error the other day when trying to use it. I used it on machine with 7.0 without a problem, but when I tried the same report on my laptop with 7.0.2 it was throwing an error.

Message: Report compilation error. Error at (135, 20: nullType: coldfusion.util.RuntimeWrapper).

It turns out the updater installs a new version of a jar file, but doesn't remove the old one. To fix the problem you have to manually go in and remove the old file.

There is an official Adobe TechNote on the issue at: http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=96ce62cd&pss=rss_coldfusion_96ce62cd

Adding virtual paths to the internal ColdFusion Web Server

Several people have asked me about this recently so I thought I’d Blog about it. ColdFusion installs with an internal web server. For stand alone installs it runs on port 8500 and for multi-server install it uses port 8300. The jrun-web.xml file can be used to configure the server. The location of this file and the default web root depends on your install type.

Multi-server install:
C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\jrun-web.xml
Webroot: C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war

Stand alone install:
C:\CFusionMX7\wwwroot\WEB-INF\jrun-web.xml
Webroot: C:\CFusionMX7\wwwroot

To add a new virtual mapping you just add a virtual-mapping tag to the configuration file and specify the resource path and system path like so:

<virtual-mapping>
<resource-path>/temp</resource-path>
<system-path>C:/temp</system-path>
</virtual-mapping>

To edit the other settings for the internal web server you have to edit the service in the jrun.xml file, but I’m not going to cover that in this post.

CFUnited Day 1

CFUnited is underway. Things seem to be going well so far. There was a big announcement about the release of Flex 2, no real shocker there. I wasn’t able to attend any sessions today as I was busy working in the community area which AboutWeb is hosting again this year and working at the AboutWeb booth. The community area favorite The Wheel of Fusion is back again this year:

We also have a new version of Shoot the Guru with a twist. This year it’s a video game written in Flash with a motion sensing gun. It’s pretty cool, but I’m bias since I wrote it.

We also did a Podcast today, which will be releasing as soon as we have time to convert it and get it up on the web. Probably not until after the conference.

CFUnited is Fast Approaching

AboutWeb is going to be running the community area again this year at CFUnited. We have an X-box 360 for the community area which we will be giving away at the end of the conference. I just picked up a couple games for it: Need for Speed and FIFA06 (in honor of the World Cup). Not generally the type of games I like to play as I prefer Adventure/RPG games, but I'm sure other people will enjoy them. The community area will also be featuring the new and improved Shoot the Guru 2 and ColdFusion Trivia.

Unfortunatly Hal Helms can't make it to the conference this year. On the plus side an AboutWeb employee Nic Tunney will be picking up Hal's "Variables and Conditions" presentation.

BlogCFC was created by Raymond Camden. This blog is running version 5.004.