<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Daryl Mathison&#039;s Java Blog</title>
	<atom:link href="http://darylmathison.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://darylmathison.wordpress.com</link>
	<description>Java Information, Tips and Tricks</description>
	<lastBuildDate>Wed, 03 Nov 2010 18:06:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='darylmathison.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/aa8e490ae6b5cb8a30215af517f75ec0?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Daryl Mathison&#039;s Java Blog</title>
		<link>http://darylmathison.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://darylmathison.wordpress.com/osd.xml" title="Daryl Mathison&#039;s Java Blog" />
	<atom:link rel='hub' href='http://darylmathison.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Ajax + Servlet</title>
		<link>http://darylmathison.wordpress.com/2010/10/20/ajax-servlet/</link>
		<comments>http://darylmathison.wordpress.com/2010/10/20/ajax-servlet/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 17:13:09 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/?p=92</guid>
		<description><![CDATA[Introduction Found myself with some spare time on my hands and decided to figure out how Ajax works and develop a little example for the blog seeing that I haven&#8217;t updated since near infinity in Internet time. Ajax Ajax (AJAX) stands for Asynchronous JavaScript and XML. It is a technique using JavaScript and server objects [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=92&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Found myself with some spare time on my hands and decided to figure out how Ajax works and develop a little example for the blog seeing that I haven&#8217;t updated since near infinity in Internet time.</p>
<h3>Ajax</h3>
<p>Ajax (AJAX) stands for Asynchronous JavaScript and XML.  It is a technique using JavaScript and server objects to change webpage content only for a specific piece of the webpage without refreshing the whole page.  This is different from normal JavaScript because the change includes an exchange of data with the server.  This allows access to services that are found on a server like databases and business logic.  Users have an experience that is more interactive.  A good example would be a series of pictures being displayed that are only accessible from a database.  Each time a picture is clicked it changes to a new picture.</p>
<p>An Ajax request has two major parts, the request and the interactive server portion that hands out the data the request wants.  The request creates a XMLHttpRequest and sends the request as a standard html post or get.  The server responds in kind with XML.  If done right, this draws a hard line between the presentation layer and the business layer.  This is not a new idea.  JSPs have backing beans.  Thick clients have remote objects (RMI anyone?).  Ajax in my opinion goes farther by removing the need for a specific type of server side support it needs.  It doesn&#8217;t care if it is PHP or a Servlet.  All it cares for is the contract to be fulfilled.  This creates a situation where the implementation of the server objects can change completely and the webpage won&#8217;t care.  That&#8217;s right, one day a bunch of server scripts can be used and then six months later, a set of servlets can be used to replace them.  That is an extreme example because of having to stand up a web container in place of the server that supports scripts but a lot of the mapping requests to the server can be done rather painlessly using servlet mapping.</p>
<h2>Example</h2>
<p>Let&#8217;s get to an example of Ajax in action.  This example updates three fields with the time from different time zones.  The first one is local time, the second is GMT and the last time is for New York.  I am using a JavaScript library named jQuery to help write the JavaScript and the Ajax request.  For more information on jQuery, go to <a href="http://jquery.com/">http://jquery.com/</a>.  Since this is a Java blog, the server object is a servlet named TimeServlet.java.  All the code can be downloaded from <a href="http://darylmathisonblog.googlecode.com/svn/example/TimeAjax">http://darylmathisonblog.googlecode.com/svn/example/TimeAjax/trunk</a>.  The example was created in Eclipse so Eclipse users can import it as a project.  The web container I used is a Tomcat 5.5 server.</p>
<h3>Webpage</h3>
<p>Here is the webpage that makes the requests to the server object.  It puts the current time from three different time zones into an html table beneath the label of the time zone it represents.  The current time is displayed every time the &#8220;Show Times&#8221; button is clicked.</p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">html</span><span style="color:teal;">&gt;</span><span style="color:teal;"> </span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">head</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">meta</span><span style="color:#7f007f;"> http-equiv</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;Content-Type&#8221;</em></span><span style="color:#7f007f;"> content</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;text/html; charset=ISO-8859-1&#8243;</em></span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">title</span><span style="color:teal;">&gt;</span><span style="color:black;">AJAX Time</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">title</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">script</span><span style="color:#7f007f;"> type</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;text/javascript&#8221;</em></span><span style="color:#7f007f;"> src</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;jquery.js&#8221;</em></span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">script</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">script</span><span style="color:#7f007f;"> type</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;text/javascript&#8221;</em></span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">$(document).ready(</span><span style="color:#7f0055;"><strong>function</strong></span><span style="color:black;">() {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>var</strong></span><span style="color:black;"> timeid = </span><span style="color:#2a00ff;">&#8220;timeid&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> $(</span><span style="color:#2a00ff;">&#8220;#timebutton&#8221;</span><span style="color:black;">).click(</span><span style="color:#7f0055;"><strong>function</strong></span><span style="color:black;">() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> $(</span><span style="color:#2a00ff;">&#8220;#local&#8221;</span><span style="color:black;">).load(</span><span style="color:#2a00ff;">&#8216;/TimeAjax/TimeServlet&#8217;</span><span style="color:black;">, {timeid: </span><span style="color:#2a00ff;">&#8220;local&#8221;</span><span style="color:black;">});</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> $(</span><span style="color:#2a00ff;">&#8220;#GMT&#8221;</span><span style="color:black;">).load(</span><span style="color:#2a00ff;">&#8220;/TimeAjax/TimeServlet&#8221;</span><span style="color:black;">, {timeid: </span><span style="color:#2a00ff;">&#8220;GMT&#8221;</span><span style="color:black;">});</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> $(</span><span style="color:#2a00ff;">&#8220;#newyork&#8221;</span><span style="color:black;">).load(</span><span style="color:#2a00ff;">&#8220;/TimeAjax/TimeServlet&#8221;</span><span style="color:black;">, {timeid: </span><span style="color:#2a00ff;">&#8220;newyork&#8221;</span><span style="color:black;">});</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> });</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> $(</span><span style="color:#2a00ff;">&#8220;#errorMsg&#8221;</span><span style="color:black;">).ajaxError(</span><span style="color:#7f0055;"><strong>function</strong></span><span style="color:black;">(event, request, settings, error){</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> $(</span><span style="color:#7f0055;"><strong>this</strong></span><span style="color:black;">).append(</span><span style="color:#2a00ff;">&#8220;ajax call failed: &#8220;</span><span style="color:black;"> + settings.url);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> alert(settings.url);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> });</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">});</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">script</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">head</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">body</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">table</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">tr</span><span style="color:teal;">&gt;</span><span style="color:black;"> </span><span style="color:teal;"> </span></span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;"> &lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><span style="color:black;">Local Time</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><span style="color:black;">GMT Time</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><span style="color:black;">New York Time</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">tr</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">tr</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;&lt;</span><span style="color:#3f7f7f;">span</span><span style="color:#7f007f;"> id</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;local&#8221;</em></span><span style="color:teal;">&gt;</span><span style="color:#2a00ff;">&amp;nbsp;</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">span</span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;&lt;</span><span style="color:#3f7f7f;">span</span><span style="color:#7f007f;"> id</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;GMT&#8221;</em></span><span style="color:teal;">&gt;</span><span style="color:#2a00ff;">&amp;nbsp;</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">span</span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;&lt;</span><span style="color:#3f7f7f;">span</span><span style="color:#7f007f;"> id</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;newyork&#8221;</em></span><span style="color:teal;">&gt;</span><span style="color:#2a00ff;">&amp;nbsp;</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">span</span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">tr</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">tr</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:#7f007f;"> colspan</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;3&#8243;</em></span><span style="color:teal;">&gt;&lt;</span><span style="color:#3f7f7f;">div</span><span style="color:#7f007f;"> id</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;errorMsg&#8221;</em></span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">div</span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">tr</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">table</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">button</span><span style="color:#7f007f;"> id</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;timebutton&#8221;</em></span><span style="color:teal;">&gt;</span><span style="color:black;">Show Times</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">button</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">body</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">html</span><span style="color:teal;">&gt;<br />
</span></span></p>
<p>The load function is where the actual request happens.</p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">$(</span><span style="color:#2a00ff;">&#8220;#local&#8221;</span><span style="color:black;">).load(</span><span style="color:#2a00ff;">&#8216;/TimeAjax/TimeServlet&#8217;</span><span style="color:black;">, {timeid: </span><span style="color:#2a00ff;">&#8220;local&#8221;</span><span style="color:black;">});</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">$(</span><span style="color:#2a00ff;">&#8220;#GMT&#8221;</span><span style="color:black;">).load(</span><span style="color:#2a00ff;">&#8220;/TimeAjax/TimeServlet&#8221;</span><span style="color:black;">, {timeid: </span><span style="color:#2a00ff;">&#8220;GMT&#8221;</span><span style="color:black;">});</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">$(</span><span style="color:#2a00ff;">&#8220;#newyork&#8221;</span><span style="color:black;">).load(</span><span style="color:#2a00ff;">&#8220;/TimeAjax/TimeServlet&#8221;</span><span style="color:black;">, {timeid: </span><span style="color:#2a00ff;">&#8220;newyork&#8221;</span><span style="color:black;">});<br />
</span></span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">This is a series of requests for items that have the id of local, GMT and newyork respectively.  They all call the servlet with different timeids to tell the servlet what time zone to display back.  These requests are sent out when the &#8220;Show Times&#8221; button is clicked.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;&lt;</span><span style="color:#3f7f7f;">span</span><span style="color:#7f007f;"> id</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;local&#8221;</em></span><span style="color:teal;">&gt;</span><span style="color:#2a00ff;">&amp;nbsp;</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">span</span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;&lt;</span><span style="color:#3f7f7f;">span</span><span style="color:#7f007f;"> id</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;GMT&#8221;</em></span><span style="color:teal;">&gt;</span><span style="color:#2a00ff;">&amp;nbsp;</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">span</span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:teal;">&lt;</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;&lt;</span><span style="color:#3f7f7f;">span</span><span style="color:#7f007f;"> id</span><span style="color:black;">=</span><span style="color:#2a00ff;"><em>&#8220;newyork&#8221;</em></span><span style="color:teal;">&gt;</span><span style="color:#2a00ff;">&amp;nbsp;</span><span style="color:teal;">&lt;/</span><span style="color:#3f7f7f;">span</span><span style="color:teal;">&gt;&lt;/</span><span style="color:#3f7f7f;">td</span><span style="color:teal;">&gt;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">These are the three table cells that are going to be changed when the responses come back.<br />
</span></p>
<h3>Servlet</h3>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">Here is the code for the TimeServlet that services the requests from the webpage.  It grabs the timeid from the request and uses <a href="http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html"></a></span>java.text.SimpleDateFormat<span style="color:black;"> to format the current time into the respective time zone.</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> TimeServlet </span><span style="color:#7f0055;"><strong>extends</strong></span><span style="color:black;"> HttpServlet {</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> long</strong></span><span style="color:black;"> </span><span style="color:#0000c0;"><em> serialVersionUID</em></span><span style="color:black;"> = 1L;</span><span style="color:black;"> </span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"><br />
</span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * Default constructor. </span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> TimeServlet() {</span><span style="color:black;"><br />
</span><span style="color:#3f7f5f;">// </span><span style="color:#7f9fbf;"><strong>TODO</strong></span><span style="color:#3f7f5f;"> Auto-generated constructor stub</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@see</strong></span><span style="color:#3f5fbf;"> HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>protected</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> doGet(HttpServletRequest request, HttpServletResponse response) </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> ServletException, IOException {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String timeid = request.getParameter(</span><span style="color:#2a00ff;">&#8220;timeid&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;timeid is &#8220;</span><span style="color:black;"> + timeid);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (timeid != </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;"> &amp;&amp; timeid.length() &gt; 0 ) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.setContentType(</span><span style="color:#2a00ff;">&#8220;text/xml&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.setHeader(</span><span style="color:#2a00ff;">&#8220;Cache-Control&#8221;</span><span style="color:black;">, </span><span style="color:#2a00ff;">&#8220;no-cache&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Calendar cal = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><span style="color:black;"> </span></span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">String dateformat = </span><span style="color:#2a00ff;">&#8220;HH:mm&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> SimpleDateFormat format = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (</span><span style="color:#2a00ff;">&#8220;local&#8221;</span><span style="color:black;">.equals(timeid)) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;in local&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> cal = Calendar.<em>getInstance</em>();</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> SimpleDateFormat(dateformat);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.getWriter().write(</span><span style="color:#2a00ff;">&#8220;&lt;time&gt;&#8221;</span><span style="color:black;"> + </span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format.format(cal.getTime())+ </span><span style="color:#2a00ff;">&#8220;&lt;/time&gt;&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> if</strong></span><span style="color:black;">(</span><span style="color:#2a00ff;">&#8220;GMT&#8221;</span><span style="color:black;">.equals(timeid)) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> cal = Calendar.<em>getInstance</em>();</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> SimpleDateFormat(dateformat);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format.setTimeZone(TimeZone.<em>getTimeZone</em>(</span><span style="color:#2a00ff;">&#8220;GMT&#8221;</span><span style="color:black;">));</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.getWriter().write(</span><span style="color:#2a00ff;">&#8220;&lt;time&gt;&#8221;</span><span style="color:black;"> + </span><span style="color:black;">format.format(cal.getTime()) + </span><span style="color:#2a00ff;">&#8220;&lt;/time&gt;&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> if</strong></span><span style="color:black;">(</span><span style="color:#2a00ff;">&#8220;newyork&#8221;</span><span style="color:black;">.equals(timeid)) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> TimeZone tz = TimeZone.<em>getTimeZone</em>(</span><span style="color:#2a00ff;">&#8220;America/New_York&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> cal = Calendar.<em>getInstance</em>();</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> SimpleDateFormat(dateformat);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format.setTimeZone(tz);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.getWriter().write(</span><span style="color:#2a00ff;">&#8220;&lt;time&gt;&#8221;</span><span style="color:black;"> + </span><span style="color:black;">format.format(cal.getTime())+ </span><span style="color:#2a00ff;">&#8220;&lt;/time&gt;&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.getWriter().write(</span><span style="color:#2a00ff;">&#8220;cannot find timeid&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.getWriter().write(</span><span style="color:#2a00ff;">&#8220;there is no timeid&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@see</strong></span><span style="color:#3f5fbf;"> HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>protected</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> doPost(HttpServletRequest request, HttpServletResponse response) </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> ServletException, IOException {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> doGet(request, response);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">The real work happens in the doGet method.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">String timeid = request.getParameter(</span><span style="color:#2a00ff;">&#8220;timeid&#8221;</span><span style="color:black;">);<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">Here is where the timeid passed from the Ajax request is pulled from the HttpServletRequest.  The passed timeid variable is tested for validity to ward off any <a href="http://download.oracle.com/javase/6/docs/api/java/lang/NullPointerException.html"></a></span>NullPointerExceptions<span style="color:black;">.<br />
</span></span></p>
<p>The below code details what I did to display the time for the local time zone.</p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (</span><span style="color:#2a00ff;">&#8220;local&#8221;</span><span style="color:black;">.equals(timeid)) {</span><span style="color:#7f0055;"><strong><br />
</strong></span></span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;in local&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> cal = Calendar.<em>getInstance</em>();</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> SimpleDateFormat(dateformat);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.getWriter().write(</span><span style="color:#2a00ff;">&#8220;&lt;time&gt;&#8221;</span><span style="color:black;"> +</span><span style="color:black;"> format.format(cal.getTime())+ </span><span style="color:#2a00ff;">&#8220;&lt;/time&gt;&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">This is easy because Java defaults to the local locale and time zone.  It gets trickier for GMT and the New York time zones but not too much because all that needs to happen is setting the time zone of the formatter.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> if</strong></span><span style="color:black;">(</span><span style="color:#2a00ff;">&#8220;GMT&#8221;</span><span style="color:black;">.equals(timeid)) {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> cal = Calendar.<em>getInstance</em>();</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> SimpleDateFormat(dateformat);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> format.setTimeZone(TimeZone.<em>getTimeZone</em>(</span><span style="color:#2a00ff;">&#8220;GMT&#8221;</span><span style="color:black;">));</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> response.getWriter().write(</span><span style="color:#2a00ff;">&#8220;&lt;time&gt;&#8221;</span><span style="color:black;"> + </span><span style="color:black;"> format.format(cal.getTime()) + </span><span style="color:#2a00ff;">&#8220;&lt;/time&gt;&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;"> </span></p>
<table style="border-collapse:collapse;" border="0">
<col style="width:6px;"></col>
<tbody>
<tr>
<td style="padding:1px;" valign="middle"></td>
</tr>
</tbody>
</table>
<p>This is repeated for the New York time zone using the &#8220;America/New_York&#8221; time zone.</p>
<h2>Last Thoughts</h2>
<p>This is another tool in the toolbox for web development.  It allows web components to be more interactive enhancing the user experience on your website.  It can also be used to further separate presentation and business logic.  If you have any comments or questions about this post, please leave them below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/92/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=92&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/10/20/ajax-servlet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>Technologies I am Working on</title>
		<link>http://darylmathison.wordpress.com/2010/06/30/technologies-i-am-working-on/</link>
		<comments>http://darylmathison.wordpress.com/2010/06/30/technologies-i-am-working-on/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 15:27:42 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[For the Little Guy]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/2010/06/30/technologies-i-am-working-on/</guid>
		<description><![CDATA[I have been combining technologies for my For the Little Guy application described in the documents in the download section. Next post I am going to give a demonstration of how I combined Eclipse, Spring Framework and Maven. Currently work has been quite demanding so I have slipped on posts. Here are some links I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=88&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been combining technologies for my <a href="http://fortlg.googlecode.com">For the Little Guy</a> application described in the documents in the download section.  Next post I am going to give a demonstration of how I combined Eclipse, Spring Framework and Maven.  Currently work has been quite demanding so I have slipped on posts.  Here are some links I have been using to make it all work.</p>
<p>Maven –                           <a href="http://maven.apache.com">http://maven.apache.com</a></p>
<p>Spring Framework &#8211;     <a href="http://www.springsource.com/">http://www.springsource.com/</a></p>
<p>Eclipse –                           <a href="http://www.eclipse.org">http://www.eclipse.org</a></p>
<p>Maven Eclipse Tools &#8211; <a href="http://www.eclipse.org/m2e/">http://www.eclipse.org/m2e/</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/88/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=88&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/06/30/technologies-i-am-working-on/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>Searching Jars</title>
		<link>http://darylmathison.wordpress.com/2010/06/04/searching-jars/</link>
		<comments>http://darylmathison.wordpress.com/2010/06/04/searching-jars/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 14:33:51 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/?p=84</guid>
		<description><![CDATA[I have run into the problem where I am looking for the right jar file from a production environment and bring it to a development environment. The problem is that when I get the ClassNotFoundException or the &#8220;I can&#8217;t find this class&#8221; error from the compiler, the right jar file does not come out and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=84&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have run into the problem where I am looking for the right jar file from a production environment and bring it to a development environment.  The problem is that when I get the ClassNotFoundException or the &#8220;I can&#8217;t find this class&#8221; error from the compiler, the right jar file does not come out and say, &#8220;Include me in your classpath!&#8221;  To solve that problem I created an application to do it.  It searches recursively through directories and finds all the jar files.  Once it finds a jar file, the jar file is opened and a regex search is done to the names to find the missing resource.  If the resource is found, the jar file that contains the resource is returned.  Otherwise, null is printed on the screen.  Below is the class file:</p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> JarsSearch {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public </strong></span><span style="color:#7f0055;"><strong>static</strong></span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>FILE_PARAM</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;&#8211;file&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>ROOT_PARAM</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;&#8211;root&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>HELP_PARAM_PATTERN</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;-? | &#8211;help&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>DEFAULT_ROOT</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;.&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;">filename</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;">rootpath</span><span style="color:black;"> = </span><span style="color:#0000c0;"><em>DEFAULT_ROOT</em></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> JarsSearch() {}</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> JarsSearch(String root, String filename) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> setRootpath(root);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> setFilename(filename);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@return</strong></span><span style="color:#3f5fbf;"> the filename</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getFilename() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:#0000c0;"> filename</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@param</strong></span><span style="color:#3f5fbf;"> filename the filename to set</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setFilename(String filename) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>this</strong></span><span style="color:black;">.</span><span style="color:#0000c0;">filename</span><span style="color:black;"> = filename;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@return</strong></span><span style="color:#3f5fbf;"> the <span style="text-decoration:underline;">rootpath</span></span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getRootpath() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:#0000c0;"> rootpath</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@param</strong></span><span style="color:#3f5fbf;"> rootpath the <span style="text-decoration:underline;">rootpath</span> to set</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setRootpath(String rootpath) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>this</strong></span><span style="color:black;">.</span><span style="color:#0000c0;">rootpath</span><span style="color:black;"> = rootpath;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/** </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * looks for the file on any jar under the <span style="text-decoration:underline;">rootpath</span><br />
</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@return</strong></span><span style="color:#3f5fbf;"> String the path of the containing jar file</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><span style="color:black;"><br />
</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String find() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String jarFilename = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> File[] jarsNdirs = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> File(getRootpath()).listFiles(</span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> JarFileFilter());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Arrays.<em>sort</em>(jarsNdirs, </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> ListingComparator());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>for</strong></span><span style="color:black;"> (File f: jarsNdirs) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (f.isDirectory()) {</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> JarsSearch js = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> JarsSearch(f.getCanonicalPath(), getFilename());</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> jarFilename = js.find();</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (Utils.<em>isStringValid</em>(jarFilename)) {</span><br />
</span></p>
<p style="padding-left:180px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>break</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong> else</strong></span><span style="color:black;"> { </span><span style="color:#3f7f5f;">// if not a directory, then a jar file</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> JarEntry entry;</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Pattern p = Pattern.<em>compile</em>(getFilename());</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Matcher m = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> JarFile jarFile = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> JarFile(f, </span><span style="color:#7f0055;"><strong>false</strong></span><span style="color:black;">, JarFile.</span><span style="color:#0000c0;"><em>OPEN_READ</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Enumeration &lt;JarEntry&gt; e = jarFile.entries();</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>while</strong></span><span style="color:black;"> (e.hasMoreElements()) {</span><br />
</span></p>
<p style="padding-left:180px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> entry = e.nextElement();</span><br />
</span></p>
<p style="padding-left:180px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f7f5f;">//System.out.println(&#8220;printing entry name: &#8221; + entry.getName());</span><br />
</span></p>
<p style="padding-left:180px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> m = p.matcher(entry.getName());</span><br />
</span></p>
<p style="padding-left:180px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (m.matches()) {</span><br />
</span></p>
<p style="padding-left:210px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> jarFilename = f.getCanonicalPath();</span><br />
</span></p>
<p style="padding-left:180px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> jarFile.close();</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;"> (Exception e) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> e.printStackTrace();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> jarFilename;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String toString() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:#2a00ff;"> &#8220;JarsSearch[ filename pattern - "</span><span style="color:black;"> + getFilename() + </span><span style="color:#2a00ff;">", root path - "</span><span style="color:black;"> + getRootpath() + </span><span style="color:#2a00ff;">" ]&#8220;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@param</strong></span><span style="color:#3f5fbf;"> args</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> main(String[] args) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String filepattern = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String rootdir = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>for</strong></span><span style="color:black;">(</span><span style="color:#7f0055;"><strong>int</strong></span><span style="color:black;"> i = 0; i &lt; args.</span><span style="color:#0000c0;">length</span><span style="color:black;">; i++) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (args[i].matches(</span><span style="color:#0000c0;"><em>HELP_PARAM_PATTERN</em></span><span style="color:black;">)) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> <em>showArgs</em>();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:#7f0055;"><strong> if</strong></span><span style="color:black;"> (args[i].matches(</span><span style="color:#0000c0;"><em>FILE_PARAM</em></span><span style="color:black;">)) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> ((i + 1) &lt; args.</span><span style="color:#0000c0;">length</span><span style="color:black;">) {</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> filepattern = args[i + 1];</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> i++;</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>continue</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:#7f0055;"><strong> if</strong></span><span style="color:black;"> (args[i].matches(</span><span style="color:#0000c0;"><em>ROOT_PARAM</em></span><span style="color:black;">)) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> ((i + 1) &lt; args.</span><span style="color:#0000c0;">length</span><span style="color:black;">) {</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> rootdir = args[i + 1];</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> i++;</span><br />
</span></p>
<p style="padding-left:150px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>continue</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> <em>showArgs</em>();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> JarsSearch js = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> JarsSearch();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (!Utils.<em>isStringValid</em>(filepattern) || </span><span style="color:#0000c0;"><em>ROOT_PARAM</em></span><span style="color:black;">.equals(filepattern)) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> <em>showArgs</em>();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> js.setFilename(filepattern.trim());</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (Utils.<em>isStringValid</em>(rootdir) &amp;&amp; !</span><span style="color:#0000c0;"><em>FILE_PARAM</em></span><span style="color:black;">.equals(rootdir)) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> js.setRootpath(rootdir.trim());</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(js.find());</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.<em>exit</em>(0);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>protected</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> showArgs() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;&#8211;help: this listing&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;-?    : this listing&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#0000c0;"><em>ROOT_PARAM</em></span><span style="color:black;"> + </span><span style="color:#2a00ff;">&#8220;: where to start looking from.  Default = &#8216;.&#8217;&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#0000c0;"><em>FILE_PARAM</em></span><span style="color:black;"> + </span><span style="color:#2a00ff;">&#8220;: regex pattern of a file to look for in a jar file&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.<em>exit</em>(0);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">}</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>class</strong></span><span style="color:black;"> JarFileFilter </span><span style="color:#7f0055;"><strong>implements</strong></span><span style="color:black;"> java.io.FileFilter {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> boolean</strong></span><span style="color:black;"> accept(File arg0) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String name = arg0.getName();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> ( arg0.canRead() &amp;&amp; ( arg0.isDirectory() || name.endsWith(</span><span style="color:#2a00ff;">&#8220;jar&#8221;</span><span style="color:black;">) ) );</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">}</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>class</strong></span><span style="color:black;"> ListingComparator </span><span style="color:#7f0055;"><strong>implements</strong></span><span style="color:black;"> java.util.Comparator&lt;File&gt; {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> int</strong></span><span style="color:black;"> compare(File arg0, File arg1) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (arg0.isDirectory() &amp;&amp; !arg1.isDirectory()) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> -1;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:#7f0055;"><strong> if</strong></span><span style="color:black;"> (!arg0.isDirectory() &amp;&amp; arg1.isDirectory()) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> 1;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> { </span><span style="color:#3f7f5f;">// ( (arg0.isDirectory() &amp;&amp; arg1.isDirectory()) || (arg0.isFile() &amp;&amp; arg1.isFile()) )</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> arg0.compareTo(arg1);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">To get a complete copy of all the source code found on this blog, go to </span></span><a href="http://darylmathisonblog.googlecode.com/svn/example/" target="_blank">http://darylmathisonblog.googlecode.com/svn/example/</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=84&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/06/04/searching-jars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>Using the Java Naming and Directory Interface (JNDI) (Part 3)</title>
		<link>http://darylmathison.wordpress.com/2010/05/28/using-the-java-naming-and-directory-interface-jndi-part-3-3/</link>
		<comments>http://darylmathison.wordpress.com/2010/05/28/using-the-java-naming-and-directory-interface-jndi-part-3-3/#comments</comments>
		<pubDate>Fri, 28 May 2010 21:00:30 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jndi]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/2010/05/26/using-the-java-naming-and-directory-interface-jndi-part-3-3/</guid>
		<description><![CDATA[Introduction In the last two posts about using JNDI, I have shown examples of how one can get and add information in a number of ways. The first post discussed LDAP servers and showed a quick program one could use to retrieve entries stored in the server.  The entry was represented as a collection of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=67&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-family:Times New Roman;font-size:12pt;"><strong>Introduction</strong><br />
</span></p>
<p><span style="font-family:Times New Roman;font-size:12pt;">In the last two posts about using JNDI, I have shown examples of how one can get and add information in a number of ways. The first <a title="Using the Java Naming and Directory Interface(JNDI) (Part 1)" href="http://wp.me/pRAwH-t" target="_blank"><span style="color:blue;text-decoration:underline;">post</span></a> discussed LDAP servers and showed a quick program one could use to retrieve entries stored in the server.  The entry was represented as a collection of attributes.  The second <a title="Using the Java Naming and Directory  Interface (JNDI) (Part 3)" href="http://wp.me/pRAwH-E" target="_blank"><span style="color:blue;text-decoration:underline;">post</span></a> demonstrated a number of ways to store and retrieve Java objects on the server.  The two examples demonstrated how objects could be saved in base-64 or as a reference to another object.  This post will demonstrate how a set of attributes can be turned into a Java object and how to store that object as a set of attributes.<br />
</span></p>
<p><span style="font-family:Times New Roman;font-size:12pt;"><strong>Setup</strong><br />
</span></p>
<p><span style="font-family:Times New Roman;font-size:12pt;">To make a Java object get saved as a set of entry attributes, I have to refactor the User class.  By implementing the <a href="http://java.sun.com/javase/6/docs/api/javax/naming/directory/DirContext.html" target="_blank">DirContext</a>, the class will organize itself into a set of attributes. Here is the new version of <a href="http://darylmathisonblog.googlecode.com/svn/trunk/src/org/mathison/jndi/example2/User.java" target="_blank">User</a>, <a href="http://darylmathisonblog.googlecode.com/svn/trunk/src/org/mathison/jndi/example4/AttrUser.java" target="_blank">AttrUser</a>.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> AttrUser </span><span style="color:#7f0055;"><strong>extends</strong></span><span style="color:black;"> DirContextAdapter {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>INIT_VALUE</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;none&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>NAME_ATTR</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;cn&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>UID_ATTR</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;uid&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>EMAIL_ATTR</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;mail&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;"><em>SN_ATTR</em></span><span style="color:black;"> = </span><span style="color:#2a00ff;">&#8220;sn&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:black;"> Attributes </span><span style="color:#0000c0;">attrs</span><span style="color:black;">;</span></span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"><br />
</span></span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> AttrUser() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#0000c0;">attrs</span><span style="color:black;"> = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> BasicAttributes(</span><span style="color:#7f0055;"><strong>true</strong></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute oc = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> BasicAttribute(</span><span style="color:#2a00ff;">&#8220;objectclass&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> oc.add(</span><span style="color:#2a00ff;">&#8220;inetOrgPerson&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> oc.add(</span><span style="color:#2a00ff;">&#8220;top&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.put(oc);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.put(</span><span style="color:#0000c0;"><em>NAME_ATTR</em></span><span style="color:black;">, </span><span style="color:#0000c0;"><em>INIT_VALUE</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.put(</span><span style="color:#0000c0;"><em>UID_ATTR</em></span><span style="color:black;">, </span><span style="color:#0000c0;"><em>INIT_VALUE</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.put(</span><span style="color:#0000c0;"><em>EMAIL_ATTR</em></span><span style="color:black;">, </span><span style="color:#0000c0;"><em>INIT_VALUE</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.put(</span><span style="color:#0000c0;"><em>SN_ATTR</em></span><span style="color:black;">, </span><span style="color:#0000c0;"><em>INIT_VALUE</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getName() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String name = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(</span><span style="color:#0000c0;"><em>NAME_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> name =  a.get().toString();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;"> (NamingException e) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f7f5f;">// </span><span style="color:#7f9fbf;"><strong>TODO</strong></span><span style="color:#3f7f5f;"> Auto-generated catch block</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> e.printStackTrace();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> name;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setName(String name) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(</span><span style="color:#0000c0;"><em>NAME_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a.clear();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a.add(name);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getUserid() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String userid = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(</span><span style="color:#0000c0;"><em>UID_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> userid = a.get().toString();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;">(NamingException ne) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ne.printStackTrace();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> userid;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setUserid(String userid) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(</span><span style="color:#0000c0;"><em>UID_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a.clear();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a.add(userid);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getEmail() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String email = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(</span><span style="color:#0000c0;"><em>EMAIL_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> email = a.get().toString();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;">(NamingException ne) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ne.printStackTrace();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> email;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setEmail(String email) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(</span><span style="color:#0000c0;"><em>EMAIL_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a.clear();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a.add(email);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setLastname(String lastname) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(</span><span style="color:#0000c0;"><em>SN_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a.clear();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a.add(lastname);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getLastname() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> String lastname = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = </span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(</span><span style="color:#0000c0;"><em>SN_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> lastname = a.get().toString();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;">(NamingException ne) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ne.printStackTrace();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> lastname;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Attributes getAttributes(Name arg0) </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> NamingException {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> getAttributes(arg0.toString());</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Attributes getAttributes(String arg0) </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> NamingException {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;">(arg0 != </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;"> &amp;&amp; arg0.length() != 0) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>throw</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> new</strong></span><span style="color:black;"> NameNotFoundException();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> (Attributes)</span><span style="color:#0000c0;">attrs</span><span style="color:black;">.clone();</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#646464;">@Override</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Attributes getAttributes(Name arg0, String[] arg1)</span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong> throws</strong></span><span style="color:black;"> NamingException {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> getAttributes(arg0.toString(), arg1);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#646464;">@Override</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Attributes getAttributes(String arg0, String[] arg1)</span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong> throws</strong></span><span style="color:black;"> NamingException {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (arg0 != </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;"> &amp;&amp; arg0.length() &gt; 0) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>throw</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> new</strong></span><span style="color:black;"> NameNotFoundException();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span></span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attributes ret = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> BasicAttributes(</span><span style="color:#7f0055;"><strong>true</strong></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>for</strong></span><span style="color:black;"> (String id: arg1) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ret.put(</span><span style="color:#0000c0;">attrs</span><span style="color:black;">.get(id));</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> ret;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p><span style="font-family:Times New Roman;font-size:12pt;"><span style="color:black;">The class looks a lot different now. Notice that backed the whole class with an instance of <a href="http://java.sun.com/javase/6/docs/api/javax/naming/directory/Attributes.html" target="_blank">Attributes</a>.  I thought the code looked cleaner that way.  Also notice that I extended a DirContextAdapter instead of implementing DirContext.  To keep clean looking code, created an adapter.   Another reason is because the context provider looks to store java objects in a certain order. The provider will look to store a java object as a reference first, serialized second and a context third.  So If I had extended User.java, the example would have not worked.  Another small difference is I now added lastname as one of the attributes to store.  Lastname is a required attribute of InetOrgPerson, and since I want AttrUser to be accepted like an InetOrgPerson, it needs to keep track like one.</span> The other class needed is the factory that will turn the context attributes back into an AttrUser.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> AttrUserFactory </span><span style="color:#7f0055;"><strong>implements</strong></span><span style="color:black;"> DirObjectFactory {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#646464;">@Override</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Object getObjectInstance(Object arg0, Name arg1, Context arg2,</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Hashtable&lt;?, ?&gt; arg3) </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> Exception {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f7f5f;">// because this is for an attributes based object, this factory method simply returns null.</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#646464;">@Override</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Object getObjectInstance(Object arg0, Name arg1, Context arg2,</span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Hashtable&lt;?, ?&gt; arg3, Attributes arg4) </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> Exception {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> AttrUser u = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (checkObject(arg0, arg4)) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> u = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> AttrUser();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Attribute a = arg4.get(AttrUser.</span><span style="color:#0000c0;"><em>NAME_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> u.setName(a.get().toString());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a = arg4.get(AttrUser.</span><span style="color:#0000c0;"><em>UID_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> u.setUserid(a.get().toString());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a = arg4.get(AttrUser.</span><span style="color:#0000c0;"><em>EMAIL_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> u.setEmail(a.get().toString());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> a = arg4.get(AttrUser.</span><span style="color:#0000c0;"><em>SN_ATTR</em></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> u.setLastname(a.get().toString());</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> u;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> boolean</strong></span><span style="color:black;"> checkObject(Object o, Attributes att) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>boolean</strong></span><span style="color:black;"> isAttrUser = </span><span style="color:#7f0055;"><strong>false</strong></span><span style="color:black;">;</span></span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (o </span><span style="color:#7f0055;"><strong>instanceof</strong></span><span style="color:black;"> DirContext) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;">(att != </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;"> &amp;&amp; att.size() &gt;= 4) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>boolean</strong></span><span style="color:black;"> areAttrThere = </span><span style="color:#7f0055;"><strong>true</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> areAttrThere &amp;= (att.get(AttrUser.</span><span style="color:#0000c0;"><em>NAME_ATTR</em></span><span style="color:black;">) != </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> areAttrThere &amp;= (att.get(AttrUser.</span><span style="color:#0000c0;"><em>SN_ATTR</em></span><span style="color:black;">) != </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> areAttrThere &amp;= (att.get(AttrUser.</span><span style="color:#0000c0;"><em>EMAIL_ATTR</em></span><span style="color:black;">) != </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> areAttrThere &amp;= (att.get(AttrUser.</span><span style="color:#0000c0;"><em>UID_ATTR</em></span><span style="color:black;">) != </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> isAttrUser = areAttrThere;</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span></span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> isAttrUser;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">}</span><br />
</span></p>
<p><span style="color:black;font-family:Times New Roman;font-size:12pt;">This class implements the same DirObjectFactory as the last time but fills out the attribute based function.  According to the DirObjectFactory contract, the function needs to return null if it is not a match for that data. Context providers can handle a range of factories.  It goes to each one and runs the factory call.  If the returned value is non-null or an exception, the traversal ends and it returns the value or allows the exception to be caught.  The setup is done, lets put it all together.<br />
</span></p>
<p><strong>The Lookup</strong></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> AttributeLookup {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>static</strong></span><span style="color:black;"> Hashtable&lt;String, String&gt; getEnv() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Hashtable&lt;String, String&gt; env = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> Hashtable&lt;String, String&gt;();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> env.put(Context.</span><span style="color:#0000c0;"><em>INITIAL_CONTEXT_FACTORY</em></span><span style="color:black;">, </span><span style="color:#2a00ff;">&#8220;com.sun.jndi.ldap.LdapCtxFactory&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> env.put(Context.</span><span style="color:#0000c0;"><em>PROVIDER_URL</em></span><span style="color:black;">, </span><span style="color:#2a00ff;">&#8220;ldap://localhost:10389/ou=Users,dc=example,dc=com&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> env.put(Context.</span><span style="color:#0000c0;"><em>OBJECT_FACTORIES</em></span><span style="color:black;">, AttrUserFactory.</span><span style="color:#7f0055;"><strong>class</strong></span><span style="color:black;">.getName());</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> env;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> main(String[] args) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> DirContext ctx = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> InitialDirContext(<em>getEnv</em>());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> AttrUser user = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> AttrUser();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setEmail(</span><span style="color:#2a00ff;">&#8220;joey@example.com&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setLastname(</span><span style="color:#2a00ff;">&#8220;Bourne&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setName(</span><span style="color:#2a00ff;">&#8220;Joey&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setUserid(</span><span style="color:#2a00ff;">&#8220;joey&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx.rebind(</span><span style="color:#2a00ff;">&#8220;uid=&#8221;</span><span style="color:black;"> + user.getUserid(), user);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span></span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f7f5f;">// read </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> AttrUser newUser = (AttrUser)ctx.lookup(</span><span style="color:#2a00ff;">&#8220;uid=&#8221;</span><span style="color:black;"> + user.getUserid());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;email is &#8220;</span><span style="color:black;"> + newUser.getEmail());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;"> (NamingException e) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> e.printStackTrace();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>finally</strong></span><span style="color:black;">{</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx.close();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;"> (Exception e) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> e.printStackTrace();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span></span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p><span style="color:black;font-family:Times New Roman;font-size:12pt;">This looks a lot like the other examples that I have covered.  The one exception is where I defined my object factory.  If I did not include the object factory, the lookup would have returned a set of Attributes like on the first example.  The data stored in the server shows this to be true.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">dn: uid=joey,ou=Users,dc=example,dc=com<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: organizationalPerson<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: person<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: inetOrgPerson<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: top<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">cn: Joey<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">sn: Bourne<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">mail: joey@example.com<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">uid: joey<br />
</span></p>
<p><span style="color:black;font-family:Times New Roman;font-size:12pt;">In fact, I could have just used the object factory and read any user entry from the server and have the entries turned into AttrUser objects and not have to worry about implementing DirContext.</span></p>
<p><span style="color:black;font-family:Times New Roman;font-size:12pt;"><strong>That was Fun</strong></span></p>
<p><span style="color:black;font-family:Times New Roman;font-size:12pt;">That is the last demonstration I have right now for JNDI.  I have shown a simple entry lookup, how to store a Java Object directly and indirectly on the server to objects that create entries and those entries can be read back into a Java object.  If you have anymore JNDI knowledge to share, leave a comment.<br />
</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/67/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=67&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/05/28/using-the-java-naming-and-directory-interface-jndi-part-3-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>One for the Little Guy</title>
		<link>http://darylmathison.wordpress.com/2010/05/27/one-for-the-little-guy/</link>
		<comments>http://darylmathison.wordpress.com/2010/05/27/one-for-the-little-guy/#comments</comments>
		<pubDate>Thu, 27 May 2010 18:00:15 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[customer]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[enterprise]]></category>
		<category><![CDATA[small business]]></category>
		<category><![CDATA[soapbox]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/?p=49</guid>
		<description><![CDATA[Motivation Those who may know me know that I was recently laid off. During that time one of my uncles gave me a challenge I could not refuse, &#8220;Could you put all of my customer contacts in a computer?&#8221; I was intrigued because I thought everyone put information that valuable in a database for lightning [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=49&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Motivation</strong></p>
<p>Those who may know me know that I was recently laid off.  During that time one of my uncles gave me a challenge I could not refuse, &#8220;Could you put all of my customer contacts in a computer?&#8221;  I was intrigued because I thought everyone put information that valuable in a database for lightning quick look-ups.  I was also in need of money so I jumped at the chance to help family.  After further discussion, I found that he had a worse problem, no website.</p>
<p><strong>Persistent Problem Needs a Solution</strong></p>
<p>While I took up the cause to place the virtual placard for his business on the web, the first problem was still knocking at the door of my mind persistent and unyielding.  I starting thinking about how many times this problem has been solved and wouldn&#8217;t solving it be like reinventing the wheel.  So I looked at other solutions but none of them really wanted me to jump up and say, &#8220;Eureka!&#8221;  I even thought of just using MS Outlook to solve the problem.  On further internal discussions, MS Outlook is actually almost perfect for their needs.  Even storing directions to the customer&#8217;s homes could be done in the notes section.</p>
<p><strong>Solution Found</strong></p>
<p>Well now that I figured it out, it is only a matter of implementing the plan putting in notebooks upon notebooks of customer data into MS Outlook.  That thought exercise was a  lot of fun and now I can get to the business of Java development.  My uncle is a small business owner who doesn&#8217;t need to have access to enterprise level software.  In fact, some would saw he doesn&#8217;t deserve it.  Wait a minute!  The only thing that is small in a small business is the number of workers in the company.  Everything else is big, why can&#8217;t software grow with the company too?</p>
<p><strong>New Solution for a Old Problem</strong></p>
<p>The answer to the question is money.  An enterprise class solution gets enterprise class money.  Not everyone can afford enterprise solutions because they all don&#8217;t have enterprise money coming in.  Even those that have the money are told by busy, honest consultants that they don&#8217;t need it.  They say things like, &#8220;Use Outlook&#8221; and go on to the next project.  However, to show future employers what I can do, designing a enterprise PIM (Personal Information Management) software would be a good way to do it.  It is familiar enough to identify, but not as simple as &#8220;Hello World.&#8221;  It also can be solved in a number of ways.  I could make a solution that is strictly for an enterprise but that would not be for the &#8220;little guy.&#8221;  If I could form a solution that can run on a single PC just as well as a network that would be for the &#8220;little guy.&#8221;  Then people like my uncle can dream big and have a solution that can grow with them.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=49&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/05/27/one-for-the-little-guy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>Spring into Action</title>
		<link>http://darylmathison.wordpress.com/2010/05/26/spring-into-action/</link>
		<comments>http://darylmathison.wordpress.com/2010/05/26/spring-into-action/#comments</comments>
		<pubDate>Thu, 27 May 2010 05:48:57 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Link]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/?p=74</guid>
		<description><![CDATA[Pardon the pun.  I was doing some research on the Spring Framework when I found these links.  Yes they are 5 years old but it still got me thinking in Spring. The Spring series, Part 1: Introduction to the Spring framework The Spring series, Part 2: When Hibernate meets Spring The Spring series, Part 3: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=74&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Pardon the pun.  I was doing some research on the Spring Framework when I found these links.  Yes they are 5 years old but it still got me thinking in Spring.</p>
<p><a href="http://www.ibm.com/developerworks/web/library/wa-spring1/" target="_blank">The Spring series, Part 1: Introduction to the Spring framework</a></p>
<p><a href="http://www.ibm.com/developerworks/web/library/wa-spring2/index.html" target="_blank">The Spring series, Part 2: When Hibernate meets Spring</a></p>
<p><a href="http://www.ibm.com/developerworks/web/library/wa-spring3/index.html">The Spring series, Part 3: Swing into Spring MVC</a></p>
<p><a href="http://www.ibm.com/developerworks/web/library/wa-spring4/index.html" target="_blank">The Spring series, Part 4: 1-2-3 messaging with Spring JMS</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/74/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=74&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/05/26/spring-into-action/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>Tips for the Home Coder</title>
		<link>http://darylmathison.wordpress.com/2010/05/22/tips-for-the-home-coder/</link>
		<comments>http://darylmathison.wordpress.com/2010/05/22/tips-for-the-home-coder/#comments</comments>
		<pubDate>Sun, 23 May 2010 00:20:25 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[beer]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[home]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">https://darylmathison.wordpress.com/?p=35</guid>
		<description><![CDATA[I don&#8217;t know about anyone else but I spend a good amount of time coding at home.  It is nice because I get to drink beer and code at the same time.  It is tough to do because I have all my home duties plus getting an hour or two to make something fun or [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=35&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t know about anyone else but I spend a good amount of time coding at home.  It is nice because I get to drink beer and code at the same time.  It is tough to do because I have all my home duties plus getting an hour or two to make something fun or learn somethings.  I started to look at what I do at work to save time since &#8220;Time is Money&#8221; at work but &#8220;Time is Time&#8221; at home.  Here is a quick list.</p>
<p><strong>Source Control</strong></p>
<p>It sounds simple enough but I looked over the times where I have blasted my code at home and spent a week restoring while a simple &#8220;svn revert&#8221; would have done the trick.  For those who are worried about standing up a new server at home, don&#8217;t.  There are many sites out there that will host your code for free.  I keep all mine at <a href="http://darylmathisonblog.googlecode.com" target="_blank">darylmathisonblog.googlecode.com</a>.  A lot of these sites do this for open source only so if you are making the next best mousetrap, you should take the time and stand one up at your abode.  Now all your changes are in one place that can be easily backed up by someone else or yourself.</p>
<p><strong>Automated Testing</strong></p>
<p>Automated testing is an investment in the future.  In the beginning, it is a real pain to set up, then it becomes awesome time saver. With the click of a button tests that could have taken minutes to finish by hand are done in seconds.  Unit tests can be combined and create test suites, now any change can be tested against the whole system or just that module.  Automated test can run while you make dinner or put the kids to bed.  The other neat thing, if you walk away from a project for a while the tests can give you an idea of how that module is supposed to run so you don&#8217;t spend a week of spare time figuring out your own code.  Don&#8217;t forget to source control your automated tests too.</p>
<p><strong>Use an IDE</strong></p>
<p>An IDE (Integrated Development Environment) will pay back in the long run.  When I am compiling, the error log created contains links to the offending code.  When I create a new Java class, I use templates to initially set up the class.  When I am starting up a new project, the IDE organizes it to make it easy to deploy.  Good ones are expandable so I can create a tool to help me create a solution if one hasn&#8217;t been created already.  I have been using Eclipse lately and the amount of add-ons it has blows me away.  I can install servers on my computer and Eclipse will start up and shutdown the server as needed.  It automatically builds source code so I know exactly where the code doesn&#8217;t compile before I do a test.</p>
<p>Well, there is my quick list.  If you can think of anything else, post a comment.</p>
<h3>Links</h3>
<p><a href="http://www.eclipse.org">www.eclipse.org</a></p>
<p><a href="http://www.googlecode.com">www.googlecode.com</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=35&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/05/22/tips-for-the-home-coder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>Using the Java Naming and Directory Interface (JNDI) (Part 2)</title>
		<link>http://darylmathison.wordpress.com/2010/05/22/using-the-java-naming-and-directory-interface-jndi-part-2/</link>
		<comments>http://darylmathison.wordpress.com/2010/05/22/using-the-java-naming-and-directory-interface-jndi-part-2/#comments</comments>
		<pubDate>Sat, 22 May 2010 22:16:13 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jndi]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/2010/05/22/using-the-java-naming-and-directory-interface-jndi-part-2/</guid>
		<description><![CDATA[In my last post, I discussed directory servers and a quick example of how to query them. To read part one click here. JNDI can be used to store whole objects into a LDAP server. In this entry, a java object will be stored on the server directly. First example uses a mechanism that Java [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=40&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In my last <a href="http://darylmathison.wordpress.com/2010/04/20/using-the-java-naming-and-directory-interface/">post</a>, I discussed directory servers and a quick example of how to query them.  To read part one click <a href="http://darylmathison.wordpress.com/2010/04/20/using-the-java-naming-and-directory-interface/">here</a>.  JNDI can be used to store whole objects into a LDAP server.  In this entry, a java object will be stored on the server directly.</p>
<p style="text-align:justify;">First example uses a mechanism that Java has had since the beginning, serialization.  I need to define a user class.  The following class is from User.java:</p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> User </span><span style="color:#7f0055;"><strong>implements</strong></span><span style="color:black;"> Serializable {</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:#7f0055;"><strong> final</strong></span><span style="color:#7f0055;"><strong> long</strong></span><span style="color:#0000c0;"><em> serialVersionUID</em></span><span style="color:black;"> = 3999866113934116781L;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;">name</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;">userid</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>private</strong></span><span style="color:black;"> String </span><span style="color:#0000c0;">email</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getName() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:#0000c0;"> name</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setName(String name) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>this</strong></span><span style="color:black;">.</span><span style="color:#0000c0;">name</span><span style="color:black;"> = name;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getUserid() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:#0000c0;"> userid</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setUserid(String userid) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>this</strong></span><span style="color:black;">.</span><span style="color:#0000c0;">userid</span><span style="color:black;"> = userid;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> String getEmail() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> </span><span style="color:#0000c0;"> email</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> setEmail(String email) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>this</strong></span><span style="color:black;">.</span><span style="color:#0000c0;">email</span><span style="color:black;"> = email;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="text-align:justify;"><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p style="text-align:justify;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">It is just a number of setters and getters for a name, user id and email.  It does implement <a href="http://java.sun.com/javase/6/docs/api/java/io/Serializable.html"></a></span>java.io.Serializable<span style="color:black;"> to make it work for storage. Here is the class that uses User.java.<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> JavaObjectLookup {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>static</strong></span><span style="color:black;"> Hashtable&lt;String, String&gt; getEnv() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Hashtable&lt;String, String&gt; env = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> Hashtable&lt;String, String&gt;();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">env.put(Context.</span><span style="color:#0000c0;"><em>INITIAL_CONTEXT_FACTORY</em></span><span style="color:black;">, </span><span style="color:#2a00ff;">&#8220;com.sun.jndi.ldap.LdapCtxFactory&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> env.put(Context.</span><span style="color:#0000c0;"><em>PROVIDER_URL</em></span><span style="color:black;">, </span><span style="color:#2a00ff;">&#8220;ldap://localhost:10389/ou=java,dc=example,dc=com&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> env;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> main(String[] args) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> DirContext ctx = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> InitialDirContext(<em>getEnv</em>());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f7f5f;">// first bind an object to the Directory</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> User user = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> User();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setName(</span><span style="color:#2a00ff;">&#8220;Joey&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setUserid(</span><span style="color:#2a00ff;">&#8220;joey&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setEmail(</span><span style="color:#2a00ff;">&#8220;joey@example.com&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx.bind(</span><span style="color:#2a00ff;">&#8220;cn=joey&#8221;</span><span style="color:black;">, user);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> User u = (User)ctx.lookup(</span><span style="color:#2a00ff;">&#8220;cn=joey&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println( </span><span style="color:#2a00ff;">&#8220;User&#8217;s email is &#8220;</span><span style="color:black;"> + u.getEmail());</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;"> (NamingException e) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> e.printStackTrace();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>finally </strong></span><span style="color:black;">{</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx.close();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;"> (Exception e) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> e.printStackTrace();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="text-align:justify;"><span style="color:black;font-family:Courier New;font-size:10pt;">}</span></p>
<p style="text-align:justify;"><span style="color:#7f0055;font-family:Courier New;font-size:10pt;"><strong><br />
</strong></span>Notice that the connection URL has been changed to &#8220;ou=java,dc=example,dc=com&#8221; so the object will be put into the java organizational unit.  On the LDAP server, the entry looks like the following:</p>
<p><span style="font-family:Courier New;font-size:10pt;">dn: cn=joey,ou=java,dc=example,dc=com<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: javaSerializedObject<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: javaObject<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: javaContainer<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: top<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">cn: joey<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaClassName: org.mathison.example.jndi.User<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaSerializedData:: rO0ABXNyAB5vcmcubWF0aGlzb24uZXhhbXBsZS5qbmRpLlVzZXI3gmE<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"> J1ipHrQIAA0wABWVtYWlsdAASTGphdmEvbGFuZy9TdHJpbmc7TAAEbmFtZXEAfgABTAAGdXNlcm<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"> lkcQB+AAF4cHQAEGpvZXlAZXhhbXBsZS5jb210AARKb2V5dAAEam9leQ==<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaClassNames: org.mathison.example.jndi.User<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaClassNames: java.lang.Object<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaClassNames: java.io.Serializable<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">The instance is stored as a base-64 encoded string.  This is good for storing an instance that a lot of different java processes will access such as a printer driver.  But what if a service wants to register its services on a LDAP server?  It is no good to store a copy of a service on a directory server because a service is only useful if it can serve.  It would be better if the entry just &#8220;referred&#8221; at the service rather being a copy of it.  This is the rational for using <a href="http://java.sun.com/javase/6/docs/api/javax/naming/Reference.html">javax.naming.Reference</a>.  To store a reference, a developer can have the class implement <a href="http://java.sun.com/javase/6/docs/api/javax/naming/Referenceable.html">java.naming.Referenceable</a> and call bind on the object or create a reference and bind the reference.  On retrieving the reference, a developer can create an object from the information in the reference instance.  If an object factory is used, the lookup returns an instance of your class.  The following example has ReferUser that implements Referencable and uses an object factory.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">Example code:<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> JavaObjectRefLookup {</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>static</strong></span><span style="color:black;"><span style="background-color:silver;"> Hashtable&lt;String, String&gt;</span> getEnv() {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Hashtable&lt;String, String&gt; env = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> Hashtable&lt;String, String&gt;();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> env.put(Context.</span><span style="color:#0000c0;"><em>INITIAL_CONTEXT_FACTORY</em></span><span style="color:black;">, </span><span style="color:#2a00ff;">&#8220;com.sun.jndi.ldap.LdapCtxFactory&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> env.put(Context.</span><span style="color:#0000c0;"><em>PROVIDER_URL</em></span><span style="color:black;">, </span><span style="color:#2a00ff;">&#8220;ldap://localhost:10389/ou=java,dc=example,dc=com&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;background-color:silver;"><strong>return</strong></span><span style="color:black;background-color:silver;"> env;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f5fbf;">/**</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> * </span><span style="color:#7f9fbf;"><strong>@param</strong></span><span style="color:#3f5fbf;"> args</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#3f5fbf;"> */</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> main(String[] args) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> DirContext ctx = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> InitialDirContext(<em>getEnv</em>());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#3f7f5f;">// first bind an object to the Directory</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ReferUser user = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> ReferUser();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setName(</span><span style="color:#2a00ff;">&#8220;Joey&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setUserid(</span><span style="color:#2a00ff;">&#8220;joey&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setEmail(</span><span style="color:#2a00ff;">&#8220;joey@example.com&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx.rebind(</span><span style="color:#2a00ff;">&#8220;cn=joey&#8221;</span><span style="color:black;">, user);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ReferUser u = (ReferUser)ctx.lookup(</span><span style="color:#2a00ff;">&#8220;cn=joey&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println( </span><span style="color:#2a00ff;">&#8220;User&#8217;s email is &#8220;</span><span style="color:black;"> + u.getEmail());</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;"> (NamingException e) {</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> e.printStackTrace();</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>finally</strong></span><span style="color:black;">{</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>try</strong></span><span style="color:black;"> {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ctx.close();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> } </span><span style="color:#7f0055;"><strong>catch</strong></span><span style="color:black;"> (Exception e) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> e.printStackTrace();</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>import</strong></span><span style="color:black;"> org.mathison.jndi.example2.User;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> ReferUser</span><span style="color:#7f0055;"><strong> extends</strong></span><span style="color:black;"> User </span><span style="color:#7f0055;"><strong>implements</strong></span><span style="color:black;"> Referenceable {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Reference getReference() </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> NamingException {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Reference ref = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> Reference(</span><span style="color:#7f0055;"><strong>this</strong></span><span style="color:black;">.getClass().getName(),</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ReferUserFactory.</span><span style="color:#7f0055;"><strong>class</strong></span><span style="color:black;">.getName(),</span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ref.add(</span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"><span style="background-color:silver;"> StringRefAddr</span>(</span><span style="color:#2a00ff;">&#8220;name&#8221;</span><span style="color:black;">, getName()));</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ref.add(</span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"><span style="background-color:silver;"> StringRefAddr</span>(</span><span style="color:#2a00ff;">&#8220;mail&#8221;</span><span style="color:black;">, getEmail()));</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ref.add(</span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"><span style="background-color:silver;"> StringRefAddr</span>(</span><span style="color:#2a00ff;">&#8220;uid&#8221;</span><span style="color:black;">, getUserid()));</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> ref;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;">}<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>import</strong></span><span style="color:black;"> javax.naming.directory.Attributes;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>import</strong></span><span style="color:black;"> javax.naming.spi.DirObjectFactory;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> </span><span style="color:#7f0055;"><strong> class</strong></span><span style="color:black;"> ReferUserFactory </span><span style="color:#7f0055;"><strong>implements</strong></span><span style="color:black;"> DirObjectFactory {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#646464;">@Override</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Object getObjectInstance(Object arg0, Name arg1, Context arg2,</span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Hashtable&lt;?, ?&gt; arg3, Attributes arg4) </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> Exception {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> getObjectInstance(arg0, arg1, arg2, arg3);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#646464;">@Override</span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong> </strong></span></span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>public</strong></span><span style="color:black;"> Object getObjectInstance(Object arg0, Name arg1, Context arg2,</span></span><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Hashtable&lt;?, ?&gt; arg3) </span><span style="color:#7f0055;"><strong>throws</strong></span><span style="color:black;"> Exception {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> ReferUser user = </span><span style="color:#7f0055;"><strong>null</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (arg0 </span><span style="color:#7f0055;"><strong>instanceof</strong></span><span style="color:black;"> Reference){</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> Reference ref = (Reference)arg0;</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;">(ref.getClassName().equals(ReferUser.</span><span style="color:#7f0055;"><strong>class</strong></span><span style="color:black;">.getName())) {</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> RefAddr name = ref.get(</span><span style="color:#2a00ff;">&#8220;name&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> RefAddr uid  = ref.get(</span><span style="color:#2a00ff;">&#8220;uid&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> RefAddr email = ref.get(</span><span style="color:#2a00ff;">&#8220;mail&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user = </span><span style="color:#7f0055;"><strong>new</strong></span><span style="color:black;"> ReferUser();</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setName(name.getContent().toString());</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setEmail(email.getContent().toString());</span><br />
</span></p>
<p style="padding-left:120px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> user.setUserid(uid.getContent().toString());</span><br />
</span></p>
<p style="padding-left:90px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>return</strong></span><span style="color:black;"> user;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;">}</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">Here is what you will find if you take a look at the Directory Server:<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">dn: cn=joey,ou=java,dc=example,dc=com<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: javaContainer<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: javaNamingReference<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: javaObject<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">objectClass: top<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">cn: joey<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaClassName: org.mathison.example.jndi.ReferUser<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaFactory: org.mathison.example.jndi.ReferUserFactory<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaReferenceAddress: #0#name#Joey<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaReferenceAddress: #1#mail#joey@example.com<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">javaReferenceAddress: #2#uid#joey<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">Notice that the object&#8217;s attributes are now stored in javaReferenceAddress attributes. Also notice how each of these techniques used special java storage attributes to the job done.  Well, the next part of this blog series will tackle creating a object without special java structures.  All of this code can be downloaded from <a href="http://darylmathisonblog.googlecode.com/svn/trunk/">http://darylmathisonblog.googlecode.com/svn/trunk/</a> via subversion or your browser.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=40&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/05/22/using-the-java-naming-and-directory-interface-jndi-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>Using the Java Naming and Directory Interface (JNDI) (Part 1)</title>
		<link>http://darylmathison.wordpress.com/2010/04/20/using-the-java-naming-and-directory-interface/</link>
		<comments>http://darylmathison.wordpress.com/2010/04/20/using-the-java-naming-and-directory-interface/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 14:30:22 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/2010/04/20/using-the-java-naming-and-directory-interface/</guid>
		<description><![CDATA[Been learning a lot at my new job at St. Mary&#8217;s University. The university has been able to serve the students, faculty and staff via the &#8220;Gateway.&#8221; The Gateway is a portal site using a Luminis system from SunGuard. SunGuard based their system on uPortal. One of the central servers is a LDAP server. It [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=29&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="margin:5pt 0;"><span style="font-family:'times new roman';"><span style="font-size:small;">Been learning a lot at my new job at </span></span><a href="http://www.stmarytx.edu" target="_blank"><span style="font-family:'times new roman';"><span style="color:#0000ff;"><span style="text-decoration:underline;"><span style="font-size:small;">St. Mary&#8217;s University</span></span></span></span></a><span style="font-family:'times new roman';"><span style="font-size:small;">.</span></span> <span style="font-family:'times new roman';"><span style="font-size:small;"> The university has been able to serve the students, faculty and staff via the &#8220;Gateway.&#8221;</span></span> <span style="font-family:'times new roman';"><span style="font-size:small;"> The Gateway is a portal site using a </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">Luminis</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> system from </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">SunGuard</span></span><span style="font-family:'times new roman';"><span style="font-size:small;">.</span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">SunGuard</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> based their system on </span></span><a href="http://www.jasig.org/uportal" target="_blank"><span style="font-family:'times new roman';"><span style="color:#0000ff;"><span style="text-decoration:underline;"><span style="font-size:small;">uPortal</span></span></span></span><span style="font-family:'times new roman';"><span style="color:#0000ff;"><span style="text-decoration:underline;"><span style="font-size:small;">.</span></span></span></span></a><span style="font-family:'times new roman';"><span style="font-size:small;"> One of the central servers is a LDAP server.</span></span> <span style="font-family:'times new roman';"><span style="font-size:small;"> It contains course information, user information, department information, information about the current term and permission groups.</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> With this much importance on one server I thought it would be a good idea to learn about it and how Java could interact with it.</span></span></p>
<p style="margin:5pt 0;"><span style="font-family:'times new roman';"><span style="font-size:small;">First some background into LDAP.   LDAP stands for </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">Lightwieght</span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">DirectoryAccess</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> Protocol. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">LDAP servers are a type of Directory Server</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> witch hold collections of </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">information(</span></span><span style="font-family:'times new roman';"><span style="font-size:small;">well like a directory). </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">Good examples are phone directories. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">The difference between a phone directory and a directory server is that a directory server can hold a directory of any data rather than contact information. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">They are not replacements for relational databases. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">Directory servers are not designed to handle generalized data relationships like relational databases. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">One needs to think more one-to-one or one-to-many data sets for LDAP servers. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">Another difference is the speed of reading vs. writing. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">Most directory servers are very fast on reading data but slow on writing.  Each LDAP entry has a distinguished name or dn.  This is the key of the entry.  It is normally composed of multiple parts of attributes found in the entry.  The other part of an entry is attributes.  Attributes have a name and a value.  For example, an entry with a dn of &#8220;uid=mborn, ou=Users, dc=example, dc=com&#8221; has an attribute named common name or cn that has the value &#8220;Max Born.&#8221;  There can be multiple values for each attribute.  In the last example, there are multiple objectClass attributes, each one with a unique value.  ObjectClasses are important for LDAP entries because they define what attributes an entry can contain or have to contain.  ObjectClasses are defined in schemas.  Normally, these files are separate from the normal configuration files.  Many of the schemas used in LDAP servers are standardized so as long as the LDAP server supports a schema.  The schemas that the examples use are InetOrgPerson and Java.<br />
</span></span></p>
<p style="margin:5pt 0;"><span style="font-family:'times new roman';"><span style="font-size:small;">To run the examples, I suggest downloading and installing both sub projects from </span></span><a href="http://directory.apache.org/" target="_blank"><span style="font-family:'times new roman';"><span style="color:#0000ff;"><span style="text-decoration:underline;"><span style="font-size:small;">The Apache Directory Project</span></span></span></span></a><span style="font-family:'times new roman';"><span style="font-size:small;">. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">The Apache Directory Studio alone makes the effort worth it. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">The studio is </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">LDAPv3 </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">compliant</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> so any LDAPv3 server can be used. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">I started using it on </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">OpenLDAP</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> because I bought &#8220;Mastering </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">OpenLDAP</span></span><span style="font-family:'times new roman';"><span style="font-size:small;">&#8221; researching this subject. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">I switched over to Apache&#8217;s Directory because of how easy it was to use with the studio in a Microsoft Windows environment. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">I used the </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">example.ldif</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> file contained the directory server&#8217;s configuration directory. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">The only modification to the file was removing the users that used references. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">I also added an </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">organzational</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> unit named java to save the java objects into. </span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> </span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">I also suggest downloading and installing a JDKv1.3 or later. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">Java 1.3 and newer already </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">contain</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> the JNDI libraries that will be used throughout. </span></span> <span style="font-family:'times new roman';"><span style="font-size:small;">I developed these examples using Java 1.6.</span></span></p>
<p style="margin:5pt 0;"><span style="font-family:'times new roman';"><span style="font-size:small;">Here is the first example,</span></span> <span style="font-family:'times new roman';"><span style="font-size:small;"> all</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> this does is connect to a server anonymously and find the user associated with a </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">uid</span></span><span style="font-family:'times new roman';"><span style="font-size:small;"> of </span></span><span style="font-family:'times new roman';"><span style="font-size:small;">mborn</span></span><span style="font-family:'times new roman';"><span style="font-size:small;">.</span></span></p>
<p style="margin:0;">
<div style="margin-left:40px;">package org.mathison.example.jndi;</div>
<div style="margin-left:40px;"></div>
<div style="margin-left:40px;">
<p>import javax.naming.Context;<br />
import javax.naming.directory.InitialDirContext;<br />
import javax.naming.directory.DirContext;<br />
import javax.naming.directory.Attributes;<br />
import javax.naming.NamingException;</p>
<p>import java.util.Hashtable;</p>
<p>public class JndiClient {</p>
<p style="padding-left:30px;">static Hashtable getEnv() {<br />
Hashtable env = new Hashtable();</p>
<p style="padding-left:30px;">env.put(Context.INITIAL_CONTEXT_FACTORY, &#8220;com.sun.jndi.ldap.LdapCtxFactory&#8221;);<br />
env.put(Context.PROVIDER_URL, &#8220;ldap://localhost:10389/dc=example,dc=com&#8221;);</p>
<p style="padding-left:30px;">return env;</p>
<p style="padding-left:30px;">
}</p>
<p style="padding-left:30px;">
/**<br />
* @param args</p>
<p style="padding-left:30px;">*/<br />
public static void main(String[] args) {</p>
<p style="padding-left:60px;">
try {</p>
<p style="padding-left:90px;">
DirContext ctx = new InitialDirContext(getEnv());<br />
Attributes attr = ctx.getAttributes(&#8220;uid=mborn,ou=Users&#8221;);<br />
System.out.println(&#8220;Mborn&#8217;s name is &#8221; + attr.get(&#8220;cn&#8221;).get());<br />
ctx.close();</p>
<p style="padding-left:60px;">
} catch (NamingException e) {</p>
<p style="padding-left:90px;">e.printStackTrace();</p>
<p style="padding-left:60px;">
}</p>
<p style="padding-left:30px;">}</p>
<p>}</p>
</div>
<p>First thing that happens is creating an DirContext.  For this an instance of Hashtable is passed to the InitialDirContext.  The hashtable sets up the Context that is used and where it will connect.  The URL has two parts, the address to the server and the directory in the server.  All of the entries that are written to or read from are assumed to be in that directory.  The next line returns the attributes for the entry that has a user id of mborn and is in the users organizational unit.  The System.out line retrieves the &#8220;cn&#8221; or common name from the entry and prints it out to the screen.  All of this is wrapped in a try-catch block in case a NamingException is thrown.  This is the basic query and retrieve from a LDAP server and is most of what happens.</p>
<p>While JNDI is excellent at pulling attributes from a LDAP server, its real power comes from when one starts storing Java objects on the server. That will be discussed on the next entry.</p>
<p>Links to resources used for this article:<br />
<a id="lls4" title="http://java.sun.com/products/jndi/tutorial/index.html" href="http://java.sun.com/products/jndi/tutorial/index.html">JNDI Tutorial</a><br />
<a onclick="return mugicPopWin(this,event);" oncontextmenu="mugicRightClick(this);" id="yzbe" title="http://www.amazon.com/Mastering-OpenLDAP-Configuring-Integrating-Directory/dp/1847191029/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1271773700&amp;sr=8-1" href="http://www.amazon.com/Mastering-OpenLDAP-Configuring-Integrating-Directory/dp/1847191029/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1271773700&amp;sr=8-1">Mastering OpenLDAP</a><br />
<a href="http://directory.apache.org/" target="_blank"><span style="font-family:'times new roman';"><span style="color:#0000ff;"><span style="text-decoration:underline;"><span style="font-size:small;">The Apache Directory Project</span></span></span></span></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=29&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/04/20/using-the-java-naming-and-directory-interface/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing Fail-Fast Code</title>
		<link>http://darylmathison.wordpress.com/2010/04/04/writing-fail-fast-code/</link>
		<comments>http://darylmathison.wordpress.com/2010/04/04/writing-fail-fast-code/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 15:08:40 +0000</pubDate>
		<dc:creator>Daryl Mathison</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[fast-fail Java tip]]></category>

		<guid isPermaLink="false">http://darylmathison.wordpress.com/?p=21</guid>
		<description><![CDATA[With projects as complex as they are now, it can be hard to modify all of the code needed when a change is made.  First is to identify all the places needed for modification. The next step is to write code that handles the new requirement. The former is typically harder than the latter. Inspect [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=21&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-family:Times New Roman;font-size:12pt;">With projects as complex as they are now, it can be hard to modify all of the code needed when a change is made.  First is to identify all the places needed for modification.  The next step is to write code that handles the new requirement.  The former is typically harder than the latter.  Inspect the following code.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>static</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> processIfQuiet(Data d) {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (d.getType() == DataType.</span><span style="color:#0000c0;"><em>XML</em></span><span style="color:black;">) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;XML Data&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:#7f0055;"><strong> if</strong></span><span style="color:black;"> (d.<span style="text-decoration:underline;">getType</span>() == DataType.</span><span style="color:#0000c0;"><em>LEGACY</em></span><span style="color:black;">) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;Legacy Data&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="font-family:Times New Roman;font-size:12pt;">This code is fine if no more data types are added.  When a new DataType is created, this code could be skipped and no one would know.  The code could be pushed to production incomplete and there will be errors that the customer will find or not depending where this snippet is located.  If this is for a data loader, the new data type could be falling through and not be stored at all.  Three months down the road when the customer is generating a report relying on the new data type, the report code blows up and it takes awhile to find the error.  Here is an implementation trick that I had to learn the hard way.<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>static</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> processIfLoud(Data d) {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>if</strong></span><span style="color:black;"> (d.getType() == DataType.</span><span style="color:#0000c0;"><em>XML</em></span><span style="color:black;">) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;XML Data&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:#7f0055;"><strong> if</strong></span><span style="color:black;"> (d.getType() == DataType.</span><span style="color:#0000c0;"><em>LEGACY</em></span><span style="color:black;">) {</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;Legacy Data&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>else</strong></span><span style="color:black;"> { // new part</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>assert</strong></span><span style="color:#7f0055;"><strong> false</strong></span><span style="color:black;">: </span><span style="color:#2a00ff;">&#8220;Unknown Data Type&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>throw</strong></span><span style="color:#7f0055;"><strong> new</strong></span><span style="color:black;"> RuntimeException(</span><span style="color:#2a00ff;">&#8220;Unknown Data Type&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> }</span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;"> }<br />
</span></p>
<p><span style="font-family:Times New Roman;font-size:12pt;">The else clause will catch any unknown type either throw an AssertionError or a RuntimeException depending whether or not assertions are enabled.  This message could be logged and not have an ugly Error or Exception popping up.  However, I would call this a &#8220;Developer&#8217;s error&#8221; meaning that it should not have got past the developer in the first place.  Making it ugly will make sure it will not be missed and can be fixed before the customer gets to see it.  This can be used for select statements too.  See the loud version of a select statement:<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>import</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"><span style="text-decoration:underline;"> blog.mathison.example1.DataType.</span></span><span style="color:#0000c0;text-decoration:underline;"><em>XML</em></span><span style="color:black;">;</span><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#7f0055;"><strong>import</strong></span><span style="color:#7f0055;"><strong> static</strong></span><span style="color:black;"><span style="text-decoration:underline;"> blog.mathison.example1.DataType.</span></span><span style="color:#0000c0;text-decoration:underline;"><em>LEGACY</em></span><span style="color:black;">;</span></span><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>static</strong></span><span style="color:#7f0055;"><strong> void</strong></span><span style="color:black;"> processCaseLoud(Data d) {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> DataType dt = d.getType();</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;background-color:silver;"><strong>switch</strong></span><span style="color:black;">(dt) {</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>case</strong></span><span style="color:#0000c0;"><em> XML</em></span><span style="color:black;">:</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;XML Data&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>break</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>case</strong></span><span style="color:#0000c0;"><em> LEGACY</em></span><span style="color:black;">:</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> System.</span><span style="color:#0000c0;"><em>out</em></span><span style="color:black;">.println(</span><span style="color:#2a00ff;">&#8220;Legacy Data&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>break</strong></span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>default</strong></span><span style="color:black;">:</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>assert</strong></span><span style="color:#7f0055;"><strong> false</strong></span><span style="color:black;">: </span><span style="color:#2a00ff;">&#8220;Unknown Data Type&#8221;</span><span style="color:black;">;</span><br />
</span></p>
<p style="padding-left:60px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> </span><span style="color:#7f0055;"><strong>throw</strong></span><span style="color:#7f0055;"><strong> new</strong></span><span style="color:black;"> RuntimeException(</span><span style="color:#2a00ff;">&#8220;Unknown Data Type&#8221;</span><span style="color:black;">);</span><br />
</span></p>
<p style="padding-left:30px;"><span style="font-family:Courier New;font-size:10pt;"><span style="color:black;"> <span style="background-color:silver;">}</span></span><br />
</span></p>
<p><span style="color:black;font-family:Courier New;font-size:10pt;"> }<br />
</span></p>
<p><span style="font-family:Times New Roman;font-size:12pt;">Just adding a default will make it fail where the problem is rather than three months down the road.  Do you think this not a good idea?  Are there better ways?  Leave me a comment.<br />
</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/darylmathison.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/darylmathison.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/darylmathison.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/darylmathison.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/darylmathison.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/darylmathison.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/darylmathison.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/darylmathison.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/darylmathison.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/darylmathison.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/darylmathison.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/darylmathison.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/darylmathison.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/darylmathison.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=darylmathison.wordpress.com&amp;blog=12771795&amp;post=21&amp;subd=darylmathison&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://darylmathison.wordpress.com/2010/04/04/writing-fail-fast-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/063c85b36c6d22439c9c53cfce49aa21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">darylmathison</media:title>
		</media:content>
	</item>
	</channel>
</rss>
