<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alagad Ally &#187; Uncategorized</title>
	<atom:link href="http://blog.alagad.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.alagad.com</link>
	<description>Web Development Task Force</description>
	<lastBuildDate>Tue, 29 Nov 2011 16:48:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>CoolBeans &#8211; an IOC container for Node.js</title>
		<link>http://blog.alagad.com/2011/11/29/coolbeans-an-ioc-container-for-node-js/</link>
		<comments>http://blog.alagad.com/2011/11/29/coolbeans-an-ioc-container-for-node-js/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 16:47:42 +0000</pubDate>
		<dc:creator>Doug Hughes</dc:creator>
				<category><![CDATA[ColdSpring]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.alagad.com/?p=1895</guid>
		<description><![CDATA[I just finished writing my first publicly available module for Node.js.  If you&#8217;re not familiar with Node.js, well, I&#8217;m just sorry to hear that.  Go learn. The module I wrote is called CoolBeans. (Thanks for the name, Mr. Chris Peterson). CoolBeans is ...]]></description>
			<content:encoded><![CDATA[<p>I just finished writing my first publicly available module for Node.js.  If you&#8217;re not familiar with Node.js, well, I&#8217;m just sorry to hear that.  <a href="http://nodejs.org">Go learn.</a></p>
<p>The module I wrote is called CoolBeans. (Thanks for the name, Mr. Chris Peterson). CoolBeans is an Inversion of Control (IOC) / Dependency Injection (DI) library for Node.js. CoolBeans is loosely based on ColdSpring for ColdFusion and Spring IOC for Java. It&#8217;s a single js file and currently appears to be quick and easy.</p>
<p>To install:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">npm install CoolBeans</pre></div></div>

<p>To use CoolBeans you simply create an instance of the CoolBeans and load the configuration file like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cb <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;CoolBeans&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
cb <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> cb<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;./config/dev.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above code is the only require function you should need in your entire application. Once you&#8217;ve required CoolBeans you need to create a new instance and pass in the path to its&#8217; configuration. This is shown above.</p>
<p>Once you have the fully loaded CoolBeans you can use it to quickly create fully configured singleton objects based on its&#8217; configuration. The config file for CoolBeans is a JSON file so the entire thing is wrapped in {}.</p>
<p>Each element in the root of the configuration file is a bean (bean = Java for object) that CoolBeans can create. Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;fs&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;fs&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is essentially the same as:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However, we now only need to define this one time for an application, rather than in each file that requires it.</p>
<p>You can also specify paths to modules that are not node_modules. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;Recipient&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;./entities/recipient&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>At the most basic, the above means that CoolBeans will call require for the module and cache the results in a variable named Recipient.</p>
<p>As a relative newb to Node.js, I think I&#8217;ve handled this correctly. CoolBeans is a node module which means that NPM will install into ./node_modues/CoolBeans. The actual CoolBeans script is in the lib directory. That means, that from the perspective of CoolBeans your components are three directories above it. For this reason, CoolBeans looks three directories above it for the module specified. So, the Recipient module above actually turns into ../../.././entities/recipient. This has the effect of making the paths to modules specified in the configuration file relative to the root of your module or application. So, if you make a module that depends on CoolBeans and later publish it via NPM I think it should work correctly when used in other projects.</p>
<p>You can get any of the configured beans by calling cb.get(&#8220;beanName&#8221;) where beanName is the name of the bean you want to get. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">cb.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Recipient&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above will lazily create the Recipient bean, cache it as a singleton, and return it.</p>
<p>You can get a lot more complex with configuration too. For example, you can specify if CoolBeans should call a constructor and what arguments to pass into the constructor. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;codeGenerator&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;./util/codeGenerator&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;constructorArgs&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
         <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span>
         <span style="color: #CC0000;">123</span>
    <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What the above is saying is that when we get the codeGenerator bean, we need to load the module specified then call new on the module and pass in the values specified in the constructorArgs section to the constructor. The above means:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">new</span> codeGenerator<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">123</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can also specify more complex values to pass into constructor arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;codeGenerator&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;./util/codeGenerator&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;constructorArgs&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #CC0000;">123</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">:</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;foo&quot;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that while you can specify a string without indicating explicitly it&#8217;s a &#8220;value&#8221;, for arrays and anonymous objects you need to provide an object with a property named &#8220;value&#8221; whose value is the value you&#8217;re trying to pass in. The above could be written more explicitly as:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;codeGenerator&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;./util/codeGenerator&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;constructorArgs&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
         <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
         <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">123</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
         <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
         <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">:</span>
             <span style="color: #009900;">&#123;</span>
                  <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;foo&quot;</span>
              <span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Strings, numbers, arrays, and anonymous objects are not the only things you can pass into constructors. You can also specify other beans that could be passed in. For example, let&#8217;s say we had a database configuration object you want to pass into any object that is used to access data you could do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;mysql&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mysql&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #3366CC;">&quot;dbConfig&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;properties&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;host&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;server.hostname.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;port&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">3306</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;user&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mysqlUser&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;password&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;password123&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;database&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;foobar&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #3366CC;">&quot;recipientDao&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;./db/recipientDao&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;constructorArgs&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;bean&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dbConfig&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;bean&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mysql&quot;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The mysql bean is simply the same as saying require(&#8220;mysql&#8221;). The dbConfig is an anonymous object with properties specified (more on this in a bit). When the recipientDao (dao = data access object) is created, CoolBeans will see the &#8220;bean&#8221; property and will create and pass into the constructor the fully-constructed dbConfig object and the mysql object. Here&#8217;s what that recipientDao might look like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">module.<span style="color: #660066;">exports</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dbConfig<span style="color: #339933;">,</span> mysql<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">listRecipients</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>userId<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> client <span style="color: #339933;">=</span> mysql.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span>dbConfig<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        client.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span>
        <span style="color: #3366CC;">&quot;SELECT id, name, addressLine1, IfNull(addressLine2, '') as addressLine2, city, state, zip, taxDeductible, created, updated, 0 as netDonations &quot;</span> <span style="color: #339933;">+</span>
        <span style="color: #3366CC;">&quot;FROM recipient &quot;</span> <span style="color: #339933;">+</span>
        <span style="color: #3366CC;">&quot;WHERE userId = ? AND deleted = 0 &quot;</span><span style="color: #339933;">+</span>
        <span style="color: #3366CC;">&quot;ORDER BY name&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span>userId<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> results<span style="color: #339933;">,</span> fields<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            client.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            callback<span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that there are no require statements. The object just gets its&#8217; dependencies when it&#8217;s instantiated and can immediately use them. These dependencies are also automatically singletons.</p>
<p>Also note that if you want to use a transient object you would still create an instance of it the way you always have.</p>
<p>I also mentioned above that CoolBeans can be used to create create and populate anonymous objects. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;dbConfig&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;properties&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;host&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;server.hostname.com&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;port&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">3306</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;user&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mysqlUser&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;password&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;password123&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;database&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;foobar&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a somewhat long-winded way of saying</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dbConfig <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;host&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;server.hostname.com&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;port&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">3306</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;user&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mysqlUser&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;password&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;password123&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;database&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;foobar&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However, once this object is configured in CoolBeans you can easily pass it into other objects when they are created.</p>
<p>You can also specify properties for not-anonymous objects. You can also mix and match constructorArgs and properties.</p>
<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;creditCardDao&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;./db/creditCardDao&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;constructorArgs&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;bean&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dbConfig&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;bean&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;authorize&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;bean&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mysql&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;bean&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;CreditCard&quot;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;properties&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #3366CC;">&quot;service&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;bean&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;service&quot;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When CoolBeans creates the creditCardDao it will first load all the beans specified in the constructorArgs. It will then create the creditCardDao and pass in the four already-created beans to the constructor. Once the object is constructed it will set the service property on the object to the specified service bean. Note, CoolBeans will look for a setter and use that if it can find it. For example, in the service property above, ColdBooks will first look for a function named setservice (note that this is case sensitive). If it can find it, it will pass in the service bean to that function. If not, it will simply set a public property on the object.</p>
<p>There are a few other interesting capabilities of CoolBeans:</p>
<p>Beans don&#8217;t have to be lazily loaded. You can set a bean to load when the container loads. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;dateFormat&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;./util/dateFormat&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;lazy&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Also, if you have a factory that is used to construct other objects, you can specify this using the factoryBean and factoryMethod properties. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;knox&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;module&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;knox&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #3366CC;">&quot;s3client&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;factoryBean&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;knox&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;factoryMethod&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;createClient&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;constructorArgs&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">:</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;key&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;myKey&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;secret&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mySecret&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;bucket&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;myBucket&quot;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above s3client bean is configured so CoolBeans uses Knox to create it. The constructor args are passed into the factoryMethod as if it were a constructor. The above essentially boils down to:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">s3client <span style="color: #339933;">=</span> knox.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;key&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;myKey&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;secret&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mySecret&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;bucket&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;myBucket&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The really nice thing about CoolBeans is that it lets the objects in your system stay focused on what they do best. It shouldn&#8217;t be your object&#8217;s responsibility to know what they need to work. They should simply get what they need to work when they&#8217;re created. CoolBeans also helps avoid situations in complex apps where you have dozens of lines of code just getting dependencies created just to create one object that otherwise happens to have a lot of dependencies. Lastly, CoolBeans allows you to easily change how your application is configured in different environments.</p>
<p>If you&#8217;re a Node.js developer I&#8217;d love to hear your thoughts on CoolBeans!  Heck, I&#8217;d love to hear your thoughts even if you&#8217;re not.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alagad.com/2011/11/29/coolbeans-an-ioc-container-for-node-js/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Contest: Help Alagad Give Away Swiss Gear Backpacks</title>
		<link>http://blog.alagad.com/2010/12/08/contest-help-alagad-give-away-swiss-gear-backpacks/</link>
		<comments>http://blog.alagad.com/2010/12/08/contest-help-alagad-give-away-swiss-gear-backpacks/#comments</comments>
		<pubDate>Wed, 08 Dec 2010 14:17:31 +0000</pubDate>
		<dc:creator>Doug Hughes</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.alagad.com/?p=1833</guid>
		<description><![CDATA[I have a bunch of promotional laptop backpacks I purchased for Alagad taking up precious space in my garage.  These are very nice swiss gear backpacks with the Alagad logo tastefully embroidered on the back pocket.  They&#8217;ve got, I&#8217;d ...]]></description>
			<content:encoded><![CDATA[<p><a style="margin-right: 15px; margin-bottom: 10px;" href="http://blog.alagad.com/wp-content/uploads/2010/10/backpack1.jpg"><img class="alignleft size-medium wp-image-1825" title="Alagad Backpack" src="http://blog.alagad.com/wp-content/uploads/2010/10/backpack1-227x300.jpg" alt="Alagad Backpack" width="227" height="300" /></a>I have a bunch of promotional laptop backpacks I purchased for Alagad taking up precious space in my garage.  These are very nice swiss gear backpacks with the Alagad logo tastefully embroidered on the back pocket.  They&#8217;ve got, I&#8217;d guess, a zillion pockets, lots of storage space, can fit a 17&#8243; laptop, and are tough as heck.  I&#8217;ve been using mine for a couple years and it still looks like new!  I really want to give them away to good homes, but I&#8217;ve been struggling on how to best do this.  Maybe you can help?</p>
<p>A few weeks ago I posted a contest where entrants would make a word search generator in an unfamiliar language.  And, well, honestly, that went over like a lead balloon.  I had precisely zero entrants.  Maybe I made it a bit too complex?</p>
<p>I&#8217;d like to turn this into a game or a contest or something of the sort and have a little fun with it!  Furthermore, a little promotional activity can&#8217;t hurt since it looks like Alagad will be winding up their current contract in April.  (It&#8217;s never too early to be looking for the next gig.)  If possible, it&#8217;d be really nice to be able to help people learn something new or, perhaps, help out those who are less fortunate, especially durring this time of year.  Winners of these contests would get an Alagad backpack.</p>
<p>So, this time I&#8217;m having a contest to come up with ideas for contests, games, or whatever.  If you have an idea on what I should do to give away these backpacks post it in the comments below.  I&#8217;ll pick one or more of the ideas and run with them.  If your idea is picked, you&#8217;ll get a very nice backpack for your troubles!</p>
<p>So, how should I give away these backpacks?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alagad.com/2010/12/08/contest-help-alagad-give-away-swiss-gear-backpacks/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Favorite Flex 4 Features</title>
		<link>http://blog.alagad.com/2010/04/06/favorite-flex-4-features/</link>
		<comments>http://blog.alagad.com/2010/04/06/favorite-flex-4-features/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 04:00:00 +0000</pubDate>
		<dc:creator>mlegrand</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I&#8217;ve been working with the public beta of Flash Builder 4 (formally Flex Builder) for a few months now.&#160; The week before last Adobe posted the final release of the Flex 4 SDK and the final version of Flash ...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with the public beta of Flash Builder 4 (formally Flex Builder) for a few months now.&#160; The week before last Adobe posted the final release of the Flex 4 SDK and the final version of Flash Builder 4.&#160; I can say with confidence that you want to be working in this IDE over Flex Builder 3.   Go learn more about Flex and Flash builder 4 <a href="http:/www.adobe.com/products/flashbuilder/">here</a>   For all of the developers on the fence as to whether or not to upgrade to the latest Flash Builder IDE, there are significant single click code generation benefits that will save you time during your day to day development activities.&#160; Additionally, you have the option of letting Flash Builder 4 introspect the coldfusion, php, java or web service and create the AS3 service layer for your Flex based application.   I&#8217;m glossing over tons of good info but I&#8217;ll just give you my top three favorite Flash Builder 4 features:   1.&#160; Generated event handlers.   2.&#160; The new package explorer view.   3. Generated getter and setter methods.   If you are like me and have a beta version of Flash Builder, please be sure to uninstall before you reinstall the latest greatest.   So run on out and buy the new Flash Builder 4 IDE.&#160; And be sure to play with all of the new multitouch features in the latest Flex 4.0 SDK or be brave and <a href="http:/opensource.adobe.com/wiki/display/flexsdk/Downloads">grab a nightly build</a> of the 4.1 SDK and hop skip over some of the release candidate bugs that were fixed after the lock down a few weeks ago (<a href="http:/bugs.adobe.com/jira/browse/SDK-25126">this one annoyed me</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alagad.com/2010/04/06/favorite-flex-4-features/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Apache&#8217;s Jmeter Part III &#8211; Using a csv to replicate real traffic</title>
		<link>http://blog.alagad.com/2009/07/15/apache-s-jmeter-part-iii-using-a-csv-to-replicate-real-traffic/</link>
		<comments>http://blog.alagad.com/2009/07/15/apache-s-jmeter-part-iii-using-a-csv-to-replicate-real-traffic/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 00:00:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Time for post number 3 regarding Jmeter, and today I will go over simulating  users performing searches for keywords feeding from a CSV file. Lets start with a basic test plan, with one thread group, a single web request to google, a random timer, and a results tree listener so we can see what is going on.&#160; It should look something like this to begin with (refer to my earlier posts to get to this point, either <a href="http:/alagad.com/go/blog-entry/site-testing-with-apache-s-jakarta-jmeter">Jmeter part I</a> or <a href="http:/alagad.com/go/blog-entry/apache-s-jmeter-part-ii-recording-a-test-script-with-the-proxy-component">Jmeter part II</a>) 
		  <p align="center"><img src="http:/www.alagad.com/upload/Picture21.png" width="423" height="339" />
		  <p align="left">Now you need to create or obtain a CSV file containing your search terms you wish to run through (or users to login, items to view, whatever you would like to change during each request).&#160; I made a super simple one here, feel free to download or create your own, and name it searches.csv.]]></description>
			<content:encoded><![CDATA[<p>Time for post number 3 regarding Jmeter, and today I will go over simulating  users performing searches for keywords feeding from a CSV file. Lets start with a basic test plan, with one thread group, a single web request to google, a random timer, and a results tree listener so we can see what is going on.  It should look something like this to begin with (refer to my earlier posts to get to this point, either <a href="http:/alagad.com/go/blog-entry/site-testing-with-apache-s-jakarta-jmeter">Jmeter part I</a> or <a href="http:/alagad.com/go/blog-entry/apache-s-jmeter-part-ii-recording-a-test-script-with-the-proxy-component">Jmeter part II</a>)</p>
<p><img src="http://www.alagad.com/upload/Picture21.png" alt="" width="423" height="339" /></p>
<p>Now you need to create or obtain a CSV file containing your search terms you wish to run through (or users to login, items to view, whatever you would like to change during each request).  I made a super simple one here, feel free to download or create your own, and name it searches.csv.</p>
<p>Download a sample <a href="http:/www.cfcode.net/searches.csv">searches.csv</a> file.</p>
<p>Now, save that csv file in the same directory that you saved your test plan file. Add a new element as a child of your thread group, called &#8216;CSV Data Set Config&#8217;.</p>
<p><img src="http://www.alagad.com/upload/Picture111.png" alt="" width="521" height="95" /></p>
<p>Set the filename to &#8216;searches.csv&#8217; (or whatever you named your data file), set the Variable Names to &#8216;TERM&#8217;, delimiter to a comma, and stop thread on EOF to false, and sharing mode to &#8216;All Threads&#8217;.  The sharing mode just sets the scope of where this variable name (defined as term above) will be available, inside this thread group, inside a single thread, or to the entire test on any thread.</p>
<p><img src="http://www.alagad.com/upload/Picture7.png" alt="" width="474" height="283" /></p>
<p>Now we have to tell our individual search to use this CSV data, which is actually really simple to do.  Go to your HTTPClient sampler, and in the HTTP parameters list, change the q value, which is currently a string &#8216;coldfusion&#8217;, to ${TERM}.</p>
<p><img src="http://www.alagad.com/upload/Picture8.png" alt="" width="490" height="341" /></p>
<p>Now when you save your plan and run it, take a look at your results tree, and click on &#8216;Request&#8217;.  You  will see that each search sent to google uses the next item from your CSV data file to perform the test.</p>
<p><img src="http://www.alagad.com/upload/Picture10.png" alt="" width="457" height="221" /></p>
<p>This is a great technique to use for simulating multiple user logins. Combine this with the cookie manager, and you can fully simulate users logging in, performing a series of actions, and logging out at the end.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alagad.com/2009/07/15/apache-s-jmeter-part-iii-using-a-csv-to-replicate-real-traffic/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Viewing MJPEG Streams in Flex</title>
		<link>http://blog.alagad.com/2008/12/16/viewing-mjpeg-streams-in-flex/</link>
		<comments>http://blog.alagad.com/2008/12/16/viewing-mjpeg-streams-in-flex/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 00:00:00 +0000</pubDate>
		<dc:creator>Doug Hughes</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[So, I'm learning Flex at long last.&#160; The reason for me finally taking up this challenge is that I finally found a project I believed it well suited to.&#160; You see, my family has lots of digital photos.&#160; About 9000 at last count.&#160; We keep these stored on a NAS in my office.&#160; All of our computers are set to show these photos as our screen savers.&#160; However, I've wanted a nice wireless digital picture frame that would randomly pull from this pool of images.&#160; Well, to make a long story short, I was unable to find a single wireless picture frame that could read from a Samba share.&#160; (And trust me, I've tried a lot of things.)&#160; 
]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;m learning Flex at long last.  The reason for me finally taking up this challenge is that I finally found a project I believed it well suited to.  You see, my family has lots of digital photos.  About 9000 at last count.  We keep these stored on a NAS in my office.  All of our computers are set to show these photos as our screen savers.  However, I&#8217;ve wanted a nice wireless digital picture frame that would randomly pull from this pool of images.  Well, to make a long story short, I was unable to find a single wireless picture frame that could read from a Samba share.  (And trust me, I&#8217;ve tried a lot of things.)</p>
<p>So, finally I became frustrated enough to take this into my own hands.  I plan make my own ideal digital picture frame.  I&#8217;m currently working on an Air application that will run in full screen and provide access to a range of picture frame components.  Obviously, a part of this will display pictures.  Other components will display weather information, stats on Alagad, and pretty much anything else I want to wire into my picture frame.  Eventually, I&#8217;ll buy a nice, thin, touch screen tablet PC, configure it to run my air app on load, get it professionally framed and hang it on the wall.</p>
<p>Now, one of the components I want this picture frame to show is a video stream from my daughters baby monitor camera.  Actually, it&#8217;s not really a baby monitor camera it&#8217;s a more-or-less generic wireless surveillance camera.  I don&#8217;t have the model information any more and it&#8217;s not branded at all.  But, from research I&#8217;ve done there are a lot of these cameras which run the same software.</p>
<p>The camera runs a basic web server so I can pull up the camera and see either a still photo or a video stream in a Java Applet or ActiveX Control.  I did a little playing around with the camera and discovered that the still photo is returned from a file called &#8220;image.jpg&#8221;.  Any time you request this file you get the latest image from the camera.  So, my first attempt a hooking into the video camera was simply to request this photo over and over again and use that as the source for an Image object.  This worked to an extent, but the Image object wouldn&#8217;t refresh quickly enough and would as a result flicker.  Beyond that, I had to make a complete HTTP request for each frame of the video and it had a very slow refresh rate.  I was able to solve the flickering problem by having two images on my canvas and alternate setting the source of each image and hiding the other image.</p>
<p>I was happy to have accomplished that, but it just wasn&#8217;t very nice.  So I decided to do a little more research.  The Java Applet on the camera was called xplug.class.  Googling that revealed a few people who had decompiled the class and used that to discover that the camera actually streams video from a file named mjpeg.cgi.  If you access this file directly you&#8217;ll simply get a long stream of binary data which makes up the video feed from the camera.  Obviously the Java Applet and Active X Control could read this stream and use it to display the video feed.</p>
<p>A little more research revealed that, sadly, mjpeg is not a standard but more of a technique.   Mjpeg really is just a concatenated stream of jpeg images, one after another.  I&#8217;m familiar with the JPEG format (at least to some degree) from my work on the <a href="/go/products-and-projects/image-component/alagad-image-component" target="_blank">Image Component</a>. At least, I knew enough to know the first few bytes that mark a JPEG image (FF D8).  So, I figured I&#8217;d look at the data in the video stream to see if I could figure it out.  I captured a few seconds of the video to disk using Curl and opened it up in a hex editor.  The first thing I noticed was that each frame of the video was denoted by an ascii string &#8220;&#8211;video boundary&#8211;&#8221;.  Looking a bit further into the file I discovered that I could find the markers that start an image.  I tested cutting the bytes from the beginning of where I thought the JPEG would start to the last byte before the video boundary into a new file and saved it as a jpeg, which I was subsequently able to read!</p>
<p>I knew that if I could read the feed by simply starting at the top of the feed, find the start of an image and the end, cutting out that image, displaying it and repeating the process forever.</p>
<p><a href="http:/blog.simb.net/" target="_blank">Simeon Bateman</a> filled in the last little piece of data I needed to know to make this work.  Namely, that you can simply provide binary data to the source property of an Image object and Flex will display the image.</p>
<p>So, after figuring out how to make a socket connection from Flex I was able to parse the video feed and display the video feed!  I did still have the flickering problem but I solved it in a similar manner.</p>
<p>Attached to this blog entry is a final Flex Component which connects to the camera by URL and streams the feed.  Anyone who has a use or it may use it, assuming your video feed is the same format as mine.  This is my first real bit of AS3/Flex development though so don&#8217;t be offended if I&#8217;m not doing things in the best way possible.</p>
<p>Here&#8217;s an example usage of the component:</p>
<div class="code">
<p>&lt;webcam:webcamImage left=&#8221;0&#8243; top=&#8221;0&#8243; right=&#8221;0&#8243; bottom=&#8221;0&#8243; host=&#8221;mycamera.com&#8221; port=&#8221;80&#8243; /&gt;</p>
</div>
<p>You can also supply username and password properties to access password protected cameras.</p>
<div><a href="/wp-content/uploads/2010/12/webcamImage.mxml_.zip">WebcamImage.mxml.zip</a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.alagad.com/2008/12/16/viewing-mjpeg-streams-in-flex/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>##TITLE##</title>
		<link>http://blog.alagad.com/1970/01/01/title-3/</link>
		<comments>http://blog.alagad.com/1970/01/01/title-3/#comments</comments>
		<pubDate>Fri, 02 Jan 1970 04:59:59 +0000</pubDate>
		<dc:creator>Doug Hughes</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.alagad.com/1970/01/01/title-3/</guid>
		<description><![CDATA[##CONTENT##]]></description>
			<content:encoded><![CDATA[<p>##CONTENT##</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alagad.com/1970/01/01/title-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
