Coldbox, ColdSpring, Transfer, Refactoring And Me (part 2)

Yesterday, I showed you how to refactor Coldbox, ColdSpring and Transfer on an unconventional server layout. Now that we`ve taken care of that, we can get the three to talk to each other. Luckily, Coldbox makes this extremely easy. (This is also my first post using the afae editor plugin for Eclipse, so bear with me if everything goes wrong (which I don't expect it will).)

Building the Coldbox application

The first thing we need to do is setup the Coldbox application. The easiest way to do that is with the build.xml Ant task in the applicationTemplate folder. Running that task will get you two distinct inputs:

What is the name of your application: (Ex:blog,forum)?
This is going to be the name of your application (appName). It's also the name of the folder where it will be installed.
What is the destination directory: (Ex:/data/webroot)?
Where to install the application (absolute path). Do not include the application folder (ex.: your application name is cwBlog and you want it to be installed in c:\wwwroot\dev\cwBlog, you would enter c:\wwwroot\dev\. cwBlog will be added by the Ant task).

Once the task is done running, you'll have a clean install of Coldbox. You can now point your browser to the application (in our case, it's http://localhost/dev/cwBlog/) and see a nice "Welcome to Coldbox" message.

Integrating ColdSpring with Coldbox

ColdSpring is an Inversion of Control (IoC) framework. In a nutshell, what it does is take away the headache of managing all your objects and their dependencies (and more!). Getting it to work with Coldbox is pretty straightforward. In Coldbox's coldbox.xml.cfm file, look for these lines:

<Setting name="IOCFramework" value="" />
<Setting name="IOCDefinitionFile" value="" />

And change them to:

<Setting name="IOCFramework" value="coldspring" />
<Setting name="IOCDefinitionFile" value="config/coldspring.xml.cfm" />

Coldbox comes with ColdSpring support built-in. With these two settings we're telling it to enable the ColdSpring plug-in (more on that later) and also where to find the ColdSpring configuration file. By default, however, Coldbox will look in /coldspring/ for the framework. Since we've refactored our installation (see part 1 ), we need to give Coldbox another location to look for it. In the "YourSettings" section of coldbox.xml.cfm, add this line:

<Setting name="ColdspringBeanFactory" value="frameworks.coldspring_1_2.beans.DefaultXMLBeanFactory" />

Now that ColdSpring is setup, we probably want to test it out. In Coldbox's model folder, create a new file named test.cfc. We just need something simple, so paste this code into it:

<cfcomponent output="false">

    <cffunction name = "init" access="public" returntype="test">

        <cfset setInitiated( now() ) />

        <cfreturn this />
    </cffunction>

    <cffunction name="setInitiated" access="public" returntype="test">
        <cfargument name="time" type="date" required="true" />

        <cfset variables.instance.initiated = arguments.time />

        <cfreturn this />
    </cffunction>

    <cffunction name="getInitiated" access="public" returntype="date">
        <cfreturn variables.instance.initiated />
    </cffunction>

</cfcomponent>

Pretty straightforward here. We have three methods: init, which is called automatically by ColdSpring when the bean is initiated (by convention); setInitiated, called by the init method; getInitiated, which returns the beans' initiated time stamp. We now need to tell ColdSpring about our object. Create a file in the application folder/config/ and name it coldspring.xml.cfm. Paste this code into it:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="test" class="cwBlog.model.test" />

</beans>

There's not much here, but if this is the first time you see a ColdSpring configuration file you probably want to know what's happening. We'll take it one line at a time. Beans represents the root element of the configuration file. The next line:

<bean id="test" class="cwBlog.model.test" />

This tells ColdSpring to create a bean named test, using the object having the class path provided (in simpler terms, it creates a reference to the object found at cwBlog.model.test).

Now that ColdSpring knows about our test.cfc, and that Coldbox knows how to talk to ColdSpring, we can get an example going. Create a new method in handlers/general.cfc and name it "testingColdSpring".

<cffunction name="testingColdSpring" access="public" returntype="void">
        <cfargument name="event" type="any" required="true" />

        <cfset event.setValue( "testBean", getPlugin( "ioc" ).getBean( "test" ) ) />

        <cfset event.setView( "testingColdSpring" ) />

    </cffunction>

Although there aren't alot of lines here, I feel it still needs a little clarification. We'll start by the inside and work our way out from there.

getPlugin( "ioc" )

This is Coldbox's way of accessing ColdSpring (notice "ioc", which stands for Inversion of Control). This statement retrieves a reference to ColdSpring and it's methods.

getPlugin( "ioc" ).getBean( "test" )

Now that we have a reference to ColdSpring, we can use it's getBean method to retrieve a reference to an object. The argument we pass in must correspond to the ID of a bean defined in coldspring.xml.cfm.

event.setValue( "testBean", getPlugin( "ioc" ).getBean( "test" ) )

With the complete statement, we store a reference of the bean in Coldbox's request collection, making it available to the view.

Create a new file in views/ and name it "testingColdspring.cfm". Paste these lines into the new file:

<cfoutput>
<p>Time the test.cfc bean was initiated: #rc.testBean.getInitiated()#</p>
</cfoutput>

Open up your browser and point it to your application and the test page. In my case, this is located at http://localhost/dev/cwBlog/index.cfm/general/testingColdSpring (in case SES is disabled, this link also works http://localhost/dev/cwBlog/index.cfm?event=general.testingColdSpring ). If all went well, you should see something like this:

Time the test.cfc bean was initiated: {ts '2008-10-15 12:55:03'}

In the next part, we'll take a look at Transfer and its integration in Coldbox.

Comments
Luis Majano's Gravatar just a clarification that event.getPlugin() does not exist. You just have to do getPlugin().

Luis
# Posted By Luis Majano | 10/21/08 4:34 PM
Francois Levesque's Gravatar Thanks for the correction, Luis. That's what happens when you modify code without testing it out first :P. I've corrected event.getPlugin() and replaced with getPlugin().
# Posted By Francois Levesque | 10/21/08 6:43 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.3.000. Contact Blog Owner