<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Cranky Bit</title>
	<atom:link href="http://blog.crankybit.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.crankybit.com</link>
	<description>Take a byte out of tech!</description>
	<pubDate>Tue, 05 May 2009 06:04:22 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Importance of Understanding Factories, Part 2: Transient Factories</title>
		<link>http://blog.crankybit.com/factories-2/</link>
		<comments>http://blog.crankybit.com/factories-2/#comments</comments>
		<pubDate>Tue, 05 May 2009 06:01:04 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<category><![CDATA[factories]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=327</guid>
		<description><![CDATA[In my last post, I discussed some of the mistakes that can be made when using ColdSpring as our factory. It became evident that using ColdSpring to inject transient objects&#8212;that is, objects that have a state for a momentary use and then are discarded&#8212;into our service objects or other related singletons is problematic. This is [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post, I discussed some of the <em>mistakes</em> that can be made when using ColdSpring as our factory. It became evident that using ColdSpring to inject transient objects&mdash;that is, objects that have a state for a momentary use and then are discarded&mdash;into our service objects or other related singletons is problematic. This is largely due to the concurrency issue with reusing that injected bean in the service object's methods, and mistakes that can be made if we forget about objects being <em>passed by reference</em>. Ray Camden also discussed this recently in <a href="http://www.coldfusionjedi.com/index.cfm/2009/5/1/ColdFusion-and-Pass-by-Reference-versus-Value" target="_blank">ColdFusion and Pass by Reference versus Value</a>.</p>
<p><strong>The answer is to build a transient factory for those beans.</strong> We can do it without losing the benefits of ColdSpring's dependency and configuration management. <em>And</em> it doesn't take much to make a factory. Take a look at this sample that works with my previous post's code.</p>
<div class="igBar"><span id="lcfm-5"><a href="#" onclick="javascript:showCodeTxt('cfm-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-5">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;">&lt;cfcomponent&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!----------------- Constructor ----------------------&gt;</span></span>&nbsp; &nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"init"</span> returntype=<span style="color: #009900;">"BeanFactory"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.config=<span style="color: #0000FF;">StructNew</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> this /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!------------------ Config --------------------------&gt;</span></span>&nbsp; &nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setConfig"</span> returntype=<span style="color: #009900;">"void"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"Settings"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"struct"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.config=arguments.settings /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"getConfig"</span> returntype=<span style="color: #009900;">"struct"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> variables.config /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!--------------- Create Methods ---------------------&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"createBean"</span> returntype=<span style="color: #009900;">"any"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> <span style="color: #000000; font-weight: bold;">var</span> com=<span style="color: #0000FF;">CreateObject</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"component"</span>,<span style="color: #009900;">"Bean"</span><span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> com.setConfig<span style="color: #0000FF;">&#40;</span>getConfig<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> com.init<span style="color: #0000FF;">&#40;</span>argumentCollection=arguments<span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;/cfcomponent&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The factory has a <code>setConfig()</code> method, just like the Bean does, and can be used to conveniently inject all configuration information with ColdSpring. Now, let's look at the <code>createBean()</code> method where the action is. It performs 3 key actions: <strong>(a)</strong> Create a fresh object. <strong>(b)</strong> Pass in configuration. <strong>(c)</strong> Call the <code>init()</code> method and return the object.</p>
<p>Our ColdSpring file can now be rewritten like this:</p>
<div class="igBar"><span id="lxml-6"><a href="#" onclick="javascript:showCodeTxt('xml-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-6">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"BeanService"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"com.BeanService"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"BeanFactory"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">"BeanFactory"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"BeanFactory"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"com.BeanFactory"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Config"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;map<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">"dsn"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>MyDSN<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/entry<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/map<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>See, we use the same XML for injecting configuration, it's just injected it into <code>BeanFactory</code>. We then inject <em>BeanFactory</em> into <code>BeanService</code>, instead of the Bean itself. It can use the factory to get a fresh bean whenever it needs one&mdash;properly instantiated and configured.</p>
<p>An example of <code>BeanService</code> tweaked to accept and use the factory:</p>
<div class="igBar"><span id="lcfm-7"><a href="#" onclick="javascript:showCodeTxt('cfm-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-7">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cfcomponent</span> <span style="color: #0000FF">hint</span>=<span style="color: #009900;">"Handles beans."</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"init"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"BeanFactory"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"BeanFactory"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.BeanFactory=Arguments.BeanFactory /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> this /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"workWithBean"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000099;"><span style="color: #990000;">&lt;cfscript&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> myBean=variables.BeanFactory.createBean<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; myBean.setX<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed X on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; myBean.setY<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed Y on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; return myBean;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #990000;">&lt;/cfscript&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #990000;">&lt;/cffunction&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #990000;">&lt;/cfcomponent&gt;</span> </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Let's make the factory better.</strong> First, your app probably has more than one type of transient object. You don't need a separate transient factory for each type of object. Just have a different <code>create<em>BeanName</em>()</code> method for each object type, with creation code specific for each object. And if your objects are all essentially created with the same conventions, you can handle create methods for all of them with a single onMissingMethod handler. Something like this:</p>
<div class="igBar"><span id="lcfm-8"><a href="#" onclick="javascript:showCodeTxt('cfm-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-8">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!--- Create Methods ---&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"onMissingMethod"</span> returntype=<span style="color: #009900;">"any"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"MissingMethodName"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"MissingMethodArguments"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000099;"><span style="color: #990000;">&lt;cfscript&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000FF;">method</span>=Arguments.MissingMethodName ;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> args=Arguments.MissingMethodArguments ;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> classList=<span style="color: #009900;">"MyBean,DiffBean,AnotherBean"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> className=<span style="color: #0000FF;">RemoveChars</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">method</span>,<span style="color: #FF0000;color:#800000;">1</span>,<span style="color: #FF0000;color:#800000;">6</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> com=<span style="color: #009900;">""</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #0000FF;">&#40;</span> <span style="color: #0000FF;">Left</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">method</span>,<span style="color: #FF0000;color:#800000;">6</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF">=</span>=<span style="color: #009900;">"create"</span> &amp;&amp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ListFindNoCase</span><span style="color: #0000FF;">&#40;</span>classList,className<span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">&#41;</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000FF;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; com=<span style="color: #0000FF;">CreateObject</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"component"</span>,className<span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; com.setConfig<span style="color: #0000FF;">&#40;</span>getConfig<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">StructKeyExists</span><span style="color: #0000FF;">&#40;</span>com,<span style="color: #009900;">"setFactory"</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; com.setFactory<span style="color: #0000FF;">&#40;</span>this<span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; return com.init<span style="color: #0000FF;">&#40;</span>argumentCollection=args<span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000FF;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #990000;">&lt;/cfscript&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #990000;">&lt;/cffunction&gt;</span> </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>See how the first <code>if</code> statement checks to make sure the method is a valid method name: One that starts with "create" and ends with an acceptable class name as we define in our <code>classList</code> variable. So this onMissingMethod handler will work with <code>createMyBean()</code>, <code>createDiffBean()</code>, and <code>createAnotherBean()</code>. Note how it also checks for the existence of a <code>setFactory()</code> method in the object it creates. If it is there, it will use the method to inject itself into the new object! This is a simple example of how autowiring is accomplished.</p>
<p><strong>Take it even <em>further!</em></strong> Head over to Paul Marcotte's blog post, <a href="http://www.fancybread.com/blog/post.cfm/A-Coldfusion-Transient-Factory-Example" target="_blank">A Coldfusion Transient Factory Example</a>, which shows how to make a very generic transient factory that receives configuration on which classes it should create through ColdSpring. Very cool stuff.</p>
<p><strong>Review.</strong> Transient factories don't require a lot of code, and are a vital step to simplifying and centralizing our instantiation and configuration of transient objects, or objects that have a state. Doing so doesn't negate the need for ColdSpring, in fact it complements it as it was intended, since ColdSpring works well with instantiation of singletons, and transient factories as described here are used as singletons. By utilizing ColdSpring and a transient factory, we can be well on our way to cleaner and more organized development.</p>
<p><strong>Get the files.</strong> Download sample code to play with: <a href='/wp-content/uploads/2009/05/coldspringbeantest.zip'>ColdSpring Bean Test</a>. It walks us through the issues discussed in the previous post as well as using transient factories like we just discussed.</p>
<p>This post has gotten pretty long, so I'll save the race condition demonstration for "Part 3". <img src='http://blog.crankybit.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/factories-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Importance of Understanding Factories, Part 1: ColdSpring Misunderstandings</title>
		<link>http://blog.crankybit.com/factories-1/</link>
		<comments>http://blog.crankybit.com/factories-1/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 13:33:26 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<category><![CDATA[ColdSpring]]></category>

		<category><![CDATA[factories]]></category>

		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=326</guid>
		<description><![CDATA[As ColdFusion developers progress in the use of object oriented programming concepts with CFCs, our applications become more flexible and able to support additional interfaces such as Ajax or Flex through web services. We also potentially benefit just from overall code maintainability, extensibility, and reusability by using best practices and design patterns common in OOP. [...]]]></description>
			<content:encoded><![CDATA[<p>As ColdFusion developers progress in the use of object oriented programming concepts with CFCs, our applications become more flexible and able to support additional interfaces such as Ajax or Flex through web services. We also potentially benefit just from overall code maintainability, extensibility, and reusability by using best practices and design patterns common in <abbr title="object-oriented programming">OOP</abbr>. </p>
<p>The benefits are many, but there are certainly pitfalls along the way. Perhaps we're ready to begin using service objects. And we'd like to create and pass in our transient objects with ColdSpring as a factory for all objects.</p>
<p>Let's cover the groundwork of our example, and then consider a pitfall. Here's our transient object:</p>
<p><strong>Bean.cfc</strong></p>
<div class="igBar"><span id="lcfm-13"><a href="#" onclick="javascript:showCodeTxt('cfm-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-13">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cfcomponent</span> <span style="color: #0000FF">hint</span>=<span style="color: #009900;">"Represents a bean."</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"init"</span> <span style="color: #0000FF">access</span>=<span style="color: #009900;">"public"</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returntype=<span style="color: #009900;">"Bean"</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"x"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"any"</span> <span style="color: #0000FF">required</span>=<span style="color: #009900;">"true"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"y"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"any"</span> <span style="color: #0000FF">required</span>=<span style="color: #009900;">"true"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"z"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"any"</span> <span style="color: #0000FF">required</span>=<span style="color: #009900;">"true"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> setX<span style="color: #0000FF;">&#40;</span>arguments.x<span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> setY<span style="color: #0000FF;">&#40;</span>arguments.y<span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> setZ<span style="color: #0000FF;">&#40;</span>arguments.z<span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> this /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!------------------- Config -------------------------&gt;</span></span>&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setConfig"</span> <span style="color: #0000FF">access</span>=<span style="color: #009900;">"public"</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output=<span style="color: #009900;">"false"</span> returntype=<span style="color: #009900;">"void"</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF">hint</span>=<span style="color: #009900;">"Method to pass in configs."</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"Settings"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"struct"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.instance.config=arguments.settings/<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!------------------ Setters -----------------------&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setX"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"newval"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.instance.<span style="color: #0000FF;">data</span>.x=arguments.newval /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setY"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"newval"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.instance.<span style="color: #0000FF;">data</span>.y=arguments.newval /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setZ"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"newval"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.instance.<span style="color: #0000FF;">data</span>.z=arguments.newval /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!------------------ Getters -----------------------&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"getMemento"</span> <span style="color: #0000FF">access</span>=<span style="color: #009900;">"public"</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returntype=<span style="color: #009900;">"struct"</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> variables.instance /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;/cfcomponent&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is an overly-simplified example of a bean. It just has 3 properties (X, Y, Z) and corresponding setter methods. It also has a typical <code>init()</code> method, a <code>setConfig()</code> method to pass in configuration, and a getter method so we can quickly dump the state of the object. It'll be enough for our samples.</p>
<p>Perhaps we might write a ColdSpring configuration like this:</p>
<div class="igBar"><span id="lxml-14"><a href="#" onclick="javascript:showCodeTxt('xml-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-14">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"BeanService"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"com.BeanService"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Bean"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">"Bean"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"Bean"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"com.Bean"</span> <span style="color: #000066;">singleton</span>=<span style="color: #ff0000;">"false"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Config"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;map<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">"dsn"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>MyDSN<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/entry<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/map<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"x"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>One<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"y"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>Two<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"z"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>Three<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This seems like it should work, eh? We're instantiating a service object, instantiating and configuring a bean, and passing the bean into the service object. Here's a really simple example of the bean service object:</p>
<p><strong>BeanService.cfc</strong></p>
<div class="igBar"><span id="lcfm-15"><a href="#" onclick="javascript:showCodeTxt('cfm-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-15">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cfcomponent</span> <span style="color: #0000FF">hint</span>=<span style="color: #009900;">"Handles beans."</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"init"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"Bean"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.Bean=Arguments.Bean /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> this /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"getBean"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> variables.Bean /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"workWithBean"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000099;"><span style="color: #990000;">&lt;cfscript&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> myBean=variables.Bean;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; myBean.setX<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed X on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; myBean.setY<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed Y on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; return myBean;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #990000;">&lt;/cfscript&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #990000;">&lt;/cffunction&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #990000;">&lt;/cfcomponent&gt;</span> </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This BeanService object has a constructor that receives the bean and stores it in its variables scope. It then has a the <code>getBean()</code> method so we can see the bean, and a <code>workWithBean()</code> method that simulates doing some work with the bean.</p>
<p><strong><em>And now, the pitfall: ColdSpring singletons and objects passed by reference.</em></strong></p>
<p>This code technically runs, and appears fine: The Bean instance that we pass into the service is <em>not</em> a singleton, so each BeanService we instantiate (<em>if</em> we were to instantiate multiple) would have a separate Bean instance. And <code>workWithBean()</code> appears to use a local var copy of Bean.</p>
<p>Wrong. </p>
<p>First, the <a target="_blank" href="http://www.coldspringframework.org/coldspring/examples/quickstart/index.cfm?page=singletons">Singleton vs. Transient Objects With ColdSpring</a> page explains that ColdSpring isn't ideal for generating transient components. Its overhead is more appropriate for less frequent creation of components that typically go in a shared scope, such as in the Application scope. Performance aside, though, the approach of injecting a transient bean into a singleton like BeanService does technically work.</p>
<p>So now the second point, how BeanService is working with Bean in our example <code>workWithBean()</code> method. The key misunderstanding is that CFCs like the Bean object are <em>passed by reference</em> when being assigned to variables. So when we do <code>var myBean=variables.Bean</code>, the local myBean variable is pointing to the same copy of Bean that <code>variables.Bean</code> points to. When it executes methods that change the bean's state, the one copy of Bean is being altered. Whoah, that wasn't an intended behavior. This could have very negative consequences, with Bean not being in the state you might expect it to be in.</p>
<p>So, how to fix it? Hmm. I don't want to use <code>CreateObject()</code> because that would become a dependency we'd have to manage, and we'd have to handle properly configuring the bean. This was the whole point of injecting the bean with ColdSpring in the first place.</p>
<p>Well, a quick solution could be to just use <code>Duplicate()</code> to make a copy of the injected bean before working on it! So, we could replace our <code>workWithBean()</code> method with this:</p>
<div class="igBar"><span id="lcfm-16"><a href="#" onclick="javascript:showCodeTxt('cfm-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-16">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"workWithBean"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000099;"><span style="color: #990000;">&lt;cfscript&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> myBean=<span style="color: #0000FF;">Duplicate</span><span style="color: #0000FF;">&#40;</span>variables.Bean<span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; myBean.setX<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed X on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; myBean.setY<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed Y on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return myBean;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #990000;">&lt;/cfscript&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #990000;">&lt;/cffunction&gt;</span> </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now, <code>variables.Bean</code> will always be the pristine, original bean injected by ColdSpring, and the local myBean variable will perform its work with a duplicate of that bean. Our code will now be working the way we intended; disaster has been averted.</p>
<p>Well, this <em>does</em> work. But it is neither graceful nor efficient, and it is <em>not</em> recommended. Consider Sean Corfield's blog post, <a href="http://corfield.org/blog/index.cfm/do/blog.entry/entry/duplicate_is_bad_for_your_objects_health" target="_blank">Duplicate() is Bad For Your (Object's) Health</a>. In an informal test, he clocked Duplicate() as being drastically slower&mdash;we're talking 50 to 1&mdash;than using <code>CreateObject()</code>. </p>
<p>Ugh. If we're back to using <code>CreateObject()</code>, what good is using ColdSpring for managing these component relationships, and what about dealing with configuration? <strong><em>Enter the world of factories.</em></strong> Yes, ColdSpring itself is a factory, but <em>it is a factory oriented around creating and configuring our singletons,</em> which can <em>include other factories</em> whose purpose is to properly create transient objects like Bean. </p>
<p>So instead of having ColdSpring inject Bean into BeanService, we can make a BeanFactory and inject <em>that</em> into BeanService, giving BeanService the ability to create Beans to work with any time it needs to. This may seem like a lot of effort, but we'll come to see that creating transient factories is easy and will clean up the mess of an architecture in this sample to something graceful and maintainable.</p>
<p>In this post, I described a few of the <em>mistakes</em> that can be made as we start to utilize ColdSpring. Hopefully these mistakes point to the importance of factories&mdash;not just ColdSpring, but our own transient factories as well. In the next blog post, we'll rework the sample to use a factory and review the benefits the factory provides and how it doesn't duplicate the purpose of ColdSpring. We'll review one more scenario that underscores the importance of factories: the dreaded race condition. And I'll provide source code that has executable demonstrations of these undesirable mistakes in action. <img src='http://blog.crankybit.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/factories-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>WPtouch</title>
		<link>http://blog.crankybit.com/wptouch/</link>
		<comments>http://blog.crankybit.com/wptouch/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 12:14:28 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[Site Configuration]]></category>

		<category><![CDATA[iPod/iPhone]]></category>

		<category><![CDATA[Apple]]></category>

		<category><![CDATA[iPhone]]></category>

		<category><![CDATA[JQuery]]></category>

		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=324</guid>
		<description><![CDATA[I have another blog that I use entirely for my own note-taking and reference, and I very frequently refer to my notes on my iPhone. Safari on the iPhone renders the pages wonderfully, but it is a bit of a pain to have to zoom in on the text of the page. This is when [...]]]></description>
			<content:encoded><![CDATA[<p>I have another blog that I use entirely for my own note-taking and reference, and I very frequently refer to my notes on my iPhone. Safari on the iPhone renders the pages wonderfully, but it <u>is</u> a bit of a pain to have to zoom in on the text of the page. This is when I realized an iPhone-optimized version of the blog would be so nifty.</p>
<p><a href="http://www.bravenewcode.com/wptouch/" target="_blank">WPtouch</a> is the <em>best</em> solution for <a href="http://wordpress.org/" target="_blank">WordPress</a> blogs. It's as simple as a plugin installation, and it is very decently polished.</p>
<p>It renders in "mobile" mode only for iPhone/iPod/Android clients. It has an option for those clients to still view the site in "normal" mode. It uses <a href="http://jquery.com/" target="_blank">JQuery</a> for various AJAX and visual effects. It just works and looks good while doing it, and I've recently installed it on this blog as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/wptouch/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Get a Struct or Array in That Simple Value Field</title>
		<link>http://blog.crankybit.com/struct-in-simple-value-field/</link>
		<comments>http://blog.crankybit.com/struct-in-simple-value-field/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 14:11:49 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=322</guid>
		<description><![CDATA[In some environments and scenarios, I used to use initialization (*.ini) files for storing simple configuration settings. Now I typically store this kind of information in a ColdSpring configuration file, or at least in an XML file of some sort. But at times I have to live (and work) with the apps I wrote that [...]]]></description>
			<content:encoded><![CDATA[<p>In some environments and scenarios, I used to use initialization (*.ini) files for storing simple configuration settings. Now I typically store this kind of information in a ColdSpring configuration file, or at least in an XML file of some sort. But at times I have to live (and work) with the apps I wrote that use *.ini configuration files.</p>
<p><strong>The nice thing about *.ini files.</strong> Not that they're all bad. It's ridiculously easy to extract settings from these files with ColdFusion. For instance:</p>
<div class="igBar"><span id="ltext-21"><a href="#" onclick="javascript:showCodeTxt('text-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">TEXT:</span>
<div id="text-21">
<div class="text">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">&#91;</span>Settings<span style="">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysetting=Hello</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">anothersetting=Goodbye </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To retrieve the <em>mysetting</em> configuration setting, I just make one call:</p>
<div class="igBar"><span id="lcfm-22"><a href="#" onclick="javascript:showCodeTxt('cfm-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-22">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">x=GetProfileString(pathToFile,&quot;Settings&quot;,&quot;mysetting&quot;); </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>It can't get much easier than that to retrieve a value from a configuration file!</p>
<p><strong>The bad thing about *.ini files.</strong> The only problem is that these files are restricted to name=value pairs of simple values. You may go into a project with no need for configuration settings that are any more complicated than simple numbers or strings, but what about 3 years from now when the business logic now could use some structs or arrays in the configuration?</p>
<p>Of course, you could change your whole configuration methodology when--and if--that time comes, but ugh. Using XML (especially in conjunction with IoC such as ColdSpring) is so much more scalable. The future you will thank you.</p>
<p>But it's too late. You and I already have complex app XYZ that is using *.ini config files, and you need to add feature X without rebuilding the configuration logic of the app. What to do?</p>
<p><strong>Embed a string representation of your struct or array in the config file!</strong> After all, ColdFusion 8 allows implicit struct and array creation, right? Well, half-right. Doing something like x=Evaluate(myStructString) just isn't allowed by ColdFusion. </p>
<p>However, we <em>can</em> effectively do the same thing with <strong>JSON</strong> representations of structs and arrays. So, try this:</p>
<div class="igBar"><span id="ltext-23"><a href="#" onclick="javascript:showCodeTxt('text-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">TEXT:</span>
<div id="text-23">
<div class="text">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">&#91;</span>Settings<span style="">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysetting=<span style="">&#123;</span>&quot;one&quot;:&quot;Hello&quot;,&quot;two&quot;:&quot;Goodbye&quot;<span style="">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">anothersetting=Yomomma </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now, retrieving <em>mysetting</em> with GetProfileString() will still get you a string, but using JSON functions built into ColdFusion 8 will get you a struct:</p>
<div class="igBar"><span id="lcfm-24"><a href="#" onclick="javascript:showCodeTxt('cfm-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-24">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">x=GetProfileString(pathToFile,&quot;Settings&quot;,&quot;mysetting&quot;);</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myStruct=DeserializeJSON(x); </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Your <em>myStruct</em> variable will be a struct with two keys ("one" and "two") just as you would expect from the JSON. </p>
<p>You could achieve similar results using XmlParse(), but in a case like this, the less verbose, the better. And JSON is less verbose than XML.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/struct-in-simple-value-field/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Reset Access Datasources Without Resetting ColdFusion</title>
		<link>http://blog.crankybit.com/reset-access-ds-not-cf/</link>
		<comments>http://blog.crankybit.com/reset-access-ds-not-cf/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 16:30:44 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=323</guid>
		<description><![CDATA[I am "stuck" with a few web applications that provide web front-ends to Access databases. I am not the owner of these databases, and for various reasons, the database owner will not upgrade it to SQL Server.  Alas, this sometimes causes headaches like the following.
The Access databases are on a network fileserver. Occasionally, this [...]]]></description>
			<content:encoded><![CDATA[<p>I am "stuck" with a few web applications that provide web front-ends to Access databases. I am not the owner of these databases, and for various reasons, the database owner will not upgrade it to SQL Server.  Alas, this sometimes causes headaches like the following.</p>
<p>The Access databases are on a network fileserver. Occasionally, this server goes down or resets, and ColdFusion's datasource connection to the database gets stuck in a disrupted state. I've come to learn two things you can try to get ColdFusion's connection to the Access datasource working again without resetting the entire ColdFusion Application Server service, which is obviously disruptive to your entire site and all of its applications.</p>
<ol>
<li><strong>Reset the ODBC services.</strong> The ColdFusion ODBC services (such as "ColdFusion 8 ODBC Agent" and "ColdFusion 8 ODBC Server") actually handle the ODBC connections to DataDirect driver databases such as Microsoft Access databases. This is handy. Just reset these services and often connectivity is restored.</li>
<li><strong>Use the "Disable Connections" option in the datasource configuration.</strong> If you are using JDBC drivers (for instance, perhaps to <a href="/msaccess2007-and-cf8/">connect to Microsoft Access 2007 databases</a>), resetting the ColdFusion 8 ODBC Server service won't help. Darn. However, if you go into the datasource's configuration in ColdFusion Administrator, you can check the "Disable Connections" checkbox and submit the change. This will force ColdFusion to disconnect from the database. Immediately go back in and uncheck the checkbox and submit again. Your datasource will now have connectivity reestablished without a ColdFusion restart!</li>
</ol>
<p>Happy days! The second option won't be helpful to you if you have many, many datasources. However, this is better than a ColdFusion reset that would disrupt all of your apps.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/reset-access-ds-not-cf/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CFExecute Output Not Going in Your Variable?</title>
		<link>http://blog.crankybit.com/cfexecute-output-in-var/</link>
		<comments>http://blog.crankybit.com/cfexecute-output-in-var/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 17:29:34 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=321</guid>
		<description><![CDATA[I'm blogging this because it's a little tidbit I always waste about 30 minutes figuring out before I remember it.
The &#60;cfexecute&#62; tag can return the output of the execution into a variable if you supply one with the variable="" attribute. However, I will try to do this, and (a) the script returns too quickly, and [...]]]></description>
			<content:encoded><![CDATA[<p>I'm blogging this because it's a little tidbit I always waste about 30 minutes figuring out before I remember it.</p>
<p>The &lt;cfexecute&gt; tag can return the output of the execution into a variable if you supply one with the variable="" attribute. However, I will try to do this, and <strong>(a)</strong> the script returns too quickly, and <strong>(b)</strong> there is no output when there should be.</p>
<p>There is an easy solution! &lt;cfexecute&gt; is expecting the timeout="" attribute. Supply it a timeout value in seconds, and it will then wait for the script to finish executing and thus have output to store in your variable.</p>
<p>You may be wondering why this is necessary, because even without the timeout="" attribute, the execution still occurs. &lt;cfexecute&gt; just doesn't return any output. That's because &lt;cfexecute&gt; is indeed running, it is just immediately timing out--effectively giving up on the executed process--and thus not returning the output. By supplying a timeout, ColdFusion hangs around waiting for the process to complete before returning its output.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/cfexecute-output-in-var/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Generating a Decrypted Memento for a Transfer Decorator</title>
		<link>http://blog.crankybit.com/generating-a-decrypted-memento-for-a-transfer-decorator/</link>
		<comments>http://blog.crankybit.com/generating-a-decrypted-memento-for-a-transfer-decorator/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 12:57:27 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<category><![CDATA[How To]]></category>

		<category><![CDATA[howto]]></category>

		<category><![CDATA[Transfer]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=319</guid>
		<description><![CDATA[In my last blog post, I discussed how to efficiently handle multiple encrypted fields while using Transfer. The key was using onMissingMethod in the Transfer decorator. However, what about when we use the memento with Transfer's getMemento() method? This approach can be especially handy to quickly transfer an object's properties when using web services to [...]]]></description>
			<content:encoded><![CDATA[<p>In my last blog post, I discussed how to efficiently handle multiple encrypted fields while using Transfer. The key was using onMissingMethod in the Transfer decorator. However, what about when we use the memento with Transfer's getMemento() method? This approach can be especially handy to quickly transfer an object's properties when using web services to Flex or AJAX front ends.</p>
<p>For reference, here was the object definition we were using in our sample Transfer configuration.</p>
<div class="igBar"><span id="lxml-27"><a href="#" onclick="javascript:showCodeTxt('xml-27'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-27">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;object</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Reg"</span> <span style="color: #000066;">table</span>=<span style="color: #ff0000;">"Regs"</span> <span style="color: #000066;">decorator</span>=<span style="color: #ff0000;">"MyProj.com.Reg"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;id</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"ID"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegID"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"numeric"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"FName"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegFName"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"LName"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegLName"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"LastMod"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"date"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegLastMod"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Expires"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"date"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegExpires"</span> <span style="color: #000066;">nullable</span>=<span style="color: #ff0000;">"true"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"EncHome"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"regHome"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"EncWork"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"regWork"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"EncMobile"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"regMobile"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"EncSMS"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"regSMS"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/object<span style="font-weight: bold; color: black;">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The process is very simple. Recall that we prefixed our Transfer properties with "Enc" to know which fields are encrypted. Let's create a getDecryptedMemento() method. It will loop through all of the keys in the memento, which is just a struct, and decrypt any of the encrypted fields, saving them as new keys in the struct (i.e. "EncHome" is decrypted as "Home" in the struct). </p>
<p>Something like this will do the trick:</p>
<div class="igBar"><span id="lcfm-28"><a href="#" onclick="javascript:showCodeTxt('cfm-28'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-28">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"getDecryptedMemento"</span> <span style="color: #0000FF">access</span>=<span style="color: #009900;">"public"</span> returntype=<span style="color: #009900;">"struct"</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> <span style="color: #000000; font-weight: bold;">var</span> memento=getMemento<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> <span style="color: #000000; font-weight: bold;">var</span> key=<span style="color: #009900;">""</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> <span style="color: #000000; font-weight: bold;">var</span> newkey=<span style="color: #009900;">""</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfloop</span> <span style="color: #0000FF">index</span>=<span style="color: #009900;">"key"</span> <span style="color: #0000FF">list</span>=<span style="color: #009900;">"#StructKeyList(memento)#"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfif</span> <span style="color: #0000FF;">Left</span><span style="color: #0000FF;">&#40;</span>key,<span style="color: #FF0000;color:#800000;">3</span><span style="color: #0000FF;">&#41;</span> is <span style="color: #009900;">"Enc"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> newkey=<span style="color: #0000FF;">RemoveChars</span><span style="color: #0000FF;">&#40;</span>key,<span style="color: #FF0000;color:#800000;">1</span>,<span style="color: #FF0000;color:#800000;">3</span><span style="color: #0000FF;">&#41;</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> memento<span style="color: #0000FF;">&#91;</span>newkey<span style="color: #0000FF;">&#93;</span>=decryptValue<span style="color: #0000FF;">&#40;</span>memento<span style="color: #0000FF;">&#91;</span>key<span style="color: #0000FF;">&#93;</span><span style="color: #0000FF;">&#41;</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cfif&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cfloop&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> memento&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Not much to it. Note that we would use the same decryption function as we are using in onMissingMethod for the dynamic accessors. I'm calling it decryptValue() for this sample.</p>
<p>In the end, our getDecryptedMemento() method returns a memento that looks just like the Transfer-generated getMemento() output, with decrypted versions of the encrypted fields!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/generating-a-decrypted-memento-for-a-transfer-decorator/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Handling Multiple Encrypted Fields in a Transfer Decorator</title>
		<link>http://blog.crankybit.com/multiple-encrypted-fields-in-a-transfer-decorator/</link>
		<comments>http://blog.crankybit.com/multiple-encrypted-fields-in-a-transfer-decorator/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 12:55:55 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<category><![CDATA[How To]]></category>

		<category><![CDATA[howto]]></category>

		<category><![CDATA[Transfer]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=318</guid>
		<description><![CDATA[I had a recent project that had multiple fields in a particular table that needed to be encrypted for privacy and security considerations. I was using Transfer for the project. The typical way to handle the encryption is to create custom accessor methods in a decorator component. The Transfer wiki has a decent explanation of [...]]]></description>
			<content:encoded><![CDATA[<p>I had a recent project that had multiple fields in a particular table that needed to be encrypted for privacy and security considerations. I was using <a href="http://docs.transfer-orm.com/" target="_blank">Transfer</a> for the project. The typical way to handle the encryption is to create custom accessor methods in a <a href="http://en.wikipedia.org/wiki/Decorator_pattern" target="_blank">decorator</a> component. The Transfer wiki has a decent explanation of this concept on a page entitled <a href="http://docs.transfer-orm.com/wiki/How_to_Encrypt_User_Passwords_Using_a_Decorator.cfm" target="_blank">How to Encrypt User Passwords Using a Decorator</a>. However, it is intentionally simple, demonstrating a single property. Multiply your getter/setter methods by how many properties you will be working with, and the lines of code can add up. This framework stuff is supposed to prevent that kind of repetition! Well, utilizing onMissingMethod() and the core concept on the Transfer decorator example, we can handle this scenario efficiently.</p>
<p>First, let's consider the object definition in our sample Transfer configuration file.</p>
<div class="igBar"><span id="lxml-31"><a href="#" onclick="javascript:showCodeTxt('xml-31'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-31">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;object</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Reg"</span> <span style="color: #000066;">table</span>=<span style="color: #ff0000;">"Regs"</span> <span style="color: #000066;">decorator</span>=<span style="color: #ff0000;">"MyProj.com.Reg"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;id</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"ID"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegID"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"numeric"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"FName"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegFName"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"LName"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegLName"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"LastMod"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"date"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegLastMod"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Expires"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"date"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"RegExpires"</span> <span style="color: #000066;">nullable</span>=<span style="color: #ff0000;">"true"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"EncHome"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"regHome"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"EncWork"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"regWork"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"EncMobile"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"regMobile"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"EncSMS"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"string"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"regSMS"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/object<span style="font-weight: bold; color: black;">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Note that I have prefixed the Transfer properties with "Enc". This isn't required, but will simplify our processing later on. So in this sample, we have 4 properties: Home, Work, Mobile, and SMS. The concept is that these are client phone numbers that we want to protect with encryption. So, Transfer will generate getEncHome(), getEncWork(), getEncMobile(), getEncSMS(), and corresponding setters. However, we want to provide getHome(), getWork(), getMobile(), getSMS(), and corresponding setters, that get and set the properties <em>after</em> encryption/decryption.</p>
<p><strong>Use onMissingMethod()</strong></p>
<p>To accomplish this, we might have an onMissingMethod() function in the decorator like this:</p>
<div class="igBar"><span id="lcfm-32"><a href="#" onclick="javascript:showCodeTxt('cfm-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-32">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"onMissingMethod"</span> <span style="color: #0000FF">access</span>=<span style="color: #009900;">"public"</span> returntype=<span style="color: #009900;">"any"</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"missingMethodName"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"string"</span> <span style="color: #0000FF">required</span>=<span style="color: #009900;">"true"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"missingMethodArguments"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"struct"</span> <span style="color: #0000FF">required</span>=<span style="color: #009900;">"true"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000099;"><span style="color: #990000;">&lt;cfscript&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> func=<span style="color: #009900;">""</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> encList=<span style="color: #009900;">"Home,Mobile,SMS,Work"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000FF;">method</span>=Arguments.missingMethodName;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> args=Arguments.missingMethodArguments;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> firstArg=args<span style="color: #0000FF;">&#91;</span><span style="color: #0000FF;">ListFirst</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">StructKeyList</span><span style="color: #0000FF;">&#40;</span>args<span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> prop=<span style="color: #0000FF;">RemoveChars</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">method</span>,<span style="color: #FF0000;color:#800000;">1</span>,<span style="color: #FF0000;color:#800000;">3</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Handle encryption/decryption of encrypted values:</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #0000FF;">&#40;</span> <span style="color: #0000FF;">ListFindNoCase</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"get,set"</span>,<span style="color: #0000FF;">Left</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">method</span>,<span style="color: #FF0000;color:#800000;">3</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span> and </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ListFindNoCase</span><span style="color: #0000FF;">&#40;</span>encList,prop<span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000FF;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; func=variables<span style="color: #0000FF;">&#91;</span><span style="color: #0000FF;">Insert</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"Enc"</span>,<span style="color: #0000FF;">method</span>,<span style="color: #FF0000;color:#800000;">3</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #0000FF;">&#40;</span> <span style="color: #0000FF;">Left</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">method</span>,<span style="color: #FF0000;color:#800000;">3</span><span style="color: #0000FF;">&#41;</span> is <span style="color: #009900;">"get"</span><span style="color: #0000FF;">&#41;</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; return decryptValue<span style="color: #0000FF;">&#40;</span>func<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; func<span style="color: #0000FF;">&#40;</span>encryptValue<span style="color: #0000FF;">&#40;</span>firstArg<span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000FF;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #990000;">&lt;/cfscript&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #990000;">&lt;/cffunction&gt;</span> </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>As a good security measure, we use an "encList" string to check that only proper methods can be called. Then we examine the method name. If it starts with "get" or "set" and ends with one of the list items, we proceed. Thus, we'll process a method like getHome(), but not a method like getFoo(). We then call getEncHome() and decrypt it for getters, encrypt and call setEncHome() for setters. </p>
<p><strong>Dynamic Method Calling</strong></p>
<p>But alas, how do we do this dynamically without a long if-then-else chain? We call the proper method by referencing it from the component's variables scope. Terrence Ryan describes this in his blog post <a href="http://www.terrenceryan.com/blog/post.cfm/cheap-and-easy-dynamic-method-calling-in-cfscript" target="_blank">Cheap and Easy Dynamic Method Calling in CFScript</a>. We construct the method name, in our case, by inserting "Enc" after the "get" or "set" prefix of the method name passed into onMissingMethod (thus "getHome" becomes the Transfer method "getEncHome"). Assign the function to the local "func" variable, then call it as if "func" was the function. Finally, handle any encryption or decryption that you will be doing. For this sample, I have encryptValue() and decryptValue() methods handling the details of that work.</p>
<p><strong>Summary</strong></p>
<p>We've used two solutions to handle multiple encrypted fields in the decorator. First, we used onMissingMethod() to handle processing of all of the properties in a single method. Then, we used dynamic method calling to call the Transfer-generated accessors within onMissingMethod().</p>
<p>The end result is being able to use setHome(), getHome(), etc. with the encryption and decryption being handled automatically. Great!</p>
<p>In addition to accessors, we may sometimes want to use the object's memento with the Transfer method getMemento(). In my next blog post, I'll demonstrate how that process can also be done very efficiently.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/multiple-encrypted-fields-in-a-transfer-decorator/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Loose Cables Cause Sporadic Behavior in Computers</title>
		<link>http://blog.crankybit.com/loose-cables-cause-sporadic-behavior-in-computers/</link>
		<comments>http://blog.crankybit.com/loose-cables-cause-sporadic-behavior-in-computers/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 13:22:25 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[Macintosh]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=315</guid>
		<description><![CDATA[Loose cables and electronics are bad. In electronics as sophisticated as computers, they can cause really odd behavior!
My wife's Mac Mini was exhibiting odd behavior, namely, sporadic loss of sound. The internal speaker would suddenly become unavailable to the operating system. Furthermore, the speakers did have a slight hum when they were plugged into the [...]]]></description>
			<content:encoded><![CDATA[<p>Loose cables and electronics are bad. In electronics as sophisticated as computers, they can cause really odd behavior!</p>
<p>My wife's Mac Mini was exhibiting odd behavior, namely, sporadic loss of sound. The internal speaker would suddenly become unavailable to the operating system. Furthermore, the speakers did have a slight hum when they were plugged into the Mac Mini. I had opened the Mac Mini up a couple times to upgrade the RAM, so I figured I must have inadvertently pulled the audio line. A few months later, the Mac Mini just outright refused to start up. I'd push the power button, I could hear it power on, but there was no startup chime and no boot-up. I figured the Mac had finally died, but I naturally wasn't going to give up on it without an autopsy.</p>
<p>If you've ever looked at a <a href="http://www.methodshop.com/gadgets/tutorials/macmini-ram/index.shtml" target="_blank">tutorial on opening the Mac Mini</a>, you know it isn't the worst thing ever, but it definitely is more complicated than opening, say, a Mac Pro, or really any tower computer. One of the challenges is separating the top chassis, since it is connected with a few cables. One of these is a ribbon in the back, and this ribbon is what was giving me trouble. Take a look at the photo below.</p>
<p style="text-align: center;"><a title="This connector was loose, thus the ribbon was not securely fastened." rel="lightbox" href="http://blog.crankybit.com/wp-content/uploads/2008/11/cimg3666.jpg"><img class="alignnone size-medium wp-image-316 glossy" title="The Loose Connector" src="http://blog.crankybit.com/wp-content/uploads/2008/11/cimg3666-300x225.jpg" alt="This connector was loose, thus the ribbon was not securely fastened." width="300" height="225" /></a></p>
<p style="text-align: left;">As you can see, the connector was actually in the open position, so the ribbon was sitting loosely in the connector, sporadically losing its connection and wreaking havoc on the system. The ribbon must have eventually pulled away enough so that the computer couldn't start up at all.</p>
<p style="text-align: left;">Here's the ribbon properly inserted and the connector closed shut:</p>
<p style="text-align: center;"><a title="The ribbon properly inserted and the connector closed shut." rel="lightbox" href="http://blog.crankybit.com/wp-content/uploads/2008/11/cimg3668.jpg"><img class="alignnone size-medium wp-image-317 glossy" title="Properly inserted and closed shut." src="http://blog.crankybit.com/wp-content/uploads/2008/11/cimg3668-300x225.jpg" alt="The ribbon properly inserted and the connector closed shut." width="300" height="225" /></a></p>
<p style="text-align: left;">The Mac started fine after that, and it hasn't had any startup or sound problems since.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/loose-cables-cause-sporadic-behavior-in-computers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Separating a Large Repository</title>
		<link>http://blog.crankybit.com/separating-large-repo/</link>
		<comments>http://blog.crankybit.com/separating-large-repo/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 12:52:45 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[Batch Scripting]]></category>

		<category><![CDATA[How To]]></category>

		<category><![CDATA[Subversion]]></category>

		<category><![CDATA[howto]]></category>

		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=312</guid>
		<description><![CDATA[A few months ago, I posted an article about combining multiple Subversion repositories into one large repository. Some folks have expressed an interest in doing the opposite--separating one large repository into multiple smaller repositories. The process is not without its quirks, but it can be done.
At first glance, you'd conclude the process would work much [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago, I posted an article about <a href="http://blog.crankybit.com/combining-repos/">combining multiple Subversion repositories into one large repository</a>. Some folks have expressed an interest in doing the opposite--separating one large repository into multiple smaller repositories. The process is not without its quirks, but it can be done.</p>
<p>At first glance, you'd conclude the process would work much the same way: Loop through the individual directories in the large repository, create smaller repositories for each one, then dump and import the contents of each directory into its small repository.</p>
<p>The tricky part is that the Subversion dump command dumps everything in the repository, by revision. In order to pull just a single directory, you must filter a complete dump with the "svndumpfilter" command. <a href="http://allmybrain.com/2007/10/15/using-piped-svndumpfilter-commands-to-separate-an-svn-repository/" target="_blank">This blog post</a> by <a href="http://allmybrain.com/" target="_blank">AllMyBrain.com</a> basically explains how to accomplish this in Linux. I usually have to work on a Windows box on the job, so I wrote up a script to accomplish this in a Windows batch script.</p>
<p>The strategy is the same as the Linux script, though. We're going to use "svnadmin dump" the large repository, then use "svndumpfilter" to filter by just the directory we want, then "svnadmin load" the results into the newly created repository. All of this can be combined into a single statement via piping:</p>
<div class="igBar"><span id="ldos-35"><a href="#" onclick="javascript:showCodeTxt('dos-35'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">DOS:</span>
<div id="dos-35">
<div class="dos">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svnadmin dump c:\my\large\repo\ |</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svndumpfilter include MyDirectory |</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svnadmin load MySmallRepo\MyDirectory </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This will make a little more sense when we look at the full script. Let's just put it out there and then go through it.</p>
<div class="igBar"><span id="ldos-36"><a href="#" onclick="javascript:showCodeTxt('dos-36'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">DOS:</span>
<div id="dos-36">
<div class="dos">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">SET</span></a> <span style="color: #448844;">SmallRepoPath</span>=c:\SmallRepos</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">SET</span></a> <span style="color: #448844;">PathToRepo</span>=c:\BigRepo</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">SET</span></a> <span style="color: #448844;">UNCToRepo</span>=file:///c:/BigRepo</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">SET</span></a> <span style="color: #448844;">PathToChkout</span>=c:\BigRepoChkout</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mkdir %<span style="color: #448888;">PathToChkout</span>%</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svn co %<span style="color: #448888;">uncToRepo</span>% %<span style="color: #448888;">PathToChkout</span>% --ignore-externals</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/dir.html"><span style="color: #b1b100; font-weight: bold;">dir</span></a> /A:D /B %<span style="color: #448888;">PathToChkout</span>%&gt; %<span style="color: #448888;">PathToChkout</span>%\dirs.tmp</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/for.html"><span style="color: #00b100; font-weight: bold;">for</span></a> /F %%i <a href="http://www.ss64.com/nt/in.html"><span style="color: #00b100; font-weight: bold;">in</span></a> <span style="color: #66cc66;">&#40;</span>%<span style="color: #448888;">PathToChkout</span>%\dirs.tmp<span style="color: #66cc66;">&#41;</span> <a href="http://www.ss64.com/nt/do.html"><span style="color: #00b100; font-weight: bold;">do</span></a> <span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.ss64.com/nt/if.html"><span style="color: #00b100; font-weight: bold;">if</span></a> <a href="http://www.ss64.com/nt/not.html"><span style="color: #000000; font-weight: bold;">not</span></a> %%i==.svn <span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.ss64.com/nt/echo.html"><span style="color: #b1b100; font-weight: bold;">echo</span></a> Processing &quot;%%i&quot;...</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; mkdir %<span style="color: #448888;">SmallRepoPath</span>%\%%i</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; svnadmin create %<span style="color: #448888;">SmallRepoPath</span>%\%%i</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; svnadmin dump %<span style="color: #448888;">PathToRepo</span>% | svndumpfilter include %%i | svnadmin load %<span style="color: #448888;">SmallRepoPath</span>%\%%i</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">del %<span style="color: #448888;">PathToChkout</span>%\dirs.tmp</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">rmdir /S /Q %<span style="color: #448888;">PathToChkout</span>% </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>First, we're setting our paths. "SmallRepoPath" will be the directory holding all of the small repositories we'll be creating. "PathToRepo" and "UNCToRepo" point to the big repository as DOS and UNC paths, respectively. "PathToChkout" points to a Subversion checkout of the large repository.</p>
<p>First, we check out the large repository with the "svn co" command. We do this just so that we can call the "dir /A:D /B" command, which says, "List just the directories in the checkout directory." We use that output to loop through each directory in the large repository.</p>
<p>Then, for each directory in the large repository, we create a corresponding small repository, then do our dump/filter/load combo. Again, we're dumping the contents of the large repository, using "svndumpfilter" to filter by directory, then loading that filtered dump into the new small repository.</p>
<p>Finally, we just do some cleanup by removing our temp files and the checkout directory.</p>
<p><strong>There <u>are</u> a few caveats with this code.</strong> </p>
<p>First, it will import <em>all</em> of the large repository's revisions into the smaller repository. There are svndumpfilter arguments to prevent this, such as --drop-empty-revs and --renumber-revs, but I found the Windows Subversion binaries to be problematic with these arguments. The end result is that you have more revision numbers than needed, but only the relevant data is actually imported into the repository, and viewing logs on just the imported directory will still obviously show revision logs related to that directory, so there's really little harm done.</p>
<p>Second, the dump/filter/load action doesn't always work on a directory that has been moved (copied/deleted) from another location within the large repository. What's worse, it won't <em>fail,</em> it just won't load any data into the small repository. To address this, use the --revision argument on the "svnadmin dump" command to do a dump starting at a revision after the move took place. Doing so will give the "svndumpfilter" command something it can work with.</p>
<p>This process is certainly more complicated to <em>explain</em>, but ultimately there's not that much more going on. Hopefully this explanation is helpful to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/separating-large-repo/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Best Reason to Use CFQueryParam: Avoid Odd SQL Parsing by ColdFusion</title>
		<link>http://blog.crankybit.com/best-reason-to-use-cfqueryparam/</link>
		<comments>http://blog.crankybit.com/best-reason-to-use-cfqueryparam/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 19:02:29 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=314</guid>
		<description><![CDATA[After banging my head against the wall for a couple hours over an odd application error, I've finally come up with the best, most definitive reason to use &#60;cfqueryparam&#62; absolutely 100% of the time: It helps you avoid odd--dare I say erroneous--SQL parsing performed by ColdFusion in the &#60;cfquery&#62; tag. 
Huh? Say again?
That's right. In my [...]]]></description>
			<content:encoded><![CDATA[<p>After banging my head against the wall for a couple hours over an odd application error, I've finally come up with the best, most definitive reason to use &lt;cfqueryparam&gt; absolutely 100% of the time: It helps you avoid odd--dare I say erroneous--SQL parsing performed by ColdFusion in the &lt;cfquery&gt; tag. </p>
<p>Huh? Say again?</p>
<p>That's right. In <em>my </em>experience, anyway, the &lt;cfquery&gt; tag will erroneously parse the SQL and tag legitimate content as disallowed SQL. Let's take a look at an example.</p>
<p>Here's a basic query that inserts some HTML content into a field:</p>
<div class="igBar"><span id="lcfm-39"><a href="#" onclick="javascript:showCodeTxt('cfm-39'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-39">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cfquery</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"insertStuff"</span> <span style="color: #0000FF">datasource</span>=<span style="color: #009900;">"myDS"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;INSERT INTO myTable (fldTitle,fldContent) </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;VALUES ('Title','&amp;middot; Grant Deadline Extended')</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;/cfquery&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Never mind <em>why</em> you might do this; the point is that it is allowable SQL. There's just an HTML special entity in the string. However, ColdFusion 8 sees the semicolon followed by the word "GRANT", and if the GRANT statement is disallowed for the ColdFusion data source, ColdFusion will throw an error similar to: "Error Executing Database Query. Executing the SQL statement is not allowed."</p>
<p>Of course, there is no error in the SQL, and the same statement executes just fine when you run it at the command line or SQL Query Analyzer. ColdFusion is doing a security check by parsing the SQL and attempting to verify that the SQL statement(s) only use allowed SQL as defined for the data source in ColdFusion Administrator. </p>
<p>If a SQL keyword is not immediately following the semicolon, ColdFusion won't make this mistake. And naturally, if your data source is configured to allow the offending keyword (in my example, "GRANT"), it will execute the SQL and it will execute properly, not actually executing the grant statement. </p>
<p>But if my code had used &lt;cfqueryparam&gt;, this wouldn't have been an issue:</p>
<div class="igBar"><span id="lcfm-40"><a href="#" onclick="javascript:showCodeTxt('cfm-40'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-40">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cfquery</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"insertStuff"</span> <span style="color: #0000FF">datasource</span>=<span style="color: #009900;">"myDS"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;INSERT INTO myTable (fldTitle,fldContent) </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;VALUES (</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #333333;"><span style="color: #990000;">&lt;cfqueryparam</span> <span style="color: #0000FF;">value</span>=<span style="color: #009900;">"Title"</span> cfsqltype=<span style="color: #009900;">"cf_sql_varchar"</span><span style="color: #990000;">&gt;</span></span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #333333;"><span style="color: #990000;">&lt;cfqueryparam</span> <span style="color: #0000FF;">value</span>=<span style="color: #009900;">"&amp;middot; Grant Deadline Extended"</span> cfsqltype=<span style="color: #009900;">"cf_sql_varchar"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;)</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;/cfquery&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This code will happily pass the SQL on to the database server. </p>
<p>This scenario may be a bit apparent when presented cleanly as I have here, but when the string being passed in is 3,000 characters or something similar, you're banging your head on the wall wondering what the heck is wrong with your query, especially when it has been working for years and now fails on just a single particular set of data. To my credit, the offending code is about 6 years old and all of my queries do indeed use &lt;cfqueryparam&gt; now. And more than ever, I'm now convinced to continue that habit. <img src='http://blog.crankybit.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>This oddity probably comes up rarely, but I'm sure someone else has or will experience this. I hope this post will help you.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/best-reason-to-use-cfqueryparam/feed/</wfw:commentRss>
		</item>
		<item>
		<title>DST in cfexchangecalendar</title>
		<link>http://blog.crankybit.com/dst-in-cfexchangecalendar/</link>
		<comments>http://blog.crankybit.com/dst-in-cfexchangecalendar/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 14:54:44 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<category><![CDATA[Adobe]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=313</guid>
		<description><![CDATA[I have an app that uses &#60;cfexchangecalendar&#62; to look at appointments in an Exchange calendar and display them on a form on a web page. Everything was going smoothly until the department called and notified me that new appointments they were entering were an hour off on the web app. The calendar in Outlook would [...]]]></description>
			<content:encoded><![CDATA[<p>I have an app that uses &lt;cfexchangecalendar&gt; to look at appointments in an Exchange calendar and display them on a form on a web page. Everything was going smoothly until the department called and notified me that new appointments they were entering were an hour off on the web app. The calendar in Outlook would say 11am, for instance, but my app would say 12pm. </p>
<p>After a little investigation, I came to realize that only new appointments with a date <em>after </em>daylight savings time ends were an hour off. Okay, that makes sense, but the query results returned by &lt;cfexchangecalendar&gt; don't provide any time zone information for me to properly handle the hour difference caused by DST!</p>
<p>As it turns out, Adobe has tracked this as Issue #70474, and the hot fix can be downloaded in <a href="http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403070" target="_blank">ColdFusion 8 Cumulative Hot Fix 3</a>. After applying the cumulative hot fix, &lt;cfexchangecalendar&gt; now returns the appointments exactly as they appear in Outlook.</p>
<p>It seems to me that it would be nice if the query results returns the GMT offset. But at least Adobe has addressed the inconsistency between the data ColdFusion retrieves and what Outlook displays.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/dst-in-cfexchangecalendar/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Avoiding the Password Prompt for SSH</title>
		<link>http://blog.crankybit.com/avoiding-the-password-prompt-for-ssh/</link>
		<comments>http://blog.crankybit.com/avoiding-the-password-prompt-for-ssh/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 16:22:11 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[How To]]></category>

		<category><![CDATA[Mac OS X]]></category>

		<category><![CDATA[Open Source]]></category>

		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=311</guid>
		<description><![CDATA[It's handy to establish an SSH key between machines so that SSH-related commands don't prompt you for a password. This is handy to quickly SSH into another machine, and it's even more handy when setting up SSH commands in automated scripts. For instance, you may want to execute some rsync statements in a script that [...]]]></description>
			<content:encoded><![CDATA[<p>It's handy to establish an SSH key between machines so that SSH-related commands don't prompt you for a password. This is handy to quickly SSH into another machine, and it's even more handy when setting up SSH commands in automated scripts. For instance, you may want to execute some rsync statements in a script that runs on a regular basis. It's better to have an established SSH key between the two machines than to have a password embedded in the script.</p>
<p>I recently reinstalled the OS on one of my Macs, and I've got backup scripts on my CentOS Linux box that use rsync to back up some pertinent data, so I had to reestablish the SSH key between the machines and had a hard time remembering how to do it. So this time I'm documenting what I had to re-learn.</p>
<p>First of all, there's a <a href="http://nixcraft.com/1196-post2.html" target="_blank">great post over at nixCraft</a> that basically explains how to do it. But allow me to explain more thoroughly, ahem, dumbed down to my level. </p>
<p>The key is remembering which machine is filling which role when you're reading the instructions. I'll call them the "Acting" machine--the one who is taking action and performing a command, let's say an rsync command--and the "Target" machine--the one who is being acted upon. In my case, the Linux server is the acting machine performing the rsync command, and my Mac is the target.</p>
<p>The process is simple. On the "Target" machine, generate a key, and then give that key to the "Acting" machine, which effectively gives it "permission" to login without the need to supply username/password credentials.</p>
<p>So, from the "Target" machine, in this case, my Mac, type the following command:</p>
<p>ssh-keygen -t rsa</p>
<p>This will generate a couple files that serve as a key for accessing the Mac. The ssh-keygen command may ask you where to store the key and what password to use. Just hit enter to use the default path and a blank password.</p>
<p>Next, still from the "Target" machine (my Mac), type:</p>
<p>ssh <em>MyUsername@ActingServer</em> "mkdir .ssh"<br />
scp .ssh/id_rsa.pub <em>MyUsername@ActingServer</em>:.ssh/authorized_keys2</p>
<p>In the code above, <em>MyUsername@ActingServer</em> would be the username and address (for instance, perhaps the IP address) of the "Acting" machine, in my case, the Linux server. In the first line, you're just creating the .ssh directory if it doesn't exist. In the second line, you're copying the key you generated from the "Target" machine to the "Acting" machine, or from the Mac to the Linux server.  Note that the scp command will ask for the password to the <em>MyUsername</em> account because it is connecting to that server to send it the key.</p>
<p>Voile. As if by magic, the "Acting" machine should now be able to SSH into the "Target" machine without a password prompt. Correspondingly, you should be able to perform rsync and other SSH commands without a password prompt. Please note, however, that <em>this is only a one-way key.</em><em> </em>We only gave my Linux server permission to access my Mac.</p>
<p><strong>What if I want my Mac to similarly login to the server without a password prompt?</strong> In that case, the Mac and the server have effectively switched roles; the Mac is now the "Acting" machine and the server is the "Target" machine, so we just have to repeat the process from the other direction. Generate a key from the server and send it to the Mac. At that point, both machines will be able to access each other without a password prompt. </p>
<p><strong>What if I have multiple "Targets" that the "Acting" machine will connect to?</strong> For instance, perhaps I have multiple Macs, and the Linux server is running scripts on all of them. When you're sending the key to the "Acting" server with the scp command, use a different name for each key file, don't overwrite the same file each time! So in the example code above, we're sending the key as "authorized_keys2". When repeating this process for multiple targets, send the keys as "authorized_keys3", and so forth.</p>
<p>Hopefully this will clear up some confusion regarding this process.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/avoiding-the-password-prompt-for-ssh/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Shrink the Unshrinkable SQL Transaction Log</title>
		<link>http://blog.crankybit.com/shrink-the-unshrinkable-sql-transaction-log/</link>
		<comments>http://blog.crankybit.com/shrink-the-unshrinkable-sql-transaction-log/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 19:20:00 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[How To]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=309</guid>
		<description><![CDATA[Various reasons may cause SQL Server to get in a rut and not empty the transaction log of a database. In my case, our database backups were failing without our knowledge for several weeks, so the backups were never successful, and the transaction logs of a few databases grew so large that the backup process [...]]]></description>
			<content:encoded><![CDATA[<p>Various reasons may cause SQL Server to get in a rut and not empty the transaction log of a database. In my case, our database backups were failing without our knowledge for several weeks, so the backups were never successful, and the transaction logs of a few databases grew so large that the backup process would still not clear out the transaction log. In one case, we had a 187MB database with a <span style="text-decoration: underline;">37GB</span> transaction log!</p>
<p>The insanity had to stop! A handful of databases like this would put us over the top on that particular server's hard drive storage.</p>
<p>The SQL Server GUI for shrinking the database rendered no effect, and even using the DBCC SHRINKFILE command was not working.</p>
<p>The key, as explained by <a href="http://blog.sqlauthority.com/2006/12/30/sql-server-shrinking-truncate-log-file-log-full/" target="_blank">Pinal Dave</a>, is to run the SHRINKFILE command <em>twice, </em>with an explicit backup log truncation in between both runs. This code here will get you up and running:</p>
<div class="igBar"><span id="lsql-42"><a href="#" onclick="javascript:showCodeTxt('sql-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-42">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DBCC SHRINKFILE<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">"MyDatabase_Log"</span>, <span style="color: #cc66cc;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BACKUP LOG MyDatabase <span style="color: #993333; font-weight: bold;">WITH</span> TRUNCATE_ONLY</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DBCC SHRINKFILE<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">"MyDatabase_Log"</span>, <span style="color: #cc66cc;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This freed up dozens of gigabytes on our server.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/shrink-the-unshrinkable-sql-transaction-log/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Combining Repositories Into One Large Repository</title>
		<link>http://blog.crankybit.com/combining-repos/</link>
		<comments>http://blog.crankybit.com/combining-repos/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 05:44:58 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[Batch Scripting]]></category>

		<category><![CDATA[Subversion]]></category>

		<category><![CDATA[howto]]></category>

		<category><![CDATA[scripting]]></category>

		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=299</guid>
		<description><![CDATA[I keep all my projects in separate Subversion repositories. I did this because it feels a lot cleaner this way, there is less risk in the event of repository corruption, and I use corresponding Trac projects that I also wanted to keep separate from one project to the next.
That said, there are advantages to having [...]]]></description>
			<content:encoded><![CDATA[<p>I keep all my projects in separate Subversion repositories. I did this because it feels a lot cleaner this way, there is less risk in the event of repository corruption, and I use corresponding Trac projects that I also wanted to keep separate from one project to the next.</p>
<p>That said, there are advantages to having one single repository. No big deal, that can be done after the fact with code.</p>
<p>Here is some Windows code to combine all the repositories in a directory into a single big repository:</p>
<div class="igBar"><span id="ldos-44"><a href="#" onclick="javascript:showCodeTxt('dos-44'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">DOS:</span>
<div id="dos-44">
<div class="dos">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">set</span></a> <span style="color: #448844;">svndir</span>=c:\Test\svn</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">set</span></a> <span style="color: #448844;">bigrepo</span>=c:\Test\BigRepo</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">set</span></a> <span style="color: #448844;">bigrepoUNC</span>=file:///c:/Test/BigRepo</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">set</span></a> <span style="color: #448844;">rev</span>=<span style="color:#800000;">0</span>:HEAD</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/echo.html"><span style="color: #b1b100; font-weight: bold;">echo</span></a> Setting up the big repository.</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">rmdir /S /Q %<span style="color: #448888;">bigrepo</span>%</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mkdir %<span style="color: #448888;">bigrepo</span>%</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svnadmin create %<span style="color: #448888;">bigrepo</span>%</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/cd.html"><span style="color: #b1b100; font-weight: bold;">cd</span></a> %<span style="color: #448888;">svndir</span>%</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/dir.html"><span style="color: #b1b100; font-weight: bold;">dir</span></a> /A:D /B&gt; dirs.tmp</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/for.html"><span style="color: #00b100; font-weight: bold;">for</span></a> /F %%i <a href="http://www.ss64.com/nt/in.html"><span style="color: #00b100; font-weight: bold;">in</span></a> <span style="color: #66cc66;">&#40;</span>dirs.tmp<span style="color: #66cc66;">&#41;</span> <a href="http://www.ss64.com/nt/do.html"><span style="color: #00b100; font-weight: bold;">do</span></a> <span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.ss64.com/nt/echo.html"><span style="color: #b1b100; font-weight: bold;">echo</span></a> Adding %<span style="color: #448888;">svnDir</span>%\%%i to the big repository:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svnadmin dump -r %<span style="color: #448888;">rev</span>% %%i &gt;&nbsp; %%i.dmp</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svn mkdir -m &quot;Making project directory %%i.&quot; --non-interactive %<span style="color: #448888;">bigrepoUNC</span>%/%%i</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svnadmin load %<span style="color: #448888;">bigrepo</span>% --parent-<a href="http://www.ss64.com/nt/dir.html"><span style="color: #b1b100; font-weight: bold;">dir</span></a> %%i&nbsp; &lt; %%i.dmp</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">del /F /Q %%i.dmp</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">del dirs.tmp </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>There's really not much happening here; the process is simple. First, we create the new "big" repository with the <strong>svnadmin create</strong> statement. Second, we loop through the directory, processing each Subversion repository in the directory with a three-step process: <strong>(a)</strong> Dump the repository with the <strong>svnadmin dump</strong> statement into a temporary *.dmp file. <strong>(b)</strong> Explicitly add a new directory in the "big" repository for the current repository we're processing, with the <strong>svn mkdir</strong> statement. <strong>(c)</strong> Import the dump into the "big" repository with the <strong>svnadmin load</strong> statement. Really, the rest of the code is just looping, commenting, or cleanup code.</p>
<p><em><strong>What have we produced?</strong></em> As you might expect, we now have one big repository that has all of the files and commits that were in all of the smaller repositories. The big repository will maintain its own revision numbering, so the revision numbers in your smaller repositories will not match the big repository's revision numbering, although the original <em>commit dates</em> will be preserved. This can be really handy for searching or similar actions that you might do from a more global perspective.</p>
<p>However, this approach is not without its caveats. During the import process, one entire repository is imported at a time. All of a particular repository's revisions will be "grouped" together in the big repository. As a result, revision numbers in the big repository will change every time you recreate it, if there was any new activity in the repositories it contains. For instance, revision #1050 in the big repository may parallel revision #500 in Repository X, but if a commit was added to a repository that is imported before it and the big repository is recreated, that revision would now be #1051. Additionally, although all history and dates are preserved in the revisions, the big repository has commits that are not in chronological order since the import was processed by repository. This inconsistent date/commit ordering can be confusing to some repository reporting tools and may actually render those tools useless to you when they are reporting by date.</p>
<p><em><strong>Filtering by revision. </strong></em>Note that my svnadmin dump statement includes the -r argument, which specifies the beginning and ending revisions to dump. By default, I'm using "0:HEAD", which basically means "dump every revision", or "dump from the first revision to the HEAD, or latest, revision". Changing the beginning and ending revisions can be useful, especially when used with <em>dates</em> instead of actual revision numbers. For instance, you could change the value to <strong>{2007-01-01}:{2007-12-31}</strong> to only dump revisions that were committed in 2007.</p>
<p>Combining all of your smaller repositories into one big repository after the fact isn't a perfect solution, but it can be handy, and it's really easy to do when you have a script like this ready to run.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/combining-repos/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Trimming a String in JavaScript</title>
		<link>http://blog.crankybit.com/trimming-a-string-in-javascript/</link>
		<comments>http://blog.crankybit.com/trimming-a-string-in-javascript/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 15:30:12 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[General Web Dev.]]></category>

		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=307</guid>
		<description><![CDATA[This is a nice clean way to implement string trimming for the String object in JavaScript:
PLAIN TEXT
JavaScript:




String.prototype.trim = function&#40;&#41;


&#123;


   return this.replace&#40;/^\s+&#124;\s+$/g,""&#41;;


&#125; 






With that implemented, you can then trim a string in the object-oriented way you would prefer and love:
PLAIN TEXT
JavaScript:




var myStr="This is a test!   ";


alert&#40;myStr.trim&#40;&#41;&#41;; 






This is handy for client-side clean-up of form information, [...]]]></description>
			<content:encoded><![CDATA[<p>This is a nice clean way to implement string trimming for the String object in JavaScript:</p>
<div class="igBar"><span id="ljavascript-47"><a href="#" onclick="javascript:showCodeTxt('javascript-47'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-47">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">String.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">trim</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/^\s+|\s+$/g</span>,<span style="color: #3366CC;">""</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>With that implemented, you can then trim a string in the object-oriented way you would prefer and love:</p>
<div class="igBar"><span id="ljavascript-48"><a href="#" onclick="javascript:showCodeTxt('javascript-48'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-48">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> myStr=<span style="color: #3366CC;">"This is a test!   "</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>myStr.<span style="color: #006600;">trim</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is handy for client-side clean-up of form information, although bear in mind that depending on client-side code for data entry clean-up isn't necessarily wise unless you have some server-side code also checking the data.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/trimming-a-string-in-javascript/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Fix 301 Error for Subversion Checkouts</title>
		<link>http://blog.crankybit.com/how-to-fix-301-error-for-subversion-checkouts/</link>
		<comments>http://blog.crankybit.com/how-to-fix-301-error-for-subversion-checkouts/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 11:41:59 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[Apache]]></category>

		<category><![CDATA[How To]]></category>

		<category><![CDATA[Open Source]]></category>

		<category><![CDATA[Subversion]]></category>

		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=306</guid>
		<description><![CDATA[My Linux box was hosting Subversion with no problem. I added a new repository to the several that were already present, and when I checked it out, it said, "301 Moved Permanently". Excuse me?
As it turns out, there is a 301 error section in the Subversion FAQs. It says that this typically means your Apache [...]]]></description>
			<content:encoded><![CDATA[<p>My Linux box was hosting Subversion with no problem. I added a new repository to the several that were already present, and when I checked it out, it said, "301 Moved Permanently". Excuse me?</p>
<p>As it turns out, there is a <a href="http://subversion.tigris.org/faq.html#301-error" target="_blank">301 error</a> section in the Subversion FAQs. It says that this typically means your Apache configuration is invalid (nope, the rest of my repositories worked just fine) or your repository has the same path as a literal directory on your web root. <em>Ahhh! </em></p>
<p>Sure enough, my subversion path was http://myserver.com/xyz/, and I had a literal directory named "xyz" in the web root. I changed that directory name, and Subversion would then allow me to checkout the repository with no problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/how-to-fix-301-error-for-subversion-checkouts/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Favorite ColdFusion Weekly Episodes</title>
		<link>http://blog.crankybit.com/favorite-coldfusion-weekly-episodes/</link>
		<comments>http://blog.crankybit.com/favorite-coldfusion-weekly-episodes/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 21:18:55 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<category><![CDATA[History]]></category>

		<category><![CDATA[IDEs]]></category>

		<category><![CDATA[IT Industry]]></category>

		<category><![CDATA[Virtualization]]></category>

		<category><![CDATA[Subversion]]></category>

		<category><![CDATA[vm]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=305</guid>
		<description><![CDATA[In light of the departure of the ColdFusion Weekly podcast (so long Matt and Peter, and thank you!), I have assembled a list of my favorite "shows" or "episodes". Really, all of the episodes were good and worth hearing. But these episodes were fantastic. 
Presented in chronological order:

v1.5 - IDEs of...April: Came at a perfect [...]]]></description>
			<content:encoded><![CDATA[<p>In light of the <a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2008#3-09" target="_blank">departure</a> of the <a href="http://www.coldfusionweekly.com" target="_blank">ColdFusion Weekly</a> podcast (so long Matt and Peter, and thank you!), I have assembled a list of my favorite "shows" or "episodes". Really, <em>all</em> of the episodes were good and worth hearing. But these episodes were <em>fantastic. </em></p>
<p>Presented in chronological order:</p>
<ul>
<li><a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2006#1-5 Beta" target="_blank">v1.5 - IDEs of...April</a>: Came at a perfect time for me, because I had just switched to CFEclipse recently. So it was great confirmation for me and I enjoyed the Eclipse plugin recommendations. This episode is good for anyone still using Dreamweaver or HomeSite.</li>
<li><a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2006#1-6 Beta" target="_blank">v1.6 - Version Control</a>: Also had great timing, because I had already committed myself to exclusively using Subversion but hadn't yet set up my Subversion server. While you're at it, check out the follow-up episode on <a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2006#1-29" target="_blank">v1.29 - Source Control Revisited</a>. These episodes are a nice way to be introduced to version control for web development.</li>
<li><a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2006#1-10" target="_blank">v1.10 - Design Pattern Safari</a>: Probably one of the most classic episodes and a must-hear. All about design patterns like beans, DAOs, etc.</li>
<li><a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2007#2-01" target="_blank">v2.01 - Rise of the Virtual Machines</a>: Discussion of virtualization technology on both Macs and Windows. Great discussion, because I think this is an important technology for web developers; I am a big proponent of virtualization.</li>
<li><a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2007#2-02" target="_blank">v2.02 - Cryptology and Security</a>: This was very interesting because of the information that was discussed regarding encryption, hashing, and salt.</li>
<li><a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2007#2-15" target="_blank">v2.15 - Mark Mandel on Transfer</a>: Very good consideration of the popular ORM, Transfer.</li>
<li><a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2007#2-27" target="_blank">v2.27 - Selenium</a>: Discussion on testing your apps on the client side with Selenium.</li>
<li><a href="http://www.coldfusionweekly.com/index.cfm?event=showArchive&amp;year=2008#3-06" target="_blank">v3.06 - Open Source BlueDragon</a>: Discussion with Vince Bonfanti about BlueDragon going open source.</li>
</ul>
<p>I will miss Matt's Vista rants. :-)  Thanks to both of you for all of your hard work.</p>
<p>If you're hankering for a podcast to listen to, Brian Meloche is picking up where Matt and Peter left off with a new podcast called <a href="http://www.cfconversations.com/" target="_blank">CFConversations</a>. W00T!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/favorite-coldfusion-weekly-episodes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CFLDAP Dies With java.lang.OutOfMemoryError</title>
		<link>http://blog.crankybit.com/cfldap-dies/</link>
		<comments>http://blog.crankybit.com/cfldap-dies/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 17:10:15 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[ColdFusion]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=304</guid>
		<description><![CDATA[I have a report that is generated by ColdFusion that assists our department with account maintenance on our AD network. It looks for a certain set of accounts, then looks up the account of the employee who "requested" or "authorized" the creation of each account to ultimately get verification from that employee that this network [...]]]></description>
			<content:encoded><![CDATA[<p>I have a report that is generated by ColdFusion that assists our department with account maintenance on our AD network. It looks for a certain set of accounts, then looks up the account of the employee who "requested" or "authorized" the creation of each account to ultimately get verification from that employee that this network account is still needed. If you don't respond and indicate you still need the support account, it is tagged for removal when we do our system account cleaning.</p>
<p>The system ran perfectly in the past several years, but yesterday it started dying on one of the LDAP calls with the error: "java.lang.OutOfMemoryError: unable to create new native thread".</p>
<p>The kicker is that the error only occurs on the production server, even though my development box is hitting the same server and running the exact same JVM and is on the same version of ColdFusion 8. And it isn't failing on the <em>first</em> LDAP call. </p>
<p>Running short on time, I finally discovered a clunky solution: I would call &lt;cfthread action="sleep" duration="750" /&gt; to give the system a "break" before it made the LDAP call that would always fail. Oddly, this worked. Setting it to 100ms still caused it to crash (although it took longer), so I just went long at 750ms.</p>
<p>My only explanation as to why this works is that Java and/or ColdFusion may not be disposing of the LDAP calls quickly enough to avoid having them pile up and eventually cause an overflow and run out of memory.</p>
<p>I am open to someone describing a more appropriate way to handle this. But in the meantime, this is just a scheduled task that can stand to run a little more slowly. I just hate solutions that feel clunky like this, though.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/cfldap-dies/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Why Isn&#8217;t @import Working in my CSS?</title>
		<link>http://blog.crankybit.com/why-isnt-import-working-in-css/</link>
		<comments>http://blog.crankybit.com/why-isnt-import-working-in-css/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 18:17:28 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
		
		<category><![CDATA[CSS]]></category>

		<category><![CDATA[General Web Dev.]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=303</guid>
		<description><![CDATA[I was improving the CSS of an old intranet web app (written in early 2001) to be closer to standards-compliance (getting it to just look good in Firefox and Safari would be good enough for me). While I was working on that, I noticed a whole bunch of my CSS that was being completely ignored. [...]]]></description>
			<content:encoded><![CDATA[<p>I was improving the CSS of an old intranet web app (written in early 2001) to be <em>closer</em> to standards-compliance (getting it to just look good in Firefox and Safari would be good enough for me). While I was working on that, I noticed a whole bunch of my CSS that was being completely ignored. As it turns out, all of the code in a separate CSS file that was being imported with @import was not being rendered.</p>
<p>FireBug showed me that the CSS wasn't being applied to the elements that it should have been applied to. Hmm. Then I used the "Net" tab of FireBug to see that Firefox wasn't even <em>attempting </em>to load the external CSS file.</p>
<p>The solution is simple. My @import directive was surrounded by other CSS instructions. Whereas IE tolerates this, the actual W3C spec declares that @import directives should appear before any other CSS instructions, and Firefox honors this restriction.</p>
<p>Thus, my @import directive was being ignored. I moved it to the top of the file and everything started working.</p>
<p>The lessons? Don't abuse @import directives. Ideally, they should be used only to have a single CSS file that imports several smaller functional CSS files as a means to provide some organization to the CSS. I was using @import mixed in with my other CSS instructions, which is not ideal.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/why-isnt-import-working-in-css/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
