| | 1 | = Custom Configuration = |
| | 2 | |
| | 3 | Often, your application will need to know some configuration information, such as datasource names or mailservers. For the sake of example, let's pretend that your application needs to use the <cfmail> tag, and you'd like to be able to store the servername, username, and password settings in a file outside of your source code. That's just good practice. |
| | 4 | |
| | 5 | In Model-Glue:Unity, it's also really easy. |
| | 6 | |
| | 7 | Every Model-Glue:Unity application (that's not an upgrade from 1.x) has a !ColdSpring.xml that's intended to serve as the central store of all configuration information for your application. |
| | 8 | |
| | 9 | The file is organized into a series of <bean> tags. Each bean represents a CFC that you can ask for inside of your Model-Glue application. Full use of !ColdSpring is beyond the scope of this document - it's capable of a lot more than we're about to cover. |
| | 10 | |
| | 11 | Model-Glue:Unity comes with a bean (!ModelGlue.Bean.!CommonBeans.!SimpleConfig) that's well suited for common configuration chores. It has a single property (config) that is a structure set through the setConfig() function. This structure serves as a "registry" of configuration settings. Each setting can be requested through the bean's getConfigSetting() function by passing the name of the setting to retrieve. |
| | 12 | |
| | 13 | To apply this to the scenario of a mail server, we could add the following to !ColdSpring.xml: |
| | 14 | |
| | 15 | {{{ |
| | 16 | <bean id="mailConfiguration" class="ModelGlue.Bean.CommonBeans.SimpleConfig"> |
| | 17 | <property name="config"> |
| | 18 | <!-- In Coldspring, a "map" represents a struct --> |
| | 19 | <map> |
| | 20 | <entry key="mailserver"> |
| | 21 | <value>mail.mydomain.com</value> |
| | 22 | </entry> |
| | 23 | <entry key="username"> |
| | 24 | <value>smtpUsername</value> |
| | 25 | </entry> |
| | 26 | <entry key="password"> |
| | 27 | <value>smtpPassword</value> |
| | 28 | </entry> |
| | 29 | </map> |
| | 30 | </property> |
| | 31 | </bean> |
| | 32 | }}} |
| | 33 | |
| | 34 | After the configuration XML is added to !ColdSpring, using the configuration settings is easy. Below is an example listener function that uses the configuration settings in a <cfmail> tag: |
| | 35 | |
| | 36 | {{{ |
| | 37 | <cffunction name="sendEmail" access="public" returnType="void" output="false"> |
| | 38 | <cfargument name="event" type="any"> |
| | 39 | |
| | 40 | <cfset var message = arguments.event.getValue("message") /> |
| | 41 | <cfset var subj = arguments.event.getValue("subj") /> |
| | 42 | <cfset var to = arguments.event.getValue("to") /> |
| | 43 | |
| | 44 | <!--- Get mail configuration ---> |
| | 45 | <cfset var mailCfg = getModelGlue().getBean("mailConfiguration") /> |
| | 46 | |
| | 47 | <!--- Use the mail configuration to send mail ---> |
| | 48 | <cfmail |
| | 49 | to="#to#" |
| | 50 | subject="#subject#" |
| | 51 | server="#mailCfg.getConfigSetting("mailserver")#" |
| | 52 | username="#mailCfg.getConfigSetting("username")#" |
| | 53 | password="#mailCfg.getConfigSetting("password")#" |
| | 54 | >#message#</cfmail> |
| | 55 | </cffunction> |
| | 56 | }}} |