<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Ashish's Tech Blog</title>
	
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<pubDate>Tue, 29 Sep 2009 11:04:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/AshishsTechBlog" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Executors in Action - Part 1 - The Life cycle</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/B2pnU_-GuAg/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/09/executors-in-action-part-1-the-life-cycle/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 10:59:36 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=322</guid>
		<description><![CDATA[From the Javadoc
… interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An Executor is normally used instead of explicitly creating threads.
Indeed, Executors provide a more robust and easy mechanism for decoupling of submitting a task, from its execution.
In [...]]]></description>
			<content:encoded><![CDATA[<p>From the Javadoc</p>
<p>… interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An Executor is normally used instead of explicitly creating threads.</p>
<p>Indeed, Executors provide a more robust and easy mechanism for decoupling of submitting a task, from its execution.</p>
<p>In this series, we shall explore this concurrency construct in detail.</p>
<p>NOTE: Have tried to avoid explaining what is already available in javadoc.</p>
<p>Let’s first see the lifecycle of an Executor</p>
<p>ExecutorService extends the Executor interface, adding a bunch of life cycle methods.</p>
<p><div id="attachment_328" class="wp-caption alignnone" style="width: 475px"><img src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2009/09/executorstatemachine1.png" alt="Executor Life Cycle" title="executorstatemachine1" width="465" height="220" class="size-full wp-image-328" /><p class="wp-caption-text">Executor Life Cycle</p></div><br />
Let’s create a simple program to demonstrate the same</p>
<pre class="brush: java">
public static void main(String[] args) {
// Executor doesn&#039;t exist before this step
// Executor created and moved to running state
ExecutorService executor = Executors.newSingleThreadExecutor();

// Executor into running state
executor.execute(new Runnable() {
public void run() {
System.out.println(&quot;Running...&quot;);
}
});

// Lets shutdown
executor.shutdown();

System.out.println(&quot; isShutdown = &quot;+executor.isShutdown());
System.out.println(&quot; isTermianted = &quot;+executor.isTerminated());
}
</pre>
<p>The program depicts the complete lifecycle. Please see the comments inline in the code</p>
<p>Initially the Executor doesn’t exist. When we create an Executor, using Executor’s we have it in Ready state (ready to accept tasks for execution).<br />
Once we submit a task for execution, the Executor moves to running state. Once it completes the task, it moves back to ready state.</p>
<p>Once we call <strong>shutdown()</strong> on Executor, it begins its shutdown cycle. Once it is completed it moves to Terminated state.</p>
<p>There are couple of things that need to be noted after shutdown is initiated. An Executor can be executing or may have couple of tasks pending to be executed.</p>
<p>Calling <strong>shutdown()</strong> initiates an orderly shutdown, means all the pending tasks shall be completed, but new tasks won’t be accepted</p>
<p>Calling <strong>shutdownNow()</strong> initiates a shutdown, by trying to halt the processing the tasks by interrupting the threads. The method returns a list of Runnable’s which were not executed. Programs can use them to reschedule them later or discard them.</p>
<p>What will happen, if we submit a task once shutdown has been initiated?<br />
This situation can be handled by using RejectExecutionHandler. The default implementation simply discards the tasks. We can have the custom logic to handle the tasks that couldn’t be executed.</p>
<p>Upcoming topic – Exploring a little on ScheduledExecutorService</p>

<p><a href="http://feedads.g.doubleclick.net/~a/2qUkV8NLQ-7CsDU1QCc7blJjxOY/0/da"><img src="http://feedads.g.doubleclick.net/~a/2qUkV8NLQ-7CsDU1QCc7blJjxOY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/2qUkV8NLQ-7CsDU1QCc7blJjxOY/1/da"><img src="http://feedads.g.doubleclick.net/~a/2qUkV8NLQ-7CsDU1QCc7blJjxOY/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/B2pnU_-GuAg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/09/executors-in-action-part-1-the-life-cycle/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/09/executors-in-action-part-1-the-life-cycle/</feedburner:origLink></item>
		<item>
		<title>MINA vs Netty Series - Closing down</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/FtF5c6x2Vao/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/09/mina-vs-netty-series-closing-down/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 15:08:31 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=318</guid>
		<description><![CDATA[Folks after posting two articles in the series, I have decided to hold my work on this for a while. I am working of new MINA User Guide, and will be busy over there.
However, this is not an end to this. May be sometime in the future, when I am have bandwidth available shall start [...]]]></description>
			<content:encoded><![CDATA[<p>Folks after posting two articles in the series, I have decided to hold my work on this for a while. I am working of new MINA User Guide, and will be busy over there.</p>
<p>However, this is not an end to this. May be sometime in the future, when I am have bandwidth available shall start working on this again.</p>
<p>You can check the new MINA User Guide at the following link http://mina.apache.org/user-guide.html</p>

<p><a href="http://feedads.g.doubleclick.net/~a/h50TA6D5MZ8u0adPl6nAqTZg5NY/0/da"><img src="http://feedads.g.doubleclick.net/~a/h50TA6D5MZ8u0adPl6nAqTZg5NY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/h50TA6D5MZ8u0adPl6nAqTZg5NY/1/da"><img src="http://feedads.g.doubleclick.net/~a/h50TA6D5MZ8u0adPl6nAqTZg5NY/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/FtF5c6x2Vao" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/09/mina-vs-netty-series-closing-down/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/09/mina-vs-netty-series-closing-down/</feedburner:origLink></item>
		<item>
		<title>If-else vs switch – Which is better?</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/edZjn_uMJkA/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 02:16:35 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=303</guid>
		<description><![CDATA["Use switch instead of if-else, its more readable and has better performance." I have to admit that this was one of my favorite code review comment. Until one fine day, while hacking Apache Sanselan's image format decoding function, I tried optimizing the code based on the same comments and while benchmark there was hardly any [...]]]></description>
			<content:encoded><![CDATA[<p>"Use switch instead of if-else, its more readable and has better performance." I have to admit that this was one of my favorite code review comment. Until one fine day, while hacking Apache Sanselan's image format decoding function, I tried optimizing the code based on the same comments and while benchmark there was hardly any difference. I thought about investigating it further. Though found some interesting mail chains, though about posting my finding.</p>
<p>To begin with decided to run some samples on switch and if-else constructs and analyze further.</p>
<p>Wrote three function<br />
1. For if-else - a if-else ladder based on int comparisons<br />
2. For Switch - switch with 21 cases, from 1 to 20<br />
3. For Switch - switch with sparse random values</p>
<p>The reason for choosing two functions for switch was this statement from <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Compiling.doc.html#14942">VM spec</a> <em>"Compilation of switch statements uses the tableswitch and lookupswitch instructions"</em></p>
<p>Lets see the function codes</p>
<h3>if-else</h3>
<pre class="brush: java">
public static void testIfElse(int jumpLabel) {
if(1 == jumpLabel) {
    System.out.println(&quot;1&quot;);
} else if(2 == jumpLabel) {
    System.out.println(&quot;2&quot;);
} else if(3 == jumpLabel) {
    System.out.println(&quot;3&quot;);
} else if(4 == jumpLabel) {
    System.out.println(&quot;4&quot;);
}
// Removed for simplicity
else {
    System.out.println(&quot;default&quot;);
}
}
</pre>
<p>Lets see the switch functions</p>
<h3>Finite switch version</h3>
<pre class="brush: java">
public static void testSwitchFinite(int jumpLable) {

switch (jumpLable) {
case 1:
    System.out.println(&quot;1&quot;);
    break;

case 2:
    System.out.println(&quot;2&quot;);
    break;

case 3:
    System.out.println(&quot;3&quot;);
    break;

case 4:
    System.out.println(&quot;4&quot;);
    break;

case 5:
    System.out.println(&quot;5&quot;);
    break;

// Removed other cases for simplicity

default:
    System.out.println(&quot;default&quot;);
    break;
}
}
</pre>
<h3>Sparse switch version</h3>
<pre class="brush: java">
public static void testSwitchSparse(int jumpLable) {

switch (jumpLable) {
case 100:
    System.out.println(&quot;1&quot;);
    break;

case -1:
    System.out.println(&quot;2&quot;);
    break;

case 5000:
    System.out.println(&quot;3&quot;);
    break;

case -8:
    System.out.println(&quot;4&quot;);
    break;

case 1600:
    System.out.println(&quot;5&quot;);
    break;

case 250:
    System.out.println(&quot;250&quot;);
    break;

// Removed other cases for simplicity

default:
    System.out.println(&quot;default&quot;);
    break;
}
}
</pre>
<p>With the groundwork done, its the benchmarking time. The benchmarking strategy was simple. Run these functions in loop and see the result, with iteration ranging from 100 to 1000.</p>
<p>Lets look at one of the functions</p>
<pre class="brush: java">
public static void testSwitchPerf(int iteration) {
    long t1 = System.nanoTime();
    for (int i = 0; i &lt; iteration; i++) {
        testSwitchFinite(i);
    }
    long t2 = System.nanoTime();
    System.out.println(&quot;Time Taken (switch) = &quot;+(t2 - t1)/1000000);
}
</pre>
<p>Well this was the ground work, after executing the conditions, here is the data</p>
<table border="1" width="100%">
<tbody>
<tr>
<td>Iteration -&gt;</td>
<td>100</td>
<td>1000</td>
</tr>
<tr>
<td>if-else</td>
<td>8 ms</td>
<td>69 ms</td>
</tr>
<tr>
<td>switch finite</td>
<td>3 ms</td>
<td>34 ms</td>
</tr>
<tr>
<td>switch sparse</td>
<td>7 ms</td>
<td>21 ms</td>
</tr>
</tbody>
</table>
<p>NOTE: There is some difference due to the way data is provided and the sample space doesn't provide precise results. However, since the sample space is same for both, it would serve its purpose</p>
<h3>Conclusion</h3>
<p>1. There is no significant execution difference between if-else and switch. The difference observed is due to the sample space choosen.<br />
2. If using if-else, its always recommended to put frequently used if condition at the top of if-else ladder<br />
3. The finite switch statement was converted to tableswitch and sparse switch was converted to lookupswitch</p>
<p>would be interested to hear from folks about their experience in relation to this. I would say, i still prefer switch for readability. Henceforth, my review comment shall be modified as "Use switch instead of if-else, its more readable <del datetime="2009-07-28T07:33:30+00:00">and has better performance</del>"</p>
<h3>Share your thoughts</h3>
<p>I would be very keen to hear from you all, about your opinions and experience about the topic</p>

<p><a href="http://feedads.g.doubleclick.net/~a/FCT9B80b84hxuqsX5rTuuwUVKfo/0/da"><img src="http://feedads.g.doubleclick.net/~a/FCT9B80b84hxuqsX5rTuuwUVKfo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/FCT9B80b84hxuqsX5rTuuwUVKfo/1/da"><img src="http://feedads.g.doubleclick.net/~a/FCT9B80b84hxuqsX5rTuuwUVKfo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/edZjn_uMJkA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%e2%80%93-which-is-better/</feedburner:origLink></item>
		<item>
		<title>[Apache Sanselan] Demystifying how Sanselan determines image format</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/U0rLiExLPWs/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-demystifying-how-sanselan-determines-image-format/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 16:08:20 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Apache Sanselan]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=292</guid>
		<description><![CDATA[Apache Sanselan is a pure Java library for reading and Writing Image formats. It has recently graduated from Incubator and is now a proud member of Apache commons proper.
In previous articles, we saw how to retrieve Image metadata and information. In this post we shall see how Sanselan guesses the Image format. We shall take [...]]]></description>
			<content:encoded><![CDATA[<p>Apache Sanselan is a pure Java library for reading and Writing Image formats. It has recently graduated from Incubator and is now a proud member of Apache commons proper.</p>
<p>In previous articles, we saw how to retrieve Image metadata and information. In this post we shall see how Sanselan guesses the Image format. We shall take it in two steps</p>
<ul>
<li>First we shall look at the ImageFormat class</li>
<li>Then, we shall look into the implementation guessFormat() API</li>
</ul>
<h4><span style="text-decoration: underline;">ImageFormat class</span></h4>
<p><em><a href="http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/ImageFormat.java?view=log" target="_blank">org.apache.sanselan.ImageFormat</a></em> Class has three members name, extension and actual. Name and extension are same, however I am not sure I understand the use of actual variable.</p>
<p>The class has list of formats (including Unknown) supported by the library. They are all instances of ImageFormat class. The list is</p>
<ol>
<li>IMAGE_FORMAT_UNKNOWN</li>
<li>IMAGE_FORMAT_PNG</li>
<li>IMAGE_FORMAT_GIF</li>
<li>IMAGE_FORMAT_ICO</li>
<li>IMAGE_FORMAT_TIFF</li>
<li>IMAGE_FORMAT_JPEG</li>
<li>IMAGE_FORMAT_BMP</li>
<li>IMAGE_FORMAT_PSD</li>
<li>IMAGE_FORMAT_PBM</li>
<li>IMAGE_FORMAT_PGM</li>
<li>IMAGE_FORMAT_PPM</li>
<li>IMAGE_FORMAT_PNM</li>
<li>IMAGE_FORMAT_TGA</li>
<li>IMAGE_FORMAT_JBIG2</li>
</ol>
<h4><span style="text-decoration: underline;">guessFormat API – Under the hood</span></h4>
<p>The guessFormat() API looks at the initial 2 to 4 bytes, also known as magic numbers, to determine the Image Format.</p>
<p>Algorithm is as follows:</p>
<ul>
<li>Read first two bytes</li>
<li>Match with existing set of magic numbers and determine format</li>
<li>It use byte 3 and 4 to determine format for JBig2</li>
</ul>
<p>The list of magic numbers for different formats is as follows</p>
<table border="1" width="300">
<tbody>
<tr>
<th width="171" scope="col">Format</th>
<th width="52" scope="col">Byte 1</th>
<th width="55" scope="col">Byte 2</th>
</tr>
<tr>
<td>IMAGE_FORMAT_GIF</td>
<td>0x47</td>
<td>0x49</td>
</tr>
<tr>
<td>IMAGE_FORMAT_PNG</td>
<td>0x89</td>
<td>0x50</td>
</tr>
<tr>
<td>IMAGE_FORMAT_JPEG</td>
<td>0xff</td>
<td>0xd8</td>
</tr>
<tr>
<td>IMAGE_FORMAT_BMP</td>
<td>0x42</td>
<td>0x4d</td>
</tr>
<tr>
<td>IMAGE_FORMAT_TIFF</p>
<p>(Motorola byte order)</td>
<td>0x4D</td>
<td>0x4D</td>
</tr>
<tr>
<td>IMAGE_FORMAT_TIFF</p>
<p>(Intel byte order)</td>
<td>0x49</td>
<td>0x49</td>
</tr>
<tr>
<td>IMAGE_FORMAT_PSD</td>
<td>0x38</td>
<td>0x42</td>
</tr>
<tr>
<td>IMAGE_FORMAT_PBM</td>
<td>0x50</td>
<td>0x31 or 0x34</td>
</tr>
<tr>
<td>IMAGE_FORMAT_PGM</td>
<td>0x50</td>
<td>0x32 or 0x35</td>
</tr>
<tr>
<td>IMAGE_FORMAT_PPM</td>
<td>0x50</td>
<td>0x33 or 0x36</td>
</tr>
<tr>
<td>IMAGE_FORMAT_JBIG2</td>
<td>0x97</td>
<td>0x4A</td>
</tr>
</tbody>
</table>
<p>In addition to this IMAGE_FORMAT_JBIG2 format, byte 3 must be equal to 0x42 and byte 4 must be equal to 0x32.</p>
<p>Based on this table, Sanselan recognizes the Image format. If the magic numbers don’t match from the one in the table, it returns IMAGE_FORMAT_UNKNOWN</p>
<p><strong><span style="text-decoration: underline;">References</span></strong></p>
<p><strong><a href="http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/Sanselan.java?view=log" target="_blank">Sanselan.java</a></strong></p>
<a href='#' id="mfg_link_1" class="mfg"></a>
<script type="text/javascript">/* <![CDATA[ */
mfg_create_link( "mfg_link_1", "Apache Sanselan decode image format", "More from Google on <em>", "<\/em> &raquo;");
/* ]]&gt; */</script>
<p><a href="http://feedads.g.doubleclick.net/~a/XgzSkg7YDltZcgOEYytGEClHcqQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/XgzSkg7YDltZcgOEYytGEClHcqQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/XgzSkg7YDltZcgOEYytGEClHcqQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/XgzSkg7YDltZcgOEYytGEClHcqQ/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/U0rLiExLPWs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-demystifying-how-sanselan-determines-image-format/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-demystifying-how-sanselan-determines-image-format/</feedburner:origLink></item>
		<item>
		<title>[Apache Sanselan] Retrieving Image Metadata using Sanselan</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/WYKjZbR6HTY/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-retrieving-image-metadata-using-sanselan/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 13:30:00 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Apache Sanselan]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=287</guid>
		<description><![CDATA[Apache Sanselan is a pure Java library for reading and Writing Image formats. It has recently graduated from Incubator and is now a proud member of Apache commons proper.
In this article we shall see how to get Image metadata using Apache Sanselan.
Let’s see how the code looks
public static void main&#40;String&#91;&#93; args&#41; &#123;
    [...]]]></description>
			<content:encoded><![CDATA[<p>Apache Sanselan is a pure Java library for reading and Writing Image formats. It has recently graduated from Incubator and is now a proud member of Apache commons proper.</p>
<p>In this article we shall see how to get Image metadata using Apache Sanselan.</p>
<p>Let’s see how the code looks</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> imageFile = args<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #808080; font-style: italic;">// Metadata details</span>
         IImageMetadata meta = Sanselan.<span style="color: #006600;">getMetadata</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #aaaadd; font-weight: bold;">File</span></a><span style="color: #66cc66;">&#40;</span>imageFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
         <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--- Image Metadata ----&quot;</span><span style="color: #66cc66;">&#41;</span>;
         <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>meta<span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>ImageReadException e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/IOException.html"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>Most of the work is done in single line ?</p>
<p>getMetadata() API returns the IImageMetadata  details. The toString() method of actual instance class of IImageMetadata  prints the image information in a formatted fashion. The instance depends upon the format of the image.</p>
<p>Running the program on one the images shipped with Sanselan source produced this output</p>
<p>--- Image Metadata ----<br />
Exif metadata:<br />
Root:<br />
Image Description: '’'<br />
Make: 'Oregon Scientific'<br />
Model: 'DS6639'<br />
Orientation: 1<br />
XResolution: 72<br />
YResolution: 72<br />
Resolution Unit: 2<br />
Software: 'Adobe Photoshop 7.0'<br />
Modify Date: '2008:07:13 21:05:34'<br />
YCbCr Positioning: 2<br />
Exif Offset: 228</p>
<p>Exif:<br />
Exposure Time: 10/10266 (0.001)<br />
FNumber: 28/10 (2.8)<br />
Exposure Program: 2<br />
ISO: 55<br />
Exif Version: 48, 50, 50, 48<br />
Date Time Original: '2003:10:20 09:01:29'<br />
Create Date: '2003:10:20 09:01:29'<br />
Components Configuration: 1, 2, 3, 0<br />
Compressed Bits Per Pixel: 2<br />
Brightness Value: 9<br />
Exposure Compensation: 0<br />
Max Aperture Value: 3<br />
Metering Mode: 1<br />
Light Source: 0<br />
Flash: 0<br />
Focal Length: 864/100 (8.64)<br />
Subject Location: 27556, 57533, 60147, 20319<br />
Flashpix Version: 48, 49, 48, 48<br />
Color Space: 1<br />
Exif Image Width: 300<br />
Exif Image Length: 225<br />
File Source: 3<br />
Scene Type: 1<br />
Custom Rendered: 0<br />
Exposure Mode: 0<br />
White Balance: 1<br />
Digital Zoom Ratio: 1<br />
Focal Length In 3 5mm Format: 42<br />
Scene Capture Type: 0<br />
Gain Control: 0<br />
Contrast: 0<br />
Saturation: 0<br />
Sharpness: 0</p>
<p>Sub:  (jpegImageData)<br />
Compression: 6<br />
XResolution: 72<br />
YResolution: 72<br />
Resolution Unit: 2<br />
Jpg From Raw Start: 838<br />
Jpg From Raw Length: 6258</p>
<p>Photoshop (IPTC) metadata:<br />
Caption/Abstract: ?</p>
<a href='#' id="mfg_link_3" class="mfg"></a>
<script type="text/javascript">/* <![CDATA[ */
mfg_create_link( "mfg_link_3", "Apache Sanselan Image Metadata retrieval", "More from Google on <em>", "<\/em> &raquo;");
/* ]]&gt; */</script>
<p><a href="http://feedads.g.doubleclick.net/~a/P0o5BStXB0aKrDcnG7yvnXV42ls/0/da"><img src="http://feedads.g.doubleclick.net/~a/P0o5BStXB0aKrDcnG7yvnXV42ls/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/P0o5BStXB0aKrDcnG7yvnXV42ls/1/da"><img src="http://feedads.g.doubleclick.net/~a/P0o5BStXB0aKrDcnG7yvnXV42ls/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/WYKjZbR6HTY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-retrieving-image-metadata-using-sanselan/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-retrieving-image-metadata-using-sanselan/</feedburner:origLink></item>
		<item>
		<title>[Apache Sanselan] Retrieving Image Information using Sanselan</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/TySev6nYWgI/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-retrieving-image-information-using-sanselan/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 13:40:45 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Apache Sanselan]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=281</guid>
		<description><![CDATA[Apache Sanselan is a pure Java library for reading and Writing Image formats. It has recently graduated from Incubator and is now a proud member of Apache commons proper.
In this article we shall see how to get Image Information using Apache Sanselan.
Let’s see how the code looks
public static void main&#40;String&#91;&#93; args&#41; &#123;
String imageFile = args&#91;0&#93;;
System.out.println&#40;&#34;Image [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://cwiki.apache.org/SANSELAN/" target="_blank">Apache Sanselan</a></strong> is a pure Java library for reading and Writing Image formats. It has recently graduated from Incubator and is now a proud member of Apache commons proper.</p>
<p>In this article we shall see how to get Image Information using Apache Sanselan.</p>
<p>Let’s see how the code looks</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> imageFile = args<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Image File = &quot;</span>+imageFile<span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
ImageInfo imageInfo = Sanselan.<span style="color: #006600;">getImageInfo</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #aaaadd; font-weight: bold;">File</span></a><span style="color: #66cc66;">&#40;</span>imageFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>imageInfo<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>ImageReadException e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/IOException.html"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>Most of the work is done in single line <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>getImageInfo() API returns the ImageInfo details. The toString() method of ImageInfo class prints the image information in a formatted fashion.</p>
<p>Running the program on one the images shipped with Sanselan source produced this output</p>
<address>Format Details: Jpeg/JFIF v.1.2</p>
<p>Bits Per Pixel: 24</p>
<p>Comments: 0</p>
<p>Format: JPEG</p>
<p>Format Name: JPEG (Joint Photographic Experts Group) Format</p>
<p>Compression Algorithm: JPEG</p>
<p>Height: 225</p>
<p>MimeType: image/jpeg</p>
<p>Number Of Images: 1</p>
<p>Physical Height Dpi: 72</p>
<p>Physical Height Inch: 3.125</p>
<p>Physical Width Dpi: 72</p>
<p>Physical Width Inch: 4.1666665</p>
<p>Width: 300</p>
<p>Is Progressive: true</p>
<p>Is Transparent: false</p>
<p>Color Type: RGB</p>
<p>Uses Palette: false</p>
</address>
<p>The ImageInfo class can be used programtically to arrange files, or for other purposes as well</p>
<p><strong>References</strong></p>
<p><a href="http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/" target="_blank"> Sanselan Source</a></p>
<p><a href="http://cwiki.apache.org/SANSELAN/" target="_blank"> Sanselan Wiki</a></p>
<a href='#' id="mfg_link_5" class="mfg"></a>
<script type="text/javascript">/* <![CDATA[ */
mfg_create_link( "mfg_link_5", "Apache Sanselan Image Information retrieve", "More from Google on <em>", "<\/em> &raquo;");
/* ]]&gt; */</script>
<p><a href="http://feedads.g.doubleclick.net/~a/1oB76h-W9hWJH2kprvEf3jsBQ9s/0/da"><img src="http://feedads.g.doubleclick.net/~a/1oB76h-W9hWJH2kprvEf3jsBQ9s/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/1oB76h-W9hWJH2kprvEf3jsBQ9s/1/da"><img src="http://feedads.g.doubleclick.net/~a/1oB76h-W9hWJH2kprvEf3jsBQ9s/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/TySev6nYWgI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-retrieving-image-information-using-sanselan/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/07/apache-sanselan-retrieving-image-information-using-sanselan/</feedburner:origLink></item>
		<item>
		<title>MINA vs Netty - A User’s perspective [Part 2]</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/NpP6_FpOm_Q/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/06/mina-vs-netty-a-users-perspective-part-2/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 16:08:10 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Apache MINA]]></category>

		<category><![CDATA[Netty]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=276</guid>
		<description><![CDATA[Back with the series. Its going to be quick and small post. Would focus around similarity on these two frameworks

Both are NIO frameworks  
Trustin has been part of both Frameworks, lately contributing to Netty (Thanks to Emmanuel for pointing this out)
Both uses Annotation, though in MINA this is more in area of MINA State Machine, [...]]]></description>
			<content:encoded><![CDATA[<p>Back with the series. Its going to be quick and small post. Would focus around similarity on these two frameworks</p>
<ul>
<li>Both are NIO frameworks <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Trustin has been part of both Frameworks, lately contributing to Netty (Thanks to Emmanuel for pointing this out)</li>
<li>Both uses Annotation, though in MINA this is more in area of MINA State Machine, whereas Netty, has used annotations for configuring the IO Chains as well.</li>
<li>Both Frameworks are working aggressively towards improving their Documentation</li>
</ul>
<p>Hmm.. that's it for this post. Will be back soon with more</p>
<a href='#' id="mfg_link_7" class="mfg"></a>
<script type="text/javascript">/* <![CDATA[ */
mfg_create_link( "mfg_link_7", "Apache MINA Netty comparison", "More from Google on <em>", "<\/em> &raquo;");
/* ]]&gt; */</script>
<p><a href="http://feedads.g.doubleclick.net/~a/QO8EkbDh5jE9GkpaaF0UmOpwMxU/0/da"><img src="http://feedads.g.doubleclick.net/~a/QO8EkbDh5jE9GkpaaF0UmOpwMxU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/QO8EkbDh5jE9GkpaaF0UmOpwMxU/1/da"><img src="http://feedads.g.doubleclick.net/~a/QO8EkbDh5jE9GkpaaF0UmOpwMxU/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/NpP6_FpOm_Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/06/mina-vs-netty-a-users-perspective-part-2/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/06/mina-vs-netty-a-users-perspective-part-2/</feedburner:origLink></item>
		<item>
		<title>MINA vs Netty - A User’s perspective [Part 1]</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/R_DJBELpH6M/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/04/mina-vs-netty-a-users-perspective-part-1/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 01:40:46 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Apache MINA]]></category>

		<category><![CDATA[Netty]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=263</guid>
		<description><![CDATA[Have came across this topic a lot, which is better MINA or Netty. Wanted to see what works out well from a User Perspective, thought about trying both Frameworks, from an End Users perspective. This series shall not just find difference, but find similarities in both frameworks as well.

Before I continue, the opinion expressed here [...]]]></description>
			<content:encoded><![CDATA[<p>Have came across this topic a lot, which is better MINA or Netty. Wanted to see what works out well from a User Perspective, thought about trying both Frameworks, from an End Users perspective. This series shall not just find difference, but find similarities in both frameworks as well.</p>
<p><code><br />
Before I continue, the opinion expressed here are purely mine, and other can tend to disagree. I am a MINA committer myself, so may have a little bias towards MINA. Shall try to keep my discussion unbiased, as much as possible. Having used MINA for a while, have a natural comfort with the framework.<br />
</code><br />
<strong><span style="text-decoration: underline;">What to Expect</span></strong></p>
<p>Shall try to evaluate both the frameworks, as if I need to use them in one of my own projects and try to provide rational for my thoughts. User comments shall provide more indepth to the posts. The details shall be covered as I move along on my journey. This post is just the beginning.</p>
<p><strong><span style="text-decoration: underline;">Latest Releases</span><br />
</strong><br />
MINA - MINA 2.0 M4<br />
Netty - 3.0.2 GA</p>
<p><strong><span style="text-decoration: underline;">Community Activity</span></strong></p>
<p><strong></strong><br />
Having an active community in an Open Source project is very important. It assures that the project is actively being looked after.</p>
<p><strong>Source of Data</strong></p>
<p><strong></strong><br />
MINA - http://mina.markmail.org/<br />
Netty - http://netty.markmail.org/</p>
<p>MINA community is very active, and scores well above Netty. Though Trustin is very active in Netty, and I hope Netty community shall have active support from more users/developers.</p>
<p><strong><span style="text-decoration: underline;">Documentation</span></strong></p>
<p><strong></strong><br />
MINA and Netty both have sufficient documentation to start with, but there is need for improvement. Netty Documentation is better organized. MINA Documentation needs a better organization. (Blame me for this, I am supposed to take care of this).</p>
<p><strong><span style="text-decoration: underline;">Coming up Next</span></strong></p>
<p><strong></strong><br />
Working of examples on both the frameworks and try to come up with Application Architectures soon.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/OrPVnGCGPjuRWamMIqql_qofEI8/0/da"><img src="http://feedads.g.doubleclick.net/~a/OrPVnGCGPjuRWamMIqql_qofEI8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/OrPVnGCGPjuRWamMIqql_qofEI8/1/da"><img src="http://feedads.g.doubleclick.net/~a/OrPVnGCGPjuRWamMIqql_qofEI8/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/R_DJBELpH6M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/04/mina-vs-netty-a-users-perspective-part-1/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/04/mina-vs-netty-a-users-perspective-part-1/</feedburner:origLink></item>
		<item>
		<title>[Spring Security] – Implementing Custom UserDetails using Hibernate</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/ABUpRVb5i78/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/02/spring-security-%e2%80%93-implementing-custom-userdetails-using-hibernate/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 11:25:31 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[Spring Security]]></category>

		<category><![CDATA[Custom User Details]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=248</guid>
		<description><![CDATA[Recently was experimenting with creating a Custom UserDetails in Spring Security. Unfortunately, it took me a while before I could implement. In this post shall share my experience and how I developed it.
Assumption
I shall concentrate only on the configuration of UserDetails service. To see it in Action, you can take Spring petclinic tutorial and add [...]]]></description>
			<content:encoded><![CDATA[<p>Recently was experimenting with creating a Custom UserDetails in Spring Security. Unfortunately, it took me a while before I could implement. In this post shall share my experience and how I developed it.</p>
<p><strong>Assumption</strong></p>
<p>I shall concentrate only on the configuration of UserDetails service. To see it in Action, you can take Spring petclinic tutorial and add the configuration to it.</p>
<p>Let’s briefly lay down the steps needed to implement a Custom UserDetails</p>
<ul>
<li><span> </span>Implement a Custom User Class that implements UserDetails interface (This class can be customized to fit your needs)</li>
<li><span> </span>Implement Custom UserDetailsService to based on Hibernate</li>
<li>Configuring in the Spring context</li>
</ul>
<p><strong>Implementing Custom UserDetails</strong></p>
<p>For simplicity perspective, I shall be omitting Hibernate related configuration from this section</p>
<pre class="java5">&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">io</span>.<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a>;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a>;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List"><span style="color: #aaaadd; font-weight: bold;">List</span></a>;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html"><span style="color: #aaaadd; font-weight: bold;">Set</span></a>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">GrantedAuthority</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">GrantedAuthorityImpl</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">userdetails</span>.<span style="color: #006600;">UserDetails</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomUser <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a>, UserDetails <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span> serialVersionUID = 1L;
         <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">long</span> id;
         <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> password;
         <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username;
         <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html"><span style="color: #aaaadd; font-weight: bold;">Set</span></a> roles;
&nbsp;
       <span style="color: #000000; font-weight: bold;">public</span> CustomUser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
       <span style="color: #000000; font-weight: bold;">public</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html"><span style="color: #aaaadd; font-weight: bold;">Set</span></a> getRoles<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
           <span style="color: #000000; font-weight: bold;">return</span> roles;
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setRoles<span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html"><span style="color: #aaaadd; font-weight: bold;">Set</span></a> roles<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">roles</span> = roles;
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> accountNonExpired;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> accountNonLocked;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> credentialsNonExpired;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> enabled;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> GrantedAuthority<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> getAuthorities<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List"><span style="color: #aaaadd; font-weight: bold;">List</span></a> list = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>Authority role : roles<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                  list.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GrantedAuthorityImpl<span style="color: #66cc66;">&#40;</span>role.<span style="color: #006600;">getAuthority</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
           <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>GrantedAuthority<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>list.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GrantedAuthority<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Take a special note of getAuthorities(). This function from UserDetails interface needs to return all the authorities for the given user. You can store Roles in whichever format you desire, and can translate them here. For simplicity sake, here is my Authorities class.</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Authority <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span> serialVersionUID = 1L;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">long</span> id;
        <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username;
        <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> authority;
<span style="color: #66cc66;">&#125;</span></pre>
<p><strong>Implementing Custom UserDetailsService</strong></p>
<p>Here using spring comes very handy. We extend HibernateDaoSupport and get most of the desired Hibernate functionality. Lets take a look at the class.</p>
<pre class="java5"> 
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List"><span style="color: #aaaadd; font-weight: bold;">List</span></a>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">dao</span>.<span style="color: #006600;">DataAccessException</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">orm</span>.<span style="color: #006600;">hibernate3</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">HibernateDaoSupport</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">userdetails</span>.<span style="color: #006600;">UserDetails</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">userdetails</span>.<span style="color: #006600;">UserDetailsService</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">userdetails</span>.<span style="color: #006600;">UsernameNotFoundException</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateUserDetailsService <span style="color: #000000; font-weight: bold;">extends</span> HibernateDaoSupport <span style="color: #000000; font-weight: bold;">implements</span> UserDetailsService <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> UserDetails loadUserByUsername<span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username<span style="color: #66cc66;">&#41;</span>
                         <span style="color: #000000; font-weight: bold;">throws</span> UsernameNotFoundException, DataAccessException <span style="color: #66cc66;">&#123;</span>
              <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List"><span style="color: #aaaadd; font-weight: bold;">List</span></a> results = getHibernateTemplate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;from CustomUser where username = ?&quot;</span>,
                                                                        <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+Object"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>username<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
              <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>results.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &amp;lt; <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                     <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> UsernameNotFoundException<span style="color: #66cc66;">&#40;</span>username + <span style="color: #ff0000;">&quot;not found&quot;</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #66cc66;">&#125;</span>
               <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>UserDetails<span style="color: #66cc66;">&#41;</span> results.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
       <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>Well, its that simple. We just implement loadUserByUsername(String username) function and inside that we query for the specified user using the HibernateTemplate</p>
<p><strong>Configuration</strong></p>
<p>It’s time to tell Spring Security to use our custom UserDetails and UserDetailsService. Ensure that petclinic application is using Hibernate Context file (applicationContext-hibernate.xml). Check the same in web.xml. Let’s take a look at what’s the configuration we need to do</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;b:bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;userDetailsService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.ap.user.HibernateUserDetailsService&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;b:property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernateTemplate&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;hibernateTemplate&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/b:bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;authentication-provider</span> <span style="color: #000066;">user-service-ref</span>=<span style="color: #ff0000;">&quot;userDetailsService&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/authentication-provider<span style="font-weight: bold; color: black;">&gt;</span></span></span>
 </pre>
<p>Add the bean definition and update the authentication-provider configuration to use our UserDetailsservice. Restart and Spring Security shall use our Custom UserDetails.</p>
<p><strong>Troubleshooting</strong></p>
<p>It took me a while before I could implement this, and though of sharing the mistakes, so that you don’t repeat them</p>
<p>•<span> </span>Test User/Authority Hibernate class in isolation – I did it initially and my test suite was not complete as to detect that all the authorities were being set for the user. This let me to dig into the Spring Security code, add traces and realizing that it’s my mistake.</p>
<p>•<span> </span>Ensure that you maintain roles with a prefix ROLE_ (case-sensitive). If you choose otherwise, you have to set the prefix as desired for authorization.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/bnNcErDtbph6pFe0Eh_K10fyjd0/0/da"><img src="http://feedads.g.doubleclick.net/~a/bnNcErDtbph6pFe0Eh_K10fyjd0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/bnNcErDtbph6pFe0Eh_K10fyjd0/1/da"><img src="http://feedads.g.doubleclick.net/~a/bnNcErDtbph6pFe0Eh_K10fyjd0/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/ABUpRVb5i78" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/02/spring-security-%e2%80%93-implementing-custom-userdetails-using-hibernate/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/02/spring-security-%e2%80%93-implementing-custom-userdetails-using-hibernate/</feedburner:origLink></item>
		<item>
		<title>[SMPP] Sending long SMS through SMPP</title>
		<link>http://feedproxy.google.com/~r/AshishsTechBlog/~3/Xokq1Zu21RY/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/01/smpp-sending-long-sms-through-smpp/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 08:40:12 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
		
		<category><![CDATA[SMPP]]></category>

		<category><![CDATA[Long SMS]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=243</guid>
		<description><![CDATA[SMS is designed to send small text messages upto 140 chars. It actually depends upon the encoding. Using 7 bit encoding, you can send 160 chars, using Unicode, you can only send 70 chars. The data is carried in User Data section.
This post talks about ways of sending Long message using SMPP
Using SMPP we can [...]]]></description>
			<content:encoded><![CDATA[<p>SMS is designed to send small text messages upto 140 chars. It actually depends upon the encoding. Using 7 bit encoding, you can send 160 chars, using Unicode, you can only send 70 chars. The data is carried in User Data section.</p>
<p>This post talks about ways of sending Long message using SMPP</p>
<p>Using SMPP we can send long SMS by two means</p>
<p><strong>Using Payload option</strong></p>
<p>Submit SM PDU has a field called message_payload. Set the data in it and you can send upto 64K octets. But this option is not supported by a lot of provider. Verify with your provider, if they support the same.</p>
<p><strong>Using Message fragementation</strong></p>
<p>This format is more common and widely supported. Essentially, the long SMS is divided into smaller messages and send with a particular format, by setting UDH.</p>
<p>The key TLV options used are</p>
<p><em><strong>sar_msg_ref_num</strong></em> - The reference number for a particular concatenated short message</p>
<p><em><strong>sar_total_segments</strong></em> - Indicates the total number of short messages within the concatenated short message</p>
<p><strong><em>sar_segment_seqnum</em></strong> - Indicates the sequence number of a particular short message fragment within the concatenated short message</p>
<p>How do you set these, depends upon the SMPP library you use. Will brief the algo</p>
<p> </p>
<ul>
<li>Break the long sms into parts, so that fragement can fit into single Submit SM PDU</li>
<li>The total number of fragements shall give the value of <strong><em>sar_total_segments</em></strong></li>
<li>Generate a unique identifier value, this becomes the values of <strong><em>sar_msg_ref_num</em></strong></li>
<li>Now in a loop send the message fragements, with values in step above. <strong><em>sar_segment_seqnum</em></strong> values shall be the value of the loop count (ensure that it starts from 1)</li>
</ul>
<p>This is it <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/pnE-EDIAjvYKGvBHK3Dw1pvBzLw/0/da"><img src="http://feedads.g.doubleclick.net/~a/pnE-EDIAjvYKGvBHK3Dw1pvBzLw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/pnE-EDIAjvYKGvBHK3Dw1pvBzLw/1/da"><img src="http://feedads.g.doubleclick.net/~a/pnE-EDIAjvYKGvBHK3Dw1pvBzLw/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/AshishsTechBlog/~4/Xokq1Zu21RY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/01/smpp-sending-long-sms-through-smpp/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.ashishpaliwal.com/blog/2009/01/smpp-sending-long-sms-through-smpp/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 5.742 seconds. --><!-- Cached page generated by WP-Super-Cache on 2009-11-10 20:23:52 -->
