I was thinking it would be really nice if Reactor let you load
multiple XML files that define objects, much as ColdSpring lets you
call loadBeans() multiple times. This is helpful for two reasons:
1. You can break down large XML files into multiple files
2. You can load in a testing XML file to override definitions in an
already loaded file

The latter may not be as useful for Reactor but the former would
certainly be useful to me.

I started to look at how Reactor handles the XML file and realized
that I could already do what I want by extending the
reactor.config.config CFC and simply using my extended version as the
argument to the reactorFactory!

Here's my ColdSpring bean file:

<beans>

        <bean id="reactorConfiguration"
class="com.adobe.hs.common.reactor.configuration">
                <constructor-arg
name="pathToConfigXml"><value>/environment/Reactor.xml</value></constructor-arg>
        </bean>
        
        <bean id="reactorFactory" class="reactor.reactorFactory">
                <constructor-arg name="configuration"><ref
bean="reactorConfiguration" /></constructor-arg>
        </bean>
        
</beans>

Here's my basic Reactor.xml file:

<reactor>
        <config>
                <project value="stanza" />
                <dsn value="stanza" />
                <type value="mysql" />
                <mapping value="/reactordata" />
                <mode value="development" />
                <username value="stanza_user" />
                <password value="******" />
        </config>
</reactor>

I can load in multiple XML files containing object definitions by
calling addObjects(pathToXMLFile) on the (new) configuration object.

Here's my extended configuration CFC, com.adobe.hs.common.reactor.configuration:

<cfcomponent extends="reactor.config.config">
        
        <cffunction name="init" returntype="reactor.config.config"
access="public" output="false" hint="I configure this config bean.">
                <cfargument name="pathToConfigXml" required="yes" type="string"
hint="I am the path to the config XML file." />
                
                <cfset super.init(arguments.pathToConfigXml) />
                
                <cfset variables.objectMap = structNew() />
                <cfset addObjectsFromXml(getConfigXml()) />
                
        </cffunction>

        <cffunction name="addObjects" returntype="void" access="public" 
output="false">
                <cfargument name="objectXmlFile" type="string" required="true" 
/>

                <cfset var xml = 0 />

                <!--- attempt to expand the path to config --->
                <cfif fileExists(expandPath(arguments.objectXmlFile))>
                        <cfset arguments.objectXmlFile = 
expandPath(arguments.objectXmlFile) />
                </cfif>

                <cfif not fileExists(arguments.objectXmlFile)>
                        <cfthrow type="reactor.config.InvalidPathToConfig"
                                message="Invalid Path To Config"
                                detail="The path #arguments.objectXmlFile# does not 
exist." />
                </cfif>

                <!--- read and parse the xml --->
                <cffile action="read" file="#arguments.objectXmlFile#" 
variable="xml" />
                <cfset xml = XMLParse(xml) />
                
                <cfset addObjectsFromXml(xml) />

        </cffunction>
        
        <cffunction name="addObjectsFromXml" returntype="void"
access="private" output="false">
                <cfargument name="configXml" type="xml" required="true" />
                <cfset var tableConfig = xmlSearch(arguments.configXml,
"/reactor/objects/object") />
                <cfset var nTables = arrayLen(tableConfig) />
                <cfset var table = 0 />
                <cfset var x = 0 />

                <cfloop from="1" to="#nTables#" index="x">
                        <cfset table = tableConfig[x] />
                        <cfif not structKeyExists(table.xmlAttributes,"alias")>
                                <cfset table.xmlAttributes.alias = 
table.XmlAttributes.name />
                        </cfif>
                        <cfset variables.objectMap[table.xmlAttributes.alias] = 
table />
                </cfloop>

        </cffunction>

        <!--- override getObjectConfig --->
        <cffunction name="getObjectConfig" access="public" hint="I return the
base configuration for a particular object.  If the object is not
explictly configure a default config is returned." output="false"
returntype="string">
                <cfargument name="alias" hint="I am the alias of the object to 
get
the configuration for" required="yes" type="string" />

                <cfset var table = 0 />

                <cfif structKeyExists(variables.objectMap,arguments.alias)>
                        <cfset table = variables.objectMap[arguments.alias] />
                <cfelse>
                        <cfset table = "<object name=""#arguments.alias#""
alias=""#arguments.alias#"" />" />
                </cfif>

                <!--- return the base config --->
                <cfreturn xmlParse(toString(table)) />
        </cffunction>

</cfcomponent>

--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Reactor for ColdFusion Mailing List
[email protected]
Archives at: http://www.mail-archive.com/reactor%40doughughes.net/
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Reply via email to