Products

Products
Viewing By Entry / Main
May 26, 2008

ColdSpring ColdboxFactory: DSN and Mail settings

My second day into figuring out Coldbox, here's a little something cool. Putting it simply, using ColdSpring, in the CS.xml you can define a 'coldboxFactory' bean to use in creating other beans that define this factory's methods as beans also, then pass that bean into a class bean's property as a bean reference.

Actually, that doesn't sound simple as it is, so here's an example. Remember that I'm a newbie, so be nice.

coldbox.xml.cfm

<MailServerSettings>
   <MailServer>mail.urserver.com</MailServer>
   <MailPort>25</MailPort>
   <MailUsername>username</MailUsername>
   <MailPassword>password</MailPassword>
</MailServerSettings>

<Datasources>
   <Datasource alias="DSNAlias" name="dsnName" dbtype="mysql" username="name" password="pass" />
</Datasources>
I've set mail settings and data source settings here in the main Coldbox config. I'll be using Coldspring for IoC and so I'll need to be able to pass this around in my beans.

Coldspring.xml

<beans>
   <bean id="coldboxFactory" class="coldbox.system.extras.ColdboxFactory" />
   <bean id="ConfigBean" factory-bean="ColdboxFactory" factory-method="getConfigBean" />
   
   <bean id="dsnBean" factory-bean="ColdboxFactory" factory-method="getDatasource">
    <constructor-arg name="alias">
    <value>DSNAlias</value>
    </constructor-arg>
   </bean>
         
   <bean id="cfctest" class="handlers.cfctest">
      <property name="dsnBean">
         <ref bean="dsnBean" />
</property>
      <property name="ConfigBean">
         <ref bean="ConfigBean" />
</property>
   </bean>
</beans>
This coldboxFactory has several methods that interface with the coldbox config file, among other things I'm sure. Check out the API for more info. But basically I create the coldboxFactory bean, then create other beans of the factory's methods. Then notice how I pass those beans into the handler beans by reference. These 'properties', as it turns out, are setters in the cfc, such as setDsnBean and setConfigBean. Then I just put them in variables for my other methods.

cfctest.cfc

<cfcomponent output="false">
   <cffunction name="setDSNBean" access="public" returntype="void">
      <cfargument name="DSNBean" type="Any" required="true"/>
      <cfset variables.dsn = arguments.DSNBean.getName()>
   </cffunction>
   
   <cffunction name="setConfigBean" access="public" returntype="void">
      <cfargument name="configBean" type="Any" required="true"/>
      <cfset variables.mailName = arguments.configBean.getKey("mailUsername")>
   </cffunction>
   
   <cffunction name="capitalize" access="public" returntype="string">
      <cfargument name="string" type="string" required="true"/>
      <cfreturn Ucase(variables.mailName & " - " & arguments.string)/>
   </cffunction>
</cfcomponent>
Don't dis my cfc, I'm just playing around and testing stuff. Notice the setters that I mentioned before. If you do a dump/abort on these at run time you'll see what use they are.

general.cfc

<cffunction name="index" access="public" returntype="void" output="false">
   <cfargument name="Event" type="coldbox.system.beans.requestContext">
   
   <!--- Logic --->
   <cfset string = "Welcome!">
   <cfset string = variables.ioc.getBean("cfctest").capitalize(string)>
   <cfset Event.setValue("welcomeMessage",string)>   
   
   <!--- Display --->
   <cfset Event.setView("home")>
</cffunction>
And finally here's the handler that uses the IoC plugin I set in variables in the handler's init function that gets the bean set in Coldspring and runs the method that utilizes all the stuff we just went through. Check out Coldbox's Coldspring documentation for more information.

There are other factories to use I'm guessing and other factory methods within those. You can use all the methods in the coldbox.system.controller as beans also. But like I said, I'm just discovering this stuff right now. Frankly I need to think over wether or not passing bean reference properties to all my CS beans is the most effective way of using global configs any ways. Seems rather repetative as apposed to putting such things in a request scope through onRequestStart or something.

Comments

The code I provided is a cut down version of actual code and does not include the view event, form code, queries, or other behind the scenes code


thanks brother...


i lice mc

<a href="http://seoprofesionals.com/">SEO Company</a> | <a href="http://www.squidoo.com/seoprofesionals">SEO Services</a> | <a href="http://aclickaheadseo.blogspot.com/">SEO Consultants</a>


http://www.cfcdeveloper.com/index.cfm/2008/5/26/ColdSpring-ColdboxFactory-DSN-and-Mail-settings



Copyright © 2005-2006 Clint Willard. All rights reserved.
Aura skin for Clint Willard's BlogCFC inspired by Brooks Bilson's Bolg.
All trademarks property of their owners.