<?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>Source Allies Blog &#187; Java</title>
	<atom:link href="http://blogs.sourceallies.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:40:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hibernate Logging</title>
		<link>http://blogs.sourceallies.com/2012/01/hibernate-logging/</link>
		<comments>http://blogs.sourceallies.com/2012/01/hibernate-logging/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 14:54:22 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[interceptor]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2541</guid>
		<description><![CDATA[Through the years I&#8217;ve encountered a recurring requirement.  Clients need to log changes to the database for auditing and legal purposes.  To satisfy this requirement you could add logging to every save/update/delete call in your code.  Or better yet, you could create an aspect that wraps these calls.  While these would [...]]]></description>
			<content:encoded><![CDATA[<p>Through the years I&#8217;ve encountered a recurring requirement.  Clients need to log changes to the database for auditing and legal purposes.  To satisfy this requirement you could add logging to every save/update/delete call in your code.  Or better yet, you could create an <a href="http://goo.gl/DNo6a">aspect</a> that wraps these calls.  While these would certainly work Hibernate provides a convenient <a href="http://goo.gl/K0pJr">interceptor</a>.</p>
<p>In this article I will show you how to add a simple logger to Hibernate.<br />
<span id="more-2541"></span></p>
<h2>Start with a Test</h2>
<p>As an avid <a href="http://goo.gl/1KEI0">TDD </a> practitioner I will of coarse start with a test.  Here is the final version of the test that I used to drive my code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">mock</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">verify</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">verifyNoMoreInteractions</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.Resource</span><span style="color: #339933;">;</span>
&nbsp;
<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.hibernate.Criteria</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.classic.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<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.runner.RunWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.ContextConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.junit4.SpringJUnit4ClassRunner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.annotation.Transactional</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.Person</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>SpringJUnit4ClassRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@ContextConfiguration
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateInterceptorTest <span style="color: #009900;">&#123;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> SessionFactory sessionFactory<span style="color: #339933;">;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> HibernateInterceptor hibernateInterceptor<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Session currentSession<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Log logger<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Date</span> NOW <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Before
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		logger <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Log.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession <span style="color: #339933;">=</span> sessionFactory.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		hibernateInterceptor.<span style="color: #006633;">setLogger</span><span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSaveLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testUpdateLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Person firstPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		firstPerson.<span style="color: #006633;">setFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>firstPerson<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Updating Person [firstName=testFirst2, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testDeleteLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Person firstPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		delete<span style="color: #009900;">&#40;</span>firstPerson<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Deleting Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> saveOrUpdate<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">saveOrUpdate</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession.<span style="color: #006633;">flush</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;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> delete<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Criteria criteria <span style="color: #339933;">=</span> currentSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> criteria.<span style="color: #006633;">list</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;">return</span> people<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Person createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> person<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I used a mock logger to gather information from the interceptor.</p>
<h2>Spring Configuration</h2>
<p>Here is the configuration required for the interceptor.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                ...
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;entityInterceptor&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;hibernateInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         ...	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:component-scan</span> <span style="color: #000066;">base-package</span>=<span style="color: #ff0000;">&quot;com.sourceallies&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        ...</pre></div></div>

<p>The &#8216;entityInterceptor&#8217; property on the &#8217;sessionFactory&#8217; takes a Hibernate interceptor.</p>
<h2>Entity</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
&nbsp;
	@Id
	@GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>1L<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
...
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Person [firstName=&quot;</span> <span style="color: #339933;">+</span> firstName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, lastName=&quot;</span>
				<span style="color: #339933;">+</span> lastName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, birthDate=&quot;</span> <span style="color: #339933;">+</span> birthDate <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For this example you need to implement the &#8216;toString&#8217; method on the entity being logged.</p>
<h2>Interceptor</h2>
<p>Here is the interceptor code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
&nbsp;
<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.apache.commons.logging.LogFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.EmptyInterceptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.type.Type</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Component</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Component</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateInterceptor <span style="color: #000000; font-weight: bold;">extends</span> EmptyInterceptor <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Log logger <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>HibernateInterceptor.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onDelete<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> state,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Deleting&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onDelete</span><span style="color: #009900;">&#40;</span>entity, id, state, propertyNames, types<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onFlushDirty<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id,
			<span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> currentState, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> previousState,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Updating&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onFlushDirty</span><span style="color: #009900;">&#40;</span>entity, id, currentState, previousState,
				propertyNames, types<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onSave<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> state,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onSave</span><span style="color: #009900;">&#40;</span>entity, id, state, propertyNames, types<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: #000066; font-weight: bold;">void</span> setLogger<span style="color: #009900;">&#40;</span>Log logger<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;">logger</span> <span style="color: #339933;">=</span> logger<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> log<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> action, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span>action <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> entity <span style="color: #339933;">+</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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While the &#8216;onSave&#8217; and &#8216;onDelete&#8217; methods are clearly named the &#8216;onUpdate&#8217; method appears to be missing.  With a little research it quickly becomes obvious that &#8216;onFlushDirty&#8217; is used for updates.</p>
<p>Logging database activity is this simple.</p>
<p>Here is the complete code. <a href="http://blogs.sourceallies.com/wp-content/uploads/2012/01/HibernateLogging.zip" > Hibernate Logging Zip </a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2012/01/hibernate-logging/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Back to Basics: hashCode() &amp; equals()</title>
		<link>http://blogs.sourceallies.com/2011/12/back-to-basics-hashcode-equals/</link>
		<comments>http://blogs.sourceallies.com/2011/12/back-to-basics-hashcode-equals/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 15:53:22 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[guava]]></category>
		<category><![CDATA[hashCode]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2598</guid>
		<description><![CDATA[So we all know that if we implement equals() we must override hashCode() too.  But why?  The best explanation of this commandment can be found in Effective Java (2nd Edition) starting on page 45.  

&#8230; Failure to do so will result in a violation of the general contract for Object.hashCode, which will
prevent [...]]]></description>
			<content:encoded><![CDATA[<p>So we all know that if we implement equals() we must override hashCode() too.  But why?  The best explanation of this commandment can be found in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a> starting on page 45.  </p>
<p><em><br />
&#8230; Failure to do so will result in a violation of the general contract for Object.hashCode, which will<br />
prevent your class from functioning properly in conjunction with all hash-based<br />
collections, including HashMap, HashSet, and Hashtable. (Bloch, Effective Java, 2nd Ed.)<br />
</em><br />
<span id="more-2598"></span><br />
If you&#8217;re like me, you like to see a problem in running code.  Well here you go.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testHashMapRetrieval_WithoutHashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		PersonWithoutHashCode person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithoutHashCode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersonWithoutHashCode person2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithoutHashCode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Map<span style="color: #339933;">&lt;</span>PersonWithoutHashCode, String<span style="color: #339933;">&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>PersonWithoutHashCode, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>person, <span style="color: #0000ff;">&quot;friend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertNull<span style="color: #009900;">&#40;</span>map.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>person2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testHashMapRetrieval_WithHashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		PersonWithHashCode person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithHashCode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersonWithHashCode person2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithHashCode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Map<span style="color: #339933;">&lt;</span>PersonWithHashCode, String<span style="color: #339933;">&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>PersonWithHashCode, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>person, <span style="color: #0000ff;">&quot;friend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertNotNull<span style="color: #009900;">&#40;</span>map.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>person2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Two equivalent &#8216;PersonWithoutHashCode&#8217; instances are not considered equivalent when the &#8216;get&#8217; method is called on HashMap.  On the other hand the &#8216;PersonWithHashCode&#8217; instances are considered equivalent for the same method.  </p>
<p>While you can implment equals() and hashCode() by hand or generate them with your IDE, you might consider using the <a href="http://docs.guava-libraries.googlecode.com/git-history/v11.0.1/javadoc/com/google/common/base/Objects.html">Objects</a> class within the <a href="http://code.google.com/p/guava-libraries/">Guava Libraries</a>.  Here is the Person class using Guava.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Objects.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span>firstName, lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> obj<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> obj.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		PersonWithGuava other <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PersonWithGuava<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Objects.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span>firstName, other.<span style="color: #006633;">firstName</span><span style="color: #009900;">&#41;</span> 
				<span style="color: #339933;">&amp;&amp;</span> Objects.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span>lastName, other.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here&#8217;s the test for this new code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testHashMapRetrieval_WithGuava<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		PersonWithGuava person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithGuava<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersonWithGuava person2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonWithGuava<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Map<span style="color: #339933;">&lt;</span>PersonWithGuava, String<span style="color: #339933;">&gt;</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>PersonWithGuava, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>person, <span style="color: #0000ff;">&quot;friend&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertNotNull<span style="color: #009900;">&#40;</span>map.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>person2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So why use this?  This helps make our code <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>.  Much of the generated code for the &#8216;hashCode()&#8217; and &#8216;equals()&#8217; method can be encapsulated.  The Guava Library has done just that for us.  If the implementation needs to be changed it can be updated in one place.</p>
<p>Now if you&#8217;re like me, you are lazy.  As a lazy person I ask myself, &#8220;Do I want to write code that I can generate?&#8221;.  My initial thought is &#8220;no&#8221;.  Luckily you can use this new library and continue to generate your &#8216;equals()&#8217;, &#8216;hashCode()&#8217; and even your &#8216;toString()&#8217; methods.  Simply add the <a href="http://marketplace.eclipse.org/content/guava-eclipse-plugin">Guava Eclipse plugin</a> to your favorite version of <a href="http://www.eclipse.org/">Eclipse</a> and voila. </p>
<p>This plugin adds new menu options.</p>
<p><img src="http://blogs.sourceallies.com/wp-content/uploads/2012/02/menu.PNG" alt="menu" title="menu" width="537" height="272" class="size-full wp-image-2607" /></p>
<p><img src="http://blogs.sourceallies.com/wp-content/uploads/2012/02/wizard1.PNG" alt="wizard" title="wizard" width="347" height="488" class="size-full wp-image-2612" /></p>
<p>This is the code that the plugin generated for the Person class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Objects.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span>firstName, lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object<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>object <span style="color: #000000; font-weight: bold;">instanceof</span> PersonWithGuava<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			PersonWithGuava that <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PersonWithGuava<span style="color: #009900;">&#41;</span> object<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> Objects.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">firstName</span>, that.<span style="color: #006633;">firstName</span><span style="color: #009900;">&#41;</span>
				<span style="color: #339933;">&amp;&amp;</span> Objects.<span style="color: #006633;">equal</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastName</span>, that.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While we all know that we must override &#8216;hashCode()&#8217; and &#8216;equals()&#8217; together, we also know that we should eliminate duplication in our code and encapsulate common functionality.  Install the Guava plugin today and remove another piece of duplication from your code.</p>
<p><strong>Please read the comments below before you use this plugin.</strong></p>
<p>Here&#8217;s the complete code <a href="http://blogs.sourceallies.com/wp-content/uploads/2012/02/HashCode.zip" > HashCode</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/12/back-to-basics-hashcode-equals/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Debugging memory leaks with VisualVM</title>
		<link>http://blogs.sourceallies.com/2011/07/debugging-memory-leaks-with-visualvm/</link>
		<comments>http://blogs.sourceallies.com/2011/07/debugging-memory-leaks-with-visualvm/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 12:45:01 +0000</pubDate>
		<dc:creator>Cory Wandling</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[vm]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2389</guid>
		<description><![CDATA[At work I had run into a memory leak when scrolling through large result sets returned from Hibernate.  I thought I had fixed it by performing a evict()/clear()/flush() in the HibernateTemplate that I was using but suddenly the leak was back.  I was using VisualVm to monitor the heap so I started poking [...]]]></description>
			<content:encoded><![CDATA[<p>At work I had run into a memory leak when scrolling through large result sets returned from Hibernate.  I thought I had fixed it by performing a evict()/clear()/flush() in the HibernateTemplate that I was using but suddenly the leak was back.  I was using <a href="http://visualvm.java.net/">VisualVm</a> to monitor the heap so I started poking around to see if there was anything to help diagnose where the leak was.<br />
<span id="more-2389"></span></p>
<p>First thing I did was to take a heap dump.</p>
<p><img src="http://www.jroller.com/clwandling/resource/memory_leak_1.png"></p>
<p>I selected the classes and I see</p>
<p><img src="http://www.jroller.com/clwandling/resource/memory_leak_2.png"></p>
<p>
Well <strong>THAT</strong> was not helpful.  It turns out that the leak is in a class that only has one instance and it is <em><b>holding onto</b></em> thousands of objects.<br />
Then on the summary page I notice the &#8220;Find 20 biggest objects by retained size:&#8221;.</p>
<p><img src="http://www.jroller.com/clwandling/resource/memory_leak_4.png"></p>
<p>I click it and a <em>long</em> time later I see</p>
<p><img src="http://www.jroller.com/clwandling/resource/memory_leak_3.png"></p>
<p>Shazam! There it is! In this faked up example I built there is a single Stack instance but it&#8217;s <em><b>retained size</b></em> is huge.  That because VisualVM computed not just the size of that object but the sizes of all of the objects it was holding onto.  How sweet is that.  I found my memory leak in like 5 minutes.</p>
<p>Another useful tool!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2011/07/debugging-memory-leaks-with-visualvm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Would you start mocking me?</title>
		<link>http://blogs.sourceallies.com/2010/08/would-you-start-mocking-me/</link>
		<comments>http://blogs.sourceallies.com/2010/08/would-you-start-mocking-me/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 14:54:39 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[Mockito]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1545</guid>
		<description><![CDATA[ ]]></description>
			<content:encoded><![CDATA[<p>One of the primary principles of <a href="http://en.wikipedia.org/wiki/Unit_testing">unit testing</a> is to test a small piece of functionality in isolation.  In order to achieve this, <a href="http://en.wikipedia.org/wiki/Mock_object">mock objects</a> are often necessary.  Historically using mocks could be quite painful.  After using several mock frameworks, my favorite by far is <a href="http://mockito.org">Mockito</a>.</p>
<h1>Tutorial</h1>
<p>In this tutorial we will walk through examples of the most common features of Mockito. My sample project can be downloaded <a href="http://github.com/kesslerdn/mockito-learning">here</a>.</p>
<h2>Interfaces and Implementation</h2>
<p>Some mocking frameworks only supported mocking interfaces.  As a result our projects became bloated with useless interfaces that were only used for testing.  Mockito creates mock objects with interfaces or classes.<br />
<span id="more-1545"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Person<span style="color: #009900;">&#123;</span>
   <span style="color: #003399;">List</span> getChildren<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">Boolean</span> isCitizen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">Integer</span> getAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">Map</span> getSchedule<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">void</span> haveChild<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> PersonImpl <span style="color: #000000; font-weight: bold;">implements</span> Person<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> getChildren<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Boolean</span> isCitizen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Map</span> getSchedule<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> haveChild<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">assertNotNull<span style="color: #009900;">&#40;</span>Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertNotNull<span style="color: #009900;">&#40;</span>Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Default Values</h2>
<p>Mockito provides stub implementations for all of the methods on the interface/class.  It also provides several common sense defaults out of the box.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertNotNull<span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, mockPerson.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertFalse<span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">isCitizen</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>, mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertNotNull<span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getSchedule</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mockito returns empty Lists and Maps to prevent NPE&#8217;s in your test code.  It defaults to false for Boolean return values.  By default Zero is returned for Integer, Long and Double.  Notice that even though PersonImpl returns null for all of its methods the Mockito version returns reasonable default values.</p>
<h2>Changing Behavior</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">35</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">35</span><span style="color: #009900;">&#41;</span>, mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mockito.when() takes a mock and a method call that returns a value.  Mockito&#8217;s thenReturn() method takes the return value that Mockito will provide to all matching calls.  By default the method getAge() returns Zero, within Mockito, but now it returns 35.  This ability to control behavior is the key to testing functionality in isolation.</p>
<h2>Verifying Method Calls</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mockito.verify() takes a mock object and verifies that the following method is called.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span>mockPerson, Mockito.<span style="color: #006633;">times</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is the same verify as before except a second parameter is passed to the verify() method telling it how many times the trailing method should be called.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span>mockPerson, Mockito.<span style="color: #006633;">atLeastOnce</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Sometimes it is not important how many times a method is called as long as it is called at least once.  Mockito.atLeastOnce() method provides this flexibility.</p>
<h2>No More Interactions</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verify</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Mockito.<span style="color: #006633;">verifyNoMoreInteractions</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mockito.verifyNoMoreInteractions() ensures that no other methods have been called on the PersonImpl class.  Since Mockito is very helpful in stubbing out all of the methods for a class or interface there is no control over which methods have been called.  Only methods that have been explicitly verified are checked for specific calls.</p>
<h2>Throw Exception</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> <span style="color: #003399;">RuntimeException</span>.<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: #000066; font-weight: bold;">void</span> testThenThrow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenThrow</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test exception&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The thenThrow() method prepares the method defined in when() to throw an Exception.  While I am not attempting to provide a tutorial on JUnit4 features, I feel it necessary to point out the Annotation verifies that a RuntimeException is thrown in the test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> <span style="color: #003399;">RuntimeException</span>.<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: #000066; font-weight: bold;">void</span> testDoThrow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Mockito.<span style="color: #006633;">doThrow</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test exception&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">haveChild</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   mockPerson.<span style="color: #006633;">haveChild</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The doThrow() syntax is necessary to throw an Exception for a void method.  The previous example threw an exception for Person.getAge() which is not a void method.  In this example Person.haveChild() is void and requires this alternate syntax in order to compile.</p>
<h2>TDD with Mockito</h2>
<p>Let&#8217;s drive a small class that we will call Bouncer.  This class will check if a Person is old enough to drink.  First let&#8217;s write a test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testCheckAgeOf_UnderAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Bouncer bouncer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bouncer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   assertFalse<span style="color: #009900;">&#40;</span>bouncer.<span style="color: #006633;">checkAgeOf</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Of coarse this does not even compile.  We need to create a Bouncer class with the checkAgeOf() method.  And here is the amazing class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Bouncer<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> checkAgeOf<span style="color: #009900;">&#40;</span>Person customer<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></pre></div></div>

<p>So why did I return false.  Well the test does not require anything more than this to pass.  TDD is about doing the simplest thing that works and then refactor the code.  This is the simplest thing that works.  Now let&#8217;s write another test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testCheckAgeOf_OfAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   Person mockPerson <span style="color: #339933;">=</span> Mockito.<span style="color: #006633;">mock</span><span style="color: #009900;">&#40;</span>PersonImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>mockPerson.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">21</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   Bouncer bouncer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bouncer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   assertTrue<span style="color: #009900;">&#40;</span>bouncer.<span style="color: #006633;">checkAgeOf</span><span style="color: #009900;">&#40;</span>mockPerson<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we need real business logic in our method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">return</span> customer.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">21</span><span style="color: #339933;">;</span></pre></div></div>

<p>Green again.  Now it is time to refactor.  In this simple case I am not sure how to simplify the code.  There are a few more tests that should be written.  A test if getAge() returns null and if Person is null and other boundary cases.  Since the focus of this article is on Mockito I will stop here.</p>
<p>Mockito is a powerful mocking framework that simplifies the creation and usage of mock objects.  I encourage you to download my <a href="http://github.com/kesslerdn/mockito-learning">sample code</a> and continue to explore the Mockito framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/08/would-you-start-mocking-me/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Rendering Global t:messages After Redirect</title>
		<link>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/</link>
		<comments>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 19:24:46 +0000</pubDate>
		<dc:creator>Max  Kuipers</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[JSF messages]]></category>
		<category><![CDATA[tomahawk]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1219</guid>
		<description><![CDATA[A common problem when working with JSF is getting global info messages  via &#60;t:messages globalOnly="true"&#62; or &#60;f:messages globalOnly="true"&#62; to display messages set in the previous request when you have a &#60;redirect/&#62; in your faces-config for a particular page You will not see your &#60;t:messages&#62; that are set on the previous page.
The Problem
For instance, say you [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem when working with JSF is getting global info messages  via <code>&lt;t:messages globalOnly="true"&gt;</code> or <code>&lt;f:messages globalOnly="true"&gt;</code> to display messages set in the previous request when you have a <code>&lt;redirect/&gt;</code> in your faces-config for a particular page You will not see your <code>&lt;t:messages&gt;</code> that are set on the previous page.</p>
<h4><span style="text-decoration: underline"><strong>The Problem</strong></span></h4>
<p>For instance, say you have two pages &#8211; page1.xhtml and page2.xhtml. In your faces-config.xml, you will have 2 entries.</p>
<p><span id="more-1219"></span></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>page1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/pages/page1.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;redirect</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>page2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/pages/page2.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;redirect</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Let&#8217;s say page1Bean is called in page1 and you want it to display messages in page2.  So your bean would have something 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: #000000; font-weight: bold;">class</span> page1Bean <span style="color: #009900;">&#123;</span>
	...
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> page1Action<span style="color: #009900;">&#123;</span>
		...
		<span style="color: #008000; font-style: italic; font-weight: bold;">/** Add message */</span>
		FacesMessage facesMessage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FacesMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		...
		<span style="color: #006633;">FacesContext</span>.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, facesMessage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		...
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;page2&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Your page2.xhtml has like the following somewhere in it.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;t:messages globalOnly=&quot;true&quot;&gt;</pre></div></div>

<p>One would expect that this would display the message on the next page, however these messages are request scoped and so are not available in this second request and you do not see your message unless you remove the <code>&lt;redirect/&gt;</code> on page2.</p>
<p>Fortunately, there is a nifty and quick solution.  Basically, it is a phase listener that saves the messages from the previous request and then restores them just before the RENDER_RESPONSE phase of the second request.</p>
<h4><span style="text-decoration: underline"><strong>The Solution</strong></span></h4>
<p>To solve the problem, save the following class into some package where your faces-config will be able to access it</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.myproject.web.jsf.phaselistener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.application.FacesMessage</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.context.FacesContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseId</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseListener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Enables messages to be rendered on different pages from which they were set.
 * To produce this behaviour, this class acts as a &lt;code&gt;PhaseListener&lt;/code&gt;.
 *
 * This is performed by moving the FacesMessage objects:
 *
	&lt;li&gt;After each phase where messages may be added, this moves the messages from
 * the page-scoped FacesContext to the session-scoped session map.
 *&lt;/li&gt;
	&lt;li&gt;Before messages are rendered, this moves the messages from the session-scoped
 * session map back to the page-scoped FacesContext.
 *&lt;/li&gt;
 * Only messages that are not associated with a particular component are ever
 * moved. These are the only messages that can be rendered on a page that is different
 * from where they originated.
 *
 * To enable this behaviour, add a &lt;code&gt;lifecycle&lt;/code&gt; block to your
 * faces-config.xml file. That block should contain a single &lt;code&gt;phase-listener&lt;/code&gt;
 * block containing the fully-qualified classname of this file.
 *
 * EDIT: This code was minimally modified by Max Kuipers to address some of the Java 1.6
 * compiler warnings.  All code was originally written by Jesse Wilson.
 *
 * @author &lt;a href=&quot;mailto:jesse@odel.on.ca&quot;&gt;Jesse Wilson&lt;/a&gt;
 * @author &lt;a href=&quot;mailto:mkuipers@sourceallies.com&quot;&gt;Max Kuipers&lt;/a&gt;
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MessageHandler <span style="color: #000000; font-weight: bold;">implements</span> PhaseListener <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: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * a name to save messages in the session under
	 */</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> sessionToken <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MULTI_PAGE_MESSAGES_SUPPORT&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Return the identifier of the request processing phase during which this
	 * listener is interested in processing PhaseEvent events.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> PhaseId getPhaseId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> PhaseId.<span style="color: #006633;">ANY_PHASE</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Handle a notification that the processing for a particular phase of the
	 * request processing lifecycle is about to begin.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> beforePhase<span style="color: #009900;">&#40;</span>PhaseEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">RENDER_RESPONSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			FacesContext facesContext <span style="color: #339933;">=</span> event.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			restoreMessages<span style="color: #009900;">&#40;</span>facesContext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Handle a notification that the processing for a particular phase has just
	 * been completed.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPhase<span style="color: #009900;">&#40;</span>PhaseEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">APPLY_REQUEST_VALUES</span> <span style="color: #339933;">||</span>
				event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">PROCESS_VALIDATIONS</span> <span style="color: #339933;">||</span>
				event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">INVOKE_APPLICATION</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			FacesContext facesContext <span style="color: #339933;">=</span> event.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			saveMessages<span style="color: #009900;">&#40;</span>facesContext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Remove the messages that are not associated with any particular component
	 * from the faces context and store them to the user's session.
	 *
	 * @return the number of removed messages.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> saveMessages<span style="color: #009900;">&#40;</span>FacesContext facesContext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// remove messages from the context</span>
		<span style="color: #003399;">List</span> messages <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getMessages</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> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			messages.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			i.<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>
		<span style="color: #666666; font-style: italic;">// store them in the session</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>messages.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">Map</span> sessionMap <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// if there already are messages</span>
		@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #003399;">List</span> existingMessages <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span><span style="color: #009900;">&#41;</span> sessionMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>sessionToken<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>existingMessages <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>
			existingMessages.<span style="color: #006633;">addAll</span><span style="color: #009900;">&#40;</span>messages<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>
			sessionMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>sessionToken, messages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// if these are the first messages</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> messages.<span style="color: #006633;">size</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: #008000; font-style: italic; font-weight: bold;">/**
	 * Remove the messages that are not associated with any particular component
	 * from the user's session and add them to the faces context.
	 *
	 * @return the number of removed messages.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> restoreMessages<span style="color: #009900;">&#40;</span>FacesContext facesContext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// remove messages from the session</span>
		<span style="color: #003399;">Map</span> sessionMap <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #003399;">List</span> messages <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span><span style="color: #009900;">&#41;</span>sessionMap.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>sessionToken<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// store them in the context</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>messages <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">int</span> restoredCount <span style="color: #339933;">=</span> messages.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> messages.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			facesContext.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> restoredCount<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then add something like the following in your faces-config.xml. Be sure to replace the example package location with your own.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.myproject.web.jsf.phaselistener.MessageHandler<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The forum post that documented this solution is <a href="http://forums.sun.com/thread.jspa?forumID=427&amp;threadID=657727">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Environment Specific Properties in Spring</title>
		<link>http://blogs.sourceallies.com/2010/03/environment-specific-properties-in-spring/</link>
		<comments>http://blogs.sourceallies.com/2010/03/environment-specific-properties-in-spring/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 19:10:16 +0000</pubDate>
		<dc:creator>Travis Klotz</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1190</guid>
		<description><![CDATA[
On many occasions I want to be able to inject environment specific property values into my Spring managed beans.  These may be things like web service endpoints, database URLs, etc.  Values I know for each environment at build time, but I want to use the same WAR/EAR file in each environment.  I [...]]]></description>
			<content:encoded><![CDATA[<p>
On many occasions I want to be able to inject environment specific property values into my Spring managed beans.  These may be things like web service endpoints, database URLs, etc.  Values I know for each environment at build time, but I want to use the same WAR/EAR file in each environment.  I would like to keep the actual values separate from the Spring config files themselves.  And I would really like to manage a set of default values for each property, so that I do not need to specify a value for every property in every environment (ex. my credit card processing URL for dev, test, uat is the same, but for production it is different.)
</p>
<p><span id="more-1190"></span></p>
<p>
Spring already has 90% of this solution already worked out.  The PropertyPlaceholderConfigurer can be used to externalize property values into Java properties files.  These properties can then be referenced &#8220;ant style&#8221; in your configuration files ex. ${myPropertyValues}.  It can even handle multiple files, checking them in order for each property,  so one file can override one or move values that are stored in another.   However the PropertyPlaceholderConfigurer doesn&#8217;t have any concept of an environment specific file.
</p>
<p>
Thats where we come in:
</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.config</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.config.PropertyPlaceholderConfigurer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.core.io.ClassPathResource</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.core.io.Resource</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> EnvironmentPropertyPlaceholderConfigurer <span style="color: #000000; font-weight: bold;">extends</span>
		PropertyPlaceholderConfigurer <span style="color: #009900;">&#123;</span>
&nbsp;
   @Override
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLocation<span style="color: #009900;">&#40;</span>Resource location<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><span style="color: #009900;">&#40;</span>location <span style="color: #000000; font-weight: bold;">instanceof</span> ClassPathResource<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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>
               <span style="color: #0000ff;">&quot;EnvironmentPropertyPlaceholderConfigurer &quot;</span> <span style="color: #339933;">+</span> 
               <span style="color: #0000ff;">&quot;locations must be on the classpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      ClassPathResource cl <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ClassPathResource<span style="color: #009900;">&#41;</span> location<span style="color: #339933;">;</span>
      <span style="color: #003399;">String</span> filename <span style="color: #339933;">=</span> cl.<span style="color: #006633;">getFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//switch  &quot;env.properties&quot; to &quot;env.dev.properties&quot;</span>
      <span style="color: #003399;">String</span> envFilename <span style="color: #339933;">=</span> filename.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">&quot;(.*)<span style="color: #000099; font-weight: bold;">\\</span>.properties&quot;</span>, 
            <span style="color: #0000ff;">&quot;$1.&quot;</span> <span style="color: #339933;">+</span> getEnvironment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.properties&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      Resource envLocation <span style="color: #339933;">=</span> cl.<span style="color: #006633;">createRelative</span><span style="color: #009900;">&#40;</span>envFilename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>envLocation.<span style="color: #006633;">exists</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         setLocations<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Resource<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> location, envLocation <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
         setLocations<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Resource<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> location <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getEnvironment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">/*
       * Magic goes here. This method must be able to determine which
       * environment it is running in without any help from injected
       * properties.
       */</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sai.env&quot;</span>, <span style="color: #0000ff;">&quot;DEV&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
This version of the configurer expects a single resource to be passed in via the location property.  In this case I restrict it only to ClassPathResource instances, but really it can be any version that can generate a new resource with a relative path.  The resource we configure here should be the &#8220;default&#8221; file, the configurer will then build an environment specific file based on the default name and check its existence.  If the environment specific file is found the are ordered up so that any properties in the environment file will override the values in the default file.  If no environment file is found, only the default file is used.
</p>
<p>
The only real magic here is how the configurer determines the environment that its running in.  In this simple case, the configurer relies on a system property to tell it what environment its running in.  In larger organizations I have found that getting system properties set reliably across environments to be extremely difficult (most shops still tack on application server administration to the jobs of the DBA&#8217;s or the Unix/Windows admins who are already overworked.)  In these cases, more extreme methods may be needed to determine the environment name.  JNDI lookups, Database queries, reading files from the file system may all be good solutions.  The important thing is that you can&#8217;t inject the environment name,  if it were that easy, we wouldn&#8217;t actually need this configurer!
</p></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/environment-specific-properties-in-spring/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Replacing and Patching Java Application and Core classes</title>
		<link>http://blogs.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/</link>
		<comments>http://blogs.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 16:00:40 +0000</pubDate>
		<dc:creator>Alexandru Luchian</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=809</guid>
		<description><![CDATA[Why would you ever need that?
Say you get a jar file. After using the jar for a while you realise that there is a bug in a class in the jar file. Unfortunately you also find out that the jar is no longer supported and there is no way you will get a fix from [...]]]></description>
			<content:encoded><![CDATA[<p>Why would you ever need that?</p>
<p>Say you get a jar file. After using the jar for a while you realise that there is a bug in a class in the jar file. Unfortunately you also find out that the jar is no longer supported and there is no way you will get a fix from the author (who is long gone fishing).</p>
<p>In order to solve this issue, you first need to get the source of the class. If you are lucky enough and the author did not obfuscate the class file you can decompile it with a decompiler (my favourite one is <a href="http://java.decompiler.free.fr/">JD-GUI</a>).</p>
<p><span id="more-809"></span></p>
<p>After you get the source, you modify it, recompile and tell the java interpreter to load your new class first before the old one. Easy right?</p>
<p>Let me give you an example</p>
<p>Say we have this &#8220;outdated&#8221; Spider class</p>
<blockquote><p>alex@tractorash:~/work/java$ pwd<br />
/home/alex/work/java<br />
alex@tractorash:~/work/java$ cat sai/Spider.java</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">sai</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Spider<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> <span style="color: #003399;">URL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.google.com/search?q=best+software+2009&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getURL<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">URL</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, we have the Main class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">sai</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Main<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: #009900;">&#123;</span>
        Spider spider <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Spider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>spider.<span style="color: #006633;">getURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, if we compile and run the Main class we will get:</p>
<blockquote><p>alex@tractorash:~/work/java$ javac sai/Main.java<br />
alex@tractorash:~/work/java$ java sai.Main<br />
<strong> http://www.google.com/search?q=best+software+2009</strong></p></blockquote>
<p>As we expected. Suppose we want to search for http://www.google.com/search?q=best+software+2010. The only way to do that is create another <strong>sai.Spider</strong> class and return the needed string.</p>
<p>So, here we go</p>
<blockquote><p>alex@tractorash:~/h$ pwd<br />
/home/alex/h<br />
alex@tractorash:~/h$ cat sai/Spider.java</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">sai</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Spider<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> <span style="color: #003399;">URL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.google.com/search?q=best+software+2010&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getURL<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">URL</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We compile our new class</p>
<blockquote><p>alex@tractorash:~/h$ javac sai/Spider.java</p></blockquote>
<p>Then, switch to previous path:</p>
<blockquote><p>alex@tractorash:~/h$ cd ~/work/java/<br />
alex@tractorash:~/work/java$ java -cp /home/alex/h:/home/alex/work/java sai.Main<br />
http://www.google.com/search?q=best+software+<strong>2010</strong></p></blockquote>
<p>So what did we do? We modified the classpath (http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/classpath.html) and told the JVM to load our new class first, since it&#8217;s first in our list of directories we specified as classpath.</p>
<p>This way we &#8220;tricked&#8221; the JVM in loading our new class first so we could get the desired output.</p>
<p>What we&#8217;ve done so far is called replacing and patching application classes.</p>
<p>You will not be able to modify/replace system classes like <em>java.lang.Integer</em> using this method.</p>
<p>You are probably wondering why would you ever want to replace java.lang.Integer. Let me give you an example. As you might know the Integer class is immutable, in other words once the object is initialised you cannot modify it&#8217;s value. So, let&#8217;s add a new method called setValue(int newVal) which will modify the current value of the Integer object.</p>
<p>As you can imagine, applying the method used in the Spider class example will not work. The reason is: to load system classes and libraries, the JVM does not use the CLASSPATH variable, but the BOOTCLASSPATH. The boot strap class loader uses the boot class path to load the main/system classes.</p>
<p>What&#8217;s interesting is that you can specify/modify the BOOTCLASSPATH as well. In order to to that we have to use the -X argument for the java launcher:</p>
<blockquote><p>alex@tractorash:~$ java -X<br />
-Xbootclasspath:&lt;directories and zip/jar files separated by :&gt;<br />
set search path for bootstrap classes and resources<br />
-Xbootclasspath/a:&lt;directories and zip/jar files separated by :&gt;<br />
append to end of bootstrap class path<br />
-Xbootclasspath/p:&lt;directories and zip/jar files separated by :&gt;<br />
prepend in front of bootstrap class path</p></blockquote>
<p>We are interested in the <em>-Xbootclasspath/p:</em> option since we want to prepend the directory that contains the patched Integer class.</p>
<p>First, let&#8217;s add that new method. Get the source of java.lang.Integer. You can get it either from the src.zip file that comes with the JDK, or download it from online.</p>
<p>Switch to our working directory:</p>
<blockquote><p>alex@tractorash:/$ cd /home/alex/work/java/</p></blockquote>
<p>Add the method:</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;">void</span> setValue<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;">this</span>.<span style="color: #006633;">value</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you are modifying a Java 6 version of Integer class you have to remove the final keyword from the value variable.</p>
<p>Was this:</p>

<div class="wp_syntax"><div 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;">final</span> <span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #339933;">;</span></pre></div></div>

<p>Has to be this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #339933;">;</span></pre></div></div>

<p>Now we need a test class where we will test the new functionality. Let&#8217;s create the IntegerTest class:</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: #000000; font-weight: bold;">class</span> IntegerTest<span style="color: #009900;">&#123;</span>
    <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: #009900;">&#123;</span>
        <span style="color: #003399;">Integer</span> tInt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;Initial value: &quot;</span> <span style="color: #339933;">+</span> tInt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        tInt.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;New value: &quot;</span> <span style="color: #339933;">+</span> tInt<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>Compiling patched versions of system classes can be a little tricky.</p>
<p>First let&#8217;s compile our Integer class:</p>
<blockquote><p>alex@tractorash:~/work/java$ javac java/lang/Integer.java<br />
java/lang/Integer.java:571: warning: sun.misc.VM is Sun proprietary API and may be removed in a future release<br />
if (!sun.misc.VM.isBooted()) {<br />
^<br />
Note: java/lang/Integer.java uses unchecked or unsafe operations.<br />
Note: Recompile with -Xlint:unchecked for details.<br />
1 warning</p></blockquote>
<p>We get the warning because we use a sun.* class in our new class. Since sun.* packages and classes are proprietary to Sun, it is strongly advised not to use them, but in our case we just reuse them (so we can ignore the warning).</p>
<p>Now, we have our Integer.class file. We have to use it when we execute our test class.</p>
<p>Now compile IntegerTest.java:</p>
<blockquote><p>alex@tractorash:~/work/java$ javac -Xbootclasspath/p:/home/alex/work/java/lang/:/usr/lib/jvm/java-6-sun-1.6.0.15/jre/lib/rt.jar IntegerTest.java<br />
./java/lang/Integer.java:571: warning: sun.misc.VM is Sun proprietary API and may be removed in a future release<br />
if (!sun.misc.VM.isBooted()) {<br />
^<br />
Note: ./java/lang/Integer.java uses unchecked or unsafe operations.<br />
Note: Recompile with -Xlint:unchecked for details.<br />
1 warning</p></blockquote>
<p>Copy Integer.class to the directory where your IntegerTest class is:</p>
<blockquote><p>alex@tractorash:~/work/java$ cp ./java/lang/Integer.class .</p></blockquote>
<p>So IntegerTest.class and Integer.class are in the same directory.</p>
<p>Finally run IntegerTest:</p>
<blockquote><p>alex@tractorash:~/work/java$ java -Xbootclasspath/p:/home/alex/work/java:/usr/lib/jvm/java-6-sun-1.6.0.15/jre/lib/rt.jar IntegerTest<br />
Initial value: 1<br />
New value: 25</p></blockquote>
<p>Success! Now you have a mutable Integer class.</p>
<p>I recommend you read the <a title="Java Covert" href="http://www.amazon.com/Covert-Java-Techniques-Decompiling-Engineering/dp/0672326388" target="_blank">Java Covert</a> book.</p>
<p>However, I have to warn you that this post is for academic purposes only. Patching/removing/replacing Java core classes can be against the Java license agreement. Please consult the license before going down this path.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Keep your dataTable clean with a custom popup</title>
		<link>http://blogs.sourceallies.com/2010/01/keep-your-datatable-clean-with-a-custom-popup/</link>
		<comments>http://blogs.sourceallies.com/2010/01/keep-your-datatable-clean-with-a-custom-popup/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:05:57 +0000</pubDate>
		<dc:creator>David  Casleton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[tomahawk]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=864</guid>
		<description><![CDATA[The basic idea is to output some data to a user in a table and allow them to take an action on each row individually.  A fairly straightforward solution is to create a separate page to link to, passing the necessary row information along.  If the action is simple enough, like a single [...]]]></description>
			<content:encoded><![CDATA[<p>The basic idea is to output some data to a user in a table and allow them to take an action on each row individually.  A fairly straightforward solution is to create a separate page to link to, passing the necessary row information along.  If the action is simple enough, like a single checkbox, you could just embed the necessary component(s) in each row of the table.  Too many components, however, can bloat the table and make the UI cumbersome to the user.  Instead we can create a popup window to overlay our page, containing whatever components are needed, and activate it by a link embedded in our table.  Passing the row information is a little trickier,  but the result is a cleaner interface and a better user experience.<br />
<span id="more-864"></span></p>
<p>The example implementation below is done using Java with JSF, a little JavaScript, a little jQuery, and some CSS.  The tag libraries being referenced are:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
xmlns:t=&quot;http://myfaces.apache.org/tomahawk&quot;</pre></div></div>

<p>The page is constructed using two separate forms, one for the main components of the page, including the datatable, and the other for the popup.  A little JavaScript and jQuery are used to show/hide the popup form and pass around the pertinent row information.  Some CSS is used to shadow the main page components while the popup is active.</p>
<p>Our simple main form is:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;someForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;actionDetailsDiv&quot;</span> <span style="color: #000066;">styleClass</span>=<span style="color: #ff0000;">&quot;info&quot;</span> </span>
<span style="color: #009900;">			<span style="color: #000066;">rendered</span>=<span style="color: #ff0000;">&quot;#{not empty myAction.actionDetails}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:outputText</span> <span style="color: #000066;">escape</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{myAction.actionDetails}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:dataTable</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;currentRow&quot;</span> </span>
<span style="color: #009900;">			<span style="color: #000066;">rowIndexVar</span>=<span style="color: #ff0000;">&quot;rowNum&quot;</span> </span>
<span style="color: #009900;">			<span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{myAction.myData}&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:facet</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;row&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/f:facet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			#{rowNum}
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:facet</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;some colummn&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/f:facet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			#{currentRow.someProperty}
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:facet</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;action links&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/f:facet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript:showHiddenForm('#{rowNum}')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>action link<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:dataTable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>	
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here the row number (rowNum) has been passed to a piece of JS, but you could just as easily pass a property from the current row object.  Also, actionDetails is a string on our backing action bean used to inform the user that the action has been taken on the appropriate data.  This should be declared in the backing bean as an empty string and thus the containing div is not initially rendered.</p>
<p>Our hidden form is:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;hiddenForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;backgroundDiv&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fullscreen outer&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;foregroundDiv&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;centerPopup inner&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Action Heading<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:inputHidden</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;hiddenInfo&quot;</span> <span style="color: #000066;">forceId</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:commandButton</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Backing Action&quot;</span> <span style="color: #000066;">actionListener</span>=<span style="color: #ff0000;">&quot;#{myAction.backingAction}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>		
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:commandButton</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;hideHiddenForm()&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Cancel&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>To prove that the data in the hidden field is being populated correctly, change the inputHidden component to inputText.</p>
<p>The CSS referenced above is:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.fullscreen</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.centerPopup</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-100px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-250px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.outer</span> <span style="color: #00AA00;">&#123;</span>
	filter<span style="color: #00AA00;">:</span> alpha<span style="color: #00AA00;">&#40;</span>opacity<span style="color: #00AA00;">=</span><span style="color: #cc66cc;">50</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
	-moz-opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.5</span><span style="color: #00AA00;">;</span>
	opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.5</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#D8D8D8</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">2000</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.inner</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000000</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">2001</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
p<span style="color: #6666ff;">.inner</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.info</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Most of the CSS is pretty straightforward.  The &#8220;outer&#8221; class is setup to make a div gray and transparent in most browsers and &#8220;centerPopup&#8221; creates a statically sized div in the middle of the screen.  The crucial part of the CSS is the z-index used by the &#8220;inner&#8221; and &#8220;outer&#8221; classes.  The higher the z-index, the higher on the stack the element is, thus to show the &#8220;inner&#8221; popup over the somewhat transparent &#8220;outer&#8221; fullscreen overlay, the popup&#8217;s z-index must be higher.  Both z-indexes are set arbitrarily high to ensure other elements don&#8217;t interfere.</p>
<p>The JavaScript/jQuery to show and hide the hidden form is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> hideHiddenForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#hiddenForm&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</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: #003366; font-weight: bold;">function</span> showHiddenForm<span style="color: #009900;">&#40;</span>selection<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//set the value of the hidden field</span>
	<span style="color: #003366; font-weight: bold;">var</span> thingy <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hiddenInfo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	thingy.<span style="color: #660066;">value</span><span style="color: #339933;">=</span>selection<span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#hiddenForm&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</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;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//initially hide the popup form</span>
	hideHiddenForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note showHiddenForm takes a single input and sets this as the value of the hiddenInfo element.  Also, we need to hide the popup form when the page is first loaded.</p>
<p>In our backing bean (MyAction) the backingAction method needs to find the hidden field and do something with the value.  Here the value is passed to the actionDetails property forcing actionDetailsDiv above the table to render when the page is rerendered.  Typically we would actually act on the value instead of merely redisplaying it.</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;">void</span> backingAction<span style="color: #009900;">&#40;</span><span style="color: #003399;">ActionEvent</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	UIComponent link <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>UIComponent<span style="color: #009900;">&#41;</span> event.<span style="color: #006633;">getSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>UIComponent divChild <span style="color: #339933;">:</span> link.<span style="color: #006633;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>divChild.<span style="color: #006633;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hiddenInfo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">String</span> rowNum <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> divChild.<span style="color: #006633;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			setActionDetails<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Action taken on row &quot;</span> <span style="color: #339933;">+</span> rowNum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</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></div></div>

<p>The hidden form (popup) can become as complicated as you need and allows your main form to remain clean.  If anyone knows an easier way to pull the value out of the hidden element, let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/01/keep-your-datatable-clean-with-a-custom-popup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring&#8217;s refreshable beans</title>
		<link>http://blogs.sourceallies.com/2010/01/springs-refreshable-beans/</link>
		<comments>http://blogs.sourceallies.com/2010/01/springs-refreshable-beans/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 14:00:19 +0000</pubDate>
		<dc:creator>Octavian Covalschi</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=854</guid>
		<description><![CDATA[A couple of days ago I found out about a really nice feature in Spring, called &#8216;refreshable bean&#8217;.
Spring&#8217;s vision a refreshable bean is a dynamic-language-backed bean that monitors changes to its source code and then reloads itself when changes occur. And it is all this is done without restarting/re-deploying entire app. Sweet!

Currently Spring supports only [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago I found out about a really nice feature in Spring, called &#8216;refreshable bean&#8217;.</p>
<p>Spring&#8217;s vision a refreshable bean is a dynamic-language-backed bean that monitors changes to its source code and then reloads itself when changes occur. And it is all this is done without restarting/re-deploying entire app. Sweet!<br />
<span id="more-854"></span><br />
Currently Spring supports only 3 dynamic languages: JRuby, Groovy, BeanShell.</p>
<p>A simple example taken from springsource&#8217;s website looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lang:groovy</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;messenger&quot;</span></span>
<span style="color: #009900;">          &lt;!-- switches refreshing on with 5 seconds between checks --<span style="color: #000000; font-weight: bold;">&gt;</span></span>
          refresh-check-delay=&quot;5000&quot; 
          script-source=&quot;classpath:Messenger.groovy&quot;&gt;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lang:property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;message&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;I Can Do The Frug&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lang:groovy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;bookingService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;x.y.DefaultBookingService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;messenger&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;messenger&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So, the &#8216;refresh-check-delay&#8217; attribute makes &#8216;messenger&#8217; bean &#8216;refreshable&#8217;. This attribute is the number of milliseconds between checks to the bean&#8217;s source code, if it has changed, the bean will be refreshed.</p>
<p>Another thing to mention would be that new changes are effective only on the bean&#8217;s next usage. For example if you make five changes to the beans source code, but never make a call to the bean between changes, it will only be reloaded once, when you actually make a call to the bean. </p>
<p>Also, refreshable beans behavior does not apply to dynamic language source code defined inline, inside the context file, it only applies to beans where changes to source code file can be detected, for example if it&#8217;s stored on the filesystem.</p>
<p>That&#8217;s about it. You can find more on springsource&#8217;s website: <a href="http://static.springsource.org/spring/docs/2.5.x/reference/dynamic-language.html">Dynamic language</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/01/springs-refreshable-beans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Musings of a SpringOne 2009 Attendee &#8211; Day 3</title>
		<link>http://blogs.sourceallies.com/2009/10/musings-of-a-springone-2009-attendee-day-3/</link>
		<comments>http://blogs.sourceallies.com/2009/10/musings-of-a-springone-2009-attendee-day-3/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 02:20:05 +0000</pubDate>
		<dc:creator>Sudhakar Ramasamy</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Conference]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=187</guid>
		<description><![CDATA[Agile Architecture &#8211; Technologies and Patterns &#8211; Kirk Knoernschild
Some of the questions this session set out to attempt to answer were

What is architecture?
What defines architecture?
What are architectural decisions?
Is architecture a forward only decision?

 Several definitions of Architecture were quoted from prior literature. Such as architecture being the the shared understanding of the system being built. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.springone2gx.com/conference/new_orleans/2009/10/session?id=15662"><strong>Agile Architecture &#8211; Technologies and Patterns &#8211; Kirk Knoernschild</strong></a></p>
<p>Some of the questions this session set out to attempt to answer were</p>
<ol>
<li>What is architecture?</li>
<li>What defines architecture?</li>
<li>What are architectural decisions?</li>
<li>Is architecture a forward only decision?</li>
</ol>
<p><em> </em>Several definitions of Architecture were quoted from prior literature. Such as architecture being the the shared understanding of the system being built. Shared understanding between a group of people who need to communicate about it &#8212; developers and architects, or technical and management etc.<br />
Lean principles are you delay <span id="more-187"></span>the decision till it is required and till after you have enough information to make an effective decision. Does this mean you should not make decisions based on incomplete information? I disagree on the &#8220;effective decision&#8221;. How do you even know what is effective decision? Something that is effective today more than likely will not be effective tomorrow. The world we live in is a naturally changing world. You delay the decision till the last responsible minute. But when you have to make the decision you do it based on the best available information at that point.<br />
The goals of architecture were then discused. I thought this was a very insigtful analysis. We were first shown how the impact of change and the cost of change influence architecture and design. Hgh impact and high cost of change require architecture and design. If the change has high impact, but low cost then architecture may not be as important as long as you have measures to mitigate the change such as unit and functional tests. Or if the change has low impact but high cost, if you are prepared to absorb the cost, then architecture may not be as important either though this is more unlikely. The end goal of architecture is to reduce the impact and cost of change.<br />
Are flexibility and complexity opposing goals? Higher flexibilty introduces higher complexity. It goes back to choosing your battles. If the impact of change is not significant then it doesn&#8217;t have to be flexible and thus not complex. So the question is which parts of the system needs to be flexible.<br />
The <a href="http://c2.com/cgi/wiki?ReuseReleaseEquivalencePrinciple">release-reuse equivalence principle</a> is that the unit of release is the unit of resue. We see this in daily practice. And consequently reuse fits nicely into modularity. In other words, what is modular can be released as a unit and reused.<br />
So then what correlations can be made between agility and modularity?</p>
<ul>
<li> Structural flexibility which defines modularity</li>
<li> Temporality or the ability of the system to accommodate future change which defines agility</li>
</ul>
<p>(This stuff is good I&#8217;m thinking!)</p>
<p>A jar file</p>
<ul>
<li> unit of resuse</li>
<li> unit of composition</li>
<li> unit of deployment</li>
<li> unit of management</li>
</ul>
<p>So why not make jar files first class citizens on the java platform? Why do we have to package a jar inside a WAR or a EAR? Why are we flattening these modular units.<br />
Increasing a system&#8217;s modularity leads to easier to maintain and extendable software. In a non-modular system, being afraid to make changes to the code base causes us to do things such a copy/paste, or write duplicate code which further degrades the codebase. Guilty as charged!<br />
Modularity helps in assessing the impact and cost of change. When we know what other modules depend on the module that needs to be changed, we have a more clearer understanding of the risks, impacts and cost of that change. I wonder if the roadblock to modularity is our platforms.  What if the WAS and Glassfish and Weblogics of the world allowed us to deploy our applications as sets of OSGi bundles?</p>
<p>There was a comment about how WAS 7 has made giant strides towards being modular internally by adopting OSGi under the hood. Apparently it starts under 5 seconds for a non-ejb application. I had to recently run WAS 7 in development mode inside my RAD workspace. And I can vouch for the fact that startup time was certainly several magnitudes more than 5 secs. Maybe I&#8217;m missing something here.<br />
So what are benefits realized in the runtime because of modularity?</p>
<ol>
<li> Dynamic deployment</li>
<li> Multiple versions</li>
<li> Enforce dependencies</li>
</ol>
<p>&#8230;.and all at runtime.<br />
Modular tension is higher ease of use of an artifact makes it more difficult to reuse. Higher ability for reuse (i.e. finer grained artifacts) makes it more difficult to use. Are reuse and use always inversely related?</p>
<p>A few thoughts around this:</p>
<ol>
<li> Public methods shouldn&#8217;t necessarily be published methods. Currently this is enforced via Javadocs unless you are using OSGi</li>
<li> Interfaces should be close to their usage and not close to the implementations. This makes sense for framework APIs. It probably applies to business applications as well. What&#8217;s good for the  goose is  good for the gander.</li>
<li> Separate the interface from the implementations not just at the package level but also as deployment artifacts. You see this in framework apis now (javaee-api.jar is separate from its implementation jar). But not so much in business applications. If we did, it would aid in providing more predictable API evolution. We would be able to fix a defect or enhance an internal function in the implementation and then release just the newer version of the implementation built against the same old version of the API. The consumers of this API would not see any suprises since the API itself hasn&#8217;t changed. Instead when we package both the interface and implmentation as a single artifact, it is more difficult to avoid suprising the consumers of the API.</li>
<li> Exceptions should be close to where they are thrown. So they should be part of the API.</li>
</ol>
<p>So is OSGi the holy grail of runtime modularity?<br />
Yes and no. It forces runtime modularity but we are still required to design our apps modularily. A sample application (kcode.googlecode.com) that echoed these principles was demoed. Another tool that was mentioned was JarAnalyzer which gives us a visual image of our jar dependencies by analyzing the project. It can be included in the build script. The tool was built by the author.<br />
Overall I really liked this session because of how it tied all the buzz and technical mumbo-jumbo around modularity and OSGi into business speak, something you can pitch to management.</p>
<p><strong><a href="http://www.springone2gx.com/conference/new_orleans/2009/10/session?id=15653">That old Spring magic has me in its SpEL: DI Wizardy with the Spring Expression Language &#8211; Craig Walls</a></strong></p>
<p>Spring Expression Language or SpEL is probably the most prevasive feature in Spring 3.0. From what I&#8217;ve seen so far it can be used in DI, in Spring Web Flow, in Spring Security, Spring Integration. So if you are looking at taking advantage of Spring 3.0 you should probably look at SpEL. A SpEL expression is provided within a #{ } similar to the JSF EL. The source code (http://spring.habuma.com/examples/SpEL-examples.zip) contains well documented examples of the different forms of SpEL expressions. Here is a sampling:</p>
<p>Instead of having to use environmentPlaceHolderConfigurer you could do:</p>
<p>#{systemProperties['region']}<br />
#{systemEnvironment.region}</p>
<p>You can refer to the following:<br />
#{bean.property}<br />
#{request.getParamenter(&#8221;action)}<br />
#{request.getAttribute(action&#8221;)}<br />
You can inject these directly into bean properties instead of having to do it in code. Very cool! This is just scratching the surface. Go now and checkout the source. There are some amazing things you can do with collections similar to what you can do with the Bean predicate except more succintly. SpEL can be used as a configuration tool (both in applicationContext.xml as well as with Spring annotations) as well as a general purpose parser a la apache commons beanutils via the SpELAntlrExpressionParser.</p>
<p>SpEL is nice but there is no type safety or syntax checking. It seems to impart a funtional flavor when used with collections.<br />
Craig had one test class with individual test methods that showcased each of the capabilities of SpEL. He would make a change in the test method and hit save and the test would be automatically run giving him immediate feedback on pass or fail. He was using <a href="http://infinitest.org/web/guest/home">Infinitest</a> who are handing out free licenses for individual developers. It uses JUnit. I hope they also provide TestNG support.</p>
<p><strong><a href="http://www.springone2gx.com/conference/new_orleans/2009/10/session?id=15643">Working with Spring Web Flow &#8211; Keith Donald</a></strong></p>
<p>I was convinced to attend this session after the Spring Faces session. Spring Web Flow is a stateful web conversation framework. The flow persistence strategy is pluggable. By default it uses Session but it doesn&#8217;t necessarily have to be session. Useful in a multi-step progression type user interaction as well as in single page AJAXy interactions.<br />
Keith showed how pages are bookmarkable and how the browser Back button works as it  should. Web flow uses an encrypted key in the URL parameter for the key. Example e5s1 where the 5 indicates the execution of time of this execution and the 1 indicates the snapshot or step in the flow. You can modify the key in the URL and the URL is still addressable.<br />
The execution has a lifecycle and upon completion it is garbage collected which means it is no longer active. It does automatic session cleanup after the flow ends. Spring Web Flow can replace t:saveState and all the extra coding that goes with it to maintain state particularily in multi-page flows.<br />
There was a question about where we can introspect the flow execution repository for use cases such as bread crumbs navigation. Apparently you can but it is still very involved and this is being addressed in the next version. Keith demonstrated the hotel web application sample from Spring and the flow for the booking scaenario. External flow definition modules are compiled and refreshed upon changing without app server restarts. Code completion is available in the editor (STS?)</p>
<p>Can use evaluate to execute some method and assign to a scoped varaible. SpEL can be used in Web Flow. The model attribute on view-state is used to bind form parameters to bean properties. I didn&#8217;t quite catch the validation part of it.</p>
<p>I definitely need to take a good look at Spring Web Flow and it&#8217;s integration with JSF.</p>
<p><strong><a href="http://www.springone2gx.com/conference/new_orleans/2009/10/session?id=15654">Spring Integration 2.0 Preview &#8211; Mark Fisher</a></strong></p>
<p>Spring integration provides an embedded message bus that lives within an applicationContext with all components being Spring managed objects. Many of the patterns from the Enterprise Integration Patterns books are implemented. A message wraps around a payload (XML/java Object) along with headers. A Message Channel is the communication link between producers and consumers and provides loose coupling.</p>
<p>This is another one of those core technologies that I think can underpin the architecture of a flexible enterprise application.</p>
<p><strong><a href="http://www.springone2gx.com/conference/new_orleans/2009/10/session?id=15651">Case Study: SRM 2.0 &#8211; A next generation shared resource management system built on SpringSource dm Server &#8211; Matt Stine</a></strong><br />
This session was about how St. Jude Children&#8217;s Research Hospital rebuilt their Shared Resource Management System as a modular application to be extensible to meet growing business needs. The business need was to track samples as they progressed through the different stages in a laboratory. There are several labroatories and each usually has it&#8217;s own set of unique requirements.<br />
The previous system consisted of:</p>
<ol>
<li> Core domain model</li>
<li> Core services platform</li>
<li> Web platform</li>
<li> Laboratory specific extensions
<ol>
<li> DB Schema &#8211; extendable by hanging tables of existing tables</li>
<li> Domain model</li>
<li> Services</li>
</ol>
</li>
</ol>
<p>Overall the shema had 200 tables that was becoming unmanageable. And they had to follow a monolithic deployment model. When it was decided that they need to rethink the application it was based around extensiblity</p>
<ol>
<li>primarily by configuration</li>
<li> feature modularity</li>
</ol>
<p>An <a href="http://en.wikipedia.org/wiki/Entity-attribute-value_model">EAV data model</a> was chosen for it&#8217;s extensibility. The system was built around some core concepts such as a kernel and business activity source. Using OSGi as the underlying  platform, they were able to introduce new technologies behind an OSGi service without running risks of breaking other parts of the application. From a development perspective OSGi lets them ship the interface out to a 3rd party team and have them implement it.</p>
<p>Used 3 to 4 days of consulting work from SpringSource the team was able to ramp up on OSGi, Spring dm server and STS starting in January &#8216;09. They then spent the first 3 to 4 months working out the kinks since the tools were still in their 1.0.0 version. In their experience, adopting OSGi led to a short time fall-of in productivity as they ramped up on the technology but they are now reaping the long term benefits of modularity. Their reporting solution was implemented against a real time data mart that is relational and using messaging via Spring Integration and a databus architecture.</p>
<p>It was pretty awesome to see that they had developed a modular enterprise application based on OSGi leveraging Spring Integration. After the presentation I got the chance to talk to the technical lead on this project from St. Jude. He told me how respecting the small set of simple tenets of good application design enforced by OSGi, depending on interfaces, and use of shared bundles was really the reason for their success. OSGi is not complex and there is no need to make it complex. Wise words.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2009/10/musings-of-a-springone-2009-attendee-day-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

