<?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>Team Lazer Beez Blog</title>
	<atom:link href="http://blog.teamlazerbeez.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.teamlazerbeez.com</link>
	<description></description>
	<lastBuildDate>Thu, 31 May 2012 22:35:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Using dynamically generated configs with puppet</title>
		<link>http://blog.teamlazerbeez.com/2010/11/04/using-dynamically-generated-configs-with-puppet/</link>
		<comments>http://blog.teamlazerbeez.com/2010/11/04/using-dynamically-generated-configs-with-puppet/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 18:41:44 +0000</pubDate>
		<dc:creator>Jon Heise</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Jon Heise]]></category>
		<category><![CDATA[Puppet]]></category>

		<guid isPermaLink="false">http://blog.teamlazerbeez.com/?p=2712</guid>
		<description><![CDATA[After using Puppet with an external node classifier for a while one starts questioning what other information could be generated by this instead of just YAML to feed the puppetmaster. When supervisor was being rolled out there was a need to a large number of near identical config files to be generated, however any special [...]]]></description>
				<content:encoded><![CDATA[<p>After using Puppet with an external node classifier for a while one  starts questioning what other information could be generated by this  instead of just YAML to feed the puppetmaster. When supervisor was being  rolled out there was a need to a large number of near identical config  files to be generated, however any special information about the configs  really had no place in Puppet. So the solution to this was to have the  Django app generate the config files and then have puppet pull them down  with a custom parser.</p>
<p>In /var/lib/puppet/lib/puppet/parser/functions lives the file webcontent.rb which has the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
<span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#6666ff; font-weight:bold;">Puppet::Parser::Functions</span>
    newfunction<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:webcontent</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:rvalue</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>args<span style="color:#006600; font-weight:bold;">|</span>
        server = args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        configpath = args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        config = <span style="color:#996600;">&quot;&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">begin</span>⋅
            <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;http://#{server}/#{configpath}/&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
             f.<span style="color:#9900CC;">each_line</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
                 config = <span style="color:#996600;">&quot;#{config}#{line}&quot;</span>
             <span style="color:#9966CC; font-weight:bold;">end</span>
            <span style="color:#9966CC; font-weight:bold;">end</span>⋅
        <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">OpenURI::HTTPError</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; e
            <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#6666ff; font-weight:bold;">Puppet::ParseError</span>, <span style="color:#996600;">&quot;404 for http://#{server}/#{configpath}/&quot;</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; e
            <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#6666ff; font-weight:bold;">Puppet::ParseError</span>, <span style="color:#996600;">&quot;content string is http://#{server}/#{configpath}/ #{e}&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> config
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Using the Ruby module open-uri content is grabbed by the puppetmaster  and placed into the catalog. Using the following Django model, view and  template a config file is easily generated and passed along to Puppet</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> SupervisorProgram<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    name = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>
    command = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">512</span><span style="color: black;">&#41;</span>
    autostart = models.<span style="color: black;">BooleanField</span><span style="color: black;">&#40;</span>default=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    autorestart = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">32</span>,choices=<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'false'</span>,<span style="color: #483d8b;">'false'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'true'</span>,<span style="color: #483d8b;">'true'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'unexpected'</span>,<span style="color: #483d8b;">'unexpected'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    startsecs = models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>default=<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
    startretries = models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>default=<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
    exitcodes = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">64</span>,default=<span style="color: #483d8b;">&quot;0,2&quot;</span><span style="color: black;">&#41;</span>
    stopsignal = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">5</span>,choices=<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'TERM'</span>,<span style="color: #483d8b;">'TERM'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'HUP'</span>,<span style="color: #483d8b;">'HUP'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'INT'</span>,<span style="color: #483d8b;">'INT'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'QUIT'</span>,<span style="color: #483d8b;">'QUIT'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'KILL'</span>,<span style="color: #483d8b;">'KILL'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'USR1'</span>,<span style="color: #483d8b;">'USR1'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'USR2'</span>,<span style="color: #483d8b;">'USR2'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,default=<span style="color: #483d8b;">&quot;TERM&quot;</span><span style="color: black;">&#41;</span>
    stopwaitsecs = models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>default=<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">user</span> = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">16</span> ,default=<span style="color: #483d8b;">&quot;nagios&quot;</span><span style="color: black;">&#41;</span>
    redirect_stderr = models.<span style="color: black;">BooleanField</span><span style="color: black;">&#40;</span>default=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    stdout_logfile = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">256</span>,default=<span style="color: #483d8b;">&quot;AUTO&quot;</span><span style="color: black;">&#41;</span>
    stdout_logfile_maxbytes = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span> max_length = <span style="color: #ff4500;">8</span>,default=<span style="color: #483d8b;">&quot;50MB&quot;</span><span style="color: black;">&#41;</span>
    stdout_logfile_backups = models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>default=<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
    stderr_logfile = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">256</span>,default=<span style="color: #483d8b;">&quot;AUTO&quot;</span><span style="color: black;">&#41;</span>
    stderr_logfile_maxbytes = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span> max_length = <span style="color: #ff4500;">8</span>,default=<span style="color: #483d8b;">&quot;50MB&quot;</span><span style="color: black;">&#41;</span>
    stderr_logfile_backups = models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>default=<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
    environment = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span> max_length=<span style="color: #ff4500;">512</span>,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    directory = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">128</span>,default=<span style="color: #483d8b;">&quot;/&quot;</span><span style="color: black;">&#41;</span>
    umask = models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    priority = models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>default=<span style="color: #ff4500;">999</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">name</span>
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta:
        ordering = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>,<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SupervisorProgramAdmin<span style="color: black;">&#40;</span>admin.<span style="color: black;">ModelAdmin</span><span style="color: black;">&#41;</span>:
    list_display = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>,<span style="color: #483d8b;">'command'</span>,<span style="color: #483d8b;">'autorestart'</span>,<span style="color: #483d8b;">'stopsignal'</span>,<span style="color: #483d8b;">'exitcodes'</span>,<span style="color: #483d8b;">'user'</span>,<span style="color: #483d8b;">'stdout_logfile'</span>,<span style="color: #483d8b;">'stderr_logfile'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The following is the view used:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> getSupervisorConfig<span style="color: black;">&#40;</span>request,service<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;getSupervisorConfig has been called for %s&quot;</span> <span style="color: #66cc66;">%</span> service
    service = get_object_or_404<span style="color: black;">&#40;</span>SupervisorProgram,name=service<span style="color: black;">&#41;</span>
    directives = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;command&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">command</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;process_name&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;priority&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>service.<span style="color: black;">priority</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;autostart&quot;</span> <span style="color: black;">&#93;</span> = service.<span style="color: black;">autostart</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;autorestart&quot;</span><span style="color: black;">&#93;</span> = service.<span style="color: black;">autorestart</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;startsecs&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>service.<span style="color: black;">startsecs</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;startretries&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>service.<span style="color: black;">startretries</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;exitcodes&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">exitcodes</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;stopsignal&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">stopsignal</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;stopwaitsecs&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>service.<span style="color: black;">stopwaitsecs</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;user&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: #dc143c;">user</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;redirect_stderr&quot;</span><span style="color: black;">&#93;</span> = service.<span style="color: black;">redirect_stderr</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;stdout_logfile&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">stdout_logfile</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;stdout_logfile_maxbytes&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">stdout_logfile_maxbytes</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;stdout_logfile_backups&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>service.<span style="color: black;">stdout_logfile_backups</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;stderr_logfile&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">stderr_logfile</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;stderr_logfile_maxbytes&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">stderr_logfile_maxbytes</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;stderr_logfile_backups&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>service.<span style="color: black;">stderr_logfile_backups</span><span style="color: black;">&#41;</span>
    directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;directory&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">directory</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> service.<span style="color: black;">environment</span>:
        directives<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;environment&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>service.<span style="color: black;">environment</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sock/supervisor.conf&quot;</span>,directives<span style="color: black;">&#41;</span></pre></div></div>

<p>With the 20 configuration options per supervisord controlled process  there are far too many options that should be sanely passed to  puppetmaster from the external node classifier.</p>
<p>Here is the Django template:</p>

<div class="wp_syntax"><div class="code"><pre class="django" style="font-family:monospace;">#generated config
[program:{{ process_name }}]
command={{ command }}
process_name=%(program_name)s
priority={{ priority }}
autostart={{ autostart }}
autorestart={{ autorestart }}
startsecs={{ startsecs }}
startretries={{ startretries }}
exitcodes={{ exitcodes }}
stopsignal={{ stopsignal }}
stopwaitsecs={{ stopwaitsecs }}
user={{ user }}
redirect_stderr={{ redirect_stderr }}
stdout_logfile={{ stdout_logfile }}
stdout_logfile_maxbytes={{ stdout_logfile_maxbytes }}
stdout_logfile_backups={{ stdout_logfile_backups }}
stderr_logfile={{ stderr_logfile }}
stderr_logfile_maxbytes={{ stderr_logfile_maxbytes }}
stderr_logfile_backups={{ stderr_logfile_backups }}
{% if environment %}
environment={{ environment }}
{% endif %}</pre></div></div>

<p>Finally all of this can be referenced with a custom define as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="puppet" style="font-family:monospace;">define supervisorconfig(
        $program,
        $server = &quot;${rserver}&quot;
) {
    file {
        &quot;${name}.conf&quot;:
            owner =&amp;gt; root,
            group =&amp;gt; root,
            mode =&amp;gt; 0644,
            path =&amp;gt; &quot;/etc/supervisord.d/${name}.conf&quot;,
            content =&amp;gt; webcontent( $server, &quot;dpuppet/sock3/getsupervisorconfig/$program&quot;)
    }
}</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/11/04/using-dynamically-generated-configs-with-puppet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Fixtures for Isolated Testing in PHP</title>
		<link>http://blog.teamlazerbeez.com/2010/11/03/database-fixtures-for-isolated-testing-in-php/</link>
		<comments>http://blog.teamlazerbeez.com/2010/11/03/database-fixtures-for-isolated-testing-in-php/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 18:31:40 +0000</pubDate>
		<dc:creator>Drew Stephens</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Drew Stephens]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.teamlazerbeez.com/?p=2717</guid>
		<description><![CDATA[Long ago, Ryan wrote the history of our fixture frameworks. Now, you too can have the awesome Team Lazer Beez database fixture for your own project. With the release of Team Lazer Beez Open Source (formerly Genius Open Source) version 1.2, our YAML-backed, easy-to-setup fixture framework has been integrated into the gosTest framework. Here is [...]]]></description>
				<content:encoded><![CDATA[<p>Long ago, Ryan wrote the <a href="http://blog.teamlazerbeez.com/2009/03/30/testing-db-dependencies-with-phpunit/">history of our fixture frameworks</a>.  Now, you too can have the awesome Team Lazer Beez database fixture for your own project.  With the release of <a href="https://launchpad.net/genius">Team Lazer Beez Open Source</a> (formerly Genius Open Source) <a href="https://launchpad.net/genius/+download">version 1.2</a>, our YAML-backed, easy-to-setup fixture framework has been integrated into the gosTest framework.</p>
<p>Here is a simple example from our <a href="http://bazaar.launchpad.net/~genius.com/genius/trunk/annotate/head:/php/Fixture/example.php">Fixture package</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Include the Genius config file</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/Core/gosConfig.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A function to get single values from a database table
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> getThingFromDB<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> gosDB_Helper<span style="color: #339933;">::</span><span style="color: #004000;">getDBByName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'main'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOne</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT s1 FROM fixture_test WHERE i1 = &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In a nearby test file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Include the Genius fixture configuration. This geneartes a database</span>
<span style="color: #666666; font-style: italic;">// and applies the schema to it.  See fixtureTestConfig.inc.php</span>
<span style="color: #666666; font-style: italic;">// for more details.</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span>GOS_ROOT <span style="color: #339933;">.</span> <span style="color: #0000ff;">'Fixture/fixtureTestConfig.inc.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> testGetThingFromDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Create a fixture</span>
    <span style="color: #000088;">$fixture</span> <span style="color: #339933;">=</span> gosTest_Fixture_Controller<span style="color: #339933;">::</span><span style="color: #004000;">getByDBName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'main'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Load the fixture into the database</span>
    <span style="color: #000088;">$fixture</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parseFixtureFile</span><span style="color: #009900;">&#40;</span>GOS_ROOT <span style="color: #339933;">.</span> <span style="color: #0000ff;">'Fixture/example_fixture.yaml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Directly access the fixture, which is identical to what the database contains</span>
    <span style="color: #000088;">$idToGet</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fixture</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fixtureName.fixture_test.i1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Pull the value we want directly from the fixture</span>
    <span style="color: #000088;">$fixtureThing</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fixture</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fixtureName.fixture_test.s1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Pull the value we want from the DB via the function we're testing</span>
    <span style="color: #000088;">$thing</span> <span style="color: #339933;">=</span> getThingFromDB<span style="color: #009900;">&#40;</span><span style="color: #000088;">$idToGet</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The fixture put <span style="color: #006699; font-weight: bold;">$fixtureThing</span> into the DB.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Our function selected <span style="color: #006699; font-weight: bold;">$thing</span> from the DB.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Run the test</span>
testGetThingFromDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first magical line here is that require_once—<tt>fixtureTestConfig.inc.php</tt> uses the database connection information defined in <tt>Fixture/fixtureConfig.yaml</tt> to create a database is used for the duration of this PHP process only.  Subsequent test runs (or invocations of this example script) will generate an entirely new database.  See <a href="http://bazaar.launchpad.net/~genius.com/genius/trunk/view/head:/php/DB/lib/gosDB/AutoDBGenerator.cls.php">gosDB_AutoDBGenerator</a> for details on these ephemeral databases.  The takeaway is that you must have a user listed in that fixture config YAML file that can create databases.</p>
<p>In the test function itself, we create a fixture on the appropriate database, in this case our <tt>main</tt> database, and load the data from our fixture file, a simple YAML file defining what data we want in the database:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;"># The name of this fixture
fixtureName:
    # A table we will insert data into
    fixture_test:
        i1: 11
        s1: str1</pre></div></div>

<p>At this point, we&#8217;re ready to start testing.  First we get the ID that we need to pass to <tt>getThingFromDB</tt>, which we simply pull from the fixture.  Next, we get the value that we are expecting from the same table in the fixture.  Now that we have the ID we want to give to the function we&#8217;re testing, and the value we expect to get back, we can execute that function and compare the results.  Go ahead and try this all for yourself; the above example works all on its own.  The easiest way is to download the <a href="http://bazaar.launchpad.net/~genius.com/genius/trunk/download/head%3A/example.php-20101020230226-k49vggonvao9pdwi-1/example.php">example.php</a> and the necessary <a href="http://bazaar.launchpad.net/~genius.com/genius/trunk/download/head%3A/example_fixture.yaml-20101021212556-i3d4ph8xswy4yo8m-1/example_fixture.yaml">YAML file</a>.</p>
<p>The gosFixture class automatically cleans up the database tables that it touched (see <tt>Core/lib/gosTest/Framework/TestCase.acls.php</tt>), so you need to re-apply the fixture for every test.  The easiest way to do this is to put the fixture initialization in the <tt>setUpExtension()</tt> of a test class, giving you completely fresh fixture data for each and every test.</p>
<p>So go forth and test your code with better isolation and known clean data.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/11/03/database-fixtures-for-isolated-testing-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Puppet External Node Classifier</title>
		<link>http://blog.teamlazerbeez.com/2010/10/13/puppet-external-node-classifier/</link>
		<comments>http://blog.teamlazerbeez.com/2010/10/13/puppet-external-node-classifier/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 18:06:59 +0000</pubDate>
		<dc:creator>Jon Heise</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[Jon Heise]]></category>
		<category><![CDATA[Puppet]]></category>

		<guid isPermaLink="false">http://eng.genius.com/blog/?p=2657</guid>
		<description><![CDATA[In Puppet the initial method of holding information about your machines is through the site.pp config file, this rapidly becomes tiresome when you have more than 5 servers. This is where an external node classier comes in as a handy tool. The first step in setting up an external node classifier is developing what will [...]]]></description>
				<content:encoded><![CDATA[<p>In Puppet the initial method of holding information about your machines is through the site.pp config file, this rapidly becomes tiresome when you have more than 5 servers. This is where an external node classier comes in as a handy tool.</p>
<p>The first step in setting up an external node classifier is developing what will be generating the YAML for the puppetmaster to read. In this situation Django chosen as a web framework as the built in admin interface saved a bit of development time for management. Additional thought behind using an ORM framework was cutting down on development time on other projects that might need access to the truth database and could simply pull YAML from it. Within the Django application there are classes describing hosts, environments, server class, puppet classes, hard drive and filesystem layout. Here is the class describing a host:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Host<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    hostname = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>null=<span style="color: #008000;">True</span>,blank=<span style="color: #008000;">False</span>,max_length=<span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>
    basename = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>null=<span style="color: #008000;">True</span>,blank=<span style="color: #008000;">True</span>,max_length=<span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>
    maintenance = models.<span style="color: black;">BooleanField</span><span style="color: black;">&#40;</span>default=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    datacenter = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">5</span>,choices=<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sjc'</span>,<span style="color: #483d8b;">'san jose'</span><span style="color: black;">&#41;</span>,
        <span style="color: black;">&#40;</span><span style="color: #483d8b;">'smc'</span>,<span style="color: #483d8b;">'server room'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    environment = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">5</span>,choices=<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'app'</span>,<span style="color: #483d8b;">'production'</span><span style="color: black;">&#41;</span>,
        <span style="color: black;">&#40;</span><span style="color: #483d8b;">'stg1'</span>,<span style="color: #483d8b;">'staging 1'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'stg2'</span>,<span style="color: #483d8b;">'staging 2'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'dev'</span>,<span style="color: #483d8b;">'development'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    env = models.<span style="color: black;">ForeignKey</span><span style="color: black;">&#40;</span>Environment<span style="color: black;">&#41;</span>
    ostype = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">32</span>,choices=<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'linux'</span>,<span style="color: #483d8b;">'linux'</span><span style="color: black;">&#41;</span>,
        <span style="color: black;">&#40;</span><span style="color: #483d8b;">'windows'</span>,<span style="color: #483d8b;">'windows'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'mac'</span>,<span style="color: #483d8b;">'mac'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    info = models.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span>blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    bit = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span>,max_length=<span style="color: #ff4500;">5</span>,choices=<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'32'</span>,<span style="color: #483d8b;">'32 bit'</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #483d8b;">'64'</span>,<span style="color: #483d8b;">'64 bit'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    puppetrun = models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span>default=<span style="color: #ff4500;">1800</span><span style="color: black;">&#41;</span>
    serverclass = models.<span style="color: black;">ForeignKey</span><span style="color: black;">&#40;</span>SvrClass<span style="color: black;">&#41;</span>
&nbsp;
    classes = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>PuppetClass,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    drives = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>Drive,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    interfaces = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>NetInterface,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    script = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>Script,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    osver = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>Osversion,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    services = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>Service,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    supervisors = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>SupervisorProgram,blank=<span style="color: #008000;">True</span>,null=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> getBranch<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">env</span>.<span style="color: black;">branch</span>.<span style="color: black;">name</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> getEnvironment<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">env</span>.<span style="color: black;">name</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: #0000cd;">__repr__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__repr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;hostname: %s environment: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hostname</span>,<span style="color: #008000;">self</span>.<span style="color: black;">env</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HostAdmin<span style="color: black;">&#40;</span>admin.<span style="color: black;">ModelAdmin</span><span style="color: black;">&#41;</span>:
    list_display    =   <span style="color: black;">&#40;</span><span style="color: #483d8b;">'hostname'</span>,<span style="color: #483d8b;">'environment'</span>,<span style="color: #483d8b;">'bit'</span>,<span style="color: #483d8b;">'ostype'</span>,<span style="color: #483d8b;">'datacenter'</span>,<span style="color: #483d8b;">'maintenance'</span><span style="color: black;">&#41;</span>
    list_filter     =   <span style="color: black;">&#40;</span><span style="color: #483d8b;">'environment'</span>,<span style="color: #483d8b;">'datacenter'</span><span style="color: black;">&#41;</span>
    ordering        =   <span style="color: black;">&#40;</span><span style="color: #483d8b;">'hostname'</span>,<span style="color: #483d8b;">'datacenter'</span>,<span style="color: #483d8b;">'environment'</span><span style="color: black;">&#41;</span>
    search_fields   =   <span style="color: black;">&#40;</span><span style="color: #483d8b;">'hostname'</span>,<span style="color: black;">&#41;</span></pre></div></div>

<p>and now the code for the Puppet class in Python, the Puppet class class:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> PuppetClass<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    name = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">name</span>
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta:
        ordering = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>,<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> PuppetClassAdmin<span style="color: black;">&#40;</span>admin.<span style="color: black;">ModelAdmin</span><span style="color: black;">&#41;</span>:
    list_display = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>,<span style="color: black;">&#41;</span></pre></div></div>

<p> With these two classes a host and what Puppet classes it has assigned to it can readily be described and the following code will output YAML describing the host as the puppetmaster understands:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> yaml
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> hostinfo<span style="color: black;">&#40;</span>request,hostname<span style="color: black;">&#41;</span>:
    host = get_object_or_404<span style="color: black;">&#40;</span>Host,hostname=hostname<span style="color: black;">&#41;</span>
    classlist = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: black;">&#91;</span>classlist.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x.<span style="color: black;">name</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> host.<span style="color: black;">classes</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #808080; font-style: italic;">#if a host's classlist is empty at this point pull from the default list</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> classlist:
        <span style="color: #808080; font-style: italic;">#default list is simply a list of puppet class objects</span>
        defaultlist = DefaultList.<span style="color: black;">objects</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>name=<span style="color: #483d8b;">&quot;standard&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: black;">&#91;</span>classlist.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x.<span style="color: black;">name</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> defaultlist.<span style="color: black;">classes</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #808080; font-style: italic;">#add in some extra parameters</span>
    params = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'datacenter'</span>:<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>host.<span style="color: black;">datacenter</span><span style="color: black;">&#41;</span>,<span style="color: #483d8b;">'machine'</span>:<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>host.<span style="color: black;">basename</span><span style="color: black;">&#41;</span>,<span style="color: #483d8b;">'serverclass'</span>:<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>host.<span style="color: black;">serverclass</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>,<span style="color: #483d8b;">'env'</span>:<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>host.<span style="color: black;">env</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>,<span style="color: #483d8b;">'branch'</span>:<span style="color: #483d8b;">'%s'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>host.<span style="color: black;">env</span>.<span style="color: black;">branch</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>,<span style="color: #483d8b;">'envmaint'</span>:<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>host.<span style="color: black;">env</span>.<span style="color: black;">maintenance</span><span style="color: black;">&#41;</span>,<span style="color: #483d8b;">'hostmaint'</span>:<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>host.<span style="color: black;">maintenance</span><span style="color: black;">&#41;</span><span style="color: black;">&#125;</span>
    params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'rserver'</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>host.<span style="color: black;">env</span>.<span style="color: black;">rserver</span><span style="color: black;">&#41;</span>
    yamlsrc = yaml.<span style="color: black;">dump</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'classes'</span>:classlist,<span style="color: #483d8b;">'parameters'</span>:params<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span>  render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sock/hostinfo.yaml&quot;</span>,<span style="color: black;">&#123;</span><span style="color: #483d8b;">'hostname'</span>:hostname,<span style="color: #483d8b;">'yaml'</span>:yamlsrc<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>One issue that was encountered with exporting data from django to yaml was the usage of utf for strings, hence the continual usage of str(). The result of the function is dumped out through the following incredibly complex template</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">---
{{ yaml }}</pre></div></div>

<p>At the end of all of this we finally get data that comes out as something similar to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">---
classes: [bots, puppet-classes, puppet-master, repos, yamlsvn]
parameters: {branch: '33', datacenter: sjc, env: app, envmaint: '0', hostmaint: '0',
  machine: repo, rserver: sjc-repo.genops.net, serverclass: none}</pre></div></div>

<p>Next we need a command that the puppetmaster can run to get the YAML, here is the current script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
curl http:<span style="color: #000000; font-weight: bold;">//</span>example.genius.com<span style="color: #000000; font-weight: bold;">/</span>dpuppet<span style="color: #000000; font-weight: bold;">/</span>sock3<span style="color: #000000; font-weight: bold;">/</span>hostinfo<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$1</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000; font-weight: bold;">|</span> \
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;s/&amp;#39;/'/g&quot;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>;</pre></div></div>

<p>Finally the puppetmaster needs to be configured to pull from the external node classifier:</p>

<div class="wp_syntax"><div class="code"><pre class="conf" style="font-family:monospace;">[main]
    # Where Puppet stores dynamic and growing data.
    # The default value is '/var/puppet'.
    vardir = /var/lib/puppet
&nbsp;
    # The Puppet log directory.
    # The default value is '$vardir/log'.
    logdir = /var/log/puppet
&nbsp;
    # Where Puppet PID files are kept.
    # The default value is '$vardir/run'.
    rundir = /var/run/puppet
&nbsp;
    # Where SSL certificates are kept.
    # The default value is '$confdir/ssl'.
    ssldir = $vardir/ssl
    # use external nodes ftw
    external_nodes = /usr/local/bin/puppetinfo.sh
    node_terminus = exec
&nbsp;
[puppetd]
    # The file in which puppetd stores a list of the classes
    # associated with the retrieved configuratiion.  Can be loaded in
    # the separate ``puppet`` executable using the ``--loadclasses``
    # option.
    # The default value is '$confdir/classes.txt'.
    classfile = $vardir/classes.txt
&nbsp;
    # Where puppetd caches the local configuration.  An
    # extension indicating the cache format is added automatically.
    # The default value is '$confdir/localconfig'.
    localconfig = $vardir/localconfig
    server =  puppetmaster.genius.com
    runinterval = 300</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/10/13/puppet-external-node-classifier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Puppet at Genius</title>
		<link>http://blog.teamlazerbeez.com/2010/10/12/puppet-at-genius/</link>
		<comments>http://blog.teamlazerbeez.com/2010/10/12/puppet-at-genius/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 18:00:18 +0000</pubDate>
		<dc:creator>Jon Heise</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Jon Heise]]></category>
		<category><![CDATA[Puppet]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://eng.genius.com/blog/?p=2651</guid>
		<description><![CDATA[Puppet is a configuration management system  that was created with the goal of making more portions of system administration tasks reuseable. At Genius puppet is used in all of our environments: production, staging, and development. Within this setup each environment has its own puppetmaster, with each master pulling against its own version of the puppet [...]]]></description>
				<content:encoded><![CDATA[<p>Puppet is a configuration management system  that was created with the goal of making more portions of system administration tasks reuseable. At Genius puppet is used in all of our environments: production, staging, and development. Within this setup each environment has its own puppetmaster, with each master pulling against its own version of the puppet classes.  All of the masters in turn pull all of their information nodes from a central external node classifier.</p>
<p>The masters act as their own clients, syncing against themselves with a puppet class that controls checkouts of an svn repository holding all puppet classes.  All of the servers have been configured with custom defines to handle subervision checkouts and configs for supervisor, additionally a custom parser has been written to allow pulling down webpages as part a content description. All servers run on an interval of 5 minutes with clients running on a 15 minute interval, with some special exceptions for dns servers.  Additionally the web app that powers the external node classier also handles generating config files and maintaining a truth database for other tasks, like imaging.</p>
<p>This is the first in a series of post regarding our setup, following posts will cover our external node classifier, truth database, customer parser, custom defines.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/10/12/puppet-at-genius/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using HornetQ without a separate JNDI server</title>
		<link>http://blog.teamlazerbeez.com/2010/10/08/using-hornetq-without-a-separate-jndi-server/</link>
		<comments>http://blog.teamlazerbeez.com/2010/10/08/using-hornetq-without-a-separate-jndi-server/#comments</comments>
		<pubDate>Fri, 08 Oct 2010 22:10:19 +0000</pubDate>
		<dc:creator>Marshall Pierce</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://eng.genius.com/blog/?p=2619</guid>
		<description><![CDATA[This tutorial shows how to get HornetQ JMS resources out of JNDI. It also includes utilities to help use an embedded HornetQ server when writing test code.]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.jboss.org/hornetq">HornetQ</a> is JBoss&#8217;s latest messaging product. It provides a JMS implementation as well as its own <a href="http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/user-manual/en/html/using-core.html">alternate API</a>. JMS is the Java Message Service: a set of APIs built around asynchronous messaging through topics (think bulletin boards) and queues (self-explanatory). For more, see <a href="http://download.oracle.com/javaee/1.3/jms/tutorial/1_3_1-fcs/doc/jms_tutorialTOC.html">the standard JMS tutorial</a>.</p>
<p>This tutorial shows how to access HornetQ JMS resources via JNDI without needing a separate JNDI server. It also provides a test utility to run an embedded HornetQ server (perfect for unit tests that need a JMS broker running!).</p>
<h2>Why JNDI?</h2>
<p>Since JMS is part of Java EE, you&#8217;ll often see tutorials that have JMS resources (e.g. connection factories or queues) being provided to your code is via JNDI. This isn&#8217;t the only choice, though: you could instead <a href="http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/user-manual/en/html/using-jms.html#d0e1186">manually instantiate</a> your preferred provider&#8217;s implementations of the resources you need.</p>
<p>If you choose the latter approach, you might use the following to get a Queue if you were using <a href="http://activemq.apache.org/">ActiveMQ</a> (another JMS implementation):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Queue queue <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActiveMQQueue<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;queueName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This works fine until you switch to another provider, at which point you need to change all your queues (and connection factories and topics and&#8230;) to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Queue queue <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HornetQQueue<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;queueName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is why it&#8217;s recommended to instead pull those objects out of JNDI. If you&#8217;re getting the queue out of JNDI with the following code, you wouldn&#8217;t need to change anything if you switch providers other than changing how the JNDI context gets populated.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Context</span> context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InitialContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Queue queue<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    queue <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Queue<span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jndiNameForQueue&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// release your context's resources when you're done with it</span>
    context.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This isn&#8217;t meant to be a JNDI tutorial, but it should be clear why getting JMS resources (as well as things like JDBC DataSource objects and other &#8220;managed&#8221; objects) out of JNDI is a good thing: it makes it easier to program to interfaces, not implementations, and maintain vendor neutrality.</p>
<h2>HornetQ&#8217;s JNDI support</h2>
<p>HornetQ comes with all you need to be able to <a href="http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/user-manual/en/html/using-jms.html#d0e1087">connect to a JNDI server</a> and pull JMS resources out of that server. You can set up the HornetQ server to serve JNDI in addition to JMS by enabling the appropriate bean in hornetq-beans.xml.</p>
<p>This approach is straightforward, but it becomes problematic if you want your HornetQ servers to be in a <a href="http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/user-manual/en/html/ha.html">HA (high availability)</a> pair. It&#8217;s hard enough to configure and test HA without also needing JNDI service to fail over as well. Though there is <a href="http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/user-manual/en/html/appserver-integration.html#d0e8107">some documentation on how to set up HA JNDI</a>, it would be simpler (and surely faster as well) if there was no need to talk to a server at all to get JMS resources. This approach makes sense if your deployment setup is simple enough that you know which queues and JMS servers you will be using and can put that information in a few config files and bundle it with your code during deployment. This is the case for many uses of JMS. If, on the other hand, you&#8217;re already tied to using a Java EE server that provides JNDI, then you might as well use that.</p>
<p>ActiveMQ provides <a href="http://activemq.apache.org/jndi-support.html">a simple way to configure JMS resources in local JNDI</a>, but HornetQ doesn&#8217;t come with anything similar. This tutorial provides a way to do local JNDI with HornetQ.</p>
<h2>Local JNDI Configuration</h2>
<p>The starting point to accessing JNDI objects is creating a new InitialContext. The specific way that the resulting Context gets populated with data is controlled by the implementation of InitialContextFactory that you&#8217;re using. You can change the implementation by specifying the <code>java.naming.factory.initial</code> property in <code>jndi.properties</code> to be the fully qualified class name of an impementation of InitialContextFactory.</p>
<p>We can use this mechanism to specify an implementation of InitialContextFactory that reads HornetQ&#8217;s config files. More sophisticated implementations might produce a Context that accesses data on some remote server, but our goal here is something that reads data out of config files and populates a memory-only Context. We&#8217;ll need to read the core HornetQ config file to get connection factory information and the JMS HornetQ config file to get the JNDI names to assign to queues, topics, etc. We&#8217;ll also need something to actually create a Context implementation since we don&#8217;t want to have to write that ourselves. Implementing Context requires a non-trivial amount of work to do well. <a href="http://www.osjava.org/simple-jndi/">simple-jndi</a> provides a basic in-memory InitialContextFactory, so we&#8217;ll use that.</p>
<p>Your <code>jndi.properties</code> would look something like this:</p>
<pre>
java.naming.factory.initial = com.genius.hornetq.XmlHornetQInitialContextFactory
hornetq.jndi.wrapped.initialcontextfactory.impl = org.osjava.sj.memory.MemoryContextFactory
hornetq.xml.jms.path = /path-to-hornetq-jms.xml
hornetq.xml.config.path = /path-to-hornetq-config.xml
</pre>
<p>The <code>java.naming.factory.initial</code> tells InitialContext which class to instantiate to return the underlying Context (look at the source that comes with the JDK if you&#8217;re curious how). In this case, we want InitialContext to use our custom implementation that reads HornetQ XML files.</p>
<p>The other properties are only relevant to our custom InitialContextFactory. See the <a href="http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/user-manual/en/html/using-server.html#using-server.configuration">HornetQ documentation</a> for more about what to put in the XML config files.<br />
<code>hornetq.jndi.wrapped.initialcontextfactory.impl</code> defines what InitialContextFactory we&#8217;ll use to actually create a Context object. In this case, it&#8217;s the simple-jndi memory-only InitialContextFactory.<br />
<code>hornetq.xml.jms.path</code> is the path in the classpath to the HornetQ JMS config file. This is typically &#8220;hornetq-jms.xml&#8221;.<br />
<code>hornetq.xml.config.path</code> is the path in the classpath to the HornetQ core config file. This is typically &#8220;hornetq-configuration.xml&#8221;.</p>
<p>Now that we&#8217;ve got jndi.properties ready, I&#8217;ve added the custom InitialContextFactory source below. The only tricky part is handling HornetQ&#8217;s LogDelegateFactory selection. The LogDelegateFactory system lets you change the logging system used by HornetQ and is configurable via the core config file. (I&#8217;d link to the documentation, but this option is undocumented. See FileConfigurationParser#parseMainConfig() in the HornetQ source.) Unfortunately, a <a href="https://jira.jboss.org/browse/HORNETQ-406">bug in the way the LogDelegateFactory instance is set</a> causes some classes to not properly pick up your custom implementation, so if you wish to use a custom LogDelegateFactory, note the commented out call to <code>Logger.setDelegateFactory</code>. Other than that, it&#8217;s pretty simple. We use HornetQ&#8217;s configuration parsing code to get the information we need out of the config files, then populate the Context we get from simple-jndi with the resulting JMS objects.</p>
<p>The only dependency outside of HornetQ is <a href="http://www.slf4j.org/">SLF4J</a> for logging.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.genius.hornetq</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.api.core.Pair</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.api.core.TransportConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.api.jms.HornetQJMSClient</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.core.config.Configuration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.core.deployers.impl.FileConfigurationParser</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.client.HornetQConnectionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.client.HornetQDestination</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.JMSServerConfigParser</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.config.ConnectionFactoryConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.config.JMSConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.config.JMSQueueConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.config.TopicConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.impl.JMSServerConfigParserImpl</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.spi.core.logging.LogDelegateFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.LoggerFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jms.Queue</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jms.Topic</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.NamingException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.spi.InitialContextFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Constructor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.InvocationTargetException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Hashtable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Parses hornetq jms and core configuration files and populates the resulting Context appropriately.
 *
 * The following properties are needed:
 *
 * hornetq.xml.jms.path = path in the classpath to hornetq jms xml file
 *
 * hornetq.xml.config.path = path in classpath to hornetq core config file
 *
 * hornetq.jndi.wrapped.initialcontextfactory.impl = fully qualified classname of InitialContextFactory to use to
 * provide the Context instance that will be populated with configured JMS objects. An instance of this class will be
 * created and passed an empty environment.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> XmlHornetQInitialContextFactory <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">InitialContextFactory</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> JMS_XML_PATH_KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hornetq.xml.jms.path&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> CONFIG_XML_PATH_KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hornetq.xml.config.path&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> INTERNAL_ICF_IMPL_KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hornetq.jndi.wrapped.initialcontextfactory.impl&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger logger <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>XmlHornetQInitialContextFactory.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> XmlHornetQInitialContextFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        logger.<span style="color: #006633;">trace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Instantiated&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Context</span> getInitialContext<span style="color: #009900;">&#40;</span>Hashtable<span style="color: #339933;">&lt;?</span>, <span style="color: #339933;">?&gt;</span> environmentHashtable<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        logger.<span style="color: #006633;">trace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Creating InitialContext&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Map<span style="color: #339933;">&lt;</span>string, String<span style="color: #339933;">&gt;</span> env <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>string, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span>.<span style="color: #006633;">Entry</span><span style="color: #339933;">&lt;?</span>, <span style="color: #339933;">?&gt;</span> entry <span style="color: #339933;">:</span> environmentHashtable.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>entry.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">String</span> <span style="color: #339933;">&amp;&amp;</span> entry.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                env.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> entry.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> entry.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">checkKeys</span><span style="color: #009900;">&#40;</span>env, JMS_XML_PATH_KEY, INTERNAL_ICF_IMPL_KEY, CONFIG_XML_PATH_KEY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Context</span> ctx <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span>env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        populateContext<span style="color: #009900;">&#40;</span>env, ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> ctx<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> populateContext<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span>string, String<span style="color: #339933;">&gt;</span> env, <span style="color: #003399;">Context</span> ctx<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InputStream</span> configXmlStream <span style="color: #339933;">=</span> getStream<span style="color: #009900;">&#40;</span>env.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>CONFIG_XML_PATH_KEY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">/*
        * A bug in HQ 2.1.2 means that the log delegate factory will not be applied to any classes that use static
        * loggers and were loaded before the config-specified delegate factory is applied. A fix is scheduled for 2.2.0.
         * Until then, we'll hardcode the delegate factory to slf4j for the first few loggers.
        */</span>
        <span style="color: #666666; font-style: italic;">//org.hornetq.core.logging.Logger.setDelegateFactory(new YourCustomLogDelegateFactory());</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> Configuration config<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            config <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FileConfigurationParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">parseMainConfig</span><span style="color: #009900;">&#40;</span>configXmlStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> getNamingException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't get configuration from xml&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> LogDelegateFactory logDelegateFactory <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#40;</span>LogDelegateFactory<span style="color: #009900;">&#41;</span> instantiate<span style="color: #009900;">&#40;</span>config.<span style="color: #006633;">getLogDelegateFactoryClassName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        org.<span style="color: #006633;">hornetq</span>.<span style="color: #006633;">core</span>.<span style="color: #006633;">logging</span>.<span style="color: #006633;">Logger</span>.<span style="color: #006633;">setDelegateFactory</span><span style="color: #009900;">&#40;</span>logDelegateFactory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InputStream</span> jmsXmlStream <span style="color: #339933;">=</span> getStream<span style="color: #009900;">&#40;</span>env.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>JMS_XML_PATH_KEY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> JMSServerConfigParser jmsServerConfigParser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JMSServerConfigParserImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        JMSConfiguration jmsConfig<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            jmsConfig <span style="color: #339933;">=</span> jmsServerConfigParser.<span style="color: #006633;">parseConfiguration</span><span style="color: #009900;">&#40;</span>jmsXmlStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> getNamingException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't get jms info from xml&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>ConnectionFactoryConfiguration cfConfig <span style="color: #339933;">:</span> jmsConfig.<span style="color: #006633;">getConnectionFactoryConfigurations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">final</span> HornetQConnectionFactory hqcf <span style="color: #339933;">=</span>
                    getHornetQConnectionFactory<span style="color: #009900;">&#40;</span>cfConfig, config.<span style="color: #006633;">getConnectorConfigurations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> jndiName <span style="color: #339933;">:</span> cfConfig.<span style="color: #006633;">getBindings</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                ctx.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>jndiName, hqcf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>JMSQueueConfiguration queueConfiguration <span style="color: #339933;">:</span> jmsConfig.<span style="color: #006633;">getQueueConfigurations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Queue queue <span style="color: #339933;">=</span> HornetQDestination.<span style="color: #006633;">createQueue</span><span style="color: #009900;">&#40;</span>queueConfiguration.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> jndiName <span style="color: #339933;">:</span> queueConfiguration.<span style="color: #006633;">getBindings</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                ctx.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>jndiName, queue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>TopicConfiguration topicConfiguration <span style="color: #339933;">:</span> jmsConfig.<span style="color: #006633;">getTopicConfigurations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Topic topic <span style="color: #339933;">=</span> HornetQDestination.<span style="color: #006633;">createTopic</span><span style="color: #009900;">&#40;</span>topicConfiguration.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> jndiName <span style="color: #339933;">:</span> topicConfiguration.<span style="color: #006633;">getBindings</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                ctx.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>jndiName, topic<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">InputStream</span> getStream<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> xmlPath<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InputStream</span> xmlStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span>xmlPath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlStream <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NamingException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cannot find resource at &lt;&quot;</span> <span style="color: #339933;">+</span> xmlPath <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> xmlStream<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Context</span> getContext<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span>string, String<span style="color: #339933;">&gt;</span> env<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> icfClassName <span style="color: #339933;">=</span> env.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>INTERNAL_ICF_IMPL_KEY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">InitialContextFactory</span> icf <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InitialContextFactory</span><span style="color: #009900;">&#41;</span> instantiate<span style="color: #009900;">&#40;</span>icfClassName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//noinspection UseOfObsoleteCollectionType</span>
        <span style="color: #000000; font-weight: bold;">return</span> icf.<span style="color: #006633;">getInitialContext</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Hashtable<span style="color: #339933;">&lt;</span>object, Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Instantiate the supplied class name via its 0-arg ctor.
     *
     * @param className class name to instantiate
     *
     * @return instance of className
     *
     * @throws NamingException if the class cannot be instantiated
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Object</span> instantiate<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> className<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Object</span> obj<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">final</span> Class<span style="color: #339933;">&lt;?&gt;</span> klass <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>className<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">final</span> Constructor<span style="color: #339933;">&lt;?&gt;</span> ctor <span style="color: #339933;">=</span> klass.<span style="color: #006633;">getConstructor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            obj <span style="color: #339933;">=</span> ctor.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ClassNotFoundException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> getNamingException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't find class &quot;</span> <span style="color: #339933;">+</span> className, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NoSuchMethodException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> getNamingException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't find 0-arg constructor for &quot;</span> <span style="color: #339933;">+</span> className, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InvocationTargetException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> getNamingException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't instantiate &quot;</span> <span style="color: #339933;">+</span> className, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InstantiationException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> getNamingException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't instantiate &quot;</span> <span style="color: #339933;">+</span> className, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalAccessException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> getNamingException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't instantiate &quot;</span> <span style="color: #339933;">+</span> className, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> obj<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">NamingException</span> getNamingException<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message, <span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">NamingException</span> ne <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NamingException</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ne.<span style="color: #006633;">initCause</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> ne<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param env  the env to look in
     * @param keys the keys to check for
     *
     * @throws NamingException if any key is not found in the env or if the value is null
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> checkKeys<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span>string, String<span style="color: #339933;">&gt;</span> env, <span style="color: #003399;">String</span>... <span style="color: #006633;">keys</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> keys<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>env.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NamingException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't find &quot;</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; property in &quot;</span> <span style="color: #339933;">+</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> HornetQConnectionFactory getHornetQConnectionFactory<span style="color: #009900;">&#40;</span>ConnectionFactoryConfiguration cfConfig,
            Map<span style="color: #339933;">&lt;</span>string, TransportConfiguration<span style="color: #339933;">&gt;</span> connectorConfigurations<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">/*
         * Implementation largely lifted from JMSServerManagerImpl
         */</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>pair<span style="color: #339933;">&lt;</span>transportConfiguration, TransportConfiguration<span style="color: #339933;">&gt;&gt;</span> connectorConfigs <span style="color: #339933;">=</span>
                <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>pair<span style="color: #339933;">&lt;</span>transportConfiguration, TransportConfiguration<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Pair<span style="color: #339933;">&lt;</span>string, String<span style="color: #339933;">&gt;</span> configConnector <span style="color: #339933;">:</span> cfConfig.<span style="color: #006633;">getConnectorNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">String</span> connectorName <span style="color: #339933;">=</span> configConnector.<span style="color: #006633;">a</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> backupConnectorName <span style="color: #339933;">=</span> configConnector.<span style="color: #006633;">b</span><span style="color: #339933;">;</span>
&nbsp;
            TransportConfiguration connector <span style="color: #339933;">=</span> connectorConfigurations.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>connectorName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>connector <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NamingException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No configured connector with name &lt;&quot;</span> <span style="color: #339933;">+</span> connectorName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            logger.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Found connector config for &lt;&quot;</span> <span style="color: #339933;">+</span> connectorName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            TransportConfiguration backupConnector <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>backupConnectorName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                backupConnector <span style="color: #339933;">=</span> connectorConfigurations.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>backupConnectorName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>backupConnector <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NamingException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No configured backup connector with name &lt;&quot;</span> <span style="color: #339933;">+</span> connectorName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                logger.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Found backup connector config for &lt;&quot;</span> <span style="color: #339933;">+</span> backupConnectorName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                logger.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Not using a backup connector for main connector &lt;&quot;</span> <span style="color: #339933;">+</span> connectorName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            connectorConfigs.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>transportConfiguration, TransportConfiguration<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>connector, backupConnector<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
        HornetQConnectionFactory cf <span style="color: #339933;">=</span> HornetQJMSClient.<span style="color: #006633;">createConnectionFactory</span><span style="color: #009900;">&#40;</span>connectorConfigs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setClientID</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getClientID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setClientFailureCheckPeriod</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getClientFailureCheckPeriod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setConnectionTTL</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getConnectionTTL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setCallTimeout</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getCallTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setCacheLargeMessagesClient</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isCacheLargeMessagesClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setMinLargeMessageSize</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getMinLargeMessageSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setConsumerWindowSize</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getConsumerWindowSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setConsumerMaxRate</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getConsumerMaxRate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setConfirmationWindowSize</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getConfirmationWindowSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setProducerWindowSize</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getProducerWindowSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setProducerMaxRate</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getProducerMaxRate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setBlockOnAcknowledge</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isBlockOnAcknowledge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setBlockOnDurableSend</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isBlockOnDurableSend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setBlockOnNonDurableSend</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isBlockOnNonDurableSend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setAutoGroup</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isAutoGroup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setPreAcknowledge</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isPreAcknowledge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setConnectionLoadBalancingPolicyClassName</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getLoadBalancingPolicyClassName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setTransactionBatchSize</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getTransactionBatchSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setDupsOKBatchSize</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getDupsOKBatchSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setUseGlobalPools</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isUseGlobalPools</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setScheduledThreadPoolMaxSize</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getScheduledThreadPoolMaxSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setThreadPoolMaxSize</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getThreadPoolMaxSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setRetryInterval</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getRetryInterval</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setRetryIntervalMultiplier</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getRetryIntervalMultiplier</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setMaxRetryInterval</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getMaxRetryInterval</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setReconnectAttempts</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getReconnectAttempts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setFailoverOnInitialConnection</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isFailoverOnInitialConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setFailoverOnServerShutdown</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">isFailoverOnServerShutdown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cf.<span style="color: #006633;">setGroupID</span><span style="color: #009900;">&#40;</span>cfConfig.<span style="color: #006633;">getGroupID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> cf<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Testing JMS code</h2>
<p>As a bonus for reading this far, here&#8217;s <code>JmsTestServerManager</code>. This class makes it easy to start and stop an embedded HornetQ server. This is ideal for testing code that reads from or writes to JMS queues or topics, for instance. You should create one instance per test class (via @BeforeClass if you&#8217;re using JUnit 4) and call start() in setup (@Before) and stop()  in teardown (@After). This means you&#8217;ll have a fresh server instance for each test. No more needing to clean up leftover messages in queues between each test! Fortunately, HornetQ stops and starts quite quickly so this is unlikely to be a performance problem for your tests.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.genius.testutil.jms</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.jcip.annotations.NotThreadSafe</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.api.core.TransportConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.core.config.Configuration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.core.config.impl.ConfigurationImpl</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.core.server.HornetQServer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.core.server.HornetQServers</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.JMSServerConfigParser</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.JMSServerManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.config.JMSConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.config.JMSQueueConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.config.TopicConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.impl.JMSServerConfigParserImpl</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hornetq.jms.server.impl.JMSServerManagerImpl</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jetbrains.annotations.NotNull</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jetbrains.annotations.Nullable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.LoggerFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.InitialContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.NamingException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * See org.hornetq.tests.util.JMSTestBase
 */</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Runs an embedded HornetQ server suitable for use in unit tests that need JMS. It should be re-used across test
 * methods; just call start() in @Before and stop() in @After.
 */</span>
@NotThreadSafe
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JmsTestServerManager <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger logger <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>JmsTestServerManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> INVM_ACCEPTOR_FACTORY <span style="color: #339933;">=</span> InVMAcceptorFactory.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getCanonicalName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> DEFAULT_HORNETQ_TEST_JMS_XML <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/hornetq-test-jms.xml&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Queues to add when the server is started
     */</span>
    @NotNull
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Set<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> queueNames<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Topics to add when the server is started
     */</span>
    @NotNull
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Set<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> topicNames<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * The context passed to the jms server (proxied to prevent close()). No objects are registered in it; it is simply
     * provided to the server so that the server doesn't try to create its own.
     */</span>
    @NotNull
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Context</span> context<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * null if the server is stopped
     */</span>
    @Nullable
    <span style="color: #000000; font-weight: bold;">private</span> JMSServerManager jmsServer<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * null if the server is stopped
     */</span>
    @Nullable
    <span style="color: #000000; font-weight: bold;">private</span> HornetQServer server<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param context    the context to give to the server.
     * @param queueNames set of queue names to create in the server
     * @param topicNames set of topic names to create in the server
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> JmsTestServerManager<span style="color: #009900;">&#40;</span>@NotNull <span style="color: #003399;">Context</span> context, @NotNull Set<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> queueNames,
            @NotNull Set<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> topicNames<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">queueNames</span> <span style="color: #339933;">=</span> queueNames<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// avoid having the server initialize its own InitialContext, and don't let it close the InitialContext</span>
        <span style="color: #666666; font-style: italic;">// since we want to keep it open across invocations</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReadOnlyContextProxy<span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">topicNames</span> <span style="color: #339933;">=</span> topicNames<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param context      The JNDI context to pass to the server
     * @param pathToJmsXml The classpath path to the hornetq jms file to read topics and queues from
     *
     * @return a configured JmsTestServerManager in the stopped state
     */</span>
    <span style="color: #000000; font-weight: bold;">static</span> JmsTestServerManager getNew<span style="color: #009900;">&#40;</span>@NotNull <span style="color: #003399;">Context</span> context, @NotNull <span style="color: #003399;">String</span> pathToJmsXml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> JMSConfiguration configuration <span style="color: #339933;">=</span> getJmsConfiguration<span style="color: #009900;">&#40;</span>pathToJmsXml<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> JmsTestServerManager<span style="color: #009900;">&#40;</span>context, getQueueNames<span style="color: #009900;">&#40;</span>configuration<span style="color: #009900;">&#41;</span>, getTopicNames<span style="color: #009900;">&#40;</span>configuration<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Create a new InitialContext and read queues &amp; topics from the default xml path.
     *
     * @return a configured JmsTestServerManager in the stopped state
     *
     * @throws NamingException if an InitialContext can't be created
     * @see JmsTestServerManager#getNew(Context, String)
     */</span>
    <span style="color: #000000; font-weight: bold;">static</span> JmsTestServerManager getNew<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// It's ok to not close this context since we want it to be left open</span>
        <span style="color: #666666; font-style: italic;">//noinspection JNDIResourceOpenedButNotSafelyClosed</span>
        <span style="color: #000000; font-weight: bold;">return</span> getNew<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InitialContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, DEFAULT_HORNETQ_TEST_JMS_XML<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Provides access to the context used by this object to avoid needing to re-create it.
     *
     * @return the context used by this reader
     */</span>
    @NotNull
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Context</span> getContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Start the server and add all queues to it.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">server</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Server already started&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">server</span> <span style="color: #339933;">=</span> HornetQServers.<span style="color: #006633;">newHornetQServer</span><span style="color: #009900;">&#40;</span>createDefaultConfig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jmsServer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JMSServerManagerImpl<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">server</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jmsServer</span>.<span style="color: #006633;">setContext</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jmsServer</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> YourCustomTestException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't start JMS server&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> queue <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">queueNames</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                logger.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Adding queue &quot;</span> <span style="color: #339933;">+</span> queue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jmsServer</span>.<span style="color: #006633;">createQueue</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span>, queue, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> topicName <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">topicNames</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                logger.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Adding topic &quot;</span> <span style="color: #339933;">+</span> topicName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jmsServer</span>.<span style="color: #006633;">createTopic</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span>, topicName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> YourCustomTestException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't create destination&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Shut down the server.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">server</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Server not started&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jmsServer</span>.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">server</span>.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> YourCustomTestException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't stop JMS server&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jmsServer</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">server</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> JMSConfiguration getJmsConfiguration<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> pathToJmsXml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InputStream</span> xmlStream <span style="color: #339933;">=</span> JmsTestServerManager.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span>pathToJmsXml<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlStream <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> YourCustomTestException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't find hornetq jms xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> JMSServerConfigParser jmsServerConfigParser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JMSServerConfigParserImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        JMSConfiguration jmsConfig<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            jmsConfig <span style="color: #339933;">=</span> jmsServerConfigParser.<span style="color: #006633;">parseConfiguration</span><span style="color: #009900;">&#40;</span>xmlStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> YourCustomTestException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't get jms info from xml&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> jmsConfig<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Set<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> getTopicNames<span style="color: #009900;">&#40;</span>JMSConfiguration jmsConfig<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Set<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> topicNames <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>TopicConfiguration topicConfiguration <span style="color: #339933;">:</span> jmsConfig.<span style="color: #006633;">getTopicConfigurations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            topicNames.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>topicConfiguration.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> topicNames<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Set<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> getQueueNames<span style="color: #009900;">&#40;</span>JMSConfiguration jmsConfig<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Set<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> queueNames <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>JMSQueueConfiguration jmsQueueConfiguration <span style="color: #339933;">:</span> jmsConfig.<span style="color: #006633;">getQueueConfigurations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            queueNames.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>jmsQueueConfiguration.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> queueNames<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Configuration createDefaultConfig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Configuration configuration <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ConfigurationImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        configuration.<span style="color: #006633;">setSecurityEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        configuration.<span style="color: #006633;">setJMXManagementEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        configuration.<span style="color: #006633;">setPersistenceEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        configuration.<span style="color: #006633;">setFileDeploymentEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        configuration.<span style="color: #006633;">getAcceptorConfigurations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TransportConfiguration<span style="color: #009900;">&#40;</span>INVM_ACCEPTOR_FACTORY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// avoid the log about using the default cluster password</span>
        configuration.<span style="color: #006633;">setClusterPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;asdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        configuration.<span style="color: #006633;">setLogDelegateFactoryClassName</span><span style="color: #009900;">&#40;</span>SLF4JLogDelegateFactory.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getCanonicalName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> configuration<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You may have noticed ReadOnlyContextProxy being used in JmsTestServerManager. That&#8217;s a rather boring Context implementation that proxies all read-only method calls to an internal Context instance and has a no-op close() implementation. This is to prevent HornetQ from closing the Context since we want to keep the Context open through all test method invocations to prevent pointlessly re-creating an InitialContext every time.</p>
<p>The rather uninteresting source of ReadOnlyContextProxy follows to save you from writing your own.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.genius.testutil.jms</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.jcip.annotations.NotThreadSafe</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.Binding</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.Name</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.NameClassPair</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.NameParser</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.NamingEnumeration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.NamingException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Hashtable</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Proxies Context methods to let us control what happens to a Context that we pass in to the embedded JMS server.
 */</span>
@NotThreadSafe
<span style="color: #000000; font-weight: bold;">class</span> ReadOnlyContextProxy <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Context</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Context</span> context<span style="color: #339933;">;</span>
&nbsp;
    ReadOnlyContextProxy<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// no op</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*
     * State-change methods not allowed. Could also no-op them if the exception becomes problematic, but
     * org.hornetq.tests.unit.util.InVMContext UOEs on many operations.
     */</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bind<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name, <span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bind<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, <span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> rebind<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name, <span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> rebind<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, <span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> unbind<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> unbind<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> rename<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> oldName, <span style="color: #003399;">Name</span> newName<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> rename<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> oldName, <span style="color: #003399;">String</span> newName<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroySubcontext<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroySubcontext<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> addToEnvironment<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> propName, <span style="color: #003399;">Object</span> propVal<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> removeFromEnvironment<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> propName<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Context</span> createSubcontext<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Context</span> createSubcontext<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*
     * Read-only methods left alone.
     */</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> NamingEnumeration<span style="color: #339933;">&lt;</span>nameClassPair<span style="color: #339933;">&gt;</span> list<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> NamingEnumeration<span style="color: #339933;">&lt;</span>nameClassPair<span style="color: #339933;">&gt;</span> list<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> NamingEnumeration<span style="color: #339933;">&lt;</span>binding<span style="color: #339933;">&gt;</span> listBindings<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">listBindings</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> NamingEnumeration<span style="color: #339933;">&lt;</span>binding<span style="color: #339933;">&gt;</span> listBindings<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">listBindings</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> lookupLink<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">lookupLink</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> lookupLink<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">lookupLink</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">NameParser</span> getNameParser<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">getNameParser</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">NameParser</span> getNameParser<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">getNameParser</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Name</span> composeName<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name, <span style="color: #003399;">Name</span> prefix<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">composeName</span><span style="color: #009900;">&#40;</span>name, prefix<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> composeName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, <span style="color: #003399;">String</span> prefix<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">composeName</span><span style="color: #009900;">&#40;</span>name, prefix<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> Hashtable<span style="color: #339933;">&lt;?</span>, <span style="color: #339933;">?&gt;</span> getEnvironment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">getEnvironment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> lookup<span style="color: #009900;">&#40;</span><span style="color: #003399;">Name</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> lookup<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getNameInNamespace<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NamingException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context.<span style="color: #006633;">getNameInNamespace</span><span style="color: #009900;">&#40;</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>With these classes, you&#8217;ve got all you need to get started writing JMS code (and testing it, too).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/10/08/using-hornetq-without-a-separate-jndi-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Making PHP Regex Errors Real</title>
		<link>http://blog.teamlazerbeez.com/2010/08/30/making-php-regex-errors-real/</link>
		<comments>http://blog.teamlazerbeez.com/2010/08/30/making-php-regex-errors-real/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 18:16:24 +0000</pubDate>
		<dc:creator>Drew Stephens</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Drew Stephens]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://eng.genius.com/blog/?p=2569</guid>
		<description><![CDATA[PHP employs Perl Compatible Regular Expressions (PCRE) in the built-in collection of preg_* functions, such as preg_match(). While PCRE is certainly the preferred regular expression library, PHP&#8217;s implementation allows the functions to fail without any explicit warning—the user must check preg_last_error() to know that an error occurred. Often, the return of a regular expression match [...]]]></description>
				<content:encoded><![CDATA[<p>PHP employs <a href="http://www.pcre.org">Perl Compatible Regular Expressions</a> (PCRE) in the built-in collection of <a href="http://us.php.net/pcre">preg_* functions</a>, such as <a href="http://us.php.net/preg_match">preg_match()</a>.  While PCRE is certainly the preferred regular expression library, PHP&#8217;s implementation allows the functions to fail without any explicit warning—the user must check <a href="http://us.php.net/preg_last_error">preg_last_error()</a> to know that an error occurred.  Often, the return of a regular expression match is checked, and different operations are performed if the regex matched or not.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Find primes with a regex.
 * http://montreal.pm.org/tech/neil_kandalgaonkar.shtml
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> isPrime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^1?$|^(11+?)\1+$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Return value is '</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Prime<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Not prime<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Looks perfectly sensible.  Through some mathematical regex trickery, we determine whether or not a number is prime.  For reasons <a href="http://zmievski.org/2010/08/the-prime-that-wasnt">beyond the scope of this article</a>, this regex fails under default <a href="http://us.php.net/pcre.configuration">PHP configurations</a> beginning at the number 22201 because PHP&#8217;s regular expression backtracking limit is exceeded.  While the documentation for <a href="http://us.php.net/preg_match">preg_match()</a> claims it will return boolean false if a PREG_BACKTRACK_LIMIT_ERROR occurs, the function actually returns integer 0.  In the case of the above function, PHP will start calling everything above 22200 a prime number.  Even if the documentation were correct we wouldn&#8217;t be much better off—every number would be classified as <a href="http://en.wikipedia.org/wiki/Composite_number">composite number</a>.</p>
<p>How do we deal with this?  You must check <a href="http://us.php.net/preg_last_error">preg_last_error()</a> <strong>every time a PCRE function is used</strong>.  That warning is bold for a reason: the results of failing to check preg_last_error() can be even more destructive than improperly classifying integers.  The function <a href="http://us.php.net/preg_replace">preg_replace()</a> returns null when an error occurs, which PHP will happily coerce to 0 or the empty string depending on context.  It is very easy to assume that your regular expression replacement went through successfully and keep trucking along, but your users will not be happy with that null value when it&#8217;s used in a string context.</p>
<p>The solution to these ails is the newly released <a href="http://bazaar.launchpad.net/~genius.com/genius/trunk/annotate/head:/Regex/example.php">gosRegex</a> module of the <a href="http://eng.genius.com/blog/2010/07/29/genius-open-source-libraries/">Genius Open Source library</a>.  This new module provides simple wrappers for all of the PCRE functions in PHP, checking preg_last_error() for you and turning any errors into exception.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Use the gosRegex functions exactly like their preg_* counterparts</span>
gosRegex<span style="color: #339933;">::</span><span style="color: #004000;">match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/foo (bar)/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foo foo bar foo baz foo'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// If you do something that causes an error, the gosRegex functions let you know</span>
try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Example from http://us.php.net/preg_last_error</span>
    gosRegex<span style="color: #339933;">::</span><span style="color: #004000;">match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(?:\D+|&lt;\d+&gt;)*[!?]/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foobar foobar foobar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>gosException_RegularExpression <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Got a regex error: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So grab the <a href="https://launchpad.net/genius/+download">Genius Open Source library</a> and start being safe with your regular expressions in PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/08/30/making-php-regex-errors-real/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building High Performance Teams</title>
		<link>http://blog.teamlazerbeez.com/2010/08/03/building-high-performance-teams/</link>
		<comments>http://blog.teamlazerbeez.com/2010/08/03/building-high-performance-teams/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 19:44:52 +0000</pubDate>
		<dc:creator>Shirley Foster</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Culture]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://eng.genius.com/blog/?p=2488</guid>
		<description><![CDATA[Creating high performance teams by fostering communication, transparency, and embracing good engineering practices.]]></description>
				<content:encoded><![CDATA[<p>As an Engineering and Operations leader in a high-growth privately held company, it’s my responsibility to deliver the most features in the shortest amount of time with the highest quality at the lowest risk.  In my experience, this is not achieved by browbeating teams into working longer hours, or writing scads of detailed requirements, but to establish a process that is iterative and transparent, and a culture founded on collaboration and communication.</p>
<p>High performance teams are typically flat (not hierarchical), self-organizing, and cross-functional. Adopting the following guidelines will help you build a team that maximizes their combined strengths to outperform expectations while having a great time!</p>
<h2>Keys to building high performance teams</h2>
<p><strong>Alignment of Tactical Deliverables with Strategic Objectives</strong><br />
It’s important that your team understands how their day-to-day contributions ‘fit’ the strategic goals of the company. This is especially true in a high-growth start-up since the team are also (typically) shareholders and have a vested interest in ensuring the work they do is positively contributing to the company’s success. Share the strategic goals and how specific features and release deliverables will help the company achieve them. Also, highlight sales and customer successes where particular features or capabilities played a key role.</p>
<p><strong>Team-based Goals</strong><br />
Measuring the team on their collective accomplishments increases teamwork by creating a sense of joint ownership and accountability.  By focusing on team goals, rather than individual goals, team members will raise issues earlier (so as not to impede the team’s progress), and seek ways to help one another to complete tasks. Mentoring and knowledge transfer will also happen more naturally as teams seek ways to become more productive.</p>
<p><strong>Transparency</strong><br />
Adopting agile practices will help increase transparency to your team&#8217;s progress, provide earlier visibility to issues and potential delays and establish a regular time for the team to reflect on what they’ve accomplished. The best way to increase transparency is to adopt <a href="http://eng.genius.com/blog/2009/12/16/agile-atgenius-com-slides/">Agile software development</a> practices. At Genius, we follow the tenets of both <a href="http://www.scrumalliance.org/learn_about_scrum">Scrum</a> and <a href="http://en.wikipedia.org/wiki/Lean_software_development">Lean</a> software development. Our <a href="http://eng.genius.com/blog/2009/04/27/story-focused-daily-standups/">daily stand-up meetings</a> keep everyone in the team on the same page and a <a href="http://www.mountaingoatsoftware.com/scrum/product-backlog">product backlog</a> that is visible to the whole company ensures that there is clarity to what&#8217;s on tap for future releases.</p>
<p><strong>Self-organizing Teams</strong><br />
While on the surface this may seem counter-intuitive to those who are more familiar with hierarchical organizations; self-organizing teams are highly effective. Providing teams with clearly understood expectations and allowing them to self-organize to meet those goals – in combination with a transparent process – results in highly motivated and effective teams. Technical teams understand how each of their skills can be best leveraged to achieve the desired result and take greater ownership in the outcome rather than simply following management direction.</p>
<p><strong>Communication and Collaboration</strong><br />
The most effective teams communicate and collaborate regularly. Creating a culture where open communication is valued and encouraged will ensure that your teams seek clarity of understanding, raise issues or concerns openly, that they are willing to challenge the status quo and that they validate ideas before implementing them. At Genius, everyone (whether junior or senior) is capable of having the best idea in the room and is encouraged to share their thinking. This results in a better product and creates an environment where everyone on the team is contributing fully.</p>
<p>Communication must happen at every level – from technical discussions to updates on the business. As a leader it’s important to set the example by being a great communicator and by demonstrating that you value everyone’s contributions and that there are no negative implications to speaking up.</p>
<p><strong>Establish Accountability</strong><br />
Establishing ownership for deliverables, in addition to setting clear goals and measuring the results, is key to improving the performance of your team. It’s best to establish a relatively short timeframe to meet a set of goals. The list of goals should be determined with the team so that they are clear on the expectations and their ability to meet them. At Genius, we have adopted <a href="http://eng.genius.com/blog/2010/04/06/an-agile-fortnight/">2 weeks sprints</a> and release new features to production <a href="http://eng.genius.com/blog/2010/07/26/releasing-every-fortnight/">every 2 weeks</a>.</p>
<p>While it’s desirable for the team to deliver on all their commitments, absolute perfection in estimation is impossible, It is important to be flexible to some degree &#8211; when issues that may affect the team&#8217;s ability to deliver on their commitments come up early they can be dealt with effectively. This is especially important at the outset as your team is transitioning to a results-based model. Remember, the goal is to build a framework for success so that the team will be motivated to do more.</p>
<p><strong>Measure, Review and Adapt</strong><br />
Performance improvement can only be made if you are willing to openly discuss things that have not gone well and to identify improvements that will increase the team&#8217;s rate of success.  Examples can range from how the team is organizing itself to how well they (really) understood the requirements.  It’s also important to reflect on what went well and to discuss how the team can adopt this into their process. Keep a record of what you’ve discussed so you can periodically look back with the team and see how far you’ve come.</p>
<p><strong>Prioritize Career Development</strong><br />
High performance teams need (and expect) to be recognized for their achievements. Not only on an ongoing basis (related to deliverables), but also in career advancement. Provide clear definitions of what’s required to achieve greater responsibility and establish a regular dialog where you articulate areas of strength and opportunities to improve. This will inspire high performers to step up to meet the next level.</p>
<p><strong>Implement Source Control and Build Management</strong><br />
One of the keys to success in an agile environment is continuous integration (check-ins to a single codebase) and continuous builds. Having effective and automatic builds ensures that the team is alerted at the earliest time about build problems.  This is best accomplished with a Build Engineer whose responsibility it is to ensure that code is branched (and merged) appropriately, that builds are correctly executed and that problems are investigated.</p>
<p><strong>Celebrate</strong><br />
Having led several Engineering and Operations teams over the past 20 years, I have seen the transformation and the results of adopting these practices multiple times. As you begin to adopt these with your team, don’t forget the most important thing of all…<strong>CELEBRATE</strong>! At Genius, we celebrate (as a company) the teams’ accomplishments every two weeks at the <a href="http://eng.genius.com/blog/2009/04/17/sprint-review-branded-cupcakes/">Sprint Review</a>. We also celebrate our most recent release at the weekly Engineering and TechOps meeting and recognize what’s been completed daily at the stand-up meetings.</p>
<p>Not only does this give the team the recognition they deserve, but it also provides regular feedback for how they are doing.  At Genius, the team has had a lot to celebrate with over 70 consecutive successful sprints and 15 on-time releases since adopting agile; providing new capabilities and features that meet our customer&#8217;s ongoing business goals.</p>
<p>Good luck! I look forward to hearing your thoughts and experiences.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/08/03/building-high-performance-teams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Genius Open Source Libraries</title>
		<link>http://blog.teamlazerbeez.com/2010/07/29/genius-open-source-libraries/</link>
		<comments>http://blog.teamlazerbeez.com/2010/07/29/genius-open-source-libraries/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 19:12:48 +0000</pubDate>
		<dc:creator>Drew Stephens</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Drew Stephens]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://eng.genius.com/blog/?p=2437</guid>
		<description><![CDATA[Genius Open Source libraries inaugural release of string sanitization functions for PHP.]]></description>
				<content:encoded><![CDATA[<p>Some time ago, Genius Engineering decided to unify the manner in which we encode values that contain user input.  We previously depended upon the PHP built-in <a href="http://us.php.net/manual/en/function.htmlentities.php">htmlentities()</a> and some simple wrappers around it for our encoding needs, but this function alone can&#8217;t safely sanitize tainted data in all contexts.  Furthermore, we didn&#8217;t have a unified vision of whether encoding should happen immediately upon receipt of data from the user or when we display that data to the user.  The ambiguity of our security arrangement, and the lack of encoding functions appropriate for all contexts led the engineering team to look for better options in PHP security for the prevention of <a href="http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet">cross-site scripting (XSS)</a> and <a href="http://ha.ckers.org/sqlinjection/">SQL injection</a> vulnerabilities.  While there is plenty of information about these issues and what must be done to fix them, there is a distinct dearth of libraries in PHP to properly encode strings for all of the situations.</p>
<p>When the right tool for the job doesn&#8217;t exist, you build it.  We came up with a set of functions to sanitize tainted data in any of the places that it is output to the user.  The functions are very straightforward: give them a string and you get back one that is fully escaped.  Output from the gosSanitizer functions can be safely used as a double-quoted string in an HTML attribute or JavaScript context, or as a single-quoted string in an SQL context.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Output an unsafe string, presumably user input</span>
<span style="color: #000088;">$xss</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;script&gt;alert(\'oh snap\');&lt;/script&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'If your entered your name as '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$xss</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">', we\'d be in trouble.&lt;br /&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Sanitize that string, and output it safely</span>
<span style="color: #000088;">$htmlContentContext</span> <span style="color: #339933;">=</span> gosSanitizer<span style="color: #339933;">::</span><span style="color: #004000;">sanitizeForHTMLContent</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xss</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;But if we sanitize your name, &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$htmlContentContext</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;, then all is well.&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h2&gt;HTML Attribute&lt;/h2&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// We can also safely sanitize it for an HTML attribute context</span>
<span style="color: #000088;">$htmlAttributeContext</span> <span style="color: #339933;">=</span> gosSanitizer<span style="color: #339933;">::</span><span style="color: #004000;">sanitizeForHTMLAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xss</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Tainted strings can also be used in an
    &lt;a href=&quot;http://google.com&quot; title=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$htmlAttributeContext</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;HTML attribute&lt;/a&gt;
    context.&lt;br /&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h2&gt;JavaScript string&lt;/h2&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// And we can even make strings used in JavaScript safe</span>
<span style="color: #000088;">$jsString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'\';alert(1);var b =\''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;script type=&quot;text/javascript&quot;&gt;
var a = \''</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$jsString</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'\';
var aSafe = \''</span> <span style="color: #339933;">.</span> gosSanitizer<span style="color: #339933;">::</span><span style="color: #004000;">sanitizeForJS</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jsString</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'\';
&lt;/script&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>We have created a <a href="http://launchpad.net/genius">project</a> on Launchpad to host the <a href="https://launchpad.net/genius/">Genius text sanitizing libraries</a>.  The project consists of three modules: Core and Utility which provide general purpose support functions, and Sanitizer, which holds the functions used above.  In the case of Sanitizer, all of the functions are static, and can be accessed through the gosSanitizer class.  To use the Genius Sanitizer, you&#8217;ll need all three modules: Core, Utility, and Sanitizer itself.  All of the Genius modules are loaded using the autoloader defined in <tt>Core/gosConfig.inc.php</tt>, so including this file is all that is needed to use any of the Genius Open Source libraries.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Include the Genius config file</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Core/gosConfig.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Use gos* classess &amp; functions here</span></pre></div></div>

<p>We plan to continue adding modules to the Genius Open Source libraries collection in the future.  Keep an eye on this blog for announcements!</p>
<p><em>Edited 2010-08-30 to reflect prefix change from &#8220;sg&#8221; to &#8220;gos&#8221;</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/07/29/genius-open-source-libraries/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Releasing Every Fortnight</title>
		<link>http://blog.teamlazerbeez.com/2010/07/26/releasing-every-fortnight/</link>
		<comments>http://blog.teamlazerbeez.com/2010/07/26/releasing-every-fortnight/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 21:08:21 +0000</pubDate>
		<dc:creator>Shirley Foster</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://eng.genius.com/blog/?p=2455</guid>
		<description><![CDATA[How Genius went from releases every month to releasing every fortnight.]]></description>
				<content:encoded><![CDATA[<p>Genius.com’s successful adoption of agile practices has been covered at some length in earlier postings, including <a href="http://eng.genius.com/blog/2009/12/16/agile-atgenius-com-slides/">Presenting on Going Agile with Scrum</a> and <a href="http://eng.genius.com/blog/2010/04/06/an-agile-fortnight/">An Agile Fortnight</a>.  Building on this success, we have most recently reached the point where the completed <a href="http://en.wikipedia.org/wiki/User_story">user stories</a> for any given sprint at not only ‘potentially shippable’ but are actually deployed to production. So, how did we get here and how long did it take?</p>
<h2>Testing as the foundation</h2>
<p>One of the key elements of our success in bi-weekly product releases is the commitment to increasing automated test coverage – both unit tests and functional automation tests.</p>
<p>With rapid rate of change – and new features in every release – it is imperative that developers know immediately if their check-ins have caused a build to break. This is only possible with a concerted investment in unit testing and QA automation. In our cases, we proceeded in phases, each taking approximately 4 months to implement:</p>
<ol>
<li><em>All check-ins must have associated unit tests.</em> While we did not take the time to retrofit existing code, all new or modified code was required to have associated unit tests</li>
<li><em>All product builds must run the complete unit test suite.</em> We use <a href="http://hudson-ci.org/">Hudson</a>, integrated with <a href="http://www.junit.org/">JUnit</a>, <a href="http://www.mbunit.com/">mbUnit</a>, <a href="http://search.cpan.org/~mcast/Test-Unit-0.25/lib/Test/Unit.pm">Test::Unit</a>, <a href="http://jsunity.com/">jsUnity</a>, and <a href="http://www.phpunit.de/">PHPUnit</a> to execute all the unit tests with every build and to report on failures at any stage</li>
<li><em>Run builds on every checkin.</em></li>
<li><em>All regression tests in <a href="http://runtestrun.com">TestRun</a> (our test plan management tool) must be automated using <a href="http://seleniumhq.org/">Selenium</a> and added to the nightly build.</em> This took some time and had to be done incrementally. With an end-to-end test that required 3 days of manual testing by the entire QA team when we started, the impact of incremental investments in test automation began to pay off quickly. Automation of existing regression tests became a background task for the QA Engineers for each sprint. Developers also pitched in, writing helper functions to ease automation and writing automated tests themselves.</li>
<li><em>All stories must have associated <a href="http://seleniumhq.org/projects/remote-control/">Selenium RC</a> automated functional tests checked in and added to the nightly build test.</em> In addition to the manual functional testing, every new story must have associated automated tests checked in and executing (via Hudson) nightly so that we were not adding to the regression debt.</li>
<li><em>Run an acceptance test of functional tests on every checkin.</em></li>
</ol>
<h2>When is a story done?</h2>
<p>We established a very rigorous definition of ‘done’ for stories to ensure a consistent quality level. We also adopted ‘<a href="http://agiletools.wordpress.com/2007/12/03/what-is-swarming/">story swarming</a>’ (applying as many developers/QA/DB to the story) to shorten times on individual stories and to avoid having many stories open at once.</p>
<p>For a story to be <em>done</em>:</p>
<ol>
<li>All phases completed (in our case ‘To Do’, ‘In Progress’, ‘Security Review’, ‘Ready for QA’, ‘In QA’, ‘Validated’)</li>
<li>Unit testing complete</li>
<li>Security reviewed (code reviewed for web application security vulnerabilities)</li>
<li>Validated by QA</li>
<li>Test cases documented in TestRun</li>
<li>Automated QA testing complete</li>
<li>Validated by Product Owner</li>
<li>All Operational considerations have been addressed</li>
</ol>
<p>Providing all these conditions have been met, the story will be demonstrated to the company at the Sprint Review on the second Friday of the two-week Sprint and released to customers the following Tuesday.</p>
<h2>What else needs to be considered?</h2>
<p>One of the things I often get asked about when moving so quickly is the coherency of the architecture and the user experience. At Genius, we employ several methods to ensure the architecture is appropriately scalable and maintainable and that the product is easy to use:</p>
<ol>
<li>NMI (needs more information) stories. For user stories that have a significant impact on user experience or the underlying architecture, the team will first complete an NMI. NMI stories are focused on a subset of the team determining user flow (with leadership from the Product Designer) and/or underlying architecture (with leadership from the Technical Leads and the Development Director). The input to an NMI story is a list of questions that need answering (such as “how will the Marketing user…?” or “How can we ensure continuous availability of this feature during system maintenance?” The output of NMIs is a user flow or technical design, and a documented list of tasks for an upcoming sprint.</li>
<li>Development framework. Ease of use is a key differentiator at Genius, as is performance. We evaluated several frameworks and determined that to achieve the level of user interactivity required (<a href="http://en.wikipedia.org/wiki/Ajax_%28programming%29">Ajax</a>) we would need to build our own lightweight PHP framework. This framework is now the basis for all new functionality added to the product &#8211; not only speeding development, but further ensuring consistency in coding and usability.</li>
<li>Designated ‘leads’ in each of the major technical components or code bases of the product, Technical Operations and User Experience with primary responsibly to making the team productive – and secondary responsibility to completing story tasks for the sprint.</li>
</ol>
<p>Another concern with bi-weekly deployments is releasing partially complete features. As a SaaS provider, all the software we release to our production servers is immediately available to customers, so our goal is to complete at least a minimal feature set within each release. That said, we do make use of a beta flag (set by the provisioning team) to preview new features with customers or internally. This, combined with feature-based provisioning, can provide a lot of control over what an individual customer user can see or access. Of course, in the case that work on an existing feature is partially complete, we will typically rollback the code to the prior version (excluding it from the current sprint) to prevent user inconsistencies.</p>
<h2>What’s up next?</h2>
<p>The next step in our process evolution is to parallelize the nightly functional build tests (which currently contains over 600 Selenium scripts and runs for over 3 hours) so they can be run with every build. We are taking a two-pronged approach to this:</p>
<ol>
<li>Virtualized Selenium servers in-house. These will be used to run functional tests against every build for a single browser.</li>
<li><a href="http://saucelabs.com/">Sauce Labs</a> Sauce On Demand for cross-browser Selenium testing of all the automated functional tests on a daily basis.</li>
</ol>
<p>In the future we will provide updates on our experiences with Sauce Labs and any other process developments.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/07/26/releasing-every-fortnight/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Serving RESTful URLs with mod_rewrite</title>
		<link>http://blog.teamlazerbeez.com/2010/05/25/serving-restful-urls-with-mod_rewrite/</link>
		<comments>http://blog.teamlazerbeez.com/2010/05/25/serving-restful-urls-with-mod_rewrite/#comments</comments>
		<pubDate>Wed, 26 May 2010 00:10:13 +0000</pubDate>
		<dc:creator>Ben VanEvery</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://eng.genius.com/blog/?p=2426</guid>
		<description><![CDATA[Using mod_rewrite to munge URLs into a RESTful API.]]></description>
				<content:encoded><![CDATA[<p>We&#8217;ve been experimenting with an internal API to our app to facilitate development of UI tests by our QA team.  After much discussion (likely to described in a blog post some time down the line), the decision was made to provide the API in a RESTful style over HTTPS.  Rather than make a separate PHP script at each location an API request could possibly land, it made more sense to have all requests routed to a single handler that delegated the requests to the appropriate class.  Doing this in Apache required a little configuration and research.</p>
<p>Apache provides a module for rewriting incoming URLs, thus giving us the ability to route all requests for the API to the PHP handler.  Configuring this required a simple regular expression.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteRule ^/api.*$ /api/handler.php [L]</pre></div></div>

<p>That was simple enough.  No more information is required in the URL by the handler, it has access to the originally requested resource through <code>$_SERVER[<span style="color: #99cc00;">'REQUEST_URI'</span>]</code>.  This now means that a request to <code>/api/team/1</code>, would be actually be sent to <code>/api/handler.php</code>, which would then determine that a request for the details of the team with id 1 should be processed.</p>
<p>You may have noticed something odd about the rewrite above: there is no query string.  Indeed, the query string is not specified and that is wrong.  Two things are at play here, (1) RewriteRule does not match against any text in the query string, and (2) the substitution URL <em>completely</em> rewrites the URL, so the original query string is not magically carried over.  This is simultaneously useful and troublesome.  The mod_rewrite module provides several directives for working with the URL, one of which is named RewriteCond, which, in addition to the URL itself, lets you match against several of the other HTTP things going on (aka Server Variables).  One of these is the query string.  Matching against the query string (or anything for that matter) in a RewriteCond gives you access to back reference those matches in an ensuing RewriteRule directive using the <code>%n</code> syntax in addition the canonical <code>$n</code> syntax of the regular expression used for RewriteRule.</p>
<p>As useful as this is, in our situation we want to maintain the entire query string, which did not require use of the RewriteCond directive at all.  We accomplish this by,</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RewriteRule ^/api.*$ /api/handler.php?%{QUERY_STRING} [L]</pre></div></div>

<p>For full details of the mod_rewrite module, check out the doc, <a title="Apache HTTP Server" href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html</a>.  The list of Server Variables available for substitution can be found in the discussion of the RewriteCond directive.  For some examples of the RewriteCond directive, check out: <a title="http://fantomaster.com/faarticles/rewritingurls.txt" href="http://fantomaster.com/faarticles/rewritingurls.txt">http://fantomaster.com/faarticles/rewritingurls.txt</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teamlazerbeez.com/2010/05/25/serving-restful-urls-with-mod_rewrite/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 18.700 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-24 10:55:21 -->
