<?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>Mark Needham &#187; Java</title>
	<atom:link href="http://www.markhneedham.com/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Sat, 31 Jul 2010 07:06:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Writing a Java function in Clojure</title>
		<link>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 10:08:20 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1868</guid>
		<description><![CDATA[A function that we had to write in Java on a project that I worked on recently needed to indicate whether there was a gap in a series of data points or not. If there were gaps at the beginning or end of the sequence then that was fine but gaps in the middle of [...]]]></description>
			<content:encoded><![CDATA[<p>A function that we had to write in Java on a project that I worked on recently needed to indicate whether there was a gap in a series of data points or not.</p>
<p>If there were gaps at the beginning or end of the sequence then that was fine but gaps in the middle of the sequence were not.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">null, 1, 2, 3 =&gt; no gaps
1, 2, 3, null =&gt; no gaps
1, null, 2, 3 =&gt; gaps</pre></div></div>

<p>The Java version looked a bit like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> hasGaps<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Iterator<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> fromHead <span style="color: #339933;">=</span> values.<span style="color: #006633;">iterator</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;">while</span> <span style="color: #009900;">&#40;</span>fromHead.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> fromHead.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><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>
        fromHead.<span style="color: #006633;">remove</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: #003399;">Collections</span>.<span style="color: #006633;">reverse</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Iterator<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> fromTail <span style="color: #339933;">=</span> values.<span style="color: #006633;">iterator</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;">while</span> <span style="color: #009900;">&#40;</span>fromTail.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> fromTail.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><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>
        fromTail.<span style="color: #006633;">remove</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: #000000; font-weight: bold;">return</span> values.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We take the initial list and then remove all the null values from the beginning of it, then reverse the list and remove all the values from the end.</p>
<p>We then check if there's a null value and if there is then it would indicate there is indeed a gap in the list.</p>
<p>To write this function in Clojure we can start off by using the '<a href="http://clojure.org/api#toc237">drop-while</a>' function to get rid of the trailing nil values.</p>
<p>I started off with this attempt:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>drop-while #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">%</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Unfortunately that gives us the following error!</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Can't take value of a macro: #'clojure.core/let (NO_SOURCE_FILE:16)</pre></div></div>

<p>It thinks we're trying to pass around the 'let' macro instead of evaluating it &#8211; I forgot to put in the brackets around the 'let'!</p>
<p>I fixed that with this next version:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>But again, no love:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">java<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>IllegalArgumentException<span style="color: #66cc66;">:</span> <span style="color: #b1b100;">let</span> requires an even number of forms in binding vector <span style="color: #66cc66;">&#40;</span>NO_SOURCE_FILE<span style="color: #66cc66;">:</span><span style="color: #cc66cc;">23</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The way I understand it the 'let' macro takes in a vector of bindings as its first argument and what I've done here is pass in two vectors instead of one.</p>
<p>In the bindings vector we need to ensure that there are an even number of forms so that each symbol can be bound to an expression.</p>
<p>I fixed this by putting the two vectors defined above into another vector:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We can simplify that further so that we don't have nested vectors:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The next step was to make 'no-nils' a function so that I could make use of that function when the list was reversed as well:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>no-nils <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I then wrote the rest of the function to reverse the list and then <a href="http://www.markhneedham.com/blog/2009/11/21/clojure-checking-for-a-nil-value-in-a-collection/">check the remaining list for nil</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#91;</span>nils-removed<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>comp no-nils <span style="color: #b1b100;">reverse</span> no-nils<span style="color: #66cc66;">&#41;</span> x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>some <span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span>nils-removed <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The '<a href="http://clojure.org/api#toc151">comp</a>' function can be used to compose a set of functions which is what I needed.</p>
<p>It seemed like the 'nils-removed' function wasn't really necessary so I inlined that:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>some <span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>comp no-nils <span style="color: #b1b100;">reverse</span> no-nils<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The function can now be used like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (has-gaps? '(1 2 3))
nil
user=&gt; (has-gaps? '(nil 1 2 3))
nil
user=&gt; (has-gaps? '(1 2 3 nil))
nil
user=&gt; (has-gaps? '(1 2 nil 3))
true</pre></div></div>

<p>I'd be intrigued to know if there's a better way to do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>F# vs C# vs Java: Functional Collection Parameters</title>
		<link>http://www.markhneedham.com/blog/2009/01/19/f-vs-c-vs-java-functional-collection-parameters/</link>
		<comments>http://www.markhneedham.com/blog/2009/01/19/f-vs-c-vs-java-functional-collection-parameters/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 09:24:25 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[functional-programming]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=860</guid>
		<description><![CDATA[I wrote a post about a month ago on using functional collection parameters in C# and over the weekend Fabio and I decided to try and contrast the way you would do this in Java, C# and then F# just for fun. Map Map evaluates a high order function on all the elements in a [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a post about a month ago on using <a href="http://www.markhneedham.com/blog/2008/12/17/functional-collection-parameters-in-c/">functional collection parameters</a> in C# and over the weekend <a href="http://fabiopereira.me/blog/">Fabio</a> and I decided to try and contrast the way you would do this in Java, C# and then F# just for fun.</p>
<h3>Map</h3>
<p>Map evaluates a high order function on all the elements in a collection and then returns a new collection containing the results of the function evaluation.</p>
<p>Given the numbers 1-5, return the square of each number</p>
<p>Java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> numbers <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> number <span style="color: #339933;">:</span> numbers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#40;</span>number<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;">private</span> <span style="color: #000066; font-weight: bold;">int</span> f<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">*</span>value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>C#</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">5</span><span style="color: #000000;">&#125;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">*</span>x<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>F#</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[1..5] |&gt; List.map (fun x -&gt; x*x) |&gt; List.iter (printfn &quot;%d&quot;);;</pre></div></div>

<h3>Filter</h3>
<p>Filter applies a predicate against all of the elements in a collection and then returns a collection of elements which matched the predicate.</p>
<p>Given the numbers 1-5, print out only the numbers greater than 3:</p>
<p>Java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> numbers <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> number <span style="color: #339933;">:</span> numbers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    f<span style="color: #009900;">&#40;</span>number<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;">private</span> <span style="color: #000066; font-weight: bold;">void</span> f<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<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>value <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>value<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>C#</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">1</span>,<span style="color: #FF0000;">2</span>,<span style="color: #FF0000;">3</span>,<span style="color: #FF0000;">4</span>,<span style="color: #FF0000;">5</span><span style="color: #000000;">&#125;</span>.<span style="color: #0000FF;">FindAll</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>F#</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[1..5] |&gt; List.filter (fun x -&gt; x &gt; 3) |&gt; List.iter (printfn &quot;%d&quot;);;</pre></div></div>

<h3>Reduce</h3>
<p>Reduce applies a high order function against all the elements in a collection and then returns a single result.</p>
<p>Given a list of numbers 1-5, add them all together and print out the answer</p>
<p>Java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> sum <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> numbers <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> number <span style="color: #339933;">:</span> numbers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    sum <span style="color: #339933;">+=</span> number<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>C#</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console.<span style="color: #006633;">WriteLine</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span>.<span style="color: #006633;">Aggregate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #009900;">&#40;</span>accumulator, x<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=&gt;</span> accumulator <span style="color: #339933;">+</span> x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>F#</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[1..5] |&gt; List.fold_left (+) 0 |&gt; printfn &quot;%d&quot;;;</pre></div></div>

<h3>In Summary</h3>
<p>I was surprised that we could achieve these results in relatively few lines of Java. The C# and F# versions are still more concise but the Java version isn't too bad. The <a href="http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/CollectionUtils.html#select(java.util.Collection,%20org.apache.commons.collections.Predicate)">Apache Commons Library has a class</a> which allows you to write these in a functional way but the need to use anonymous methods means it's not as clean as what you can achieve in C# and F#.</p>
<p>I think there is still a bit of a mindset switch to make from thinking procedurally about these things to thinking in a way that allows you to make the most of functional programming concepts.</p>
<p>Keeping the code as declarative as possible and reducing the amount of state in our code are the most obvious things I have learned so far from playing with F#.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/01/19/f-vs-c-vs-java-functional-collection-parameters/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Coding Dojo #5: Uno</title>
		<link>http://www.markhneedham.com/blog/2009/01/08/coding-dojo-5-uno/</link>
		<comments>http://www.markhneedham.com/blog/2009/01/08/coding-dojo-5-uno/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 13:41:57 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding Dojo]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=830</guid>
		<description><![CDATA[We ran our 5th coding dojo on Thursday night, writing the card game Uno in Java. We didn't all know the rules so this video explained it &#8211; surely a parody but you never know! The Format We used the Randori approach again with 6 people participating for the majority of the session. Everyone paired [...]]]></description>
			<content:encoded><![CDATA[<p>We ran our 5th coding dojo on Thursday night, writing the card game <a href="http://en.wikipedia.org/wiki/Uno_(game)">Uno</a> in Java. We didn't all know the rules so <a href="http://www.youtube.com/watch?v=biNXzYOxmb8">this video</a> explained it &#8211; surely a parody but you never know!</p>
<h3>The Format</h3>
<p>We used the <a href="http://codingdojo.org/cgi-bin/wiki.pl?RandoriKata">Randori</a> approach again with 6 people participating for the majority of the session. Everyone paired with everyone else at least once and sometimes a couple of times.</p>
<p>We had the pair driving at the front of the room and everyone else further back to stop the tendency of observers to whiteboard stuff.</p>
<h3>What We Learnt</h3>
<ul>
<li><strong>Modeling games is really good for practicing design skills</strong>. Most people had played the game so we had domain experts who could use their knowledge to help drive out the API of the various classes. We didn't get to the scoring part of the game in the time available but it was quite cool to see our code with all the terms detailed in Wikipedia's entry on the term.</li>
<li>We managed to <strong>drive the design much more effectively</strong> than we have done on previous sessions. The flexibility to move between classes depending on where it made most sense to test from next was finally there and we didn't end up with the problem we've had on previous sessions where we ended up with coarsely grained tests and then tried to code the whole application in one go. </li>
<li>It was quite painful for me personally having to manually perform operations on collections in Java rather than having the selection of <a href="http://www.markhneedham.com/blog/2008/12/17/functional-collection-parameters-in-c/">functional operators</a> that are available in C# 3.0.  </li>
<li>It wasn't a new learning but I've noticed in my project work that I've become a lot more keen to <strong>keep the steps really small</strong> &#8211; there is a bit of pressure on you to do this in a dojo situation and I think it's just continued over from there. Every time I try to be too clever and take a big step something inevitably doesn't work and I end up doing the small steps anyway. It's also a lot of fun coding in this type of environment and watching how others approach problems and how they pair with each other. If you get a chance to attend a dojo I think it'd definitely be worthwhile.</li>
</ul>
<h3>Other Dojo Thoughts</h3>
<ul>
<li>Some ideas for future coding dojos that we discussed were:
<ul>
<li>Concurrency &#8211; using the <a href="http://code.google.com/p/retlang/">Retlang</a>/<a href="http://code.google.com/p/jetlang/">Jetlang</a> libraries</li>
<li>Do some stuff with <a href="http://code.google.com/p/webdriver/">Web Driver</a></li>
<li>Modeling games</li>
<li>Taking an open source project and refactoring it</li>
</ul>
</li>
<li>I notice there are a couple of sessions of coding/coding dojos planned for Jason Gorman's <a href="http://parlezuml.com/softwarecraftsmanship/sessions/programme.htm">Software Craftsmanship conference</a>. It will be interesting to see how those work out, especially if there are high numbers of participants. We've always had a fairly small number of people involved which I think has helped to keep everyone involved. I'm not convinced it would be effective with many more participants.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/01/08/coding-dojo-5-uno/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JUnit Theories: First Thoughts</title>
		<link>http://www.markhneedham.com/blog/2008/12/12/junit-theories-first-thoughts/</link>
		<comments>http://www.markhneedham.com/blog/2008/12/12/junit-theories-first-thoughts/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 14:34:17 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=730</guid>
		<description><![CDATA[One of my favourite additions to JUnit 4.4 was the @Theory annotation which allows us to write parameterised tests rather than having to recreate the same test multiple times with different data values or creating one test and iterating through our own collection of data values. Previously, as far as I'm aware, it was only [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favourite additions to <a href="http://www.testingreflections.com/node/view/5736">JUnit 4.4</a> was the @Theory annotation which allows us to write parameterised tests rather than having to recreate the same test multiple times with different data values or creating one test and iterating through our own collection of data values.</p>
<p>Previously, as far as I'm aware, it was only possible to parameterise tests by using the <a href="http://testng.org/doc/">TestNG</a> library which has some nice ideas around grouping tests but had horrible reporting the last time I used it.</p>
<p>To create parameterisable tests using Theories we need to write some code like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.experimental.theories.DataPoints</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.experimental.theories.Theories</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.experimental.theories.Theory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>Theories.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeTest <span style="color: #009900;">&#123;</span>
	@Theory
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testTheNewTheoriesStuff<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// test which involves int value	</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> @DataPoints <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> values <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The 'testTheNewTheoriesStuff' Theory is then executed with each of the values defined in the values array decorated with the @DataPoints annotation.</p>
<p>The error message reported for a failure is reasonably good and makes it quite easy to figure out which one of the data points causes the problem. </p>
<p>An example error message for an assertion which failed inside a theory might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">org.junit.experimental.theories.internal.ParameterizedAssertionError: testTheNewTheoriesStuff(values[1])</pre></div></div>

<p>It's 0 indexed so this error message tells us that there was an error when running the theory with the 2nd data point, therefore allowing us to go and work out why that's the case and fix it.</p>
<p>This approach is actually particularly useful for testing the scope in which classes we pull from a dependency injection container are available from in our application.</p>
<p>Another potential use for this would be to test the edge cases of our classes &#8211; perhaps this would work best if we can randomise the data it uses.</p>
<p>This seems to be more the approach Microsoft are taking with the the <a href="http://research.microsoft.com/Pex/">Pex</a> framework, a similar idea in the .NET space.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/12/12/junit-theories-first-thoughts/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Logging with Pico Container</title>
		<link>http://www.markhneedham.com/blog/2008/11/11/logging-with-pico-container/</link>
		<comments>http://www.markhneedham.com/blog/2008/11/11/logging-with-pico-container/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 14:08:16 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[picocontainer]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=600</guid>
		<description><![CDATA[One thing that we've been working on recently is the logging for our current code base. Nearly all the objects in our system are being created by Pico Container so we decided that writing an interceptor that hooked into Pico Container would be the easiest way to intercept and log any exceptions throw from our [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that we've been working on recently is the logging for our current code base.</p>
<p>Nearly all the objects in our system are being created by <a href="http://picocontainer.org/">Pico Container</a> so we decided that writing an interceptor that hooked into Pico Container would be the easiest way to intercept and log any exceptions throw from our code.</p>
<p>Our initial Googling led us to the <a href="http://picocontainer.org/interception.html">AOP Style Interception</a> page on the Pico website which detailed how we could create a static proxy for a class that we put in the container.</p>
<p>The code to do this was as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        DefaultPicoContainer pico <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultPicoContainer<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Intercepting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pico.<span style="color: #006633;">addComponent</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span>, ConcreteInterceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Intercepted intercepted <span style="color: #339933;">=</span> pico.<span style="color: #006633;">getComponentAdapter</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAdapterOfType</span><span style="color: #009900;">&#40;</span>Intercepted.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intercepted.<span style="color: #006633;">addPreInvocation</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> InterceptableReporter<span style="color: #009900;">&#40;</span>intercepted.<span style="color: #006633;">getController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intercepted.<span style="color: #006633;">addPostInvocation</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> InterceptableReporter<span style="color: #009900;">&#40;</span>intercepted.<span style="color: #006633;">getController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        Interceptable a1 <span style="color: #339933;">=</span> pico.<span style="color: #006633;">getComponent</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        a1.<span style="color: #006633;">methodThatThrowsException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Interceptable <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> methodThatThrowsException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">    <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;">class</span> InterceptableReporter <span style="color: #000000; font-weight: bold;">implements</span> Interceptable <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> Intercepted.<span style="color: #006633;">Controller</span> controller<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> InterceptableReporter<span style="color: #009900;">&#40;</span>Intercepted.<span style="color: #006633;">Controller</span> controller<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;">controller</span> <span style="color: #339933;">=</span> controller<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> methodThatThrowsException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error happened&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>While this approach works, the problem is that we need to define an individual proxy for every class that we want to intercept. It works as  a strategy if we just need to intercept a few classes but not on a larger scale.</p>
<p>Luckily it is possible to create a <a href="http://lizdouglass.wordpress.com/2008/08/31/small-things-amuse-small%E2%80%A6-hmph-well-anyway%E2%80%A6/">dynamic proxy</a> on the container so that we can intercept all the objects without having to create a static proxy for each one.</p>
<p>The code to do this was as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        DefaultPicoContainer pico <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultPicoContainer<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LoggingAwareByDefault<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pico.<span style="color: #006633;">addComponent</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span>, ConcreteInterceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        Interceptable interceptable <span style="color: #339933;">=</span> pico.<span style="color: #006633;">getComponent</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        interceptable.<span style="color: #006633;">methodThatThrowsException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.LogFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.Characteristics</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.ComponentAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.ComponentMonitor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.LifecycleStrategy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.Parameter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.behaviors.AbstractBehaviorFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Properties</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoggingAwareByDefault <span style="color: #000000; font-weight: bold;">extends</span> AbstractBehaviorFactory <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> <span style="color: #003399;">String</span> DO_NOT_LOG_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;support-team-opt-out&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Properties</span> DO_NOT_LOG <span style="color: #339933;">=</span> Characteristics
            .<span style="color: #006633;">immutable</span><span style="color: #009900;">&#40;</span>DO_NOT_LOG_NAME, Characteristics.<span style="color: #000066; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> ComponentAdapter<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> createComponentAdapter<span style="color: #009900;">&#40;</span>ComponentMonitor componentMonitor,
                                                          LifecycleStrategy lifecycleStrategy,
                                                          <span style="color: #003399;">Properties</span> componentProperties,
                                                          <span style="color: #003399;">Object</span> componentKey, Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> componentImplementation,
                                                          Parameter... <span style="color: #006633;">parameters</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>removePropertiesIfPresent<span style="color: #009900;">&#40;</span>componentProperties, DO_NOT_LOG<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;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">createComponentAdapter</span><span style="color: #009900;">&#40;</span>componentMonitor, lifecycleStrategy, componentProperties, componentKey,
                    componentImplementation, parameters<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>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> LoggingAware<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">createComponentAdapter</span><span style="color: #009900;">&#40;</span>componentMonitor,
                    lifecycleStrategy, componentProperties, componentKey,
                    componentImplementation, parameters<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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.Log</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.ComponentAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.ComponentMonitor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.PicoContainer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.behaviors.HiddenImplementation</span><span style="color: #339933;">;</span>
&nbsp;
<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.lang.reflect.Method</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoggingAware<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">extends</span> HiddenImplementation <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> LoggingAware<span style="color: #009900;">&#40;</span><span style="color: #003399;">ComponentAdapter</span> delegate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>delegate<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;">protected</span> <span style="color: #003399;">Object</span> invokeMethod<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> componentInstance, <span style="color: #003399;">Method</span> method, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args, PicoContainer container<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
        ComponentMonitor componentMonitor <span style="color: #339933;">=</span> currentMonitor<span style="color: #009900;">&#40;</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>
            componentMonitor.<span style="color: #006633;">invoking</span><span style="color: #009900;">&#40;</span>container, <span style="color: #000000; font-weight: bold;">this</span>, method, componentInstance<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">long</span> startTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Object</span> object <span style="color: #339933;">=</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>componentInstance, args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            componentMonitor.<span style="color: #006633;">invoked</span><span style="color: #009900;">&#40;</span>container,
                                     <span style="color: #000000; font-weight: bold;">this</span>,
                                     method, componentInstance, <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> startTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> object<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: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InvocationTargetException</span> ite<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            componentMonitor.<span style="color: #006633;">invocationFailed</span><span style="color: #009900;">&#40;</span>method, componentInstance, ite<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// log the error</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">throw</span> ite.<span style="color: #006633;">getTargetException</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>From what I recall from looking at the source code I think in order to create a proxy around an object it needs to implement an interface otherwise the proxy will not be created.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/11/11/logging-with-pico-container/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hamcrest Matchers &#8211; Make the error message clear</title>
		<link>http://www.markhneedham.com/blog/2008/11/08/hamcrest-matchers-make-the-error-message-clear/</link>
		<comments>http://www.markhneedham.com/blog/2008/11/08/hamcrest-matchers-make-the-error-message-clear/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 16:46:59 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[hamcrest]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=590</guid>
		<description><![CDATA[We have been making good use of Hamcrest matchers on my current project for making assertions, and have moved almost entirely away from the more traditional JUnit assertEquals approach. There are several reasons why I find the Hamcrest matcher approach to be more productive &#8211; it's more flexible, more expressive and when an assertion fails [...]]]></description>
			<content:encoded><![CDATA[<p>We have been making good use of <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> matchers on my current project for making assertions, and have moved almost entirely away from the more traditional JUnit assertEquals approach.</p>
<p>There are several reasons why I find the Hamcrest matcher approach to be more productive &#8211; it's more flexible, more expressive and when an assertion fails we have a much better idea about why it has failed than if we use a JUnit assertion for example.</p>
<p>This applies especially when we get a test failing as part of the build as compared to running a test from the IDE where the source code is close at hand and non descriptive error messages may not be such a problem.</p>
<p>It therefore makes sense when writing custom Hamcrest matchers to ensure that we do indeed provide a clear error message so that it is obvious how to fix the test.</p>
<p>The convention seems to be that we should first state the static method name of the matcher and then in brackets list the expected arguments.</p>
<p>To give an example from a matcher we wrote yesterday:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hamcrest.Description</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hamcrest.Factory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hamcrest.TypeSafeMatcher</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContainsAllOf<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">extends</span> TypeSafeMatcher<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> messages<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ContainsAllOf<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">messages</span><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;">messages</span> <span style="color: #339933;">=</span> messages<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> describeTo<span style="color: #009900;">&#40;</span>Description description<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        description.<span style="color: #006633;">appendText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;containsAllOf(&quot;</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> message <span style="color: #339933;">:</span> messages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            description.<span style="color: #006633;">appendText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            description.<span style="color: #006633;">appendValue</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        description.<span style="color: #006633;">appendText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Factory
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> ContainsAllOf containsAllOf<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">messages</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;">new</span> ContainsAllOf<span style="color: #009900;">&#40;</span>messages<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;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> matchesSafely<span style="color: #009900;">&#40;</span>T t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> contains<span style="color: #009900;">&#40;</span>t, messages<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;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> contains<span style="color: #009900;">&#40;</span>T t, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> messages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">boolean</span> containsAllMessages <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</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> message <span style="color: #339933;">:</span> messages<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: #339933;">!</span>t.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>message<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;">return</span> <span style="color: #000066; font-weight: bold;">false</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;">return</span> containsAllMessages<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If we call this in our test with a value that doesn't exist:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">assertThat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mark's cool message&quot;</span>, containsAllOf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mark&quot;</span>, <span style="color: #0000ff;">&quot;cool&quot;</span>, <span style="color: #0000ff;">&quot;message&quot;</span>, <span style="color: #0000ff;">&quot;notThere&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Running the test results in the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.AssertionError: 
Expected: containsAllOf(,&quot;mark&quot;,&quot;cool&quot;,&quot;message&quot;,&quot;notThere&quot;)
     got: &quot;mark's cool message&quot;</pre></div></div>

<p>We can easily see what the problem is and how to go about fixing it, which I feel is the most important thing when it comes to test assertions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/11/08/hamcrest-matchers-make-the-error-message-clear/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keep Java checked exceptions in a bounded context</title>
		<link>http://www.markhneedham.com/blog/2008/10/23/keep-java-checked-exceptions-in-a-bounded-context/</link>
		<comments>http://www.markhneedham.com/blog/2008/10/23/keep-java-checked-exceptions-in-a-bounded-context/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 11:22:26 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[checked-exceptions]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=533</guid>
		<description><![CDATA[One of the features that I dislike in Java compared to C# is checked exceptions. For me an exception is about a situation which is exceptional, and if we know that there is a possibility of it happening and even have that possibility defined in our code then it doesn't seem all that exceptional to [...]]]></description>
			<content:encoded><![CDATA[<p>One of the features that I dislike in Java compared to C# is <a href="http://radio.weblogs.com/0122027/stories/2003/04/01/JavasCheckedExceptionsWereAMistake.html">checked exceptions</a>.</p>
<p>For me an exception is about a situation which is <strong>exceptional</strong>, and if we know that there is a possibility of it happening and even have that possibility defined in our code then it doesn't seem all that exceptional to me.</p>
<p>Having said that they do at least provide information which you can't help but notice about what can go wrong when you make a call to a particular method.</p>
<p>The problem is that often these checked exceptions just get passed on &#8211; i.e. not handled &#8211; until we end up with an exception on the page the user sees which is completely irrelevant to the action they are trying to undertake.</p>
<p>To give an example, we have been using the <a href="http://www.ognl.org/">OGNL</a> library to hydrate some objects for testing using the builder pattern.</p>
<p>We have something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FooBuilder <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> bar<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> FooBuilder setBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> bar<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;">bar</span> <span style="color: #339933;">=</span> bar<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Foo toFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setValue<span style="color: #009900;">&#40;</span>foo, <span style="color: #0000ff;">&quot;bar&quot;</span>, bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> foo<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object, <span style="color: #003399;">String</span> propertyName, <span style="color: #003399;">Object</span> propertyValue<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            OgnlWrapper.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span>object, propertyName, propertyValue<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>OgnlException 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> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</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>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.DefaultMemberAccess</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.MemberAccess</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.Ognl</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.OgnlContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.OgnlException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OgnlWrapper <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object, <span style="color: #003399;">String</span> propertyName, <span style="color: #003399;">Object</span> propertyValue<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> OgnlException <span style="color: #009900;">&#123;</span>
        Ognl.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span>propertyName, createOgnlContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, object, propertyValue<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;">private</span> <span style="color: #000000; font-weight: bold;">static</span> OgnlContext createOgnlContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MemberAccess memberAccess <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultMemberAccess<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>
        OgnlContext ognlContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> OgnlContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ognlContext.<span style="color: #006633;">setMemberAccess</span><span style="color: #009900;">&#40;</span>memberAccess<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> ognlContext<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We can then build an instance of 'Foo' like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FooBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setBar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;barValue&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What is interesting here is not the OGNL library in itself but the checked 'OgnlException' which the 'Ognl.setValue(&#8230;)' method defines.</p>
<p>If I am using the FooBuilder I don't care how the Foo object is created, all I care is that I get it. Therefore we don't want to bubble the implementation details of how we are creating the object upwards.</p>
<p>I only care about the OgnlException if I am calling the OgnlWrapper and therefore that is where the exception should be caught and then rethrown as a Runtime exception.</p>
<p>I like to refer to this area of OgnlWrapper callees as being a <a href="http://domaindrivendesign.org/discussion/messageboardarchive/BoundedContext.html">bounded context</a> &#8211; that exception should only be applicable in that particular area and beyond that it should not exist.</p>
<p>Doing this allows us more flexibility around the way we implement things. If I decide in the future to use a different library instead of OGNL to do the same job I don't need to worry that the callees of FooBuilder will all need to be updated. I can just make the change inside FooBuilder and that's it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/10/23/keep-java-checked-exceptions-in-a-bounded-context/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tomcat &#8211; No caching of RESTlet resources for Firefox</title>
		<link>http://www.markhneedham.com/blog/2008/10/22/tomcat-no-caching-of-pages-for-firefox/</link>
		<comments>http://www.markhneedham.com/blog/2008/10/22/tomcat-no-caching-of-pages-for-firefox/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 12:00:33 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[no-cache]]></category>
		<category><![CDATA[no-store]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=525</guid>
		<description><![CDATA[One problem that we've been trying to solve today is how to make a RESTlet resource non cacheable. The reason for this is that when a user logs out of the system and then hits the back button they shouldn't be able to see that page, but instead should see the login form. After several [...]]]></description>
			<content:encoded><![CDATA[<p>One problem that we've been trying to solve today is how to make a <a href="http://www.restlet.org/">RESTlet</a> resource non cacheable.</p>
<p>The reason for this is that when a user logs out of the system and then hits the back button they shouldn't be able to see that page, but instead should see the login form.</p>
<p>After several hours of trawling Google and trying out various different suggestions we <a href="http://www.experts-exchange.com/Software/Server_Software/Web_Servers/Apache/Q_20880931.html?qid=20880931">came across the idea</a> of setting 'cache-control' with the value 'no-store' in the response headers.</p>
<p>The code to make this happen is as follows (use inside a class which extends <a href="http://www.restlet.org/documentation/1.0/api/org/restlet/resource/Resource.html">Resource</a>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">HttpResponse response <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpResponse<span style="color: #009900;">&#41;</span> getResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Series<span style="color: #339933;">&lt;</span>Parameter<span style="color: #339933;">&gt;</span> headers <span style="color: #339933;">=</span> response.<span style="color: #006633;">getHttpCall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResponseHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
headers.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cache-control&quot;</span>, <span style="color: #0000ff;">&quot;no-store&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The important part in this example is the last line. As long as it's added to the Http Response Headers that response should no longer be cached.</p>
<p>A bit of research revealed that Internet Explorer may change the 'no-store' value to 'no-cache' so I'm not sure if this will work for that browser.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/10/22/tomcat-no-caching-of-pages-for-firefox/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java vs .NET: An Overview</title>
		<link>http://www.markhneedham.com/blog/2008/10/15/java-vs-net-an-overview/</link>
		<comments>http://www.markhneedham.com/blog/2008/10/15/java-vs-net-an-overview/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 14:09:05 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=491</guid>
		<description><![CDATA[A couple of months ago my colleague Mark Thomas posted about working on a C# project after 10 years working in Java, and being someone who has worked on projects in both languages fairly consistently (3 Java projects, 2 .NET projects) over the last two years I thought it would be interesting to do a [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago my colleague Mark Thomas posted about <a href="http://markthomas.info/blog/?p=47">working on a C# project after 10 years working in Java</a>, and being someone who has worked on projects in both languages fairly consistently (3 Java projects, 2 .NET projects) over the last two years I thought it would be interesting to do a comparison between the two.</p>
<p>The standard ThoughtWorks joke is that you just need to remember to capitalise the first letter of method names in C# and then you're good to go but I think there's more to it than that.</p>
<h3>The Language &#038; Framework</h3>
<p>There is really not much difference between the syntax of Java and C# and I'm not that interested in going into it it massive detail here. There are <a href="http://www.javacamp.org/javavscsharp/">other</a> <a href="http://www.25hoursaday.com/CsharpVsJava.html">websites</a> which cover it in more detail.</p>
<p>Language features wise C# seems to be marginally ahead &#8211; the introduction of lambda expressions, implicitly typed local variables and extension methods in <a href="http://www.developer.com/net/csharp/article.php/3561756">C# 3.0</a> is something not yet matched in Java.</p>
<p>From my experience C#/.NET has much better support for front end rich GUI applications (WinForms, WPF) while Java is probably better for back end work. When it comes to web applications Java probably holds a marginal edge although the soon to be production released <a href="http://www.asp.net/mvc/">ASP.NET MVC framework</a> is a very nice piece of kit.</p>
<p>I have no data to justify saying that, merely thoughts based on experience, but from conversations with friends who work in investment banking I have learned that this is the way the two languages are used there as well.</p>
<h3>Other language support</h3>
<p>If you are looking for language support on the respective platforms beyond Java/C#, Java probably has a slight edge.</p>
<p><a href="http://groovy.codehaus.org/">Groovy</a> is a dynamic lanuage with a Java style syntax and should therefore be easier for Java developers to pick up. I'm not aware of a dynamic language with C# style syntax for .NET although <a href="http://boo.codehaus.org/">Boo</a> is an alternative which compiles to run on the <a href="http://boo.codehaus.org/Common+Language+Infrastructure">Common Language Infrastructure</a>.</p>
<p>If you need Ruby support Java has <a href="http://jruby.codehaus.org/">JRuby</a> while .NET has <a href="http://www.ironruby.net/">IronRuby</a>. JRuby is the more mature of the two options here. If Python is what you need then both contenders compete here too with Java's offering of <a href="http://www.jython.org/Project/">Jython</a> and .NET's <a href="http://www.codeplex.com/IronPython">IronPython</a>.</p>
<p>Functional language wise .NET has a CTP release of <a href="http://research.microsoft.com/fsharp/fsharp.aspx">F#</a>, while Java has support for <a href="http://www.scala-lang.org/">Scala</a>.</p>
<h3>Use of 3rd party APIs/Open Source Software</h3>
<p>I've found that in the Java projects that I've worked on use significantly more open source software than the .NET ones. I'm yet to be convinced that this is necessarily a good thing although my Java colleagues are confident that it is.</p>
<p>To give an example, there are multiple different Java libraries for Xml parsing  whereas in C# everyone just uses the default one that's provided.</p>
<p>This provides the opportunity to learn new and better ways of doing things on the one hand, but the potential to spend serious amounts of time evaluating which tool to use instead of just getting on with it on the other.</p>
<p>From a Java perspective it certainly provides extra challenges in trying to get your applications integrated with the range of different application and web servers on the market. In .NET it would simply be a case of getting it to work on IIS &#8211; of course easier said than done!</p>
<h3>IDEs</h3>
<p>I think Java clearly leads in this area with <a href="http://www.jetbrains.com/idea/">IntelliJ</a> out ahead of anything else I've ever worked with. <a href="http://www.eclipse.org/">Eclipse</a> is a popular open source alternative but for me it is far less intuitive to use than IntelliJ.</p>
<p>Visual Studio only becomes usable once <a href="http://www.jetbrains.com/resharper/">Resharper</a> is installed but when that's done it becomes better than eclipse if not quite as usable as IntelliJ. My colleague Pat Kua also <a href="http://www.thekua.com/atwork/2007/05/31/speeding-up-visual-studio-2005/">listed some ideas to make it run even better</a>. <a href="http://www.icsharpcode.net/OpenSource/SD/">SharpDevelop</a> is a free IDE for .NET development although I haven't used it so I'm not sure how good it is.</p>
<h3>Build and Deployment</h3>
<p>Partly due to its better support of Ruby, Java has a much wider range of tools for working with the build. </p>
<p>In .NET <a href="http://nant.sourceforge.net/">NAnt</a> is the only serious contender, and although <a href="http://msdn.microsoft.com/en-us/library/0k6kkbsd.aspx">msbuild</a> is often used to handle the compiling of the code its verbosity of non intuitive approach means I can't imagine recommending it for a whole build file.</p>
<p>Java wise we have <a href="http://ant.apache.org/">Ant</a>, <a href="http://maven.apache.org/">Maven</a>, a Groovy based wrapper around Ant called <a href="http://gant.codehaus.org/">Gant</a>, the Ruby based <a href="http://incubator.apache.org/buildr/">buildr</a> and the dependency management tool <a href="http://ant.apache.org/ivy/">Ivy</a>.</p>
<h3>Communities</h3>
<p>From my experience the community around .NET is more accessible to your average developer than what I've noticed in the Java world.</p>
<p>The <a href="http://altdotnet.org/">Alt.NET</a> group is an initiative <a href="http://www.infoq.com/news/2007/10/fowler-alt.net">started last year</a> by several of the leading lights in the .NET world and aims to make the world of .NET development a better and more productive place.</p>
<p>Java has the <a href="http://jcp.org/en/home/index">Java Community Process</a> driving it forward from a community perspective and perhaps due to the lower reliance on the drag and drop tools which are encouraged by Microsoft tools, the standard of your average Java developer may in fact be higher.</p>
<p>When it comes to finding the answers to questions both are mainstream enough that this is fairly easy.</p>
<h3>Overall</h3>
<p>I've tried to cover some of the areas which I considered important when using these two approaches. I'm sure there are some comparisons I have missed out so it would be interesting to hear from others who have worked on both platforms.</p>
<p>This is all written from my knowledge (and a bit of research) so if I've missed anything please mention it in the comments.</p>
<p>*Updated*<br />
The paragraph about 'Other Language Support' was updated to reflect Robin Clowers' comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/10/15/java-vs-net-an-overview/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Connecting to LDAP server using OpenDS in Java</title>
		<link>http://www.markhneedham.com/blog/2008/09/29/connecting-to-ldap-server-using-opends-in-java/</link>
		<comments>http://www.markhneedham.com/blog/2008/09/29/connecting-to-ldap-server-using-opends-in-java/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 13:27:37 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[opends]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=393</guid>
		<description><![CDATA[A colleague and I have spent the past couple of days spiking solutions for connecting to LDAP servers from Ruby. We decided that the easiest way to do this is by using OpenDS, an open source directory service based on LDAP. One option we came up with for doing this was to make use of [...]]]></description>
			<content:encoded><![CDATA[<p>A colleague and I have spent the past couple of days spiking solutions for connecting to LDAP servers from Ruby.</p>
<p>We decided that the easiest way to do this is by using <a href="https://opends.dev.java.net/">OpenDS</a>, an open source directory service based on LDAP.</p>
<p>One option we came up with for doing this was to make use of the Java libraries for connecting to the LDAP server and then calling through to these from our Ruby code using the <a href="http://rjb.rubyforge.org/">Ruby Java Bridge</a>.</p>
<p>This post is not about Ruby, but about how we did it in Java to check that the idea was actually feasible.</p>
<p>The interfaces and classes we need to use to do this are not very <a href="http://domaindrivendesign.org/discussion/messageboardarchive/IntentionRevealingInterfaces.html">obvious</a> so it was a little bit fiddly getting it to work. The following code seems to do the trick though:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.opends.server.admin.client.ldap.JNDIDirContextAdaptor</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.directory.DirContext</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.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.ldap.LdapName</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.ldap.InitialLdapContext</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.jndi.ldap.LdapCtx</span><span style="color: #339933;">;</span>
&nbsp;
<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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OpenDs <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<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;">DirContext</span> dirContext <span style="color: #339933;">=</span> createLdapContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        JNDIDirContextAdaptor adaptor <span style="color: #339933;">=</span>  JNDIDirContextAdaptor.<span style="color: #006633;">adapt</span><span style="color: #009900;">&#40;</span>dirContext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// do other stuff with the adaptor</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;">DirContext</span> createLdapContext<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: #003399;">Hashtable</span> env <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Hashtable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        env.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">INITIAL_CONTEXT_FACTORY</span>, <span style="color: #0000ff;">&quot;com.sun.jndi.ldap.LdapCtxFactory&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        env.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">PROVIDER_URL</span>, <span style="color: #0000ff;">&quot;ldap://localhost:389&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        env.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">SECURITY_AUTHENTICATION</span>, <span style="color: #0000ff;">&quot;simple&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        env.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">SECURITY_PRINCIPAL</span>, <span style="color: #0000ff;">&quot;cn=Directory Manager&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        env.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">SECURITY_CREDENTIALS</span>, <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InitialLdapContext</span><span style="color: #009900;">&#40;</span>env, <span style="color: #000066; font-weight: bold;">null</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></td></tr></table></div>

<p>Some points about the code:</p>
<ul>
<li>
Port 389 is the default port for the LDAP server so unless it's in use this is probably the port you need to connect to.</li>
<li>'Directory Manager' is the default 'Root User DN' that was setup when we installed OpenDS although there is more information on what this value may need to be on the <a href="http://java.sun.com/products/jndi/tutorial/ldap/security/ldap.html">official documentation</a>.</li>
<li>We originally tried to connect using <a href="http://www.opends.org/promoted-builds/1.0.0/javadoc/org/opends/server/admin/client/ldap/JNDIDirContextAdaptor.html">JNDIDirContextAdaptor.simpleBind(&#8230;)</a> but it didn't seem to work for us so we went with the JNDIDirContextAdaptor.adapt(&#8230;) approach.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/09/29/connecting-to-ldap-server-using-opends-in-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
