Photo from Chile

Dynamic Datasources with Transfer and Coldspring

I have different datasource names on my local machine, in my dev environment and in my production environment, and I wanted to figure out a way to automagically tell transfer which datasource to use based on the server it's running on. I couldn't find any programmatic way of doing this with Transfer, as it seems to require a datasource.xml file. I decided to blog my solution, as it may be useful to others, or perhaps someone will point out a much better way to do it.

Here's what I'm doing:

I create 3 separate xml files, datasource_localhost.xml, datasource_dev.xml and datasource_prod.xml. Each one contains the name of the appropriate datasource.

In my Coldspring.xml file, where I define the Transfer bean, I use this code:

view plain print about
1<bean id="Transfer" class="transfer.TransferFactory">
2    <constructor-arg name="datasourcePath"><value>${TransferDatasourcePath}</value></constructor-arg>
3    <constructor-arg name="configPath"><value>/_MyModel/config/transfer.xml.cfm</value></constructor-arg>
4    <constructor-arg name="definitionPath"><value>/_MyModel/config/transfer</value></constructor-arg>
5</bean>

In my CF code which creates the TransferFactory, I do the following:

view plain print about
1<cfset TransferDataSource = "datasource_prod.xml.cfm" />
2<cfif getPageContext().getRequest().getServerName() EQ "localhost">
3    <cfset TransferDataSource = "datasource_localhost.xml.cfm" />
4<cfelseif getPageContext().getRequest().getServerName() CONTAINS "dev.mydomainname">
5    <cfset TransferDataSourcePath = "datasource_dev.xml.cfm" />
6</cfif>
7<cfset defaultProperties = StructNew() />
8<cfset defaultProperties.TransferDataSourcePath = "/_MyModel/config/" & TransferDataSource />
9<cfset serviceFactory = CreateObject("component","coldspring.beans.DefaultXmlBeanFactory").init (defaultProperties=defaultProperties) />

I'm setting other default properties, unrelated to Transfer, based on ServerName, but I left them out in the interest of brevity.

It does seem a bit ugly, but it works very well. I realize that ANT is probably the answer to this, but, only working two days per week, I haven't found the time to figure ANT out yet, so I came up with this solution in the meantime.

TweetBacks
Comments