Passing Application scoped variables to ColdSpring within Model Glue

Passing defaultProperties in ColdSpring with Model Glue

Or Using defaultProperties with CS and MG. I couldn't figure out which title would be better.

Basically this is a guide on how to pass variables into ColdSpring. In my case from the Application scope. Recently I've been working on a Model Glue, Transfer, and ColdSpring app and came across a problem which I couldn't find a direct answer to online. The application I'm working is only a small part of the site and has some settings in the Application scope which needs to remain there. Usually I put all my variables in a ColdSpring/Model Glue simpleConfig Bean (using the class ModelGlue.Bean.CommonBeans.SimpleConfig) but this time I needed CS to get the variables from the application scope. This actually isn't too hard as you can pass properties(dynamic variables) into the config.xml e.g.

Here's the code for creating the bean factory. You would find this in onApplicationStart or at the top of your Application.cfm.

Code for Application.cfm/cfc

<!-- First we create our global vars in the Application scope -->
<cfset application.settings = structNew() />
<!-- Add our DSN name to the Settings struct -->
<cfset application.settings.dsn = "myDSN" />
<!-- If our bean factory doesn't exist then we create it. You could also check for a url.reinit var as well here -->
<cfif NOT isDefined("application.beanFactory")>
   <!-- Our struct to hold the variables we are going to pass to ColdSpring -->
   <cfset properties = structNew() />
   <!-- Our struct to hold the variables we are going to pass to ColdSpring -->
   <cfset properties.dsn = "dsn_reference">
   <!-- Create our bean object -->   
   <cfset application.beanFactory = createObject("component","coldspring.beans.DefaultXmlBeanFactory").init(structNew(), properties)/>
   <!-- Pass a fully qualified path to a bean definition xml file -->
   <cfset application.beanFactory.loadBeansFromXmlFile(expandPath('/config/beans.xml'), true)/>
</cfif>

In our Beans.xml we need to reference the properties structure by the key.

Code for Beans.xml

<?xml version="1.0" encoding="utf-8"?>
<beans>
<bean id="myDAO" class="com.myDAO">
      <!-- ${dsn} comes from the properties struct we passed in from the Application.cfc/cfm code -->
<constructor-arg name="dsn"><value>${dsn}</value></constructor-arg>
</bean>
</beans>

Now that we have our beans.xml reading dynamic variable how can we pass this to the default ColdSpring.xml that Model Glue uses?

First off when it comes to Model Glue there is already ColdSpring.xml which holds all the config imformation for the application, ORM etc etc. We're not going to touch that so this method is something you can introduce midway through a project. What we are going to do is introduce Beans.xml as the Parent Bean Factory to the child ColdSpring.xml.

Create a SimpleConfig bean from Model Glue (ModelGlue.Bean.CommonBeans.SimpleConfig) and save this in the Beans.xml file. This config bean will hold details for our email server. The values are going to come from the Application scope.

You xml file should look like:

<beans>
   <!-- Put definitions for your own beans and services here using ModelGlue.Bean.CommonBeans.SimpleConfig class -->
   <bean id="EmailConfig" class="ModelGlue.Bean.CommonBeans.SimpleConfig">
      <property name="config">
         <map>
            <entry key="emailServer">
               <value>${emailServer}</value>
            </entry>   
            <entry key="emailUName">   
               <value>${emailUName}</value>
            </entry>
            <entry key="emailPWord">
               <value>${emailPWord}</value>
            </entry>
         </map>
      </property>   
   </bean>
</beans>

In the Appliacation.cfm add the following code

<!-- If our bean factory doesn't exist then we create it. You could also check for a url.reinit var as well here -->
<cfif NOT isDefined("application.beanFactory")>
   <cfset application.emailServer   ="" />
   <cfset application.emailUName   ="" />
   <cfset application.emailPWord   ="" />

   <!--
      Put the email settings into the properties structure which
      get passed to a Parent ColdSpring Factory
   -->

   <cfset properties = structNew() />
   <cfset properties.emailServer = application.emailServer />
   <cfset properties.emailUName = application.emailUName />
   <cfset properties.emailPWord = application.emailPWord />

   <cfset application.beanFactory2 = createObject("component","coldspring.beans.DefaultXmlBeanFactory").init(defaultProperties=properties)/>
   <cfset application.beanFactory2.loadBeansFromXmlFile("#expandPath('/config/beans.xml')#",true)/>
</cfif>

At this point we have our bean factory stored in application.beanFactory2. We are going to reference this in the index.cfm of MG under the HIERARCHIAL BEAN FACTORY SUPPORT area. Look for the commented out <cfset> which sets the variable ModelGlue_PARENT_BEAN_FACTORY. Uncomment the <cfset> and add the following:

<!--
   **HIERARCHIAL BEAN FACTORY SUPPORT**

   If you'd like to designate a parent bean factory for the one that powers Model-Glue,
   simply do whatever you need to do to set the following value to the parent bean factory
   instance:
-->   
<cfset ModelGlue_PARENT_BEAN_FACTORY = application.beanFactory2 />

At this point the hard work is done. All that is left is to add a bean that uses the config setting. In my case I have a CFC in a Model folder which accepts the constructor argument "EmailConfig" e.g.

<bean id="email" class="model.emailCFC">
   <constructor-arg name="EmailConfig">
      <ref bean="EmailConfig" />
   </constructor-arg>
</bean>

Thats it. From your emailCFC you just need to reference variables.emailConfig.getconfigSetting('emailServer') to the get the server etc.

This post couldn't of been done without the help of http://www.danvega.org/blog/index.cfm/2006/11/25/Passing-Properties-To-ColdSpring and http://groups.google.com/group/model-glue/browse_thread/thread/de628a4fb85c6ebd/ so for futher investigation you can start there.

Comments (0) Print Send del.icio.us Digg It! Linking Blogs

1194 Views | Posted At : December 6, 2007 5:32 AM | Posted By : Andy Jarrett
Related Categories: ColdFusion, model-glue, ColdSpring, Rough Guide

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)