<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CertPal &#187; java</title>
	<atom:link href="http://www.certpal.com/blogs/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.certpal.com/blogs</link>
	<description>Technology and certifications</description>
	<lastBuildDate>Mon, 18 Jul 2011 06:48:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Why certification related career goals hurt everyone</title>
		<link>http://www.certpal.com/blogs/2011/07/why-certification-related-career-goals-hurt-everyone/</link>
		<comments>http://www.certpal.com/blogs/2011/07/why-certification-related-career-goals-hurt-everyone/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 05:44:18 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[certification]]></category>
		<category><![CDATA[exam]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=1057</guid>
		<description><![CDATA['Acquire a certification in technology X' &#124; 'Earn a Domain Y certification'

Do one of those statements list in your yearly goals ? It is almost certainly a bad idea. Let me explain why.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F07%2Fwhy-certification-related-career-goals-hurt-everyone%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F07%2Fwhy-certification-related-career-goals-hurt-everyone%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>&#8216;<span style="color: #000080;">Acquire a certification in technology X</span>&#8216; | &#8216;<span style="color: #ff6600;">Earn a Domain Y certification</span>&#8216;</p>
<p>Do one of those statements list in your yearly goals ? It is almost certainly a bad idea. Let me explain why. I knew a company that required its employees to be acquire at least one certification on a technology per year. Its employees would work hard throughout the year and during appraisal time it is not uncommon to hear this converation</p>
<blockquote><p><strong>Foo: </strong> &#8216;Dude did you take that certification you were supposed to ?&#8217;<br />
<strong>Bar:</strong>  &#8216;Rats ! I didnt realize it was a part of our goals&#8217;<br />
<strong>Foo: </strong> &#8216;hmm&#8230; what do we do ? John has already passed it and that puts him ahead of us&#8217;</p></blockquote>
<p>Foo and Bar will probably end up reading real exam questions (also referred to as brain <a href="http://en.wikipedia.org/wiki/Brain_dump" target="_blank">dumps</a>) and passing the exam with only a day&#8217;s worth of preparation. That another way of saying &#8216;They cheated&#8217;.  This hurts everyone.</p>
<ul>
<li>It hurts John.</li>
</ul>
<p style="padding-left: 60px;">He worked hard to pass this exam. The inclusion of Foo and Bar into the &#8216;pass pool&#8217; dilutes his achievement.</p>
<ul>
<li>It hurts your employer.</li>
</ul>
<p style="padding-left: 60px;">The company probably reimbursed the certification and wasted its financial resources.</p>
<ul>
<li>It hurts the community.</li>
</ul>
<p style="padding-left: 60px;">When interviewers come across scores of folks that are certified for technology X, but their skills do not match what they were certified for, it reduces the value of that certification</p>
<p>Foo and Bar also do not realize how foolish they are going to look in their next job interview. If you are an employer, please consider removing this as a valid goal. If you are an employee who is stuck with this goal, do yourself a favor and either acquire the certification by working hard for it or default.</p>
<p><span style="color: #008000;"><strong>PS:</strong></span> I am not suggesting that all employees take the easy way out. I am merely pointing out a general observation that trends in our industry.</p>
<p><script type="text/javascript">// <![CDATA[
var dzone_url = 'http://www.certpal.com/blogs/2011/07/why-certification-related-career-goals-hurt-everyone/';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_title = 'Why certification related career goals hurt everyone';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_blurb = ''Acquire a certification in technology X' | 'Earn a Domain Y certification'</p>
<p>Does one of those statements list in your yearly goals ? It is almost certainly a bad idea. Let me explain why.';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_style = '2';
// ]]&gt;</script><br />
<script type="text/javascript" language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F07%2Fwhy-certification-related-career-goals-hurt-everyone%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F07%2Fwhy-certification-related-career-goals-hurt-everyone%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2011/07/why-certification-related-career-goals-hurt-everyone/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Java webcam support using gstreamer</title>
		<link>http://www.certpal.com/blogs/2011/07/java-webcam-gstreamer/</link>
		<comments>http://www.certpal.com/blogs/2011/07/java-webcam-gstreamer/#comments</comments>
		<pubDate>Thu, 14 Jul 2011 10:23:37 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[gstreamer]]></category>
		<category><![CDATA[jmf]]></category>
		<category><![CDATA[webcam]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=1049</guid>
		<description><![CDATA[Java can interface with your webcam using the JMF library or the gstreamer API. Here is a short tutorial on streaming content from the webcam to a JFrame or File using gstreamer.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F07%2Fjava-webcam-gstreamer%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F07%2Fjava-webcam-gstreamer%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Java&#8217;s side that is not too well known would be its interface with external devices. Java can read <a href="http://code.google.com/p/zxing/" target="_blank">bar codes</a> interface with<a href="http://www.oracle.com/technetwork/java/index-jsp-141752.html" target="_blank"> serial / parallel ports</a> , and communicate with a <a href="http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140239.html" target="_blank">web cam</a> (JMF)</p>
<p>Speaking of webcams, the following snippet in the Java media framework (JMF) should list your webcam as a device</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Vector</span> info <span style="color: #339933;">=</span> CaptureDeviceManager.<span style="color: #006633;">getDeviceList</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The problem is that this piece of code does not work all the time. Your webcam should be compatible with JMF to be listed by this call. The gstreamer API can come to your rescue should the JMF call fail. Here is how the gstreamer API describes itself</p>
<blockquote><p>GStreamer is a library for constructing graphs of media-handling components. The applications it supports range from simple Ogg/Vorbis playback, audio/video streaming to complex audio (mixing) and video (non-linear editing) processing.</p></blockquote>
<p>The API supports pipelines, which stream data from one node on the pipe to the next. A source node which plugs to a webcam can pass through a video-filter node and end up in a file node thereby recording the video from the webcam and saving it to a file. So where does java fit into this picture ?</p>
<p>The<a href="http://code.google.com/p/gstreamer-java/" target="_blank"> gstreamer-java project</a> is a java interface to the gstreamer API. It allows one to define the piepelines in java and run them in the JVM. The excerpt to a simple video based pipeline is shown below</p>
<p><strong>Video test with a simple pipeline: (No webcams)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> VideoTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> VideoTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Pipeline pipe<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        args <span style="color: #339933;">=</span> Gst.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;VideoTest&quot;</span>, args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pipeline<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;VideoTest&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Element</span> videosrc <span style="color: #339933;">=</span> ElementFactory.<span style="color: #006633;">make</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;videotestsrc&quot;</span>, <span style="color: #0000ff;">&quot;source&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Element</span> videofilter <span style="color: #339933;">=</span> ElementFactory.<span style="color: #006633;">make</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;capsfilter&quot;</span>, <span style="color: #0000ff;">&quot;filter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        videofilter.<span style="color: #006633;">setCaps</span><span style="color: #009900;">&#40;</span>Caps.<span style="color: #006633;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;video/x-raw-yuv, width=720, height=576&quot;</span>
                <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, bpp=32, depth=32, framerate=25/1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">SwingUtilities</span>.<span style="color: #006633;">invokeLater</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                VideoComponent videoComponent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> VideoComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003399;">Element</span> videosink <span style="color: #339933;">=</span> videoComponent.<span style="color: #006633;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pipe.<span style="color: #006633;">addMany</span><span style="color: #009900;">&#40;</span>videosrc, videofilter, videosink<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003399;">Element</span>.<span style="color: #006633;">linkMany</span><span style="color: #009900;">&#40;</span>videosrc, videofilter, videosink<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// Now create a JFrame to display the video output</span>
                <span style="color: #003399;">JFrame</span> frame <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JFrame</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Swing Video Test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                frame.<span style="color: #006633;">setDefaultCloseOperation</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">JFrame</span>.<span style="color: #006633;">EXIT_ON_CLOSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                frame.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>videoComponent, <span style="color: #003399;">BorderLayout</span>.<span style="color: #006633;">CENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                videoComponent.<span style="color: #006633;">setPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Dimension</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">720</span>, <span style="color: #cc66cc;">576</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                frame.<span style="color: #006633;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                frame.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// Start the pipeline processing</span>
                pipe.<span style="color: #006633;">setState</span><span style="color: #009900;">&#40;</span>State.<span style="color: #006633;">PLAYING</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To convert this program to accept the feed from a webcam, all one has to do is replace the videotestsrc with the <a href="http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-good-plugins/html/gst-plugins-good-plugins-v4l2src.html" target="_blank">v4l2src</a> that accepts a stream from a webcam or a tv. Voila, you now have a stream from a webcam to your java app. If you want to save the stream as an image to a file, you can always construct another pipeline to accept the jpegdec and filesink pipeline elements.</p>
<p>As for platform neutrality, the gstreamer API is itself <a href="http://gstreamer.freedesktop.org/download/" target="_blank">supported in the distributions as noted on this page </a></p>
<p><strong>Note:</strong> There is a working example of content streaming through a webcam <a href="http://groups.google.com/group/gstreamer-java/browse_thread/thread/2303841535c35911/18d3a7f34a64a06c?lnk=gst&amp;q=webcam#18d3a7f34a64a06c" target="_blank">on this forum post</a>. Unfortunately the source code in question (displayed below) did not work for the author of that post. It certainly worked when I tried it. Have fun trying it on your webcam <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><strong>Webcam and gstreamer:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">gstreamervideo</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.BorderLayout</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Dimension</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JFrame</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.SwingUtilities</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.gstreamer.Caps</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.gstreamer.Element</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.gstreamer.ElementFactory</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.gstreamer.Gst</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.gstreamer.Pipeline</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.gstreamer.State</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.gstreamer.swing.VideoComponent</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Pipeline pipe<span style="color: #339933;">;</span> 
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        args <span style="color: #339933;">=</span> Gst.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SwingVideoTest&quot;</span>, args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        pipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pipeline<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pipeline&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #666666; font-style: italic;">// This is from VideoTest example and gives test image </span>
        <span style="color: #666666; font-style: italic;">// final Element videosrc = </span>
ElementFactory.<span style="color: #006633;">make</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;videotestsrc&quot;</span>, <span style="color: #0000ff;">&quot;source&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #666666; font-style: italic;">// This gives black window with VideoComponent </span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Element</span> videosrc <span style="color: #339933;">=</span> ElementFactory.<span style="color: #006633;">make</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;v4l2src&quot;</span>, 
<span style="color: #0000ff;">&quot;source&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Element</span> videofilter <span style="color: #339933;">=</span> ElementFactory.<span style="color: #006633;">make</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;capsfilter&quot;</span>, 
<span style="color: #0000ff;">&quot;flt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        videofilter.<span style="color: #006633;">setCaps</span><span style="color: #009900;">&#40;</span>Caps.<span style="color: #006633;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;video/x-raw-yuv, 
width=640, height=480&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #003399;">SwingUtilities</span>.<span style="color: #006633;">invokeLater</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
                VideoComponent videoComponent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> VideoComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #666666; font-style: italic;">// This gives only black window </span>
                <span style="color: #003399;">Element</span> videosink <span style="color: #339933;">=</span> videoComponent.<span style="color: #006633;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #666666; font-style: italic;">// This gives 2nd window with stream from webcam </span>
                <span style="color: #666666; font-style: italic;">// Element videosink = </span>
ElementFactory.<span style="color: #006633;">make</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xvimagesink&quot;</span>, <span style="color: #0000ff;">&quot;sink&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                pipe.<span style="color: #006633;">addMany</span><span style="color: #009900;">&#40;</span>videosrc, videofilter, videosink<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #003399;">Element</span>.<span style="color: #006633;">linkMany</span><span style="color: #009900;">&#40;</span>videosrc, videofilter, videosink<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #003399;">JFrame</span> frame <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JFrame</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Swing Video Test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                frame.<span style="color: #006633;">setDefaultCloseOperation</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">JFrame</span>.<span style="color: #006633;">EXIT_ON_CLOSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                frame.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>videoComponent, <span style="color: #003399;">BorderLayout</span>.<span style="color: #006633;">CENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                videoComponent.<span style="color: #006633;">setPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Dimension</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">640</span>, 
<span style="color: #cc66cc;">480</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                frame.<span style="color: #006633;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                frame.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #666666; font-style: italic;">// Start the pipeline processing </span>
                pipe.<span style="color: #006633;">setState</span><span style="color: #009900;">&#40;</span>State.<span style="color: #006633;">PLAYING</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #009900;">&#125;</span> 
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2011/07/java-webcam-gstreamer/';</script><br />
<script type="text/javascript">var dzone_title = 'Java webcam support using gstreamer';</script><br />
<script type="text/javascript">var dzone_blurb = 'Java can interface with your webcam using the JMF library or the gstreamer API. Here is a short tutorial on streaming content from the webcam to a JFrame or File using gstreamer.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F07%2Fjava-webcam-gstreamer%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F07%2Fjava-webcam-gstreamer%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2011/07/java-webcam-gstreamer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Delving deeper into Jconsole and VisualVm</title>
		<link>http://www.certpal.com/blogs/2011/06/delving-deeper-into-jconsole-and-visualvm/</link>
		<comments>http://www.certpal.com/blogs/2011/06/delving-deeper-into-jconsole-and-visualvm/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 06:13:49 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jconsole]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=1040</guid>
		<description><![CDATA[Jconsole and visual VM help monitor java applications in any environment. Here are a few additional details that can help you monitor your java applications better]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F06%2Fdelving-deeper-into-jconsole-and-visualvm%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F06%2Fdelving-deeper-into-jconsole-and-visualvm%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Jconsole and visual VM help monitor java applications in any environment. Here are a few additional details that can help you monitor your java applications better</p>
<p><strong>Jconsole and permissions:</strong></p>
<p>Jconsole lists the PIDs of local java applications when you start it. But what if there are java applications that are running under a different user ? Jconsole will not display those java PIDs that do not belong to the user who started Jconsole. The <a href="http://download.oracle.com/javase/1.5.0/docs/guide/management/faq.html" target="_blank">JConsole FAQ</a> lists a few troubleshooting tips to get around this problem. You can alternatively expose the Jconsole connection through a remote port using the -Dcom.sun.management.jmxremote.port property. Securing the remote JMX URL with a username and password should do the trick.</p>
<p><strong>Viewing Jconsole / JMC Beans with VisualVm:</strong><br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/jconsole_plugins.png"><img class="aligncenter size-medium wp-image-1043" title="jconsole_plugins" src="http://www.certpal.com/blogs/wp-content/uploads/jconsole_plugins-300x63.png" alt="" width="300" height="63" /></a></p>
<p>While it is great that you can profile your applications on VisualVm, some users miss some of the features provided by Jconsole such as viewing JMX beans etc. You can use the Tools -&gt; Plugins option in VisualVm to install Jconsole / Jmx or related plugins. There is of course the option of writing a custom plugin to display more information. This article already explains how to go about <a href="http://marxsoftware.blogspot.com/2008/08/using-orson-charts-with-jconsole-custom.html" target="_blank">writing a custom plugin</a>.</p>
<p><strong>OQL Plugin:</strong></p>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/jconsole_plugins_2.png"><img class="aligncenter size-medium wp-image-1044" title="jconsole_plugins_2" src="http://www.certpal.com/blogs/wp-content/uploads/jconsole_plugins_2-300x214.png" alt="" width="300" height="214" /></a></p>
<p>There are some cool plugins on the way for visual VM. One of them is support for OQL syntax. The language allows you to go through objects on the heap based on a selection criteria. You can use <a href="http://download.oracle.com/javase/6/docs/technotes/tools/share/jhat.html" target="_blank">jhat</a> and <a href="http://download.oracle.com/javase/6/docs/technotes/tools/share/jmap.html" target="_blank">jmap</a> to get a feel for what the language is like. The syntax is already supported on the existing heap dump analyzers.</p>
<p><strong>Performance:</strong></p>
<p>Switching on <a href="http://weblogs.java.net/blog/emcmanus/archive/2006/07/how_much_does_i.html" target="_blank">jconsole monitoring in production</a> is a bad idea. The article linked on the last sentence made a performance comparison around 2006, so here is a more recent comparison running a java program which solves the <a href="http://apps.topcoder.com/wiki/display/tc/SRM+498 " target="_blank">FoxSequence problem</a> (a problem at topcoder.com) 1000000 times. The overhead is highlighted by the orange area. The difference represents overhead in milli seconds.</p>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/jconsole_overhead.png"><img class="aligncenter size-full wp-image-1041" title="jconsole_overhead" src="http://www.certpal.com/blogs/wp-content/uploads/jconsole_overhead.png" alt="jconsole overhead" width="327" height="262" /></a></p>
<p>The average overhead comes to around ~7% when calculated across several trials. The question to ask though is &#8217;7% of what ?&#8217;. For applications that take on huge traffic, an overhead of 7% spread across millions of transactions is costly. The overhead % itself may vary for your application based on a variety of factors.</p>
<p>Take these factors into consideration the next time you profile / monitor java apps.</p>
<p><script type="text/javascript">// <![CDATA[
var dzone_url = 'http://www.certpal.com/blogs/2011/06/delving-deeper-into-jconsole-and-visualvm/';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_title = 'Delving deeper into Jconsole and VisualVm';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_blurb = 'Jconsole and visual VM help monitor java applications in any environment. Here are a few additional details that can help you monitor your java applications better';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_style = '2';
// ]]&gt;</script><br />
<script src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F06%2Fdelving-deeper-into-jconsole-and-visualvm%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F06%2Fdelving-deeper-into-jconsole-and-visualvm%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2011/06/delving-deeper-into-jconsole-and-visualvm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Protobufs and other custom formats</title>
		<link>http://www.certpal.com/blogs/2011/06/protobufs-and-other-custom-formats/</link>
		<comments>http://www.certpal.com/blogs/2011/06/protobufs-and-other-custom-formats/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 09:38:29 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[msgpack]]></category>
		<category><![CDATA[protobuf]]></category>
		<category><![CDATA[protobufs]]></category>
		<category><![CDATA[thrift]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=1035</guid>
		<description><![CDATA[Protobufs and other custom formats can be used to serve internal RPC services. We take a high level overview of what it means to have the wire level format of a service replaced with protobuf / thrift etc.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F06%2Fprotobufs-and-other-custom-formats%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F06%2Fprotobufs-and-other-custom-formats%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div id="_mcePaste">Developers on the web increasingly deal with remote APIs. SOAP Web services / REST rest services are consumed by a myriad of consumers. What format this communication should be carried on is a matter of debate. Each format has its own pros and cons. Inevitably any format that is sent / received from the service needs to be serialized and deserialized. There is a cost associated with this process and the bulky nature of payloads (XML is pretty verbose for example) becomes a factor too. To address these problems you can switch to alternate formats such as <a href="http://code.google.com/p/protobuf/" target="_blank">google protobufs</a> and <a href=" http://msgpack.org/" target="_blank">message pack</a>.</div>
<div id="_mcePaste"></div>
<p><br/></p>
<div><strong>Pros of using custom formats:</strong></div>
<div id="_mcePaste">
<ul>
<li>A lighter payload</li>
<li>Better serialization / deserialization performance</li>
<li>Payload is not verbose</li>
</ul>
</div>
<div id="_mcePaste"><strong>Cons:</strong></div>
<div id="_mcePaste">
<ul>
<li>Lack of tooling / plugins can be a cause for concern</li>
<li>Debugging the format over the wire can prove troublesome</li>
<li>Documentation and support are as vibrant as the community behind the format</li>
</ul>
</div>
<div></div>
<div id="_mcePaste">Google enables protocol buffers by defining structures with .proto files like so. The .proto files are compiled using protoC to produce java , C++ , python related artifacts which are used in the serialization / deserialization process.  </div>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">message Person <span style="color: #009900;">&#123;</span>
required int32 id <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
required string name <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
optional string email <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div id="_mcePaste">Message pack achieves the same with annotations (in Java. There is also support for dynamic typing)</div>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@MessagePackMessage
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> str<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> num<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// new field</span>
@Optional
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> flag <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div>Custom formats are advisable for any service that is consumed internally within your network (Google actually mentions this in the protocol-buffers landing page). The cons mentioned above can make it difficult to expose such formats to the outside world in general. Formats like XML and JSON have better tooling and support than propritary formats. Should you chance upon the need to provide an alternate format to consume your services internally, take a look at the <a href="http://code.google.com/p/protobuf/" target="_blank">protobufs</a> <a href="http://msgpack.org/" target="_blank">message pack</a>, <a href=" http://thrift.apache.org/" target="_blank">thrift</a> etc.</div>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2011/06/protobufs-and-other-custom-formats/';</script><br />
<script type="text/javascript">var dzone_title = 'Protobufs and other custom formats';</script><br />
<script type="text/javascript">var dzone_blurb = 'Protobufs and other custom formats can be used to serve internal RPC services. We take a high level overview of what it means to have the wire level format of a service replaced with protobuf / thrift etc.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F06%2Fprotobufs-and-other-custom-formats%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F06%2Fprotobufs-and-other-custom-formats%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2011/06/protobufs-and-other-custom-formats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java and immutability</title>
		<link>http://www.certpal.com/blogs/2011/02/java-immutable/</link>
		<comments>http://www.certpal.com/blogs/2011/02/java-immutable/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 15:50:29 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[immutable]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[wrapper]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=1011</guid>
		<description><![CDATA[Many classes in java are immutable. However not all classes follow classic immutability. Wrapper classes contain methods that give the illusion of a mutable object. The problem extends to pre / post increment operations on wrappers.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F02%2Fjava-immutable%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F02%2Fjava-immutable%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Java is sprinkled with classes that are immutable. Wrapper classes especially make for a good example. A co-worker recently asked me &#8220;Give me an example of immutability.&#8221;. &#8220;The String class&#8221; I answered, to which he scoffed. He had a good reason however. Consider this code&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GenericTest
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> GenericTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;David&quot;</span><span style="color: #339933;">;</span>
        name.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D&quot;</span>, <span style="color: #0000ff;">&quot;d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Those of you with a keen eye for detail would have quickly seen the problem with this code. If you didnt catch it, dont blame yourself. The String &#8220;name&#8221; is immutable and the replaceAll() method creates a new String with all the &#8216;D&#8217;s replaced with &#8216;d&#8217;. It appears as though the String has been mutated, but in reality nothing has changed. The problem has a simple fix. Simply change a line of code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">name <span style="color: #339933;">=</span> name.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D&quot;</span>, <span style="color: #0000ff;">&quot;d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The bigger problem is with the way the API works. The replaceAll() method suggests that the class is mutable while it really is not. It gives the illusion of mutability which can confuse a lot of developers (me included). You are better off leaving the mutable operations to StringBuilder. When a String needs to be mutated, there is always the StringBuilder(String str) constructor that can help. </p>
<p>The problem is not isolated to Strings. Integer autoboxing and unboxing for example, take place when a &#8216;++&#8217; operation is executed on an Integer wrapper reference. Each &#8216;++&#8217; operation creates a new Integer while giving the illusion of mutability.</p>
<p>Take some lessons from the code above and try to keep immutable classes, well&#8230; immutable. There is nothing technically wrong per se with the code. But when it comes to usability, confusing method names / operations can cause lost time. Besides innumerable additional objects are created from these operations.</p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2011/02/java-immutable/';</script><br />
<script type="text/javascript">var dzone_title = 'Java and immutability';</script><br />
<script type="text/javascript">var dzone_blurb = 'Many classes in java are immutable. However not all classes follow classic immutability. Wrapper classes contain methods that give the illusion of a mutable object. The problem extends to pre / post increment operations on wrappers.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F02%2Fjava-immutable%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F02%2Fjava-immutable%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2011/02/java-immutable/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Antlr tutorial: Hello Antlr</title>
		<link>http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/</link>
		<comments>http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 14:10:01 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[antlr]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=997</guid>
		<description><![CDATA[An introduction / overview to ANTLR, the parser generator. We take a look at grammar that allows a computer to determine whether number A is bigger than number B. Not exactly the most complex operation in the world, but it makes for an easy intro to ANTLR.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Fantlr-tutorial-hello-antlr%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Fantlr-tutorial-hello-antlr%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/antlr_3.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/antlr_3.png" alt="" title="antlr_3" width="332" height="213" class="aligncenter size-full wp-image-1007" /></a></p>
<p>Domain specific languages (DSL) are great tools to communicate with non-programmers. Normally this group includes business users that would like to configure a system / rule using a fluent language (as in &#8211; a natural language). It also includes those like my 8 year old neighbor that knows absolutely nothing about programming. He would love to tell the computer how to perform a small series of operations, without delving into the specifics. Coincidentally, I have been reading up on methodologies to approach DSLs and was introduced to <a href="http://www.antlr.org/" target="_blank">ANTLR</a>.</p>
<h2>Enter ANTLR:</h2>
<p>What my neighbor needs is an English like grammar. This grammar needs to be parsed into something meaningful at runtime. Every time the grammar changes, the parser would need to change too. ANTLR, is a &#8216;parser generator&#8217;. Once a grammar is defined, ANTLR can code-gen a lexer and a parser for this grammar. The lexer identifies tokens in any input that adheres to the grammar and the parser makes sense of these tokens.</p>
<p>A GUI by the name ANTLRWorks helps you generate the grammar and its corresponding lexer+parser code. Lets take a look at an example that asks the computer if a number A is &#8216;bigger than&#8217; or &#8216;smaller than&#8217; number B.</p>
<p>Before you delve into the code, it might be a good idea to download the <a href="http://www.antlr.org/" target="_blank">ANTLR</a> library and the related ANTLRWorks <a href="http://martinfowler.com/articles/languageWorkbench.html" target="_blank">language workbench</a>.</p>
<h2>The grammar:</h2>
<p>Here is the grammar presented in full. Lets analyze it one piece at a time. (Some parts of the grammar are based on <a href="http://www.antlr.org/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3" target="_blank">examples</a> from ANTLR. You will find it easier to delve into documentation that way.  )</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">grammar SimpleCalc<span style="color: #339933;">;</span>
&nbsp;
@header <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.antlr</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
@members <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setAns<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> num1, <span style="color: #003399;">String</span> num2<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Integer</span> a <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>num1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Integer</span> b <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>num2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">&gt;</span>b<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yes&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;no&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
 @lexer<span style="color: #339933;">::</span>header <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.antlr</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/</span>
&nbsp;
is    returns <span style="color: #009900;">&#91;</span><span style="color: #003399;">String</span> expr<span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span>    <span style="color: #0000ff;">'IS'</span> 
                <span style="color: #009900;">&#40;</span>
                a<span style="color: #339933;">=</span>NUMBER <span style="color: #0000ff;">'BIGGERTHAN'</span> b<span style="color: #339933;">=</span>NUMBER <span style="color: #009900;">&#123;</span>setAns<span style="color: #009900;">&#40;</span>$a.<span style="color: #006633;">text</span>,$b.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> 
                <span style="color: #339933;">|</span> 
                a<span style="color: #339933;">=</span>NUMBER <span style="color: #0000ff;">'SMALLERTHAN'</span> b<span style="color: #339933;">=</span>NUMBER <span style="color: #009900;">&#123;</span>setAns<span style="color: #009900;">&#40;</span>$b.<span style="color: #006633;">text</span>,$a.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>$expr<span style="color: #339933;">=</span>answer<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/</span>
&nbsp;
NUMBER    <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>DIGIT<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #339933;">;</span>
&nbsp;
WHITESPACE <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\u</span>000C'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>     <span style="color: #009900;">&#123;</span> $channel <span style="color: #339933;">=</span> HIDDEN<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #339933;">;</span>
&nbsp;
fragment DIGIT    <span style="color: #339933;">:</span> <span style="color: #0000ff;">'0'</span>..<span style="color: #0000ff;">'9'</span> <span style="color: #339933;">;</span></pre></div></div>

<p><strong>Grammar definition:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">grammar SimpleCalc<span style="color: #339933;">;</span>
&nbsp;
@header <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.antlr</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This defines the name of the grammar and the header under which the code needs to be generated. Lexer and parser code can be generated under different packages.</p>
<p><strong>Custom java code:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@members <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setAns<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> num1, <span style="color: #003399;">String</span> num2<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Integer</span> a <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>num1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Integer</span> b <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>num2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">&gt;</span>b<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yes&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;no&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a piece of java code. It is embedded into the parser and can be called when a rule is executed. We maintain a private String by the name &#8216;answer&#8217;. When the rule that compares two numbers is invoked, the setAns() method can be called with the numbers in order to compare them.</p>
<p><strong>Lexer rules:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/</span>
&nbsp;
NUMBER    <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>DIGIT<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #339933;">;</span>
&nbsp;
WHITESPACE <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\u</span>000C'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>     <span style="color: #009900;">&#123;</span> $channel <span style="color: #339933;">=</span> HIDDEN<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #339933;">;</span>
&nbsp;
fragment DIGIT    <span style="color: #339933;">:</span> <span style="color: #0000ff;">'0'</span>..<span style="color: #0000ff;">'9'</span> <span style="color: #339933;">;</span></pre></div></div>

<p>In the lexer section, we identify the bits of the input grammar that the parser can make sense of. A DIGIT ranges from 0-9. When the parser encounters one or more DIGITs, it represents a NUMBER. We also tell ANTLR that any whitespace needs to ignored.</p>
<p><strong>Parser rules:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/</span>
&nbsp;
is    returns <span style="color: #009900;">&#91;</span><span style="color: #003399;">String</span> expr<span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span>    <span style="color: #0000ff;">'IS'</span> 
                <span style="color: #009900;">&#40;</span>
                a<span style="color: #339933;">=</span>NUMBER <span style="color: #0000ff;">'BIGGERTHAN'</span> b<span style="color: #339933;">=</span>NUMBER <span style="color: #009900;">&#123;</span>setAns<span style="color: #009900;">&#40;</span>$a.<span style="color: #006633;">text</span>,$b.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> 
                <span style="color: #339933;">|</span> 
                a<span style="color: #339933;">=</span>NUMBER <span style="color: #0000ff;">'SMALLERTHAN'</span> b<span style="color: #339933;">=</span>NUMBER <span style="color: #009900;">&#123;</span>setAns<span style="color: #009900;">&#40;</span>$b.<span style="color: #006633;">text</span>,$a.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>$expr<span style="color: #339933;">=</span>answer<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The parser section of the grammar, defines a &#8216;is&#8217; operation that returns a String. The pattern to match is &#8216;IS NUMBER &#8216;BIGGERTHAN&#8217; | &#8216;SMALLERTHAN&#8217; NUMBER&#8217;. The numbers are stored in variables a and b. The code inside the {} brackets is embedded into the parser, just like the code inside @members. As such this code can call the methods in @members and is executed when an appropriate match is made.</p>
<p>The match &#8216;IS NUMBER BIGGERTHAN NUMBER&#8217; results in the following call setAns($a.text,$b.text);. When the match is for the &#8216;SMALLERTHAN&#8217; input, simply reverse the numbers in the method call.</p>
<h2>Running the pieces:</h2>
<p>Now that the grammar is in place, it can be debugged in ANTLRWorks or code-gened and ported to a java project. Debugging in ANTLRWorks reveals interesting internal details. Once can visualize how ANTLR goes about generating a parse tree for our grammar or what the syntax tree for the &#8216;is&#8217; rule looks like.</p>
<p><strong>ANTLRWorks:</strong><br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/antlr_1.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/antlr_1.png" alt="" title="antlr_1" width="735" height="356" class="aligncenter size-full wp-image-1004" /></a></p>
<p><strong>Syntax tree:</strong><br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/antlr_2.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/antlr_2.png" alt="" title="antlr_2" width="634" height="101" class="aligncenter size-full wp-image-1005" /></a></p>
<p>The code-gen route allows one to execute the parser for a user defined input.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.antlr.runtime.ANTLRStringStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.antlr.runtime.CommonTokenStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CalcTest
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> CalcTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            SimpleCalcLexer lex <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCalcLexer<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ANTLRStringStream<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;IS 19 SMALLERTHAN 20&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            CommonTokenStream tokens <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CommonTokenStream<span style="color: #009900;">&#40;</span>lex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            SimpleCalcParser parser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCalcParser<span style="color: #009900;">&#40;</span>tokens<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> eval <span style="color: #339933;">=</span> parser.<span style="color: #006633;">is</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>eval<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Modifying bits of the program above would allow the input to be obtained from the console. So my neighbor types in &#8216;IS 20 BIGGERTHAN 19&#8242; and is happy with the result. Now he wants me to make a language that will print multiplication tables. His homework is due this week <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/ ‎';</script><br />
<script type="text/javascript">var dzone_title = 'Hello Antlr';</script><br />
<script type="text/javascript">var dzone_blurb = 'An introduction / overview to ANTLR, the parser generator. We take a look at grammar that allows a computer to determine whether number A is bigger than number B. Not exactly the most complex operation in the world, but it makes for an easy intro to ANTLR.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Fantlr-tutorial-hello-antlr%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Fantlr-tutorial-hello-antlr%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CodePro overview</title>
		<link>http://www.certpal.com/blogs/2011/01/taking-codepro-for-a-test-drive/</link>
		<comments>http://www.certpal.com/blogs/2011/01/taking-codepro-for-a-test-drive/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 09:44:11 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[codepro]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=967</guid>
		<description><![CDATA[The CodePro plugin can be used to analyze code in a variety of ways. This tutorial gives a light weight overview of what to expect when using CodePro]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Ftaking-codepro-for-a-test-drive%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Ftaking-codepro-for-a-test-drive%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Google recently donated a <a href="http://code.google.com/javadevtools/eclipse-donation-faq.html" target="_blank">code profiling</a> product to eclipse. A related code analytics product named <a href="http://code.google.com/javadevtools/download-codepro.html" target="_blank">CodePro</a> is worth a look. It integrates with eclipse nicely. Lets take it for a test drive</p>
<div><strong></p>
<h2>Code Audit:</h2>
<p></strong><strong> </strong></p>
</div>
<div id="_mcePaste">This tool allows you to check for obvious mistakes in your code. Think <a href="http://findbugs.sourceforge.net/" target="_blank">findbugs</a>. You can add / edit rule sets and then run a code audit. At the end of the auditing process, the results display shortcomings in the code. If you already work with findbugs, there is nothing new to see here.</div>
<p><strong>Code review:</strong><br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/code_pro_4.png"><img class="aligncenter size-full wp-image-981" title="code_pro_4" src="http://www.certpal.com/blogs/wp-content/uploads/code_pro_4.png" alt="" width="565" height="202" /></a></p>
<div><strong></p>
<h2>Testing tools:</h2>
<p></strong><strong> </strong></p>
</div>
<div id="_mcePaste">Ever spent time writing tons of junit code ? CodePro can help cut that short by writing codegen that will automatically test paths in your code. Take this class for example</div>
<p><strong>Hello world</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.test</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">==</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div id="_mcePaste">The method go() has 2 flows. If i is 10, an exception is thrown. For any other value, everything is fine. To generate automated test cases, right click on the java file to be tested and click &#8216;CodePro -&gt; Generate Test cases&#8217;. This automatically creates a new test project and a test java file. A junit test editor details the test cases</div>
<div><strong>Junit test editor:</strong></div>
<div id="_mcePaste"><a href="http://www.certpal.com/blogs/wp-content/uploads/code_pro_1.png"><img class="aligncenter size-full wp-image-968" title="code_pro_1" src="http://www.certpal.com/blogs/wp-content/uploads/code_pro_1.png" alt="" width="428" height="149" /></a></div>
<div id="_mcePaste">Notice that the cases i=10 and i=&lt;Any number goes here&gt; are covered. That is great. You can also edit the test cases in the window shown above. Changing the values in the editor changes the codegen appropriately</div>
<p><strong>Codegened test cases:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testGo_1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        HelloWorld fixture <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
        fixture.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// add additional test code here</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">RuntimeException</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testGo_2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        HelloWorld fixture <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
        fixture.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// add additional test code here</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<div><strong></p>
<h2>Dependency Analysis:</h2>
<p></strong><strong> </strong></p>
</div>
<div id="_mcePaste">This tool helps you untangle your dependencies. Dependencies are calculated at different levels. Project level dependencies on libraries; Package dependencies; Class dependencies etc. Here is an example of the same.</div>
<div><strong>Dependency:</strong></div>
<div><a href="http://www.certpal.com/blogs/wp-content/uploads/code_pro_2.png"><img class="aligncenter size-full wp-image-969" title="code_pro_2" src="http://www.certpal.com/blogs/wp-content/uploads/code_pro_2.png" alt="" width="272" height="265" /></a></div>
<p>There are commercial eclipse plugins that do the same job. With CodePro this comes as one of features that can be used.</p>
<div><strong></p>
<h2>Code coverage:</h2>
<p></strong><strong> </strong></p>
</div>
<div id="_mcePaste">Not much to say here. Code coverage is built on <a href="http://emma.sourceforge.net/" target="_blank">EMMA</a>. You get everything that EMMA can provide, plus &#8211; Report generation; Historic view of coverage; Frequency with which code is executed; etc.</div>
<div><strong></p>
<h2>Metrics:</h2>
<p></strong><strong> </strong></p>
</div>
<div id="_mcePaste">This gets pretty interesting. There are several factors involved in writing readable code that are seldom measured. Number of lines of code per method is one of them. If you have ever had the misfortune of analyzing a method with 1200 lines of code (yes it happened to me <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> ), you would love to have this metrics tool. It helps catch violations like this quickly. The metrics that I found most useful were</div>
<div id="_mcePaste">Average number of&#8230;</p>
<ul>
<li>lines per method</li>
<li>fields per type</li>
<li>Comment ratio</li>
<li>Methods per type</li>
</ul>
</div>
<div><strong></p>
<h2>Wasteful code:</h2>
<p></strong><strong> </strong></p>
</div>
<div><strong><br />
</strong></div>
<div><strong><br />
</strong></div>
<div id="_mcePaste">CodePro allows you to find duplicate code and dead code that will never be used. Dead code analysis is a little tricky and didnt quite work the way I expected it would. As for similar code, the tool does a good job and indicates which lines are similar to each other in the code base.</div>
<div><strong>Duplicate code:</strong></div>
<div id="_mcePaste"><a href="http://www.certpal.com/blogs/wp-content/uploads/code_pro_3.png"><img class="aligncenter size-full wp-image-970" title="code_pro_3" src="http://www.certpal.com/blogs/wp-content/uploads/code_pro_3.png" alt="" width="617" height="182" /></a></div>
<div id="_mcePaste">Give <a href="http://code.google.com/javadevtools/download-codepro.html" target="_blank">CodePro</a> a try today. Consult the <a href="http://code.google.com/javadevtools/codepro/doc/index.html" target="_blank">detailed documentation</a> for more information</div>
<p><script type="text/javascript">// <![CDATA[
var dzone_url = 'http://www.certpal.com/blogs/2011/01/taking-codepro-for-a-test-drive/';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_title = 'Taking Google CodePro for a test drive';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_blurb = 'The CodePro plugin released by google can be used to analyze java code in a variety of ways. This tutorial gives a light weight overview of what to expect when using CodePro';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_style = '2';
// ]]&gt;</script><br />
<script src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Ftaking-codepro-for-a-test-drive%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Ftaking-codepro-for-a-test-drive%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2011/01/taking-codepro-for-a-test-drive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jettison &#8211; A JSON streaming parser that uses Stax</title>
		<link>http://www.certpal.com/blogs/2010/12/jettison-a-json-streaming-parser-that-uses-stax/</link>
		<comments>http://www.certpal.com/blogs/2010/12/jettison-a-json-streaming-parser-that-uses-stax/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 11:21:23 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[stax]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=956</guid>
		<description><![CDATA[Jettison is a JSON streaming parser which uses the same interface used by Stax to write XML documents. While this style may seem weird at first, it makes it easier for frameworks that already support XML, to now support the JSON data format.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F12%2Fjettison-a-json-streaming-parser-that-uses-stax%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F12%2Fjettison-a-json-streaming-parser-that-uses-stax%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://jettison.codehaus.org/" target="_blank">Jettison</a> is a library that provides a streaming parser for the JSON data format. Reading that sentence again makes you want to compare it to Stax, the streaming parser for XML. Jettison provides implementation classes that inherit from the same interfaces that a caller would use for XML reading / writing. Diving into some code reveals how JSON is written using this API&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            MappedNamespaceConvention con <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MappedNamespaceConvention<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            XMLStreamWriter writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MappedXMLStreamWriter<span style="color: #009900;">&#40;</span>con, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PrintWriter</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">writeStartDocument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">writeStartElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;alice&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">writeCharacters</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bob&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">writeEndElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">writeEndDocument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output:</strong> {&#8220;alice&#8221;:&#8221;bob&#8221;}</p>
<p>Notice that the XMLStreamWriter is the one in the javax.xml.stream package. The underlying data format that is written using this interface is abstracted. You could use the XMLStreamWriter to write a XML document for example. A XMLStreamReader can be used in the same way to read a XML or JSON document.</p>
<p>This style is quite beneficial especially for those that write frameworks / APIs. Axis2 supports JSON <a href="http://wso2.org/library/768" target="_blank">using jettison</a>. The amount of code changes necessary to support a new format would be minimal, if the code is already modularized into chunks and the interfaces used are the same. Of course we make the assumption that the data formats in question can be supported using a common set of callback methods / events.</p>
<p>While this style allows support for a new format with minimal code changes, the interfaces / method names / events can get in the way to create confusion. What business does one have writing JSON from an interface called XMLStreamWriter ? None really. The confusion that the interface and implementation can cause is also sizable. However after working with jettison for a while and reaping some of its benefits, at least for me, the benefits outweighed the minor drawbacks.</p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/12/jettison-a-json-streaming-parser-that-uses-stax/';</script><br />
<script type="text/javascript">var dzone_title = 'Jettison - A JSON streaming parser that uses Stax';</script><br />
<script type="text/javascript">var dzone_blurb = 'Jettison is a JSON streaming parser which uses the same interface used by Stax to write XML documents. While this style may seem weird at first, it makes it easier for frameworks that already support XML, to now support the JSON data format.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F12%2Fjettison-a-json-streaming-parser-that-uses-stax%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F12%2Fjettison-a-json-streaming-parser-that-uses-stax%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/12/jettison-a-json-streaming-parser-that-uses-stax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using a PipedInputstream and PipedOutputstream</title>
		<link>http://www.certpal.com/blogs/2010/11/using-a-pipedinputstream-and-pipedoutputstream/</link>
		<comments>http://www.certpal.com/blogs/2010/11/using-a-pipedinputstream-and-pipedoutputstream/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 14:01:12 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[io]]></category>
		<category><![CDATA[pipe]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=936</guid>
		<description><![CDATA[Converting an Outputstream into an Inputstream is possible using a PipedInputstream and PipedOutputstream. The java programming language provides these classes to build a pipe. This example shows how to use these classes. This tutorial also guides developers on how to avoid gotchas such as the Write end dead IOException and performance problems related to pipes.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Fusing-a-pipedinputstream-and-pipedoutputstream%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Fusing-a-pipedinputstream-and-pipedoutputstream%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Every once in a while, there comes a need to convert an OutputStream into an Inputstream. The PipedInputStream and PipedOutPutstream classes allow one to achieve this. The idea behind the piped stream is that at one end of the pipe, a writer thread writes to a PipedOutputStream. A PipedInputStream thread concurrently reads whatever is written on the other side. Here is an example use case&#8230;</p>
<p>Assume that a thread streams random words and another thread needs to read these words and write them to System.out. Lets call the producer of the words, a DataSource, and the client the DataConsumer (which is the consumer of the producer). The DataConsumer has a problem. Some of the words from the DataSource are deemed inappropriate by the DataConsumer. The consumer would rather receive &#8216;***&#8217; than the inappropriate word.</p>
<p>What would be great, is if we can pass the stream that is generated from the source to a middle-man which will filter out these words and pass them across to the consumer. This is what the DataSource and DataConsumer look like. The DataSource structures the data using XML, not that it is necessary.</p>
<p><strong>DataSource and DataConsumer:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.pipe</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.stream.XMLOutputFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.stream.XMLStreamWriter</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataSource <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">OutputStream</span> outputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> words <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;crack&quot;</span>,<span style="color: #0000ff;">&quot;hi&quot;</span>,<span style="color: #0000ff;">&quot;you&quot;</span>,<span style="color: #0000ff;">&quot;what&quot;</span>,<span style="color: #0000ff;">&quot;snort&quot;</span>,<span style="color: #0000ff;">&quot;me&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> 
    <span style="color: #000000; font-weight: bold;">private</span> XMLStreamWriter writer <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> DataSource<span style="color: #009900;">&#40;</span><span style="color: #003399;">OutputStream</span> stream<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        outputStream <span style="color: #339933;">=</span> stream<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            XMLOutputFactory output <span style="color: #339933;">=</span> XMLOutputFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer <span style="color: #339933;">=</span> output.<span style="color: #006633;">createXMLStreamWriter</span><span style="color: #009900;">&#40;</span> outputStream <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">writeStartElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> counter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> counter <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span> counter<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span>
                <span style="color: #003399;">String</span> word <span style="color: #339933;">=</span> words<span style="color: #009900;">&#91;</span>result<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                writer.<span style="color: #006633;">writeStartElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;word&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                writer.<span style="color: #006633;">writeCharacters</span><span style="color: #009900;">&#40;</span>word<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                writer.<span style="color: #006633;">writeEndElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            writer.<span style="color: #006633;">writeEndElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">finally</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">try</span>
            <span style="color: #009900;">&#123;</span>
                outputStream.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                outputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.pipe</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStreamReader</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataConsumer <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">InputStream</span> inputStream<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> DataConsumer<span style="color: #009900;">&#40;</span><span style="color: #003399;">InputStream</span> inputStream<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">inputStream</span> <span style="color: #339933;">=</span> inputStream<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">BufferedReader</span> bufferedReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span> inputStream <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> temp<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">=</span>bufferedReader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Firstly, the producer and consumer cannot be connected without a pipe. The producer streams data to an OutputStream while the consumer consumes it through its InputStream. They can be connected to each other by passing one end of the pipe to each class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> BUFFER <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2048</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">PipedInputStream</span> convertPipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PipedInputStream</span><span style="color: #009900;">&#40;</span>BUFFER<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">PipedOutputStream</span> dataPipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PipedOutputStream</span><span style="color: #009900;">&#40;</span>convertPipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
DataSource dataSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DataSource<span style="color: #009900;">&#40;</span>dataPipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
DataConsumer dataConsumer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DataConsumer<span style="color: #009900;">&#40;</span>convertPipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Reading with a pipe:</strong><br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/simple-pipe.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/simple-pipe.png" alt="" title="simple-pipe" width="674" height="117" class="aligncenter size-full wp-image-937" /></a></p>
<p>When the PipedOutputStream is instantiated, it &#8216;connects&#8217; with the PipedInputStream. Which is a fancy way of saying, data that is streamed to the PipedOutputStream will be passed on to the PipedInputStream. This is achieved by storing the data in an internal byte[] buffer, whose size can be chosen when instantiating the PipedInputStream.</p>
<p>It is also possible to insert another thread in the middle by attaching one more pipe. The WordWatcher thread, reads from one end of the pipe (PipedInputStream), filters out objectionable words, and writes data to the other end of the pipe(PipedOutputstream). It uses StaX to parse whatever was written (again, not that it matters)</p>
<p><strong>WordWatcher:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.pipe</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.stream.XMLInputFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.stream.XMLStreamConstants</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.stream.XMLStreamReader</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WordWatcher <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">InputStream</span> inputStream<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">OutputStream</span> outputStream<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> WordWatcher<span style="color: #009900;">&#40;</span><span style="color: #003399;">InputStream</span> inputStream, <span style="color: #003399;">OutputStream</span> outputStream<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">inputStream</span> <span style="color: #339933;">=</span> inputStream<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">outputStream</span> <span style="color: #339933;">=</span> outputStream<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            XMLInputFactory factory <span style="color: #339933;">=</span> XMLInputFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            XMLStreamReader reader <span style="color: #339933;">=</span> factory.<span style="color: #006633;">createXMLStreamReader</span><span style="color: #009900;">&#40;</span>inputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>reader.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">int</span> next <span style="color: #339933;">=</span> reader.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">case</span> XMLStreamConstants.<span style="color: #006633;">CHARACTERS</span><span style="color: #339933;">:</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> reader.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>text.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;crack&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;*****&quot;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    outputStream.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>text.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">finally</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">try</span>
            <span style="color: #009900;">&#123;</span>
                outputStream.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                outputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Multiple pipes:<br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/multi-pipe.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/multi-pipe.png" alt="" title="multi-pipe" width="577" height="119" class="aligncenter size-full wp-image-938" /></a></p>
<p>The following code connects all the pieces. It creates 2 pipes and passes them along to three threads. The threads read and write to these pipes transparently without knowing what implementation class the InputStream and OutputStream belong to.</p>
<p><strong>Connecting the dots:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.pipe</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PipedInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PipedOutputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Start
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> Start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">long</span> past <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> BUFFER <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2048</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">PipedInputStream</span> convertPipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PipedInputStream</span><span style="color: #009900;">&#40;</span>BUFFER<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">PipedOutputStream</span> dataPipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PipedOutputStream</span><span style="color: #009900;">&#40;</span>convertPipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">PipedInputStream</span> convertedWordPipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PipedInputStream</span><span style="color: #009900;">&#40;</span>BUFFER<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">PipedOutputStream</span> outputPipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PipedOutputStream</span><span style="color: #009900;">&#40;</span>convertedWordPipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            DataSource dataSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DataSource<span style="color: #009900;">&#40;</span>dataPipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            WordWatcher pipe <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WordWatcher<span style="color: #009900;">&#40;</span>convertPipe,outputPipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            DataConsumer dataConsumer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DataConsumer<span style="color: #009900;">&#40;</span>convertedWordPipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">Thread</span> producerThread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>dataSource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Thread</span> pipeThread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>pipe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Thread</span> consumerThread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>dataConsumer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            producerThread.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            pipeThread.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            consumerThread.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            consumerThread.<span style="color: #006633;">join</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">long</span> now <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Time taken: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>now<span style="color: #339933;">-</span>past<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; ms&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Gotchas:</h3>
<hr /><br/></p>
<h3>Flush and close, lest you want a &#8216;Write end dead&#8217;:</h3>
<p><br/><br />
When one end of the stream is done writing data, always remember to flush and close the output stream. Failure to do this will result in an IOException with the very helpful message &#8220;Write end dead&#8221;. The exception is supposed to imply that the thread that was at the end of the pipe writing the data, is now dead. Talk about cryptic messages !</p>
<h3>Taking care of the pipeSize buffer:</h3>
<p><br/><br />
In the programs above, try reducing the buffer size, say to 16 instead of 2048. Doing this will make the program slam the brakes. In the real world, there can be a latency between a read from one end and a write to the other end of the pipe. If the buffer is not large enough, the pipe will wait for a read before it can fill in the next chunk of data written by the OutputStream. That will inevitably slow down the program.</p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/11/using-a-pipedinputstream-and-pipedoutputstream/';</script><br />
<script type="text/javascript">var dzone_title = 'Converting an OutputStream into an InputStream';</script><br />
<script type="text/javascript">var dzone_blurb = 'Converting an OutputStream into an InputStream is possible using a PipedInputstream and PipedOutputstream. The java programming language provides these classes to build a pipe. This example shows how to use these classes. This tutorial also guides developers on how to avoid gotchas such as the Write end dead IOException and performance problems related to pipes.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Fusing-a-pipedinputstream-and-pipedoutputstream%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Fusing-a-pipedinputstream-and-pipedoutputstream%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/11/using-a-pipedinputstream-and-pipedoutputstream/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>System.out.println performance</title>
		<link>http://www.certpal.com/blogs/2010/11/system-out-println-performance/</link>
		<comments>http://www.certpal.com/blogs/2010/11/system-out-println-performance/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 14:59:01 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=927</guid>
		<description><![CDATA[The System.out.println statement has rampant use within java programs. Performance is not the first obstacle that comes to mind when these statements are encountered. But given enough System.out.println statements, they can begin to slow down the application.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Fsystem-out-println-performance%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Fsystem-out-println-performance%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Any system that logs vast amounts of information, needs to think about performance. The activity of logging cannot be a synchronous blocking call that returns only when the message has been logged to a persistence store. Enterprise logging systems usually make use of a message bus to carry messages asynchronously to their target persistence store. Be it a database or a file.</p>
<p>Talking about logging brings us to System.out.println() (Lets call is SOP for short). It is a surprisingly commonly method to &#8220;log&#8221; messages. SOP is not meant to be used as a logging system, but unfortunately there is no dearth of projects that have these statements scattered around the code base. The adverse effects that this statement can bring on the performance of the system is often not recognized as well as it should be. </p>
<p>Why is SOP a bottleneck for performance ? This is why&#8230;</p>
<p><strong>Code excerpt from PrintStream.java:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> write<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                ensureOpen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                textOut.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                textOut.<span style="color: #006633;">flushBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                charOut.<span style="color: #006633;">flushBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>autoFlush <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                    out.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedIOException</span> x<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Thread</span>.<span style="color: #006633;">currentThread</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">interrupt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> x<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            trouble <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>All SOP calls on a String result in a corresponding call to the private write(String s) method in PrintStream.java. The synchronized block ensures that every thread has to wait for this call to end before it can proceed. Whats more, the calls to flushBuffer() on the BufferedWriter that is textOut result in the execution of more synchronized blocks. These blocks are expensive to execute.</p>
<p>Here is a chart that shows how performance degrades when a program logs 100,000 messages through various threads. The rate of degradation also depends on the number of characters that are passing through the stream.</p>
<p><strong> System.out.println performance:</strong><br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/sop_performance.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/sop_performance.png" alt="System.out.println performance" title="sop_performance" width="554" height="307" class="aligncenter size-full wp-image-928" /></a><br />
<!-- http://chart.apis.google.com/chart?chxl=0:|10|15|20|25|30|1:|6041+|32621|2:|Number+of+threads|3:|Time+%28ms%29&#038;chxp=2,40|3,50&#038;chxr=0,25,46|1,15,100|2,0,105&#038;chxs=0,3366CC,11.5,-1,l,3399CC|1,FF9900,11.5,0,l,FF9900|2,676767,10.5,-1,l,676767&#038;chxt=x,y,x,y&#038;chs=550x300&#038;cht=lc&#038;chco=3072F3,FF9900&#038;chds=6041,32621,8692,32621&#038;chd=t:6041,8692,10683,13433,14371|11847,16272,20663,27947,32621&#038;chdl=SOP+with+20+characters|SOP+with+70+characters&#038;chg=14.3,14,0,0&#038;chls=2,4,0|2,5,0&#038;chma=0,0,10|10,15&#038;chm=B,76A4FB94,0,0,0|b,FFCC33D0,0,1,0&#038;chtt=SOP+Performance&#038;chts=224499,11.5 --></p>
<p>The degradation experienced in &#8216;Web application X / Y&#8217; may vary, but it cannot be discounted. Avoid using SOPs to log messages in your app. Even one or two that are left over can harm performance under the right conditions. Let us also not forget about e.printStackTrace() and other forms of writing to console output that follow the same synchronized pattern.</p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/11/system-out-println-performance/';</script><br />
<script type="text/javascript">var dzone_title = 'System.out.println and its side effect on performance';</script><br />
<script type="text/javascript">var dzone_blurb = 'The System.out.println statement has rampant use within java programs. Performance is not the first obstacle that comes to mind when these statements are encountered. But given enough System.out.println statements, they can begin to slow down the application.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Fsystem-out-println-performance%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Fsystem-out-println-performance%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/11/system-out-println-performance/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Learning from design mistakes</title>
		<link>http://www.certpal.com/blogs/2010/11/learning-from-design-mistakes/</link>
		<comments>http://www.certpal.com/blogs/2010/11/learning-from-design-mistakes/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 11:35:34 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[mistakes]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=916</guid>
		<description><![CDATA[A list of design mistakes that were made in the java API and how one can learn from them. The java SDK APIs make good use of patterns and practices, but there are places where it falls short.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Flearning-from-design-mistakes%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Flearning-from-design-mistakes%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>While coming up with APIs or a framework, design forms a crucial part of the end result. While a solid design does indeed allow for flexible usage of an API, usability goes hand in hand with the design. What better way is there to create an API, than to learn from the mistakes and successes of other developers first ? Lets take a look at some examples in the java API where things could have been better&#8230;</p>
<h2>Stack and Properties:</h2>
<p>A java.util.Stack provides a standard Stack implementation in the java language. But did you know it extend a java.util.Vector ? Why is this wrong ? Because you can do this</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.design</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Stack</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Vector</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StackTests
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> StackTests<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Vector<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> vector <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Stack<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Stack</span> <span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> stack <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Stack</span> <span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> vector<span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> peek <span style="color: #339933;">=</span> stack.<span style="color: #006633;">peek</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>peek<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        stack.<span style="color: #006633;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;next&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        stack.<span style="color: #006633;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;item&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> remove <span style="color: #339933;">=</span> vector.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Removed: &quot;</span> <span style="color: #339933;">+</span> remove<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        peek <span style="color: #339933;">=</span> stack.<span style="color: #006633;">peek</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>peek<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Output<span style="color: #339933;">:</span>
&nbsp;
first
Removed<span style="color: #339933;">:</span> item
next</pre></div></div>

<p>There are so many things that are going on there that are wrong.</p>
<ol>
<li>Vector is a slow DS. Its methods are synchronized and there is no way around this right now since this contract has been sealed. It cannot be changed without changing backward compatibility.</li>
<li>Vector has exposed its own methods to a stack. You should not be able to add() on to a stack.</li>
<li>Typecasting between Vector / Stack is plain weird.</li>
<li>Since the contract methods exposed by both classes operate on the same internal data structure, the outcome can often be confusing.</li>
</ol>
<p>This is something the API folks got wrong. This is not the only class that should have used composition over inheritance. The<a href="http://download.oracle.com/javase/6/docs/api/java/util/Properties.html" target="_blank"> java.util.Properties</a> class is another example. It extends from HashTable and now there is no way around that.</p>
<h2>Dummy variables:</h2>
<p>This one is probably not well known. A HashSet() has a default constructor which takes a boolean variable called dummy. It is never used. So why does it exist ? Its there because a LinkedHashSet (which extends the HashSet) uses this constructor to tell its super class that the backing data structure should be a LinkedHashMap. The advantage of doing this is that LinkedHashSet will have very little code to house into its implementation. </p>
<p><strong>From HashSet.java:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
* Constructs a new, empty linked hash set.  (This package private
* constructor is only used by LinkedHashSet.) The backing
* HashMap instance is a LinkedHashMap with the specified initial
* capacity and the specified load factor.
*
* @param      initialCapacity   the initial capacity of the hash map
* @param      loadFactor        the load factor of the hash map
* @param      dummy             ignored (distinguishes this
*             constructor from other int, float constructor.)
* @throws     IllegalArgumentException if the initial capacity is less
*             than zero, or if the load factor is nonpositive
*/</span>
<span style="color: #003399;">HashSet</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> initialCapacity, <span style="color: #000066; font-weight: bold;">float</span> loadFactor, <span style="color: #000066; font-weight: bold;">boolean</span> dummy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedHashMap<span style="color: #009900;">&#40;</span>initialCapacity, loadFactor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It is not a great idea to write a separate constructor just because one sub class might need it. Secondly the javadoc explicitly mentions that only a LinkedHashSet uses this constructor. That message is a little confusing. Does that mean other subclasses are not meant to use this constructor ? Why does a subclass even need to know that ?</p>
<p>The side effect of this decision is not far reaching, as you may have already noted. Had the constructor been protected, the consequences would have been worse.</p>
<h2>Usability:</h2>
<p>The java community is pretty awesome. When a solution is too complicated, folks come out with a new framework / API to combat it. Take EJB for example. Versions 1 and 2 were pure horror. Version 3 is a little better. JEE6 has also learned a little from Spring.</p>
<p>This brings us to the java Date and its formatting APIs. Usability wise, many deprecated methods and confusing constructors make for a difficult time using the API. Which is why other solutions like <a href="http://joda-time.sourceforge.net/ " target="_blank">Joda time</a> and <a href="http://commons.apache.org/lang/api-release/org/apache/commons/lang/time/DateUtils.html" target="_blank">Apache commons date utils</a> pop up. The <a href="http://joda-time.sourceforge.net/ " target="_blank">&#8216;Why Joda Time?&#8217;</a> section helps explain what problem the API intends to solve where the JDK fell short.</p>
<p>In general, it will be hard to judge an API / framework without letting a few developers toy around with it first. The methods / constructors may well do what the contract defines them to do, but if it takes 100 steps against 10 to reach from point A to point B, guess which route a developer wants to take ?</p>
<hr/>
<p>In conclusion, these points are not about nitpicking the JDK / Java APIs in any manner. As long as developers learn from these mistakes, they can avoid them in their own code. The JDK is abundant with good usages of design patterns and some of its API are well thought out. Just avoid the bad design decisions whenever possible <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/11/learning-from-design-mistakes/';</script><br />
<script type="text/javascript">var dzone_title = 'Learning from design mistakes';</script><br />
<script type="text/javascript">var dzone_blurb = 'A list of design mistakes that were made in the java API and how one can learn from them. The java SDK APIs make good use of patterns and practices, but there are places where it falls short.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Flearning-from-design-mistakes%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F11%2Flearning-from-design-mistakes%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/11/learning-from-design-mistakes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Will you pay for a JVM on Mac OS X</title>
		<link>http://www.certpal.com/blogs/2010/10/will-you-pay-for-a-jvm-on-mac-os-x/</link>
		<comments>http://www.certpal.com/blogs/2010/10/will-you-pay-for-a-jvm-on-mac-os-x/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 16:41:14 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[mac]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=907</guid>
		<description><![CDATA[A poll that tries to find out if developers would be willing to pay for a JVM release on the Mac OS X platform. ]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F10%2Fwill-you-pay-for-a-jvm-on-mac-os-x%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F10%2Fwill-you-pay-for-a-jvm-on-mac-os-x%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This has been a sad week for java. One of the string of bad things happening to it, is the abandonement by Mac OS X. Among the interesting thoughts that have been thrown out there for a possible rescue plan, <a href="http://www.adam-bien.com/roller/abien/entry/jdk_java_7_on_mac" target="_blank">this one seemed to stand out</a>.</p>
<p>In a nutshell, the link points to a post by Adam-Bien suggesting that users might pay for a JVM on the Mac, thereby opening up a business opportunity. That might be far fetched considering</p>
<ol>
<li>Apple wants to be a control freak of sorts. Anything that is not objective-C is a target <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </li>
<li>Folks usually have a very hard time convincing themselves to pay for something that they used to get for free.</li>
<li>It would be pretty difficult to write a JVM targeted for the Mac from scratch, given that many native API calls are secrets.</li>
</ol>
<p>But who knows. Why dont you be the judge and tell us what you think. Would you shell out some dough for a JVM on the Mac ? Take a small poll. The comments on Adam&#8217;s post seemed to swing both ways, so perhaps a poll will enumerate what people feel.</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/10/will-you-pay-for-a-jvm-on-mac-os-x/';</script><br />
<script type="text/javascript">var dzone_title = 'Poll: Will you pay for a JVM on Mac OS X';</script><br />
<script type="text/javascript">var dzone_blurb = 'A poll that tries to find out if developers would be willing to pay for a JVM release on the Mac OS X platform. ';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F10%2Fwill-you-pay-for-a-jvm-on-mac-os-x%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F10%2Fwill-you-pay-for-a-jvm-on-mac-os-x%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/10/will-you-pay-for-a-jvm-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Visualizing application errors with Simile timeline</title>
		<link>http://www.certpal.com/blogs/2010/10/visualizing-application-errors-with-simile-timeline/</link>
		<comments>http://www.certpal.com/blogs/2010/10/visualizing-application-errors-with-simile-timeline/#comments</comments>
		<pubDate>Sat, 02 Oct 2010 14:03:33 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[simile]]></category>
		<category><![CDATA[timeline]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=884</guid>
		<description><![CDATA[An introduction to the simile timeline framework and how it can be used to visualize application errors in log files. This simile timeline example shows how to convert log files to event XML files and quickly get to errors]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F10%2Fvisualizing-application-errors-with-simile-timeline%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F10%2Fvisualizing-application-errors-with-simile-timeline%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Every developer out there has to troubleshoot a problem in PROD every now and then. The series of steps goes something like this</p>
<ol>
<li> User notices something fishy</li>
<li> Clicks on a button that reads &#8216;Do not click here&#8217; <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </li>
<li> Boom !</li>
<li> Developer tries to figure out what happened by fishing through log files.</li>
</ol>
<p>Now step 4 can be relatively more simple than it needs to be. It is often complicated by bad practices or obstacles like the ones mentioned below</p>
<ol>
<li> Developers log all application logs into a central server log</li>
<li> Access to logs are restricted by a &#8216;log viewing&#8217; application.</li>
</ol>
<p>Instead of fishing around for errors, would it not be cool if you could visualize the errors that occur in your application through some sort of timeline ? After investigating a couple of frameworks that would allow one to do this, I figured &#8216;<a href="http://code.google.com/p/simile-widgets/wiki/Timeline" target="_blank">Simile Timeline</a>&#8216; would be a good fit.</p>
<p>Timeline is a framework that allows you to view a series of events through well&#8230; a timeline. You can write a page that uses the timeline JS to view events that occur over time. Events can be fed to this framework using a XML. So here is what you can do to visualize logs for your application and get to the error real quick</p>
<ol>
<li> Write an application (batch ?) to stream though your log files and build an event XML for each log file</li>
<li> Use a small web application to load this event file and display it to the developer</li>
</ol>
<p>Now, all the developer has to do is to scroll through the event timeline to figure out what went wrong and when it happened. Enough talk. Go ahead and check out how <a href="http://www.certpal.com/LogViewer/index.html" target="_blank">application errors in logs can be displayed through simile timeline</a>.</p>
<p>You might also be interested in the <a href="http://www.certpal.com/LogViewer/timeline.xml" target="_blank">event XML</a> that helped display the timeline. Most production environments will retain logs for a while and then discard them. The presence of a XML describing all the errors that happened say a year back, can prove to be useful. That way when you scratch your head thinking &#8216;Hmm&#8230; did we not encounter this same problem 3 months back ?&#8217; you can scroll back in time and quickly verify it. Archiving these XML files is easier since they will not exceed more than a few KB in size.</p>
<p><strong>Timeline generation:</strong></p>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/timeline.png"><img class="aligncenter size-full wp-image-887" title="timeline" src="http://www.certpal.com/blogs/wp-content/uploads/timeline.png" alt="" width="731" height="522" /></a></p>
<p>For those that want to play around with the framework itself, feel free to take a look at the <a href="http://www.certpal.com/LogViewer/index.html" target="_blank">HTML source</a> of the example page. The project <a href="http://code.google.com/p/simile-widgets/wiki/Timeline" target="_blank">wiki</a> is also helpful.<br />
<script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/10/visualizing-application-errors-with-simile-timeline/';</script><br />
<script type="text/javascript">var dzone_title = 'A cool way to visualize application errors from logs';</script><br />
<script type="text/javascript">var dzone_blurb = 'An introduction to the simile timeline framework and how it can be used to visualize application errors in log files.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F10%2Fvisualizing-application-errors-with-simile-timeline%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F10%2Fvisualizing-application-errors-with-simile-timeline%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/10/visualizing-application-errors-with-simile-timeline/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The 5 minute fix</title>
		<link>http://www.certpal.com/blogs/2010/08/the-5-minute-fix/</link>
		<comments>http://www.certpal.com/blogs/2010/08/the-5-minute-fix/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 01:06:05 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[expectation]]></category>
		<category><![CDATA[fix]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=862</guid>
		<description><![CDATA[A developer's perspective on setting the right expectations for any work that you do. Sometimes that is more important than the work itself.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F08%2Fthe-5-minute-fix%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F08%2Fthe-5-minute-fix%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/bomb_fuse.png"><img class="alignright size-full wp-image-825" title="bomb_fuse" src="http://www.certpal.com/blogs/wp-content/uploads/bomb_fuse.png" alt="" width="128" height="128" /></a>It was a normal day. The sun rose, birds were singing and everything seemed to be going fine until Kaboom ! A bug appeared in the live environment. Things turned downhill from there that day. But we are more interested in how things got fixed so lets focus on that.</p>
<p>An investigation of the code revealed that the bug had crept in because of a minor mistake. A variable was referring to the wrong column index on a table. &#8216;Hmmm&#8230; simple fix&#8217; I thought. I told my manager it would not take more than 5 minutes to do. &#8216;Its a one line fix&#8217;. Mistake.</p>
<p>There were loads of other things to do after fixing this bug. This is what I ended up doing over the next 4 hours.</p>
<ul>
<li> Analyze if any other code flow would explode.</li>
<li> Code the fix &#8211; This only took 5 minutes as expected.</li>
<li> Move fix to DEV</li>
<li> DEV server crashed. Great. Fix this and get DEV up and running.</li>
<li> Test fix</li>
<li> Export the old data before the fix</li>
<li> Export the new data after the fix</li>
<li> Compare new data with old data and verify the result for each column (Wrote a small program to do this).</li>
<li> Move fix to PROD</li>
<li> Close the bugzilla bugs and fill in a lot of bug details</li>
<li> Inform PROD users about the fix</li>
</ul>
<p>So ! There we are. All done. Loose ends tied. But at the end of the day, I had not set the right expectations when I went about fixing this problem. When you say things like &#8216;It will be done in 5 minutes&#8217; and &#8216;It is a one line fix&#8217;, you need to remember that to an end user / manager that might sound something like &#8216;It will start working again after 5 minutes&#8217;. But that is not the case. Well&#8230; a manager with a technical background would be more understanding but that is not my point.</p>
<p>Sometimes there are things you cannot put down in writing when you provide an estimated time to fix stuff. I find it is always better to say &#8216;Sometime by Tuesday noon&#8217; instead of &#8216;One line code change / 5 minutes&#8217;. It sets the right expectation and you don&#8217;t have people asking you &#8216;Its not done yet !?&#8217;. Explaining your work is sometimes more important than the work itself.</p>
<p><script type="text/javascript">// <![CDATA[
var dzone_url = 'http://www.certpal.com/blogs/2010/08/the-5-minute-fix/';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_title = 'The 5 minute fix';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_blurb = 'A developer's perspective on setting the right expectations for any work that you do. Sometimes that is more important than the work itself.';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_style = '2';
// ]]&gt;</script><br />
<script src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F08%2Fthe-5-minute-fix%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F08%2Fthe-5-minute-fix%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/08/the-5-minute-fix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Neuroph tutorial &#8211; The code classifier</title>
		<link>http://www.certpal.com/blogs/2010/07/java-neuroph-tutorial-the-code-classifier/</link>
		<comments>http://www.certpal.com/blogs/2010/07/java-neuroph-tutorial-the-code-classifier/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 17:34:59 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[neural]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=844</guid>
		<description><![CDATA[The Neuroph API can be used to train neural networks in java. This experiment tries to use the API to enable automatic classification of source code based on the programming language it belongs to]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F07%2Fjava-neuroph-tutorial-the-code-classifier%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F07%2Fjava-neuroph-tutorial-the-code-classifier%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/neuroph_logo.png"><img class="alignright size-full wp-image-666" title="neuroph_logo" src="http://www.certpal.com/blogs/wp-content/uploads/neuroph_logo.png" alt="" width="257" height="148" /></a> An <a href="http://www.ibm.com/developerworks/library/l-neural/" target="_blank">article was written a while back</a> about how neural networks can be used to classify source code. Yes the source code that you write to feed to compilers / interpreters.</p>
<p>The article explains at a high level what method could be used to perform this activity. In the end the author claims some level of success and wonders how other neural-network implementations / techniques would solve the same problem. This got me curious enough and I spent a weekend trying to crack this with Neuroph, the neural network library for java. I present to you my analysis and results below. For the impatient here is the code-classifier <a href="http://www.certpal.com/NeuralWeb/index.html" target="_blank">DEMO</a>.</p>
<h2>Why solve this problem with neural networks ?</h2>
<p>So where do we begin ? How about the question &#8216;Why do we need neural networks to solve this problem ?&#8217;. Well actually it might not be the &#8216;best&#8217; way to solve the problem. A lexer is a better solution to this problem since it can scan the source file and probably give you a much better accuracy on the clasification. But neural networks are also great for this problem since they can keep learning based on the input that is provided. Add one more programming language into the bag and the network can learn about this language without a programmer needing to type extra code into a lexer.</p>
<h2>So how do we do it ?</h2>
<p>One of the features that differentiates one source code type from another is the keywords used by the programming language in question. So lets target that. Our neural network will try to identify if source code belongs to the java language or the python language. </p>
<p>It takes as its input a list of all keywords available in both languages. Mind you that there may even be some overlap between the keywords. For example the keyword &#8216;if&#8217; is used in both languages. But there may be keywords that are used solely in one language alone.</p>
<p><strong>Neural network viewed in graph mode: (The network is too huge to fit into this picture <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )<br />
</strong></p>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/neural_code_classifier.png"><img class="aligncenter size-full wp-image-848" title="neural_code_classifier" src="http://www.certpal.com/blogs/wp-content/uploads/neural_code_classifier.png" alt="" width="738" height="552" /></a></p>
<p>A middle layer will contain a few nodes that will back propagate anything that it learns. The output layer (just one node) will decide if the language is java / python. The value will range from 0-1, where 0 represents closeness to python and 1 represents closeness to java.</p>
<h2>How do we train it ?</h2>
<p>The process of training is made easy by the neuroph API. First we need a few java / python files. A program was written that reads each file and splits it based on the whitespace delimiter. Based on a known set of keywords, the network is fed the number of times a keyword appears in a source file.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span>Double, Double<span style="color: #339933;">&gt;</span> map <span style="color: #339933;">:</span> keywordStrength<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Collection<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span> values <span style="color: #339933;">=</span> map.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Vector<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    input.<span style="color: #006633;">addAll</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    SupervisedTrainingElement supervisedTrainingElement <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SupervisedTrainingElement<span style="color: #009900;">&#40;</span>input,output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    trainingSet.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span>supervisedTrainingElement<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For example, recognized keywords may be &#8216;public&#8217; represented by the input index 0 and &#8216;static&#8217; represented by the input index 1.  A java file may contain the word pubic 10 times and static 5 times. The input to the network in this case would be 10 5. We can now use these numbers to tell the network &#8216;Hey if you see this pattern it is likely that the source code belongs to language X&#8217;. Neat !. Once the network is trained you can load it and use the same process to classify source code for any given input String.</p>
<h2>Well&#8230; show us some stats:</h2>
<p>Before I say anything else, I did not expect this approach to succeed more than 10-20% of the time. This is because the number of files that I trained this network with was around 20. Java + Python included. But I was surprised to see the network classifying the data correctly most of the time. There were very few cases where it failed.</p>
<p>The real test for this network is now in your hands. I made a java web app on top of this &#8216;code classifier&#8217; application and built a simple UI using Jquery. You can type in any code / junk you want and hit the trained network. If it manages to classify your code correctly at least 60% of the time, I would consider that an enormous victory (for Neuroph ?) <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So what are you waiting for, <a href="http://www.certpal.com/NeuralWeb/index.html" target="_blank">take her for a spin</a>.</p>
<h2>How do I game the results:</h2>
<p>This is pretty simple. There are certain rules you must follow. Not following them will give you weird results <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>1. Make sure that your code does not contain comments. I do not strip comments from code before analyzing them for keywords. Including java like keywords in a python file comment will confuse the network.<br />
2. Input only those files that will compile / run correctly on Java / Python. In theory you could feed in a Shakespeare novel to my network and it will still try to classify it as source code <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>So does the network work for you ? Feel free to leave a comment.</p>
<p><em>I also wrote a simpler <a href="http://www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/" target="_new">tutorial for Neuroph</a> that is less practical and more academic. Check it out if it interests you</em></p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/07/java-neuroph-tutorial-the-code-classifier/';</script><br />
<script type="text/javascript">var dzone_title = 'Java Neuroph tutorial - The code classifier';</script><br />
<script type="text/javascript">var dzone_blurb = 'The Neuroph API can be used to train neural networks in java. This experiment tries to use the API to enable automatic classification of source code based on the programming language it belongs to. ';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F07%2Fjava-neuroph-tutorial-the-code-classifier%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F07%2Fjava-neuroph-tutorial-the-code-classifier%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/07/java-neuroph-tutorial-the-code-classifier/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

