LakerNetman
Coronel
Joined: 05/27/09
Posts: 72 |
Passing a Datasource in ColdBox config to ColdSpring
06/11/09 10:32 AM
Shucks, I was hoping someone else would have posted a topic before I needed more help :">
This is my first effort at using ColdSpring with ColdBox and I am trying to reconcile how to get my datasource setting passed to ColdSpring.
Here is my ColdSpring config:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName">
<bean id="customerDAO" class="model.customerDAO">
<constructor-arg name="dsn"><value>${dsn}</value></constructor-arg>
</bean>
<bean id="customerGateway" class="model.customerGateway">
<constructor-arg name="dsn"><value>${dsn}</value></constructor-arg>
</bean>
<bean id="customerService" class="model.customerService">
<constructor-arg name="customerDAO">
<ref bean="customerDAO"/>
</constructor-arg>
<constructor-arg name="customerGateway">
<ref bean="customerGateway"/>
</constructor-arg>
</bean>
</beans>
Where ${dsn} refers to:
<YourSettings>
<!-- @YOURSETTINGS@ -->
<Setting name="dsn" value="betaDB" />
</YourSettings>
in my ColdBox config.
However, it seems more correct that I should be able to pass config info from this part of my ColdBox config directly to ColdSpring:
<Datasources>
<Datasource alias="betaDB" name="betaDB" dbtype="mssql" username="" password="" />
</Datasources>
since it's redundant to the setting I created. Reading through the ColdBox Config and Autowire Guides made me think this should be possible, but I am still working on learning so much about the rest of the framework the pieces didn't jibe.
So, more generally, can I or how do I access other sections of the ColdBox config for use in setting up ColdSpring (or Transfer, Reactor, etc)? (A reading reference would suffice if I simply didn't look at the right docs.)
TIA, Laker
|
|
Link | Top | Bottom
|
analogdigital
Veteran Master
Joined: 04/26/09
Posts: 121 |
RE: Passing a Datasource in ColdBox config to ColdSpring
06/11/09 11:35 AM
Welcome! Ok, well, I'll address what I can this morning. (I'm about to post an issue myself) You can see the docs for what I'm posting here:
http://ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbTransferExtras
First, if you want to do this with Transfer you can use the following in your coldbox.xml.cfm:
<YourSettings>
<Setting name="TransferSettings.DefinitionsPath" value="config/definitions" />
<Setting name="TransferSettings.configPath" value="config/transfer.xml.cfm" />
<Setting name="TransferSettings.TransferDSNAlias" value="MyDataSource" />
</YourSettings>
This references MyDataSource defined in coldbox.xml.cfm
You can then define a bean in ColdSpring
<!-- MyDatasource -->
<bean id="MyDataSource" factory-bean="ColdBoxFactory" factory-method="getDatasource">
<constructor-arg name="alias">
<value>${TransferSettings.TransferDSNAlias}</value>
</constructor-arg>
</bean>
<!-- Coldbox-transfer Config Factory -->
<bean id="TransferConfigFactory" class="coldbox.system.extras.transfer.TransferConfigFactory" singleton="true" />
<bean id="TransferFactory" class="transfer.TransferFactory" singleton="true">
<constructor-arg name="configuration">
<bean factory-bean="TransferConfigFactory" factory-method="getTransferConfig">
<!-- Config Path -->
<constructor-arg name="configPath"><value>${TransferSettings.configPath}</value></constructor-arg>
<!-- ColdBox Datasource Bean -->
<constructor-arg name="dsnBean"><ref bean="MyDataSource" /></constructor-arg>
</bean>
</constructor-arg>
</bean>
<bean id="Transfer" factory-bean="TransferFactory" factory-method="getTransfer" />
You can then inject Transfer into any other bean.
Or there is a TransferLoader interceptor:
<!-- Transfer Loader -->
<Interceptor class="coldbox.system.extras.transfer.TransferLoader">
<Property name="ConfigPath">/${AppMapping}/config/transfer.xml.cfm</Property>
<Property name="definitionPath">/${AppMapping}/config/definitions</Property>
<Property name="datasourceAlias">MyDataSource</Property>
</Interceptor>
This can be used to load transfer using the datasources defined in your ColdBox config. You load Transfer into the ColdBox cache and use it as a bean like so:
<!-- Define Transfer to be used as a bean -->
<bean id="Transfer" factory-bean="ColdboxOCM" factory-method="get">
<constructor-arg name="objectKey"><value>Transfer</value></constructor-arg>
</bean>
<bean id="TransferTransaction" factory-bean="ColdboxOCM" factory-method="get">
<constructor-arg name="objectKey"><value>TransferTransaction</value></constructor-arg>
</bean>
So, as for the response to your original question, when you need the datasource information for whatever purpose:
<!-- MyDatasource -->
<bean id="MyDataSource" factory-bean="ColdBoxFactory" factory-method="getDatasource">
<constructor-arg name="alias">
<value>DATASOURCEALIASGOESHERE</value>
</constructor-arg>
</bean>
Basically you call the getDatasource method on ColdBoxFactory and ask for whichever datasource you want.
I'm a little tired, but this should be mostly correct.
Hope you are enjoying ColdBox!
|
|
Link | Top | Bottom
|
LakerNetman
Coronel
Joined: 05/27/09
Posts: 72 |
RE: Passing a Datasource in ColdBox config to ColdSpring
06/11/09 2:26 PM
Thanks for your response. My head was swimming a bit after the first part, but I got the gist of it after reading through it a couple of times.
So, rather than passing the value (sort of) directly, via the ${} mechanism, I should access the ColdBox framework via ColdSpring's methods and ask for the datasource using the appropriate ColdBox method. Thereby eliminating the need for the entry under "YourSettings".
Is that about right?
Laker
|
|
Link | Top | Bottom
|
analogdigital
Veteran Master
Joined: 04/26/09
Posts: 121 |
RE: Passing a Datasource in ColdBox config to ColdSpring
06/11/09 7:10 PM
Yes, that's right. And for Transfer you have the special TransferLoader and the Transfer config factory extras. :)
|
|
Link | Top | Bottom
|
willesv
Joined: 02/08/10
Posts: 4 |
RE: Passing a Datasource in ColdBox config to ColdSpring
02/08/10 10:09 AM
Any news? Having the same problem over here.
The coldboxfactory initializing the datasource bean is not getting the correct structure via
var datasources = application[coldboxAppKey].getSetting("Datasources");
The problem is probably in the AppliationLoaderCFC
<!--- parseDatasources ---> <cffunction name="parseDatasources" output="false" access="public" returntype="void" hint="Parse Datsources"> <cfargument name="oConfig" type="any" required="true" hint="The config object"/> <cfargument name="config" type="struct" required="true" hint="The config struct"/> <cfscript> var configStruct = arguments.config; var datasources = arguments.oConfig.getPropertyMixin("datasources","variables",structnew()); var key = "";
// Defaults configStruct.datasources = structnew(); //loop over datasources for( key in datasources ){ if( NOT structKeyExists(datasources[key],"name") ){ getUtil().throwit("This datasource #key# entry's name cannot be blank","","CFCApplicationLoader.DatasourceException"); } // defaults if( structKeyExists(datasources[key],"username") ){ datasources[key].username = ""; } if( structKeyExists(datasources[key],"password") ){ datasources[key].password = ""; } if( structKeyExists(datasources[key],"dbType") ){ datasources[key].dbType = ""; } // save datasoure definition configStruct.datasources[key] = datasources[key]; } </cfscript> </cffunction>
|
|
Link | Top | Bottom
|
willesv
Joined: 02/08/10
Posts: 4 |
RE: Passing a Datasource in ColdBox config to ColdSpring
02/08/10 10:17 AM
forgot to mention IMO the test should be like this:
if( not structKeyExists(datasources[key],"username") ){
|
|
Link | Top | Bottom
|
lmajano
Veteran Master
Joined: 01/29/05
Posts: 1209 |
RE: Passing a Datasource in ColdBox config to ColdSpring
02/08/10 10:48 AM
Thanks!!
This is now fixed on SVN targeted for M5 Luis Majano
|
|
Link | Top | Bottom
|