<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3144090126613685403</id><updated>2026-07-17T10:53:21.846-07:00</updated><category term="Spring"/><category term="Hbase composite key"/><category term="hbase dynamic columns"/><category term="hbase partial key scan"/><category term="hbase partial table scan"/><category term="Encryption"/><category term="JAVA"/><category term="Objective C"/><category term="Unit testing"/><category term="ios"/><category term="iphone"/><category term="APO"/><category term="ActiveMQ Network Connector vs Network Bridge"/><category term="Advice"/><category term="App"/><category term="Form Multipart"/><category term="Hbase Index"/><category term="Hibernate"/><category term="Interceptor"/><category term="JDBC date rounding"/><category term="Kids"/><category term="Multiplication"/><category term="Proxy"/><category term="SQL Server"/><category term="SQL Server datetime rounding"/><category term="Spring Transactional  annotation not working"/><category term="alias context path"/><category term="apple"/><category term="appstore"/><category term="att"/><category term="custom context path"/><category term="date precision loss"/><category term="deadlock"/><category term="iphone unlock"/><category term="ipod"/><category term="junit"/><category term="mock objects"/><category term="multiple context path"/><category term="redis insert at tail"/><category term="redis insert at the end of list"/><category term="redis list lpush"/><category term="redis lpush command"/><category term="tomcat 5 6"/><category term="user defined context path"/><title type='text'>what I learned</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>20</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-5115026568777754392</id><published>2012-12-08T11:55:00.002-08:00</published><updated>2012-12-08T11:56:19.877-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="att"/><category scheme="http://www.blogger.com/atom/ns#" term="iphone unlock"/><title type='text'>ATT Unlock iphone device/sim</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I wanted to unlock my iphone so that I can use other carrier&#39;s sim, ATT has this &lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;a href=&quot;https://www.att.com/deviceunlock&quot;&gt;online form&lt;/a&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
where you can simply submit the request online and you will receive the instructions once iphone is unlocked.&lt;br /&gt;
&lt;br /&gt;
Here is the eligibility requirement to unlock iphone:&lt;br /&gt;
&lt;br /&gt;
&lt;h3 class=&quot;answerHeading&quot; style=&quot;-webkit-text-size-adjust: none; color: #333333; font-family: arial, helvetica, sans-serif; font-size: 0.95em !important; line-height: 1.38em; margin: 20px 0px 3px; padding: 0px;&quot;&gt;
General Eligibility Requirements for Unlocking iPhone&lt;/h3&gt;
&lt;div class=&quot;answerText&quot; style=&quot;-webkit-text-size-adjust: none; color: #515151; font-family: Arial, Helvetica, sans-serif; font-size: 0.82em; line-height: 1.38em; padding: 0px;&quot;&gt;
AT&amp;amp;T will unlock an iPhone under the following circumstances:&lt;/div&gt;
&lt;ul class=&quot;answerItems&quot; style=&quot;-webkit-text-size-adjust: none; color: #333333; font-family: Arial, sans-serif; font-size: 16px; list-style-image: none; list-style-position: outside; margin: 0px; padding: 0px;&quot;&gt;
&lt;li style=&quot;float: left; font-size: 0.82em; line-height: 1.38em; margin: 4px 0px 0px 20px; padding: 0px; position: relative; width: 896px;&quot;&gt;The person requesting the unlock is:&lt;ul style=&quot;list-style-image: none; list-style-position: outside; margin: 0px; padding: 0px;&quot;&gt;
&lt;li style=&quot;float: left; font-size: 1em; line-height: 1.38em; margin: 4px 0px 0px 20px; padding: 0px; position: relative; width: 896px;&quot;&gt;a current AT&amp;amp;T customer&amp;nbsp;&lt;strong style=&quot;margin: 0px; padding: 0px;&quot;&gt;or&lt;/strong&gt;&lt;/li&gt;
&lt;li style=&quot;float: left; font-size: 1em; line-height: 1.38em; margin: 4px 0px 0px 20px; padding: 0px; position: relative; width: 896px;&quot;&gt;a former AT&amp;amp;T customer who can provide the phone number or account number for the account&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li style=&quot;float: left; font-size: 0.82em; line-height: 1.38em; margin: 4px 0px 0px 20px; padding: 0px; position: relative; width: 896px;&quot;&gt;The iPhone was designed for use on AT&amp;amp;T&#39;s network;&lt;/li&gt;
&lt;li style=&quot;float: left; font-size: 0.82em; line-height: 1.38em; margin: 4px 0px 0px 20px; padding: 0px; position: relative; width: 896px;&quot;&gt;All contract obligations, including any term commitment, associated with the device to be unlocked have been fully satisfied;&amp;nbsp;&lt;strong style=&quot;margin: 0px; padding: 0px;&quot;&gt;and&lt;/strong&gt;&lt;/li&gt;
&lt;li style=&quot;float: left; font-size: 0.82em; line-height: 1.38em; margin: 4px 0px 0px 20px; padding: 0px; position: relative; width: 896px;&quot;&gt;The iPhone has&amp;nbsp;&lt;strong style=&quot;margin: 0px; padding: 0px;&quot;&gt;not&lt;/strong&gt;&amp;nbsp;been reported lost or stolen.&lt;/li&gt;
&lt;li style=&quot;float: left; font-size: 0.82em; line-height: 1.38em; margin: 4px 0px 0px 20px; padding: 0px; position: relative; width: 896px;&quot;&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/5115026568777754392/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2012/12/att-unlock-iphone-devicesim.html#comment-form' title='24 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/5115026568777754392'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/5115026568777754392'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2012/12/att-unlock-iphone-devicesim.html' title='ATT Unlock iphone device/sim'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>24</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-5143431784384903855</id><published>2012-11-10T07:55:00.000-08:00</published><updated>2012-11-12T08:42:23.791-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="App"/><category scheme="http://www.blogger.com/atom/ns#" term="apple"/><category scheme="http://www.blogger.com/atom/ns#" term="appstore"/><category scheme="http://www.blogger.com/atom/ns#" term="iphone"/><category scheme="http://www.blogger.com/atom/ns#" term="ipod"/><category scheme="http://www.blogger.com/atom/ns#" term="Kids"/><category scheme="http://www.blogger.com/atom/ns#" term="Multiplication"/><title type='text'>IOS Iphone and Ipod Multiplication App For Kids</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I created an &lt;a href=&quot;https://itunes.apple.com/us/app/multiplication-app/id569775522?mt=8&quot;&gt;IOS Multiplication App&lt;/a&gt; for practicing multiplication. It has Learn, Practice and Test features.&lt;br /&gt;
Its now available on&amp;nbsp;iTunes&amp;nbsp;app store. Here is the &lt;a href=&quot;https://itunes.apple.com/us/app/multiplication-app/id569775522?mt=8&quot;&gt;appstore &lt;/a&gt;link to it:&lt;br /&gt;
&lt;br /&gt;
and here are some more &lt;a href=&quot;http://mobileappcode.com/ios-multiplication-app-is-published-in-app-store/&quot;&gt;screenshots &lt;/a&gt;of app.&lt;br /&gt;
&lt;br /&gt;
&lt;iframe bordercolor=&quot;#000000&quot; frameborder=&quot;0&quot; height=&quot;150&quot; hspace=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://ad.doubleclick.net/adi/N7433.148119.BLOGGEREN/B6695650.984;sz=180x150;ord=[timestamp]?;lid=41000613802463973;pid=sku3524121;usg=AFHzDLtLT-7DJ2tfEYYSEo8DmuTvfUzajQ;adurl=http%253A%252F%252Fwww.samsclub.com%252Fsams%252Fipad-2-wi-fi-16gb-black%252Fprod3111459.ip%253Fpid%253D_DoubleClick_Affiliates%2526ci_src%253D15781033%2526ci_sku%253Dsku3524121;pubid=595859;price=%24389.00;title=iPad+2+Wi-Fi+16GB+-+Bl...;merc=Sam%27s+Club;imgsrc=http%3A%2F%2Fs7d2.scene7.com%2Fis%2Fimage%2Fsamsclub%2Fs7product%2F0088590945758_A.jpg;width=85;height=85&quot; vspace=&quot;0&quot; width=&quot;180&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/5143431784384903855/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2012/11/ios-multiplication-app-for-kids.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/5143431784384903855'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/5143431784384903855'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2012/11/ios-multiplication-app-for-kids.html' title='IOS Iphone and Ipod Multiplication App For Kids'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-3784420164443352259</id><published>2012-06-06T22:42:00.000-07:00</published><updated>2012-11-10T07:58:20.609-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="redis insert at tail"/><category scheme="http://www.blogger.com/atom/ns#" term="redis insert at the end of list"/><category scheme="http://www.blogger.com/atom/ns#" term="redis list lpush"/><category scheme="http://www.blogger.com/atom/ns#" term="redis lpush command"/><title type='text'>Redis: LPUSH Command</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Redis is a key value store. I recently used it in multiple projects and quite impressed by its simplicity and performance. it can be used for caching and storing simple nosql data.&lt;br /&gt;
&lt;br /&gt;
In my later blogs, I will explain how to use it as reliable queue using RPOPLPUSH command. Before looking at that lets look at &lt;a href=&quot;http://redis.io/commands/lpush&quot;&gt;LPUSH &lt;/a&gt;command. LPUSH command works on Redis list data structure and it inserts an item at the tail of the list. Following shows it nicely:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEklCG-pWUTp5W1-yY1_3xdHk2gcJrVGss1XLYBqNZbq3pNR7Qz8HISmpdNqS1UYVJ-SXcsM8S6w9YKHtpoqqSHFL-QqGJJ__WynNi0zB-5saNVmy2YRhwUuExfN6KOEIqQOmvy8bw7hA/s1600/lpush.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Redis LPUSH Command&quot; border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEklCG-pWUTp5W1-yY1_3xdHk2gcJrVGss1XLYBqNZbq3pNR7Qz8HISmpdNqS1UYVJ-SXcsM8S6w9YKHtpoqqSHFL-QqGJJ__WynNi0zB-5saNVmy2YRhwUuExfN6KOEIqQOmvy8bw7hA/s400/lpush.png&quot; title=&quot;Redis LPUSH Command&quot; width=&quot;330&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;REDIS &amp;nbsp;LPUSH COMMAND&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;iframe bordercolor=&quot;#000000&quot; frameborder=&quot;0&quot; height=&quot;250&quot; hspace=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://ad.doubleclick.net/adi/N7433.148119.BLOGGEREN/B6533661.1045;sz=300x250;ord=[timestamp]?;lid=41000613802463918;pid=62567;usg=AFHzDLvN8eWCoOpY5AB5yRsJLR-TgahENQ;adurl=http%253A%252F%252Fwww.abt.com%252Fproduct%252F62567%252FGoogle-NEXUS7ASUS1B16.html;pubid=595859;price=%24219.99;title=Google+Nexus+7+Black+Wi-Fi+16GB+Tablet+-+NEXUS7ASUS1B16;merc=Abt+Electronics+%26+Appliances;imgsrc=http%3A%2F%2Fcontent.abt.com%2Fmedia%2Fimages%2Fproducts%2FBDP_Images%2Fbig_NEXUS7ASUS1B16.jpg;width=82;height=135&quot; vspace=&quot;0&quot; width=&quot;300&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/3784420164443352259/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2012/06/redis-lpush-command.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/3784420164443352259'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/3784420164443352259'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2012/06/redis-lpush-command.html' title='Redis: LPUSH Command'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEklCG-pWUTp5W1-yY1_3xdHk2gcJrVGss1XLYBqNZbq3pNR7Qz8HISmpdNqS1UYVJ-SXcsM8S6w9YKHtpoqqSHFL-QqGJJ__WynNi0zB-5saNVmy2YRhwUuExfN6KOEIqQOmvy8bw7hA/s72-c/lpush.png" height="72" width="72"/><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-7237829774331985396</id><published>2012-05-31T20:17:00.000-07:00</published><updated>2012-11-10T07:57:31.952-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hbase composite key"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase dynamic columns"/><category scheme="http://www.blogger.com/atom/ns#" term="Hbase Index"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase partial key scan"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase partial table scan"/><title type='text'>HBase Secondary Index: Scaning Using Multiple Keys</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In HBase one can define row keys to store primary key and do scan using partial or full row keys as explained in &lt;a href=&quot;http://riteshadval.blogspot.com/2012/03/hbase-composite-row-key-design-doing_11.html&quot;&gt;previous blogs&lt;/a&gt; but what if we need another dimension to do search? That is we may need do a look up based on some other field which is not in our primary table row key.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;To achieve this we need to define a second table where row key have this second dimension and it stores the value of the primary row key. Lets look at the example of my &lt;a href=&quot;http://riteshadval.blogspot.com/2012/03/hbase-composite-row-key-design-doing.html&quot;&gt;previous blog&lt;/a&gt;, where we are storing sessions of users.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;
&lt;code&gt;&lt;b style=&quot;font-family: monospace;&quot;&gt;userIdBytes+seperatorByte+dateStringByte+seperatorByte+sessionIdBytes&lt;/b&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Here seperatorByte should be choosen in such a way which does not conflict with userId and sessionId bytes values. For example use LF (decimal 10)&lt;br /&gt;
&lt;br /&gt;
Lets call above key as &lt;b&gt;primaryKey&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Note that using above row key we can store, column and values in one or more column families for each&lt;br /&gt;
user session row. For example we may want to store session startTime and endTime as two values in two columns in a column family.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;Now lets say that we want to get all user sessions in a given geographical area (Get all users in US or get all users in UK), so how do we store this information? To do this can define a second table where row key is geographical area code plus &lt;b&gt;primaryRow &lt;/b&gt;Key (ie. userId date and sessionId.)&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;code&gt;
&lt;b style=&quot;font-family: monospace;&quot;&gt;geoAreaCodeBytes+seperatorByte+primaryKey&lt;/b&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Now we can do a search for user sessions using geographical area, similar to what I explained in &lt;a href=&quot;http://riteshadval.blogspot.com/2012/03/hbase-composite-row-key-design-doing_11.html&quot;&gt;my previous blog&lt;/a&gt;.
Also for each of these rows, we can find the primaryKey row for this row key itself and use it to find additional information that we store for user sessions in primaryKey.

&lt;span style=&quot;font-family: monospace;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;iframe bordercolor=&quot;#000000&quot; frameborder=&quot;0&quot; height=&quot;250&quot; hspace=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://ad.doubleclick.net/adi/N7433.148119.BLOGGEREN/B6533661.1039;sz=300x250;ord=[timestamp]?;lid=41000613802463918;pid=61257;usg=AFHzDLuLmc6DWMPl24buH3c33FlgTl4IZA;adurl=http%253A%252F%252Fwww.abt.com%252Fproduct%252F61257%252FSamsung-GTP5113TSYXAR.html;pubid=595859;price=%24349.00;title=Samsung+16GB+Galaxy+Tab+2+Wi-Fi+Tablet+-+GT-P5113TSYXAR;merc=Abt+Electronics+%26+Appliances;imgsrc=http%3A%2F%2Fcontent.abt.com%2Fmedia%2Fimages%2Fproducts%2FBDP_Images%2Fbig_GTP5113TSYXAR.jpg;width=93;height=135&quot; vspace=&quot;0&quot; width=&quot;300&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/7237829774331985396/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2012/05/hbase-secondary-index-scaning-using.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/7237829774331985396'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/7237829774331985396'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2012/05/hbase-secondary-index-scaning-using.html' title='HBase Secondary Index: Scaning Using Multiple Keys'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-5706429945913199710</id><published>2012-03-18T23:21:00.002-07:00</published><updated>2012-03-18T23:21:36.256-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ActiveMQ Network Connector vs Network Bridge"/><title type='text'>ActiveMQ Network Of Brokers (Network Connector) Vs Network Bridge</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;a href=&quot;http://activemq.apache.org/networks-of-brokers.html&quot;&gt;Network Connector:&lt;/a&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
ActiveMQ network of brokers configuration is achieved using network connector. A network connector&amp;nbsp;specifies&amp;nbsp;one or&amp;nbsp;more&amp;nbsp;ActiveMQ servers/brokers where messages are &lt;b&gt;forwarded or pushed&lt;/b&gt; from the ActiveMQ server which has this network connector is configured.&lt;br /&gt;
&lt;br /&gt;
Following diagram makes it clear:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr2g5T1VdYQ4lTp9vbEQBVrtK7HWczt0juBzW1_-Idq3QC5daFyb90M4UNwT_Uf351Galcr0IwGLmZmVO2usTe10oIwzVEIiwQ9uJoAZ4i06MtxV82qZ36gCH_SleOzpjjq8Euw1IamnQ/s1600/network-connector.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;136&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr2g5T1VdYQ4lTp9vbEQBVrtK7HWczt0juBzW1_-Idq3QC5daFyb90M4UNwT_Uf351Galcr0IwGLmZmVO2usTe10oIwzVEIiwQ9uJoAZ4i06MtxV82qZ36gCH_SleOzpjjq8Euw1IamnQ/s320/network-connector.jpg&quot; style=&quot;cursor: move;&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
There is a duplex property on network connector configuration which allows bi-directional message flow similar to network bridge. Wth this property set to true, all destinations configured in network connector will be available for bi-directional message flow, where as in network bridge one can choose&amp;nbsp;explicit&amp;nbsp;destinations. For example you want to forward all the messages from Hub to spoke but only want to forward selective destination message from spoke to hub then a network bridge is better.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;a href=&quot;http://activemq.apache.org/jms-to-jms-bridge.html&quot;&gt;Network Bridge:&lt;/a&gt;&lt;/b&gt;&lt;br /&gt;
A network bridge configuration on a ActiveMQ server/broker, allows message to be selectively consumed from other&amp;nbsp;ActiveMQ brokers or forwarded from this source ActiveMQ server/broker to other ActiveMQ server/broker.&lt;br /&gt;
&lt;br /&gt;
Following diagram makes it clear:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioAznInCOLL9JI5tWulwDyywd6CmGgXT6ja7PsGML1blSTFZoc3oq0thi38fk_RCeV-4V-VvkTnuuAToFNCtl-eg23OT5wPAt8jOnqkaYZQWU6m_58vebJqsKB_MANMWebrXyCwa3IKAs/s1600/network-bridge.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;153&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioAznInCOLL9JI5tWulwDyywd6CmGgXT6ja7PsGML1blSTFZoc3oq0thi38fk_RCeV-4V-VvkTnuuAToFNCtl-eg23OT5wPAt8jOnqkaYZQWU6m_58vebJqsKB_MANMWebrXyCwa3IKAs/s320/network-bridge.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/5706429945913199710/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2012/03/activemq-network-of-brokers-network.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/5706429945913199710'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/5706429945913199710'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2012/03/activemq-network-of-brokers-network.html' title='ActiveMQ Network Of Brokers (Network Connector) Vs Network Bridge'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr2g5T1VdYQ4lTp9vbEQBVrtK7HWczt0juBzW1_-Idq3QC5daFyb90M4UNwT_Uf351Galcr0IwGLmZmVO2usTe10oIwzVEIiwQ9uJoAZ4i06MtxV82qZ36gCH_SleOzpjjq8Euw1IamnQ/s72-c/network-connector.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-4787715985668482017</id><published>2012-03-15T23:48:00.000-07:00</published><updated>2012-03-15T23:48:55.322-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Spring Transactional  annotation not working"/><title type='text'>Spring enable @Transactional annotation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In Spring to enable @Transactional Annotation processing you would need to specify&amp;nbsp;&lt;span style=&quot;background-color: white; font-size: 14px; text-align: justify;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;bold&quot; style=&quot;background-color: white; font-size: 14px; text-align: justify;&quot;&gt;&lt;b&gt;&amp;lt;tx:annotation-driven&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: 14px;&quot;&gt;in your Spring xml configuration file.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: 14px;&quot;&gt;Example:&lt;/span&gt;&lt;/div&gt;
&lt;code&gt;
 &lt;b&gt;
&amp;lt;tx:annotation-driven transaction-manager=&quot;txManager&quot;&amp;gt;
&amp;lt;/tx:annotation-driven&amp;gt;
&lt;/b&gt;
&lt;/code&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/4787715985668482017/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2012/03/spring-enable-transactional-annotation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/4787715985668482017'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/4787715985668482017'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2012/03/spring-enable-transactional-annotation.html' title='Spring enable @Transactional annotation'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-592951186266281729</id><published>2012-03-11T22:31:00.000-07:00</published><updated>2012-11-10T07:59:18.911-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hbase composite key"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase dynamic columns"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase partial key scan"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase partial table scan"/><title type='text'>HBase Composite Row Key Design: Doing Table Scan Using Partial Row Key Part2</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In my &lt;a href=&quot;http://riteshadval.blogspot.com/2012/03/hbase-composite-row-key-design-doing.html&quot;&gt;previous blog&lt;/a&gt; I described how to design HBase table row key which allows partial row key scan. Now in this second blog lets look at another example of composite row key where we will have 3 seperate fields in the key.&lt;br /&gt;
&lt;br /&gt;
The key will consist of userId, date and sessionId. Something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;
&lt;b style=&quot;font-family: monospace;&quot;&gt;userIdBytes+seperatorByte+dateStringByte+seperatorByte+sessionIdBytes&lt;/b&gt;&lt;/code&gt;&lt;br /&gt;
Here seperatorByte should be choosen in such a way which does not conflict with userId and sessionId bytes values. For example use LF (decimal 10)

&lt;b style=&quot;font-family: monospace;&quot;&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;line-height: 18px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: monospace;&quot;&gt;This key design allows partial key scan in following ways:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: monospace;&quot;&gt;To find all the sessions for a given user just specify&amp;nbsp;&lt;/span&gt;
&lt;b style=&quot;font-family: monospace;&quot;&gt;userIdBytes&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;font-family: monospace;&quot;&gt;as start row when creating table &lt;a href=&quot;http://archive.apache.org/dist/hbase/docs/apidocs/org/apache/hadoop/hbase/client/Scan.html&quot;&gt;Scan &lt;/a&gt;object.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: monospace;&quot;&gt;To find all the session for a given user on a given date or a given range of date &amp;nbsp;just specify&amp;nbsp;&lt;/span&gt;
&lt;b style=&quot;font-family: monospace;&quot;&gt;userIdBytes+seperatorByte+dateStringByte&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;font-family: monospace;&quot;&gt;&amp;nbsp;as start row and end row, when creating a table &lt;a href=&quot;http://archive.apache.org/dist/hbase/docs/apidocs/org/apache/hadoop/hbase/client/Scan.html&quot;&gt;Scan &lt;/a&gt;object.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: monospace;&quot;&gt;To find a specific sessionId, specify whole key&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: monospace;&quot;&gt;userIdBytes+seperatorByte+dateStringByte+&lt;b&gt;seperatorByte+&lt;/b&gt;sessionIdBytes, &lt;/b&gt;&lt;span style=&quot;font-family: monospace;&quot;&gt;as start row, when creating table &lt;a href=&quot;http://archive.apache.org/dist/hbase/docs/apidocs/org/apache/hadoop/hbase/client/Scan.html&quot;&gt;Scan &lt;/a&gt;object.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
So we can see the key design facilitates two different ways of doing a partial scan on the HBase table.&lt;br /&gt;
&lt;span style=&quot;font-family: monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;iframe bordercolor=&quot;#000000&quot; frameborder=&quot;0&quot; height=&quot;250&quot; hspace=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://ad.doubleclick.net/adi/N7433.148119.BLOGGEREN/B6695650.1055;sz=300x250;ord=[timestamp]?;lid=41000613802463973;pid=sku3524121;usg=AFHzDLtLT-7DJ2tfEYYSEo8DmuTvfUzajQ;adurl=http%253A%252F%252Fwww.samsclub.com%252Fsams%252Fipad-2-wi-fi-16gb-black%252Fprod3111459.ip%253Fpid%253D_DoubleClick_Affiliates%2526ci_src%253D15781033%2526ci_sku%253Dsku3524121;pubid=595859;price=%24389.00;title=iPad+2+Wi-Fi+16GB+-+Black+-+Gifts+for+Kids;merc=Sam%27s+Club;imgsrc=http%3A%2F%2Fs7d2.scene7.com%2Fis%2Fimage%2Fsamsclub%2Fs7product%2F0088590945758_A.jpg;width=135;height=135&quot; vspace=&quot;0&quot; width=&quot;300&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/592951186266281729/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2012/03/hbase-composite-row-key-design-doing_11.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/592951186266281729'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/592951186266281729'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2012/03/hbase-composite-row-key-design-doing_11.html' title='HBase Composite Row Key Design: Doing Table Scan Using Partial Row Key Part2'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-6689117090623919257</id><published>2012-03-06T22:34:00.000-08:00</published><updated>2012-11-10T07:59:40.290-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hbase composite key"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase dynamic columns"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase partial key scan"/><category scheme="http://www.blogger.com/atom/ns#" term="hbase partial table scan"/><title type='text'>HBase Composite Row Key Design: Doing Table Scan Using Partial Row Key</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;a href=&quot;http://hbase.apache.org/&quot;&gt;HBase &lt;/a&gt;is a column oriented NoSql Database which uses Hadoop file system as the data storage.The rows in HBase are stored in sorted order, meaning they are alphabetically sorted.Typically one creates an &lt;a href=&quot;http://archive.apache.org/dist/hbase/docs/apidocs/org/apache/hadoop/hbase/client/HTable.html&quot;&gt;HTable &lt;/a&gt;with one or more column family and store data in it.&lt;br /&gt;
&lt;br /&gt;
A column family is a collection of dynamic columns, meaning column name can be defined when storing data and so one can have n number of columns and there is no limit to it.Data in a table is stored using a row key and specifying column family, column name and column value. The row key is just a byte array.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The row key of a HBase table can be a composite key, consisting of multiple individual keys.For example one can design a key to keep track of user and number of session a user has.In this case a row key can be:&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;b&gt; userIdBytes+seperatorByte+sessionIdBytes.&lt;/b&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Here seperatorByte should be choosen in such a way which does not conflict with userId and sessionId bytes values. For example use LF (decimal 10)&lt;br /&gt;
&lt;br /&gt;
This row key design allows partial table scanning, where to get all the sessionIds for a user&amp;nbsp;one can simple create a HBase &lt;a href=&quot;http://archive.apache.org/dist/hbase/docs/apidocs/org/apache/hadoop/hbase/client/Scan.html&quot;&gt;Scan &lt;/a&gt;object with userId as the start row of the &lt;a href=&quot;http://archive.apache.org/dist/hbase/docs/apidocs/org/apache/hadoop/hbase/client/Scan.html&quot;&gt;Scan &lt;/a&gt;and get all table rows for this userId. Meaning one can get all the rows which start with this userId.&lt;br /&gt;
&lt;br /&gt;
The other advantage of this key design is that one can extract sessionId from the row key itself&amp;nbsp;without actually loading data from HBase.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;iframe bordercolor=&quot;#000000&quot; frameborder=&quot;0&quot; height=&quot;250&quot; hspace=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://ad.doubleclick.net/adi/N7433.148119.BLOGGEREN/B6695650.1062;sz=300x250;ord=[timestamp]?;lid=41000613802463973;pid=sku8334425;usg=AFHzDLsJeU6tge-8wIO1j_vVmHzmz6f_yQ;adurl=http%253A%252F%252Fwww.samsclub.com%252Fsams%252Fhalo-4-console-bndl-street-date-11-6-12%252Fprod7710450.ip%253Fpid%253D_DoubleClick_Affiliates%2526ci_src%253D15781033%2526ci_sku%253Dsku8334425;pubid=595859;price=%24346.98;title=Halo+4+Limited+Edition+Xbox+360+320GB+Console+Bundle+-+Xbox+360;merc=Sam%27s+Club;imgsrc=http%3A%2F%2Fs7d2.scene7.com%2Fis%2Fimage%2Fsamsclub%2Fs7product%2F0088537047280_A.jpg;width=135;height=135&quot; vspace=&quot;0&quot; width=&quot;300&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/6689117090623919257/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2012/03/hbase-composite-row-key-design-doing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/6689117090623919257'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/6689117090623919257'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2012/03/hbase-composite-row-key-design-doing.html' title='HBase Composite Row Key Design: Doing Table Scan Using Partial Row Key'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-2148693165494281457</id><published>2011-09-10T10:06:00.000-07:00</published><updated>2012-11-10T08:05:00.172-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Encryption"/><title type='text'>example of PKCS7Padding and ISO10126Padding</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In my previous blog about &lt;a href=&quot;http://riteshadval.blogspot.com/2010/11/how-padding-affects-block-cipher.html&quot;&gt;How Padding Affects Block Cipher Encryption: PKCS7Padding vs ISO10126Padding&lt;/a&gt;, I explained why using the correct padding is important when using block cipher. Here is an example which demonstrate what happens, if we use the wrong padding for decryption.&lt;br /&gt;
&lt;br /&gt;
The case1 demonstrates what I explained in previous blog, Here we encrypt password using PKCS7Padding and decrypt using ISO10126Padding and it works fine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The case2 demonstrate the case where we encrypt password using ISO10126Padding and &lt;br /&gt;
decrpyt using PKCS7Padding and this fails with an error like:&lt;br /&gt;
&lt;br /&gt;
javax.crypto.BadPaddingException: pad block corrupted decrypted password with PKCS7Padding: null   &lt;br /&gt;
&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color: black;&quot;&gt;import&amp;nbsp;java.io.ByteArrayInputStream;&lt;br /&gt;
import&amp;nbsp;java.io.ByteArrayOutputStream;&lt;br /&gt;
import&amp;nbsp;java.io.InputStream;&lt;br /&gt;
import&amp;nbsp;java.io.OutputStream;&lt;br /&gt;
import&amp;nbsp;java.security.Security;&lt;br /&gt;
import&amp;nbsp;javax.crypto.Cipher;&lt;br /&gt;
import&amp;nbsp;javax.crypto.spec.IvParameterSpec;&lt;br /&gt;
import&amp;nbsp;javax.crypto.spec.SecretKeySpec;&lt;br /&gt;
import&amp;nbsp;com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;&lt;br /&gt;
&lt;br /&gt;
public&amp;nbsp;class&amp;nbsp;EncryptionPaddingTest&amp;nbsp;{&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;final&amp;nbsp;String&amp;nbsp;KEY&amp;nbsp;=&amp;nbsp;&quot;myowntestkey01234567890123456789&quot;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;final&amp;nbsp;String&amp;nbsp;vector&amp;nbsp;=&amp;nbsp;&quot;myowntestvector!&quot;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;void&amp;nbsp;main(String[]&amp;nbsp;args)&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Security.addProvider(new&amp;nbsp;org.bouncycastle.jce.provider.BouncyCastleProvider());&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;password&amp;nbsp;=&amp;nbsp;&quot;1234&quot;;&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt;//Case1:&amp;nbsp;encrypt&amp;nbsp;password&amp;nbsp;using&amp;nbsp;PKCS7Padding&amp;nbsp;padding&amp;nbsp;and&amp;nbsp;decrypt&amp;nbsp;using&amp;nbsp;ISO10126Padding&amp;nbsp;padding&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;doEncryptionDecryption(password,&amp;nbsp;&quot;PKCS7Padding&quot;,&amp;nbsp;&quot;ISO10126Padding&quot;);&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt;//Case2:&amp;nbsp;encrypt&amp;nbsp;password&amp;nbsp;using&amp;nbsp;ISO10126Padding&amp;nbsp;padding&amp;nbsp;and&amp;nbsp;decrypt&amp;nbsp;using&amp;nbsp;PKCS7Padding&amp;nbsp;padding&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;doEncryptionDecryption(password,&amp;nbsp;&quot;ISO10126Padding&quot;,&amp;nbsp;&quot;PKCS7Padding&quot;);&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;void&amp;nbsp;doEncryptionDecryption(String&amp;nbsp;input,&amp;nbsp;String&amp;nbsp;encryptionPadding,&amp;nbsp;String&amp;nbsp;decryptionPadding)&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;encodedPassword&amp;nbsp;=&amp;nbsp;encryptUsingAES(input,&amp;nbsp;encryptionPadding);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println(&quot;encrypted&amp;nbsp;password&amp;nbsp;with&amp;nbsp;&quot;&amp;nbsp;+&amp;nbsp;encryptionPadding&amp;nbsp;+&quot;:&amp;nbsp;&quot;&amp;nbsp;+&amp;nbsp;encodedPassword);&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//(2)&amp;nbsp;decrypted&amp;nbsp;password&amp;nbsp;using&amp;nbsp;encryptionPadding&amp;nbsp;padding&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;decodedPassword1&amp;nbsp;=&amp;nbsp;decryptUsingAES(encodedPassword,&amp;nbsp;encryptionPadding);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println(&quot;decrypted&amp;nbsp;password&amp;nbsp;with&amp;nbsp;&quot;&amp;nbsp;+encryptionPadding&amp;nbsp;+&amp;nbsp;&quot;:&amp;nbsp;&quot;+&amp;nbsp;decodedPassword1);&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//(3)&amp;nbsp;decrypted&amp;nbsp;password&amp;nbsp;using&amp;nbsp;decryptionPadding&amp;nbsp;padding&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;decodedPassword2&amp;nbsp;=&amp;nbsp;decryptUsingAES(encodedPassword,&amp;nbsp;decryptionPadding);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println(&quot;decrypted&amp;nbsp;password&amp;nbsp;with&amp;nbsp;&quot;&amp;nbsp;+decryptionPadding&amp;nbsp;+&amp;nbsp;&quot;:&amp;nbsp;&quot;+&amp;nbsp;decodedPassword2);&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;String&amp;nbsp;encryptUsingAES(String&amp;nbsp;input,&amp;nbsp;String&amp;nbsp;padding)&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[]&amp;nbsp;bytes&amp;nbsp;=&amp;nbsp;input.getBytes(&quot;UTF-8&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Cipher&amp;nbsp;cipher&amp;nbsp;=&amp;nbsp;Cipher.getInstance(&quot;Rijndael/CBC/&quot;+padding);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SecretKeySpec&amp;nbsp;keySpec&amp;nbsp;=&amp;nbsp;new&amp;nbsp;SecretKeySpec(KEY.getBytes(),&amp;nbsp;&quot;Rijndael&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cipher.init(Cipher.ENCRYPT_MODE,&amp;nbsp;keySpec,&amp;nbsp;new&amp;nbsp;IvParameterSpec(vector.getBytes()));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[]&amp;nbsp;encrypted&amp;nbsp;=&amp;nbsp;cipher.doFinal(bytes);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[]&amp;nbsp;base64Encoded&amp;nbsp;=&amp;nbsp;encodeBase64(encrypted);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;new&amp;nbsp;String(base64Encoded,&amp;nbsp;&quot;UTF-8&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;e)&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.printStackTrace();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;null;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;String&amp;nbsp;decryptUsingAES(String&amp;nbsp;input,&amp;nbsp;String&amp;nbsp;padding)&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[]&amp;nbsp;bytes&amp;nbsp;=&amp;nbsp;input.getBytes(&quot;UTF-8&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[]&amp;nbsp;base64Decoded&amp;nbsp;=&amp;nbsp;decodeBase64(bytes);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Cipher&amp;nbsp;cipher&amp;nbsp;=&amp;nbsp;Cipher.getInstance(&quot;Rijndael/CBC/&quot;+padding);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SecretKeySpec&amp;nbsp;keySpec&amp;nbsp;=&amp;nbsp;new&amp;nbsp;SecretKeySpec(KEY.getBytes(),&amp;nbsp;&quot;Rijndael&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cipher.init(Cipher.DECRYPT_MODE,&amp;nbsp;keySpec,&amp;nbsp;new&amp;nbsp;IvParameterSpec(vector.getBytes()));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[]&amp;nbsp;decrypted&amp;nbsp;=&amp;nbsp;cipher.doFinal(base64Decoded);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;new&amp;nbsp;String(decrypted,&amp;nbsp;&quot;UTF-8&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;e)&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.printStackTrace();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;null;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;byte[]&amp;nbsp;encodeBase64(byte[]&amp;nbsp;b)&amp;nbsp;throws&amp;nbsp;Exception&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ByteArrayOutputStream&amp;nbsp;baos&amp;nbsp;=&amp;nbsp;new&amp;nbsp;ByteArrayOutputStream();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OutputStream&amp;nbsp;b64os&amp;nbsp;=&amp;nbsp;MimeUtility.encode(baos,&amp;nbsp;&quot;base64&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b64os.write(b);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b64os.close();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;baos.toByteArray();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;byte[]&amp;nbsp;decodeBase64(byte[]&amp;nbsp;b)&amp;nbsp;throws&amp;nbsp;Exception&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ByteArrayInputStream&amp;nbsp;bais&amp;nbsp;=&amp;nbsp;new&amp;nbsp;ByteArrayInputStream(b);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;InputStream&amp;nbsp;b64is&amp;nbsp;=&amp;nbsp;MimeUtility.decode(bais,&amp;nbsp;&quot;base64&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[]&amp;nbsp;tmp&amp;nbsp;=&amp;nbsp;new&amp;nbsp;byte[b.length];&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int&amp;nbsp;n&amp;nbsp;=&amp;nbsp;b64is.read(tmp);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[]&amp;nbsp;res&amp;nbsp;=&amp;nbsp;new&amp;nbsp;byte[n];&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.arraycopy(tmp,&amp;nbsp;0,&amp;nbsp;res,&amp;nbsp;0,&amp;nbsp;n);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;res;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
}&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br/&gt;
&lt;br/&gt;
&lt;br/&gt;
&lt;/div&gt;

&lt;iframe bordercolor=&quot;#000000&quot; frameborder=&quot;0&quot; height=&quot;250&quot; hspace=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://ad.doubleclick.net/adi/N7433.148119.BLOGGEREN/B6533661.1055;sz=300x250;ord=[timestamp]?;lid=41000613802463918;pid=62567;usg=AFHzDLvN8eWCoOpY5AB5yRsJLR-TgahENQ;adurl=http%253A%252F%252Fwww.abt.com%252Fproduct%252F62567%252FGoogle-NEXUS7ASUS1B16.html;pubid=595859;price=%24219.99;title=Google+Nexus+7+Black+Wi-Fi+16GB+Tablet+-+NEXUS7ASUS1B16;merc=Abt+Electronics+%26+Appliances;imgsrc=http%3A%2F%2Fcontent.abt.com%2Fmedia%2Fimages%2Fproducts%2FBDP_Images%2Fbig_NEXUS7ASUS1B16.jpg;width=82;height=135&quot; vspace=&quot;0&quot; width=&quot;300&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/2148693165494281457/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2011/09/example-of-pkcs7padding-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/2148693165494281457'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/2148693165494281457'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2011/09/example-of-pkcs7padding-and.html' title='example of PKCS7Padding and ISO10126Padding'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-4527699044909435401</id><published>2011-08-28T16:56:00.000-07:00</published><updated>2011-08-28T16:56:53.910-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="junit"/><category scheme="http://www.blogger.com/atom/ns#" term="Spring"/><category scheme="http://www.blogger.com/atom/ns#" term="Unit testing"/><title type='text'>Spring: Unit Test, Reload Context After Each Test Or Test Class</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Recently I came across a situation while doing some unit test using spring SpringJUnit4ClassRunner where after each test, the spring application context is no longer good for it to be reused in the next test. After some digging, I found out that there is this &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/test/annotation/DirtiesContext.html&quot;&gt;DirtiesContext&lt;/a&gt;&amp;nbsp;annotation which one can use in the unit test to force spring to reload context.&lt;br /&gt;
&lt;br /&gt;
It has two options:&lt;br /&gt;
&lt;br /&gt;
(1) &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/test/annotation/DirtiesContext.ClassMode.html#AFTER_EACH_TEST_METHOD&quot;&gt;One is reload context after each test.&lt;/a&gt;&lt;br /&gt;
(2) &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/test/annotation/DirtiesContext.ClassMode.html#AFTER_CLASS&quot;&gt;Second is to reload context after all the tests of a class.&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
This is very useful and it also means that when running unit test using spring, by default the application context is loaded once and used to run all the tests.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/4527699044909435401/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2011/08/spring-unit-test-reload-context-after.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/4527699044909435401'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/4527699044909435401'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2011/08/spring-unit-test-reload-context-after.html' title='Spring: Unit Test, Reload Context After Each Test Or Test Class'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-6056368726675563194</id><published>2010-11-06T22:52:00.000-07:00</published><updated>2010-11-06T22:52:31.794-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Encryption"/><title type='text'>How Padding Affects Block Cipher Encryption: PKCS7Padding vs ISO10126Padding</title><content type='html'>&lt;b&gt;Why Padding:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
To understand why you need to use padding in block cipher encryption, you need to first understand what a &lt;a href=&quot;http://en.wikipedia.org/wiki/Block_cipher&quot;&gt;block cipher&lt;/a&gt; is? A block cipher is an encryption algorithm which operates on a fixed length of bits or block of input data size for encryption. A block cipher requires that input which needs to be encrypted has to be multiple of this block size.&lt;br /&gt;
&lt;br /&gt;
So there you go, since not all input will be a multiple of this block size, you need to use padding to add additional bits to the input data to make it a multiple of block size before doing encryption using a block cipher. Similarly when doing decryption the padding needs to be removed to obtained the original input which was encrypted.&lt;br /&gt;
&lt;br /&gt;
To given an example, a block cipher like AES (aka Rijndael) has a fixed block size of 128 bits and a key size of 128, 192, or 256 bits. So any input which is &amp;nbsp;encrypted or decrypted needs to be a multiple of 128 bits.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What happens if you use a wrong padding?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
It is very important that one use the same padding scheme for both encryption and decryption. A padding is part of contract for a successful encryption and decryption. First lets look at two padding schemes:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;PKCS7Padding:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The PKCS #7 padding string consists of a sequence of bytes, each of which has value equal to the total number of padding bytes added.&lt;br /&gt;
&lt;br /&gt;
The following example shows how these modes work. Given a block cipher of 8 bytes size, If input data length is 9 bytes, then number of padding bytes equal to 7 bytes, each having value as 07.&lt;br /&gt;
&lt;br /&gt;
Data: FF FF FF FF FF FF FF FF FF&lt;br /&gt;
&lt;br /&gt;
PKCS7 padding: FF FF FF FF FF FF FF FF FF 07 07 07 07 07 07 07&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;ISO10126Padding:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The ISO10126 padding string consists of random value data and the last byte has the value of total number of padding bytes added.&lt;br /&gt;
&lt;br /&gt;
The following example shows how this mode works. Given a block cipher of 8 bytes size, If input data length is 9 bytes, then number of padding bytes equal to 7 bytes, the first 6 bytes will have random value and last one will have value 07 to denote total number of padding bytes added.&lt;br /&gt;
&lt;br /&gt;
Data: FF FF FF FF FF FF FF FF FF&lt;br /&gt;
&lt;br /&gt;
ISO10126 padding: FF FF FF FF FF FF FF FF FF 7D 2A 75 EF F8 EF 07&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Using PKCS7Padding for encryption and ISO10126Padding for decryption:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Now to illustrate what wrong could happen, assume that a system is designed for password encryption using AES 256 bit key size and it is using PKCS7Padding for encryption and decryption for password. Everything works fine. Now a few years later a new system is designed which will decrypt the password and use it. Now see what happens if it uses a wrong padding:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The new system by mistake starts using ISO10126Padding padding when doing password decryption.&lt;br /&gt;
&lt;u&gt;The surprising thing is that decryption will work!!!&lt;/u&gt; If you look at the example of paddings above, the ISO10126Padding will read last 07 bytes of data encoded with PKCS7Padding (FF FF FF FF FF FF FF FF FF 07 07 07 07 07 07 07) and will find 07 as the last byte value and remove the last 7 bytes.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Using ISO10126Padding for encryption and PKCS7Padding for decryption:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Now assume that this new system also starts encrypting password and it uses ISO10126Padding.&lt;br /&gt;
If the old system tried to decrypted this password using PKCS7Padding it will most like fail with pad block corruption error. To see why if you look at the example of paddings above, the PKCS7Padding  will read data padded with ISO10126Padding (FF FF FF FF FF FF FF FF FF 7D 2A 75 EF F8 EF 07) and will find 07 as the last byte value so based on PKCS7Padding scheme it will try to validate that last 7 bytes should have 07 as the value, but they are not so it will result in an error.&lt;br /&gt;
&lt;br /&gt;
So in essence it is very important to make sure when designing any new system which rely on old legacy encryption contract to follow the same padding scheme or you will end up with subtle&amp;nbsp;errors like the one I highlighted above and will bite you later.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/6056368726675563194/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/11/how-padding-affects-block-cipher.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/6056368726675563194'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/6056368726675563194'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/11/how-padding-affects-block-cipher.html' title='How Padding Affects Block Cipher Encryption: PKCS7Padding vs ISO10126Padding'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-3342460092576704672</id><published>2010-10-17T15:24:00.000-07:00</published><updated>2012-11-10T08:10:48.736-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ios"/><category scheme="http://www.blogger.com/atom/ns#" term="JAVA"/><category scheme="http://www.blogger.com/atom/ns#" term="Objective C"/><title type='text'>Java Vs Objective C: From Interface to Protocol</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In my previous &lt;a href=&quot;http://riteshadval.blogspot.com/2010/10/java-vs-objective-c-object-method.html&quot;&gt;blog entry&lt;/a&gt;, I compared Java method invocation and Objective C messaging. Today I am writing about Objective C &lt;b&gt;protocols&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Objective C offers something called protocol which is equivalent to Java Interface. An interface is a contract which you can implement in a class so that object of that class conforms to this contract.&lt;br /&gt;
&lt;br /&gt;
A Java Interface is defined using interface keyword. For example in AWT library of JAVA a MouseListener is an interface which declares method for mouse handling, something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span style=&quot;color: #961a65;&quot;&gt;package&lt;/span&gt; java.awt.event;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;interface&lt;/span&gt; MouseListener &lt;span style=&quot;color: #961a65;&quot;&gt;extends&lt;/span&gt; EventListener {&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #4d78c5;&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; * Invoked when the mouse button has been clicked (pressed&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; * and released) on a component.&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; */&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; mouseClicked(MouseEvent e);&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #4d78c5;&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; * Invoked when a mouse button has been pressed on a component.&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; */&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; mousePressed(MouseEvent e);&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #4d78c5;&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; * Invoked when a mouse button has been released on a component.&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; */&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; mouseReleased(MouseEvent e);&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #4d78c5;&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; * Invoked when the mouse enters a component.&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; */&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; mouseEntered(MouseEvent e);&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #4d78c5;&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; * Invoked when the mouse exits a component.&lt;/div&gt;
&lt;div style=&quot;color: #4d78c5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; */&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; mouseExited(MouseEvent e);&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
}&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
This interface you can implement in a class and then implement the methods, something like this:&lt;br /&gt;
&lt;br /&gt;&lt;br/&gt;

&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt; myMouseHandler &lt;span style=&quot;color: #961a65;&quot;&gt;implements&lt;/span&gt; MouseListener {&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; mouseClicked(MouseEvent e) {&lt;/div&gt;
&lt;div style=&quot;color: #489075; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;/span&gt;// &lt;span style=&quot;color: #8fb0c8;&quot;&gt;TODO&lt;/span&gt; Auto-generated method stub&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;
&amp;nbsp;&amp;nbsp;//other methods are omitted&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Monaco; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Monaco; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Objective C protocols:&lt;br /&gt;
&lt;br /&gt;
Similarly in Objective C, protocol declares a set of methods which one can implement in a class to conform to the protocol contract. For example Objective C (and iOS) has&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;UIApplicationDelegate&lt;/span&gt;&amp;nbsp;protocol which specifies set of methods for handling application lifecycle and these can be implemented by an iOS application so that it can receive these method calls and handle them appropriately. Some methods of this protocol are:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span style=&quot;color: #bf2e9d;&quot;&gt;@protocol&lt;/span&gt; UIApplicationDelegate&amp;lt;&lt;span style=&quot;color: #7340a3;&quot;&gt;NSObject&lt;/span&gt;&amp;gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #bf2e9d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
@optional&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
- (&lt;span style=&quot;color: #bf2e9d;&quot;&gt;void&lt;/span&gt;)applicationDidFinishLaunching:(&lt;span style=&quot;color: #7340a3;&quot;&gt;UIApplication&lt;/span&gt; *)application;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
- (&lt;span style=&quot;color: #bf2e9d;&quot;&gt;BOOL&lt;/span&gt;)application:(&lt;span style=&quot;color: #7340a3;&quot;&gt;UIApplication&lt;/span&gt; *)application didFinishLaunchingWithOptions:(&lt;span style=&quot;color: #7340a3;&quot;&gt;NSDictionary&lt;/span&gt; *)launchOptions &lt;span style=&quot;color: #7a482f;&quot;&gt;__OSX_AVAILABLE_STARTING&lt;/span&gt;(&lt;span style=&quot;color: #7a482f;&quot;&gt;__MAC_NA&lt;/span&gt;,&lt;span style=&quot;color: #7a482f;&quot;&gt;__IPHONE_3_0&lt;/span&gt;);&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
- (&lt;span style=&quot;color: #bf2e9d;&quot;&gt;void&lt;/span&gt;)applicationDidBecomeActive:(&lt;span style=&quot;color: #7340a3;&quot;&gt;UIApplication&lt;/span&gt; *)application;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
- (&lt;span style=&quot;color: #bf2e9d;&quot;&gt;void&lt;/span&gt;)applicationWillResignActive:(&lt;span style=&quot;color: #7340a3;&quot;&gt;UIApplication&lt;/span&gt; *)application;&lt;/div&gt;
&lt;div style=&quot;color: #008326; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;- (&lt;/span&gt;&lt;span style=&quot;color: #bf2e9d;&quot;&gt;BOOL&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)application:(&lt;/span&gt;&lt;span style=&quot;color: #7340a3;&quot;&gt;UIApplication&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; *)application handleOpenURL:(&lt;/span&gt;&lt;span style=&quot;color: #7340a3;&quot;&gt;NSURL&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; *)url;&amp;nbsp; &lt;/span&gt;// no equiv. notification. return NO if the application can&#39;t open for some reason&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #008326; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;- (&lt;/span&gt;&lt;span style=&quot;color: #bf2e9d;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)applicationDidReceiveMemoryWarning:(&lt;/span&gt;&lt;span style=&quot;color: #7340a3;&quot;&gt;UIApplication&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; *)application;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;// try to clean up as much memory as possible. next step is to terminate app&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
- (&lt;span style=&quot;color: #bf2e9d;&quot;&gt;void&lt;/span&gt;)applicationWillTerminate:(&lt;span style=&quot;color: #7340a3;&quot;&gt;UIApplication&lt;/span&gt; *)application;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;To implement a protocol in a class you specify protocol as part of your class declaration, for example in the header file (.h) you declare that you are implementing UIApplicationDelegate:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #bf2e9d;&quot;&gt;@interface&lt;/span&gt; SampleAppDelegate : NSObject&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Lucida Grande&#39;; white-space: pre-wrap;&quot;&gt;&amp;lt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Menlo; white-space: normal;&quot;&gt;UIApplicationDelegate&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Lucida Grande&#39;; white-space: pre-wrap;&quot;&gt;&amp;gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Menlo; white-space: normal;&quot;&gt;&amp;nbsp;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Menlo; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
And then in class implementation (.m file) you can implement the method of UIApplicationDelegate, something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span style=&quot;color: #bf2e9d;&quot;&gt;@implementation&lt;/span&gt; SampleAppDelegate&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #bf2e9d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #7a482f;&quot;&gt;#pragma mark -&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #7a482f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
#pragma mark Application lifecycle&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;
- (&lt;span style=&quot;color: #bf2e9d;&quot;&gt;BOOL&lt;/span&gt;)application:(&lt;span style=&quot;color: #7340a3;&quot;&gt;UIApplication&lt;/span&gt; *)application didFinishLaunchingWithOptions:(&lt;span style=&quot;color: #7340a3;&quot;&gt;NSDictionary&lt;/span&gt; *)launchOptions { &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Menlo; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;//implement it here&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Menlo; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Menlo; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times; font-size: medium;&quot;&gt;For more details on protocol, see &lt;a href=&quot;http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProtocols.html%23//apple_ref/doc/uid/TP30001163-CH15&quot;&gt;this&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;iframe bordercolor=&quot;#000000&quot; frameborder=&quot;0&quot; height=&quot;250&quot; hspace=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://ad.doubleclick.net/adi/N7433.148119.BLOGGEREN/B6695650.1058;sz=300x250;ord=[timestamp]?;lid=41000613802463973;pid=sku3524121;usg=AFHzDLtLT-7DJ2tfEYYSEo8DmuTvfUzajQ;adurl=http%253A%252F%252Fwww.samsclub.com%252Fsams%252Fipad-2-wi-fi-16gb-black%252Fprod3111459.ip%253Fpid%253D_DoubleClick_Affiliates%2526ci_src%253D15781033%2526ci_sku%253Dsku3524121;pubid=595859;price=%24389.00;title=iPad+2+Wi-Fi+16GB+-+Black+-+Gifts+for+Kids;merc=Sam%27s+Club;imgsrc=http%3A%2F%2Fs7d2.scene7.com%2Fis%2Fimage%2Fsamsclub%2Fs7product%2F0088590945758_A.jpg;width=135;height=135&quot; vspace=&quot;0&quot; width=&quot;300&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/3342460092576704672/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/10/java-vs-objective-c-from-interface-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/3342460092576704672'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/3342460092576704672'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/10/java-vs-objective-c-from-interface-to.html' title='Java Vs Objective C: From Interface to Protocol'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-551894037101490680</id><published>2010-10-03T22:39:00.000-07:00</published><updated>2010-10-03T22:39:41.447-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ios"/><category scheme="http://www.blogger.com/atom/ns#" term="iphone"/><category scheme="http://www.blogger.com/atom/ns#" term="JAVA"/><category scheme="http://www.blogger.com/atom/ns#" term="Objective C"/><title type='text'>Java Vs Objective C: Object Method Invocation to Object Messaging</title><content type='html'>I recently started looking into iphone and ios app development. The primary language for ios based applications is Objective C, which is an object oriented C like language. Coming from Java background there are certain aspects and concepts in the Objective C language, which are different for a Java programmer.&lt;br /&gt;
&lt;br /&gt;
So here I am starting a series of blog posts, to give an overview of Objective C language and contrast it with Java programming language.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;In this blog, I want to compare Java object method invocation to Objective C object messaging. They are pretty much doing the same thing.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
In Java you define a class and methods which can be invoked on the object of this class. For example let say you have a class called Window and it has a method called &lt;i&gt;getBounds&lt;/i&gt; which returns Rectangle bounds of the Window. It can looks something like this:&lt;br /&gt;
&lt;code&gt;&lt;div style=&quot;color: #961a65; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;public&lt;span style=&quot;color: black;&quot;&gt; &lt;/span&gt;class&lt;span style=&quot;color: black;&quot;&gt; Window {&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;private&lt;/span&gt; Rectangle &lt;span style=&quot;color: #032fc3;&quot;&gt;myBounds&lt;/span&gt;;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Rectangle getBounds() {&lt;/div&gt;&lt;div style=&quot;color: #961a65; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;return&lt;span style=&quot;color: black;&quot;&gt; &lt;/span&gt;this&lt;span style=&quot;color: black;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;myBounds&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;}&lt;/div&gt;&lt;/code&gt;&lt;br /&gt;
And this is how you invoke a method on an object:&lt;br /&gt;
&lt;code&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;span class=&quot;Apple-style-span&quot; style=&quot;white-space: normal;&quot;&gt;Window window = &lt;span style=&quot;color: #961a65;&quot;&gt;new&lt;/span&gt; Window();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Rectangle rect = window.getBounds();&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
In Objective C you will define a class by first defining an interface in the header file. Here  in Window interface we have defined a method called bounds:&lt;br /&gt;
&lt;code&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #bf2e9d;&quot;&gt;@interface&lt;/span&gt; Window : NSObject {&lt;/div&gt;&lt;div style=&quot;color: #7340a3; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;CGRect&lt;span style=&quot;color: black;&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: #4b8186;&quot;&gt;rect&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;- (&lt;span style=&quot;color: #7340a3;&quot;&gt;CGRect&lt;/span&gt;*) bounds;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
and then in class file you define the implementation:&lt;br /&gt;
&lt;code&gt;&lt;div style=&quot;color: #bf2e9d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;@implementation&lt;span style=&quot;color: black;&quot;&gt; Window&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;- (&lt;span style=&quot;color: #7340a3;&quot;&gt;CGRect&lt;/span&gt;*) bounds {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bf2e9d;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #4b8186;&quot;&gt;rect&lt;/span&gt;; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #bf2e9d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;@end&lt;/div&gt;&lt;/code&gt;&lt;br /&gt;
&lt;b&gt;Objective C messaging:&lt;/b&gt;&lt;br /&gt;
In objective C to call a method of an object you do it by messaging an object.A message is the method signature, along with the parameter information the method needs.Messages are enclosed by brackets ([ and ]). Inside the brackets, the object receiving the message is on the left side and the message (along with any parameters required by the message) is on the right.&lt;br /&gt;
&lt;br /&gt;
For example to invoke bounds method on an instance of Window object you use messaging like shown below:&lt;br /&gt;
&lt;code&gt;&lt;div style=&quot;color: #008326; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;//create an instance of Window by messaging alloc on Window class. alloc is like static&lt;/div&gt;&lt;div style=&quot;color: #008326; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;//method on Window class&lt;/div&gt;&lt;div style=&quot;color: #4b8186; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;Window&lt;span style=&quot;color: black;&quot;&gt; *&lt;/span&gt;window&lt;span style=&quot;color: black;&quot;&gt; = [&lt;/span&gt;Window&lt;span style=&quot;color: black;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #3f217c;&quot;&gt;alloc&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;];&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: #008326; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;//now call bounds on window&lt;/div&gt;&lt;div style=&quot;font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #7340a3;&quot;&gt;CGRect&lt;/span&gt; *rect = [&lt;span style=&quot;color: #4b8186;&quot;&gt;window&lt;/span&gt; &lt;span style=&quot;color: #3f217c;&quot;&gt;bounds&lt;/span&gt;];&lt;/div&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
This shows how messaging works in Objective C. In future blogs we will look at other features of Objective C language.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/551894037101490680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/10/java-vs-objective-c-object-method.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/551894037101490680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/551894037101490680'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/10/java-vs-objective-c-object-method.html' title='Java Vs Objective C: Object Method Invocation to Object Messaging'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-7716167913534429936</id><published>2010-09-23T14:51:00.000-07:00</published><updated>2010-09-23T14:51:09.282-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="deadlock"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server"/><title type='text'>SQL Server deadlock: Transaction (Process ID XXX) was deadlocked on lock resources with another process and has been chosen as the deadlock victim</title><content type='html'>I recently hit SQL Server Transaction deadlock and message was something like below:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Transaction (Process ID 128) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The issue was two transactions were trying to do select on a shared resource (table), while they each were holding exclusive lock on the same table index because of previous insert statement issued on the same table in each transaction.&lt;br /&gt;
&lt;br /&gt;
Let me first go over how do you identify which resources are deadlocking. In Sql Server 2005, (thats the one I was using) there are two ways to do it.&lt;br /&gt;
&lt;br /&gt;
(1) Use SQL Server Profiler. SQL Server Profiler is a graphical tool which helps you capture deadlock trace.More info can be obtained if you look at &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms187929.aspx&quot;&gt;msdn documentation&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
(2) Second option is to enable flag 1222 with “DBCC TRACEON (1222, -1)” or by adding “-T1222” as a SQL startup parameter. This will log trace information on sql server log. More information on this can be obtained &lt;a href=&quot;http://msdn.microsoft.com/enus/library/ms188396.aspx&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
A trace with DBCC flag 1222 looks like below:&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
08/04/2010 11:58:44,spid4s,Unknown,Deadlock encountered .... Printing deadlock information&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;waiter id=processaa8d48 mode=S requestType=wait&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;waiter-list&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;owner id=processa1bc48 mode=X&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;owner-list&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;keylock hobtid=72057594058113024 dbid=6 objectname=OrderDB_Debug.dbo.CustomerInstances indexname=PK_CustomerInstances id=lock59481a00 mode=X associatedObjectId=72057594058113024&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;waiter id=processa1bc48 mode=S requestType=wait&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;waiter-list&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;owner id=processaa8d48 mode=X&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;owner-list&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;keylock hobtid=72057594058113024 dbid=6 objectname=OrderDB_Debug.dbo.CustomerInstances indexname=PK_CustomerInstances id=lock3b8a2500 mode=X associatedObjectId=72057594058113024&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,resource-list&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,set implicit_transactions on&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,inputbuf&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,unknown&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,frame procname=unknown line=1 sqlhandle=0x000000000000000000000000000000000000000000000000&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;select orderentry0.order_entry_id as order1_11&lt;c&gt; orderentry0.audit_creation_timestamp as audit2_11_&lt;c&gt; orderentry0.audit_creation_user as audit3_11_&lt;c&gt; orderentry0.audit_updated_timestamp as audit4_11_&lt;c&gt; orderentry0.audit_updated_user as audit5_11_&lt;c&gt; orderentry0.customer_instance_id as customer_instance9_11&lt;c&gt; orderentry0.line_item_id as line_item10_11&lt;c&gt; orderentry0.is_deleted as is6_11_&lt;c&gt; orderentry0.is_expired as is7_11_&lt;c&gt; orderentry0.expiration_timestamp as expiration8_11_ from Orders orderentry0&lt;c&gt; CustomerInstances customer1_ where orderentry0.customer_instance_id=customer1_.customer_instance_id and customer1_.customer_instance_guid=@P0 and orderentry0.is_deleted=0&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,frame procname=adhoc line=1 stmtstart=40 sqlhandle=0x0200000085a17c084fc7a9b5a8d6545805f76b6d8b65d45e&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,executionStack&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;process id=processaa8d48&lt;/b&gt; taskpriority=0 logused=5724 waitresource=KEY: 6:72057594058113024 (09005c5740ca) waittime=3062 ownerId=1120537422 transactionname=implicit_transaction lasttranstarted=2010-08-04T11:58:35.153 XDES=0x586cb450 lockMode=S schedulerid=2 kpid=8100 status=suspended spid=130 sbid=0 ecid=0 priority=0 transcount=1 lastbatchstarted=2010-08-04T11:58:35.513 lastbatchcompleted=2010-08-04T11:58:35.497 clientapp=Microsoft SQL Server JDBC Driver hostname=testpc-wxp hostpid=0 loginname=OrderDBApp isolationlevel=read committed (2) xactid=1120537422 currentdb=6 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128058&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,set implicit_transactions on&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,inputbuf&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,unknown&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,frame procname=unknown line=1 sqlhandle=0x000000000000000000000000000000000000000000000000&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;select customer0_.customer_instance_id as customer_instance1_5_&lt;c&gt; customer0_.audit_creation_timestamp as audit2_5_&lt;c&gt; customer0_.audit_creation_user as audit3_5_&lt;c&gt; customer0_.audit_updated_timestamp as audit4_5_&lt;c&gt; customer0_.audit_updated_user as audit5_5_&lt;c&gt; customer0_.customer_instance_creation_timestamp as timestamp6_5_&lt;c&gt; customer0_.address_id as address15_5_&lt;c&gt; customer0_.is_deleted as is7_5_&lt;c&gt; customer0_.description as descript8_5_&lt;c&gt; customer0_.stop_offset as stop9_5_&lt;c&gt; customer0_.is_expired as is10_5_&lt;c&gt; customer0_.expiration_timestamp as expiration11_5_&lt;c&gt; customer0_.customer_instance_guid as guid12_5_5&lt;c&gt; customer0_.account_domain_id as account16_5_&lt;c&gt; customer0_.start_offset as start13_5_&lt;c&gt; customer0_.customer_instance_title as title14_5_ from CustomerInstances customer0_ where customer0_.customer_instance_guid=@P0 and customer0_.is_deleted=0&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/c&gt;&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,frame procname=adhoc line=1 stmtstart=40 sqlhandle=0x02000000ccb2262052f4ec84e1255595ca29f072773c6739&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,executionStack&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;process id=processa1bc48&lt;/b&gt; taskpriority=0 logused=6160 waitresource=KEY: 6:72057594058113024 (0a00b2f8f5d8) waittime=2906 ownerId=1120537421 transactionname=implicit_transaction lasttranstarted=2010-08-04T11:58:34.950 XDES=0x21dc8958 lockMode=S schedulerid=1 kpid=6888 status=suspended spid=123 sbid=0 ecid=0 priority=0 transcount=1 lastbatchstarted=2010-08-04T11:58:35.670 lastbatchcompleted=2010-08-04T11:58:35.653 clientapp=Microsoft SQL Server JDBC Driver hostname=radval-wxp hostpid=0 loginname=OrderDBApp isolationlevel=read committed (2) xactid=1120537421 currentdb=6 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128058&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;process-list&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;deadlock victim=processaa8d48&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid14s,Unknown,&lt;b&gt;deadlock-list&lt;/b&gt;&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,ResType:LockOwner Stype:&#39;OR&#39;Xdes:0x586CB450 Mode: S SPID:130 BatchID:0 ECID:0 TaskProxy:(0x1F74C378) Value:0x358e9820 Cost:(0/5724)&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Victim Resource Owner:&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Log Viewer could not read information for this log entry. Cause: Data is Null. This method or property cannot be called on Null values.. Content:&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,ResType:LockOwner Stype:&#39;OR&#39;Xdes:0x586CB450 Mode: S SPID:130 BatchID:0 ECID:0 TaskProxy:(0x1F74C378) Value:0x358e9820 Cost:(0/5724)&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Requested By:&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Input Buf: Language Event: set implicit_transactions on&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,SPID: 123 ECID: 0 Statement Type: SELECT Line #: 1&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Owner:0x54FEB960 Mode: X        Flg:0x0 Ref:0 Life:02000000 SPID:123 ECID:0 XactLockInfo: 0x21DC897C&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Grant List 0:&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,KEY: 6:72057594058113024 (09005c5740ca) CleanCnt:2 Mode:X Flags: 0x0&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Node:2&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Log Viewer could not read information for this log entry. Cause: Data is Null. This method or property cannot be called on Null values.. Content:&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,ResType:LockOwner Stype:&#39;OR&#39;Xdes:0x21DC8958 Mode: S SPID:123 BatchID:0 ECID:0 TaskProxy:(0x54D9C378) Value:0x54feb740 Cost:(0/6160)&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Requested By:&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Input Buf: Language Event: set implicit_transactions on&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,SPID: 130 ECID: 0 Statement Type: SELECT Line #: 1&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Owner:0x5C48D120 Mode: X        Flg:0x0 Ref:0 Life:02000000 SPID:130 ECID:0 XactLockInfo: 0x586CB474&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Grant List 1:&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,KEY: 6:72057594058113024 (0a00b2f8f5d8) CleanCnt:3 Mode:X Flags: 0x0&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Node:1&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Log Viewer could not read information for this log entry. Cause: Data is Null. This method or property cannot be called on Null values.. Content:&lt;br /&gt;
08/04/2010 11:58:38,spid4s,Unknown,Wait-for graph&lt;br /&gt;
&lt;b&gt;08/04/2010 11:58:38,spid4s,Unknown,Deadlock encountered .... Printing deadlock information&lt;br /&gt;
&lt;/b&gt;&lt;b&gt;08/04/2010 11:57:55,spid126,Unknown,DBCC TRACEON 1222&lt;c&gt; server process ID (SPID) 126. This is an informational message only; no user action is required.&lt;br /&gt;
&lt;/c&gt;&lt;/b&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
You need to read above trace from bottom to top. What this is telling us is that there two transaction with processid &lt;b&gt;processaa8d48&lt;/b&gt; and &lt;b&gt;processa1bc48&lt;/b&gt; involved in the deadlock. SQL Server chooses process with id processaa8d48 as the deadlock victim, meaning this transaction is rollbacked by SQL Server.&lt;br /&gt;
&lt;br /&gt;
Process &lt;b&gt;processa1bc48 &lt;/b&gt;was executing &quot;select customer0_.customer_instance_id ...&quot; and process &lt;b&gt;processaa8d48 &lt;/b&gt;was executing &quot;select orderentry0.order_entry_id as order1_11 ...&quot; when the deadlock occurred.&lt;br /&gt;
&lt;br /&gt;
Looking further at &lt;b&gt;owner-list&lt;/b&gt;, you will notice one interesting and very confusing information. What this is saying is as follows:&lt;br /&gt;
&lt;br /&gt;
(1) There is an exclusive (X) lock on index &lt;b&gt;PK_CustomerInstances&lt;/b&gt; owned by owner process &lt;b&gt;processaa8d48&lt;/b&gt; and process &lt;b&gt;processa1bc48&lt;/b&gt; is waiting on it requesting a shared lock (S).&lt;br /&gt;
&lt;br /&gt;
(2)There is an exclusive (X) lock on index &lt;b&gt;PK_CustomerInstances&lt;/b&gt; owned by owner process &lt;b&gt;processa1bc48&lt;/b&gt; and process &lt;b&gt;processaa8d48&lt;/b&gt; is waiting on it requesting a shared lock (S).&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;So both process are holding an exclusive lock on index PK_CustomerInstances and both are requesting a shared lock on it and so we have a deadlock. Confusing right.. how can both have exclusive lock and requesting shared lock on the same index???&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Reproducing deadlock with simple query:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
This has to do with how locking and lock escalation works in SQL Server In fact I can reproduce above deadlock very easily with some simple queries:&lt;br /&gt;
&lt;br /&gt;
(1) Create a table called employee with primary key id.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
CREATE TABLE [dbo].[employee](&lt;br /&gt;
[id] [int] NOT NULL,&lt;br /&gt;
[fname] [nchar](50) NOT NULL,&lt;br /&gt;
[lname] [nchar](50) NOT NULL,&lt;br /&gt;
[created_at] [datetime] NOT NULL,&lt;br /&gt;
CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED &lt;br /&gt;
(&lt;br /&gt;
[id] ASC&lt;br /&gt;
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]&lt;br /&gt;
) ON [PRIMARY]&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
(2) Open a new sql server client session (call it session1) start a transaction and insert a record without committing it:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
BEGIN TRANSACTION&lt;br /&gt;
INSERT INTO [employee]([id],[fname],[lname])VALUES(1,&#39;john&#39;,&#39;smith&#39;)&lt;br /&gt;
GO&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
(3) Now open another new sql server client session (call it session2) start a transaction and insert a record without committing it:&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
BEGIN TRANSACTION &lt;br /&gt;
INSERT INTO [employee]([id],[fname],[lname])VALUES(2,&#39;mary&#39;,&#39;carter&#39;)&lt;br /&gt;
GO&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
(4) Now go back to client session1 and do a select *, you will notice that results are pending.&lt;br /&gt;
This is due to the fact that session2 is not yet committed and it also hold a lock on the whole&lt;br /&gt;
index of employee table.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
SELECT * FROM employee&lt;br /&gt;
GO&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
(5) Go back to client session2 and do a select *:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
SELECT * FROM employee&lt;br /&gt;
GO&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Now you will notice that either session1 or session2 will deadlock and other session will show&lt;br /&gt;
the results. This has the same issue as I was having with a complex query and for which I did enable tracing as explained earlier. The reason why deadlock is happening is because we are doing &quot;Select *&quot; and it will scan whole table. Since we have inserted rows in two different transactions without committing them, the select statement blocks in each session and one deadlocks.&lt;br /&gt;
&lt;br /&gt;
This same whole index locking is happening with the complex query for which we have enabled tracing.Somehow the select statements are trying to select rows which were inserted in a different transaction which was not yet committed.&lt;br /&gt;
&lt;br /&gt;
So far we have looked at how deadlock trace looks like in SQL Server and how to reproduce it with a simple query. In next blog I will talk about using various approaches to avoid deadlocks.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/7716167913534429936/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/09/sql-server-deadlock-transaction-process.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/7716167913534429936'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/7716167913534429936'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/09/sql-server-deadlock-transaction-process.html' title='SQL Server deadlock: Transaction (Process ID XXX) was deadlocked on lock resources with another process and has been chosen as the deadlock victim'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-4613539474719341400</id><published>2010-09-06T17:24:00.000-07:00</published><updated>2010-09-06T17:24:36.056-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="mock objects"/><category scheme="http://www.blogger.com/atom/ns#" term="Spring"/><category scheme="http://www.blogger.com/atom/ns#" term="Unit testing"/><title type='text'>Spring: Using Mock Objects For Unit Testing MVC Controllers</title><content type='html'>Spring has a &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#mock-objects-servlet&quot;&gt;nice framework&lt;/a&gt; to unit test controller for web/rest applications and it integrates nicely with Junit.Assuming that you wrote a CustomerController for handling REST requests to create a customer. A basic CustomerController looks like this:&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;b&gt;@Controller&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;b&gt;@RequestMapping&lt;/b&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #3b3df5;&quot;&gt;&lt;b&gt;&quot;/customers&quot;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt; CustomerController {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #777777;&quot;&gt;&lt;b&gt;@RequestMapping&lt;/b&gt;&lt;/span&gt;&lt;b&gt; (method = RequestMethod.&lt;/b&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;&lt;b&gt;POST&lt;/b&gt;&lt;/span&gt;&lt;b&gt;)&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; createCustomer(HttpServletRequest request,&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; HttpServletResponse response) {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  //your code here&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;}&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Now to test this controller using Spring Mock objects, the basic steps are as follows in your unit test:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step1:&lt;/b&gt; Declare that you want to run test using JUnit. For example to run test using JUnit4 you need to use &lt;a href=&quot;http://junit.sourceforge.net/javadoc/org/junit/runner/RunWith.html&quot;&gt;@RunWith&lt;/a&gt; annotation as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #777777;&quot;&gt;&lt;b&gt;@RunWith&lt;/b&gt;&lt;/span&gt;&lt;b&gt;(SpringJUnit4ClassRunner.&lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt;&lt;b&gt;)&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #3b3df5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: black;&quot;&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt; CustomerControllerTest &lt;span style=&quot;color: #961a65;&quot;&gt;extends&lt;/span&gt; TestCase {&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;@Test&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; testCreateCustomer() &lt;span style=&quot;color: #961a65;&quot;&gt;throws&lt;/span&gt; Exception {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Monaco; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;b&gt;Step2:&lt;/b&gt; Specify the spring bean configuration using &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/test/context/ContextConfiguration.html&quot;&gt;@ContextConfiguration&lt;/a&gt; so that Controller works from unit test.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #777777;&quot;&gt;@RunWith&lt;/span&gt;(SpringJUnit4ClassRunner.&lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt;)&lt;/div&gt;&lt;div style=&quot;color: #3b3df5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #777777;&quot;&gt;&lt;b&gt;@ContextConfiguration&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;b&gt;&quot;classpath:spring/customer/rest-config.xml&quot;&lt;/b&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt; CustomerControllerTest &lt;span style=&quot;color: #961a65;&quot;&gt;extends&lt;/span&gt; TestCase {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;@Test&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; testCreateCustomer() &lt;span style=&quot;color: #961a65;&quot;&gt;throws&lt;/span&gt; Exception {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Monaco; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;b&gt;Step3:&lt;/b&gt; Setup &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html&quot;&gt;DispatcherServlet&lt;/a&gt;. DispatchServlet will be used to send mock request to the controller. Setting up DispatchServlet requires implementing &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/ApplicationContextAware.html&quot;&gt;ApplicationContextAware&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #777777;&quot;&gt;@RunWith&lt;/span&gt;(SpringJUnit4ClassRunner.&lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt;)&lt;/div&gt;&lt;div style=&quot;color: #3b3df5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #777777;&quot;&gt;@ContextConfiguration&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&quot;classpath:spring/customer/rest-config.xml&quot;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt; CustomerControllerTest &lt;span style=&quot;color: #961a65;&quot;&gt;extends&lt;/span&gt; TestCase &lt;span style=&quot;color: #961a65;&quot;&gt;implements&lt;/span&gt; &lt;b&gt;ApplicationContextAware&lt;/b&gt; {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;private&lt;/b&gt;&lt;/span&gt;&lt;b&gt; Servlet &lt;/b&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;&lt;b&gt;servlet&lt;/b&gt;&lt;/span&gt;&lt;b&gt;;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;white-space: pre;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;private&lt;/b&gt;&lt;/span&gt;&lt;b&gt; ApplicationContext &lt;/b&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;&lt;b&gt;applicationContext&lt;/b&gt;&lt;/span&gt;&lt;b&gt;;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;b&gt;@SuppressWarnings&lt;/b&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #3b3df5;&quot;&gt;&lt;b&gt;&quot;serial&quot;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;@Before&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;private&lt;/b&gt;&lt;/span&gt;&lt;b&gt; &lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;void&lt;/b&gt;&lt;/span&gt;&lt;b&gt; &lt;/b&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;&lt;b&gt;init()&lt;/b&gt;&lt;/span&gt;&lt;b&gt; {&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;&lt;b&gt;servlet&lt;/b&gt;&lt;/span&gt;&lt;b&gt; = &lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/span&gt;&lt;b&gt; DispatcherServlet() {&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;   &lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;@Override&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;   &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;protected&lt;/b&gt;&lt;/span&gt;&lt;b&gt; WebApplicationContext createWebApplicationContext(&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;     &lt;/b&gt;&lt;/span&gt;&lt;b&gt;WebApplicationContext parent) &lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;throws&lt;/b&gt;&lt;/span&gt;&lt;b&gt; BeansException {&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;    &lt;/b&gt;&lt;/span&gt;&lt;b&gt;GenericWebApplicationContext genericContext = &lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/span&gt;&lt;b&gt; GenericWebApplicationContext();&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;    &lt;/b&gt;&lt;/span&gt;&lt;b&gt;genericContext.setParent(&lt;/b&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;&lt;b&gt;applicationContext&lt;/b&gt;&lt;/span&gt;&lt;b&gt;);&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;    &lt;/b&gt;&lt;/span&gt;&lt;b&gt;genericContext.refresh();&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;    &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/span&gt;&lt;b&gt; genericContext;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;   &lt;/b&gt;&lt;/span&gt;&lt;b&gt;}&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;b&gt;};&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;b&gt;}&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;@Test&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; testCreateCustomer() &lt;span style=&quot;color: #961a65;&quot;&gt;throws&lt;/span&gt; Exception {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/span&gt;&lt;b&gt; &lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;void&lt;/b&gt;&lt;/span&gt;&lt;b&gt; setApplicationContext(ApplicationContext arg0)&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;   &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;throws&lt;/b&gt;&lt;/span&gt;&lt;b&gt; BeansException {&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #032fc3; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;this&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;.&lt;/b&gt;&lt;/span&gt;&lt;b&gt;applicationContext&lt;/b&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt; = arg0;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;b&gt;}&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;b&gt;Step4:&lt;/b&gt; Finally use &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/mock/web/MockHttpServletRequest.html&quot;&gt;MockHttpServletRequest&lt;/a&gt; to build the request, send it and process the response.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777;&quot;&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: black; font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777; font-family: Monaco; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: black; font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777; font-family: Monaco; font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: black; font-family: Times;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #777777; font-family: Monaco; font-size: small;&quot;&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #777777;&quot;&gt;@RunWith&lt;/span&gt;(SpringJUnit4ClassRunner.&lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt;)&lt;/div&gt;&lt;div style=&quot;color: #3b3df5; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #777777;&quot;&gt;@ContextConfiguration&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&quot;classpath:spring/customer/rest-config.xml&quot;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;class&lt;/span&gt; CustomerControllerTest &lt;span style=&quot;color: #961a65;&quot;&gt;extends&lt;/span&gt; TestCase &lt;span style=&quot;color: #961a65;&quot;&gt;implements&lt;/span&gt; ApplicationContextAware {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;private&lt;/span&gt; Servlet &lt;span style=&quot;color: #032fc3;&quot;&gt;servlet&lt;/span&gt;;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;private&lt;/span&gt; ApplicationContext &lt;span style=&quot;color: #032fc3;&quot;&gt;applicationContext&lt;/span&gt;;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;@SuppressWarnings&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #3b3df5;&quot;&gt;&quot;serial&quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;@Before&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;text-decoration: underline;&quot;&gt;init()&lt;/span&gt; {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;servlet&lt;/span&gt; = &lt;span style=&quot;color: #961a65;&quot;&gt;new&lt;/span&gt; DispatcherServlet() {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;/span&gt;@Override&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;protected&lt;/span&gt; WebApplicationContext createWebApplicationContext(&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;WebApplicationContext parent) &lt;span style=&quot;color: #961a65;&quot;&gt;throws&lt;/span&gt; BeansException {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;GenericWebApplicationContext genericContext = &lt;span style=&quot;color: #961a65;&quot;&gt;new&lt;/span&gt; GenericWebApplicationContext();&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;genericContext.setParent(&lt;span style=&quot;color: #032fc3;&quot;&gt;applicationContext&lt;/span&gt;);&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;genericContext.refresh();&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;return&lt;/span&gt; genericContext;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;};&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #777777; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;@Test&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; testCreateCustomer() &lt;span style=&quot;color: #961a65;&quot;&gt;throws&lt;/span&gt; Exception {&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;b&gt;MockHttpServletRequest request = &lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/span&gt;&lt;b&gt; MockHttpServletRequest();&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #489075; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;// set &lt;/b&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;&lt;b&gt;uri&lt;/b&gt;&lt;/span&gt;&lt;b&gt;, request parameter, request body, omitted here&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;b&gt;MockHttpServletResponse response = &lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/span&gt;&lt;b&gt; MockHttpServletResponse();&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #489075; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;// send the request&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;b&gt;MockServletConfig servletConfig = &lt;/b&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/span&gt;&lt;b&gt; MockServletConfig();&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;&lt;b&gt;servlet&lt;/b&gt;&lt;/span&gt;&lt;b&gt;.init(servletConfig);&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #032fc3;&quot;&gt;&lt;b&gt;servlet&lt;/b&gt;&lt;/span&gt;&lt;b&gt;.service(request, response);&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #489075; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;// validate response&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;b&gt;String &lt;/b&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;&lt;b&gt;responseContent&lt;/b&gt;&lt;/span&gt;&lt;b&gt; = response.getContentAsString();&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/span&gt;&lt;b&gt; &lt;/b&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;&lt;b&gt;responseCode&lt;/b&gt;&lt;/span&gt;&lt;b&gt; = response.getStatus();&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #961a65;&quot;&gt;void&lt;/span&gt; setApplicationContext(ApplicationContext arg0)&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;throws&lt;/span&gt; BeansException {&lt;/div&gt;&lt;div style=&quot;color: #032fc3; font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #961a65;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;.&lt;/span&gt;applicationContext&lt;span style=&quot;color: black;&quot;&gt; = arg0;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 15.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font: 11.0px Monaco; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;}&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/code&gt;&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/4613539474719341400/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/09/spring-using-mock-objects-for-unit.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/4613539474719341400'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/4613539474719341400'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/09/spring-using-mock-objects-for-unit.html' title='Spring: Using Mock Objects For Unit Testing MVC Controllers'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-8989259005894451132</id><published>2010-08-21T16:25:00.000-07:00</published><updated>2010-08-21T16:25:55.458-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Advice"/><category scheme="http://www.blogger.com/atom/ns#" term="APO"/><category scheme="http://www.blogger.com/atom/ns#" term="Interceptor"/><category scheme="http://www.blogger.com/atom/ns#" term="Proxy"/><category scheme="http://www.blogger.com/atom/ns#" term="Spring"/><title type='text'>Spring: Creating Automatic Proxy And Applying Advice</title><content type='html'>Spring provides comprehensive Aspect Oriented Programming (&lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-introduction-defn&quot;&gt;AOP&lt;/a&gt;) support. Without going into details of Spring AOP, an Advice can be broadly defined as an action taken during execution of a method. &lt;br /&gt;
&lt;br /&gt;
In my previous &lt;a href=&quot;http://riteshadval.blogspot.com/2010/08/spring-using-hibernate-interceptor.html&quot;&gt;blog entry&lt;/a&gt;, I have explained hibernate interceptor which uses spring&#39;s proxy objects to intercept method execution. Similarly Spring facilitates AOP advice by creating a proxy to intercept method execution and then calling the advice logic.&lt;br /&gt;
&lt;br /&gt;
Spring provides various &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop-api.html#aop-api-advice-types&quot;&gt;advices types&lt;/a&gt; which can be applied around a method execution. Some examples are : Before Return Advice, After Return Advice, After Throwing Advice, After Finally Advice etc.&lt;br /&gt;
&lt;br /&gt;
One way to automatically intercept method execution on one or more objects and then apply an advice is to use Spring&#39;s &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/aop/framework/autoproxy/BeanNameAutoProxyCreator.html&quot;&gt;BeanNameAutoProxyCreator&lt;/a&gt; and &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop-api.html#aop-api-advice&quot;&gt;Advice API&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Following is an example where we create an AfterReturningAdvice, which is called when a target bean&#39;s method returns successfully. &lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
public class HibernateAdvice implements AfterReturningAdvice {&lt;br /&gt;
&lt;br /&gt;
 @Override&lt;br /&gt;
 public void afterReturning(Object arg0, Method arg1, Object[] arg2,&lt;br /&gt;
Object arg3) throws Throwable {&lt;br /&gt;
 }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
And here is how above advice can be applied to a set of beans. In this example all methods of all the beans, whose name ends with Dao will have above advice applied.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&amp;lt;bean class=&quot;org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator&quot;&amp;gt;&lt;br /&gt;
&amp;lt;property name=&quot;beanNames&quot; value=&quot;*Dao&quot;/&amp;gt;&lt;br /&gt;
&amp;lt;property name=&quot;&lt;b&gt;interceptorNames&lt;/b&gt;&quot;&amp;gt;&lt;br /&gt;
&amp;lt;list&amp;gt;&lt;br /&gt;
 &amp;lt;value&amp;gt;&lt;b&gt;hibernateAdvice&lt;/b&gt;&amp;lt;/value&amp;gt;&lt;br /&gt;
&amp;lt;/list&amp;gt;&lt;br /&gt;
&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;lt;/bean&amp;gt;&lt;br /&gt;
&amp;lt;bean id=&quot;&lt;b&gt;hibernateAdvice&lt;/b&gt;&quot; class=&quot;com.example.HibernateAdvice&quot;/&amp;gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Note that &lt;b&gt;interceptorNames&lt;/b&gt; can be an advice type as shown above or an &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-schema-advisors&quot;&gt;advisor&lt;/a&gt;. An advisor will allow more fine grained control on how a specific advice should be applied. (For example apply advice only on selected methods of a bean etc)&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/8989259005894451132/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/08/spring-creating-automatic-proxy-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/8989259005894451132'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/8989259005894451132'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/08/spring-creating-automatic-proxy-and.html' title='Spring: Creating Automatic Proxy And Applying Advice'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-350206426749825752</id><published>2010-08-14T11:09:00.000-07:00</published><updated>2010-08-14T11:26:44.743-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hibernate"/><category scheme="http://www.blogger.com/atom/ns#" term="Spring"/><title type='text'>Spring: Using Hibernate Interceptor</title><content type='html'>Hibernate is a powerful OR framework and works really well with Spring. In Hibernate you define your entities and queries in &lt;a href=&quot;http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql.html&quot;&gt;HQL&lt;/a&gt; (you could also have native &lt;a href=&quot;http://docs.jboss.org/hibernate/stable/core/reference/en/html/querysql.html&quot;&gt;SQL&lt;/a&gt;). Most of the heavy lifting is done by Hibernate but  sometimes you may need to inspect and/or manipulate properties of a persistent object before it is saved, updated, deleted or loaded. For example you may want to dynamically change the SQL generated by Hibernate.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Hibernate Interceptor:&lt;/b&gt;&lt;br /&gt;
Enter &lt;a href=&quot;http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/Interceptor.html&quot;&gt;Hibernate Iterceptor&lt;/a&gt;. Hibernate Iterceptor provides an API which one can implement to intercept various operation like save and delete of an entity.&lt;br /&gt;
&lt;br /&gt;
Typically it is better to extend &lt;a href=&quot;http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/EmptyInterceptor.html&quot;&gt;EmptyInterceptor&lt;/a&gt; and override the methods which you want to implement.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
public class HibernateInterceptor extends EmptyInterceptor {&lt;br /&gt;
&amp;#160;&amp;#160; /* (non-Javadoc)&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;* @see org.hibernate.EmptyInterceptor#onPrepareStatement(java.lang.String)&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;*&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;@Override&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;public String onPrepareStatement(String sql) {&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;br /&gt;
}&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Spring Hibernate Interceptor:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
In Spring you can easily use easily use Hibernate Interceptor by using &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html
&quot;&gt;AnnotationSessionFactoryBean&lt;/a&gt; or super class &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/LocalSessionFactoryBean.html&quot;&gt;LocalSessionFactoryBean&lt;/a&gt; which defines setEntityInterceptor(org.hibernate.Interceptor entityInterceptor) API. Here is an example of how to use it in Spring xml configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;!-- Define an Interceptor --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;bean id=&quot;myInterceptor&quot; class=&quot;com.example.HibernateInterceptor&quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/b&gt;&amp;lt;bean id=&quot;mySessionFactory&quot; class=&quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&quot; &amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;property name=&quot;dataSource&quot; ref=&quot;myDataSource&quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;!-- Refer to Interceptor Here --&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;property name=&quot;entityInterceptor&quot;&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;ref bean=&quot;myInterceptor&quot;/&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;/property&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/b&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;property name=&quot;mappingResources&quot;&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;value&amp;gt;example/persistence/hibernate/customer-dao-named-queries.hbm.xml&amp;lt;/value&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;/property&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;property name=&quot;annotatedClasses&quot;&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;!-- fully qualified model class names --&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;value&amp;gt;com.example.persistence.model.Customer&amp;lt;/value&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;/property&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;property name=&quot;hibernateProperties&quot;&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;props&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;prop key=&quot;hibernate.show_sql&quot;&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;/props&amp;gt;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;lt;/bean&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/350206426749825752/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/08/spring-using-hibernate-interceptor.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/350206426749825752'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/350206426749825752'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/08/spring-using-hibernate-interceptor.html' title='Spring: Using Hibernate Interceptor'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-4317357141647293823</id><published>2010-08-05T20:50:00.000-07:00</published><updated>2010-08-14T11:41:32.174-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Form Multipart"/><category scheme="http://www.blogger.com/atom/ns#" term="Spring"/><title type='text'>Spring: Enabling Multipart Form Upload Http Request Processing</title><content type='html'>In &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/&quot;&gt;Spring&lt;/a&gt; you can write http controller using &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller&quot;&gt;@Controller&lt;/a&gt; annotation at class level to process http requests. The methods defined in controller can take HttpServletRequest as one of the arguments.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
@Controller&lt;br /&gt;
@RequestMapping(&quot;/customers&quot;)&lt;br /&gt;
class CustomerController {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;@RequestMapping(method = RequestMethod.POST)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;public void postCustomer(HttpServletRequest request, HttpServletResponse response) throws Exception {&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//processing code goes here&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
}&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Handling Form Multipart Request:&lt;br /&gt;
&lt;br /&gt;
If you like to handle form multipart requests, spring provides an easy way to handle this by using &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/multipart/MultipartHttpServletRequest.html&quot;&gt;MultipartHttpServletRequest&lt;/a&gt;. This allows you to access multipart files available in the request.&lt;br /&gt;
&lt;br /&gt;
To use MultipartHttpServletRequest, you can specify &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/multipart/commons/CommonsMultipartResolver.html&quot;&gt;CommonsMultipartResolver&lt;/a&gt; bean in your spring xml configuration file.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Times;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;code&gt; &amp;lt;bean class=&quot;&lt;b&gt;org.springframework.web.multipart.commons.CommonsMultipartResolver&lt;/b&gt;&quot; id=&quot;multipartResolver&quot;&amp;gt;&lt;br /&gt;
&amp;lt;/bean&amp;gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Once you specify you can change the signature of your controller methods to use &lt;br /&gt;
MultipartHttpServletRequest in place of HttpservletRequest:&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
@Controller&lt;br /&gt;
@RequestMapping(&quot;/customers&quot;)&lt;br /&gt;
class CustomerController {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;@RequestMapping(method = RequestMethod.POST)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;public void postCustomer(&lt;b&gt;MultipartHttpServletRequest&lt;/b&gt; request, HttpServletResponse response) throws Exception {&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//processing code goes here&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
}&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/4317357141647293823/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/08/spring-enabling-multipartform-http.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/4317357141647293823'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/4317357141647293823'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/08/spring-enabling-multipartform-http.html' title='Spring: Enabling Multipart Form Upload Http Request Processing'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-1517625239276156293</id><published>2010-07-30T20:00:00.000-07:00</published><updated>2010-07-31T13:45:29.546-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="date precision loss"/><category scheme="http://www.blogger.com/atom/ns#" term="JDBC date rounding"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server datetime rounding"/><title type='text'>MS SQL Server datetime precision rounding effect</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Microsoft SQL server has &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;datetime&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt; data type to support date with time. JDBC &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;timestamp&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt; sql type maps to datetime type in SQL server. JDBC timestamp extends &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;java.util.Date&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt; and adds nanosecond precision.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium; line-height: 28px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;java.util.Date&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt; has millisecond precision where as SQL server datetime has a precision of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007 seconds.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium; line-height: 28px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 28px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;The issue with mapping JDBC timestamp to SQL server datetime is that there is a loss of precision when it gets stored to SQL server. So far example if your date in java is 01/01/98 23:59:59.999 it will get stored as 1998-01-02 00:00:00.000.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font: 19.0px Times; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 23.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Here is the table from SQL server documentation which shows rounding effect of dates.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;code&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cols=&quot;2&quot; frame=&quot;box&quot; rules=&quot;all&quot; style=&quot;width: 571px;&quot;&gt;&lt;tbody&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;th class=&quot;label&quot; width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;Java Date&lt;/span&gt;&lt;/th&gt;&lt;th class=&quot;label&quot; width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;SQL Server datetime rounded example&lt;/span&gt;&lt;/th&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.999&lt;/span&gt;&lt;/td&gt;&lt;td width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;1998-01-02 00:00:00.000&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.995,&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.996, &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.997, or&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.998&lt;/span&gt;&lt;/td&gt;&lt;td width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;1998-01-01 23:59:59.997&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.992, &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.993, &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.994&lt;/span&gt;&lt;/td&gt;&lt;td width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;1998-01-01 23:59:59.993&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.990 or&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;01/01/98 23:59:59.991&lt;/span&gt;&lt;/td&gt;&lt;td width=&quot;50%&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;1998-01-01 23:59:59.990&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font: 19.0px Times; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 23.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;So a java date which is persisted in SQL server datetime field may have millisecond value which is &amp;nbsp;slightly different than the millisecond value of the original date. Care must me taken when comparing a date which is persisted in SQL server with original date. It is best to ignore millisecond when doing date comparison. For more details see SQL Server &lt;/span&gt;&lt;/span&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/aa258277%28SQL.80%29.aspx&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;datetime rounding effect&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt; documentation.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #55208b;&quot;&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com/&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;Promote your blog&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/1517625239276156293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/07/ms-sql-server-datetime-precision.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/1517625239276156293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/1517625239276156293'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/07/ms-sql-server-datetime-precision.html' title='MS SQL Server datetime precision rounding effect'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3144090126613685403.post-5008584350200921853</id><published>2010-07-25T18:24:00.000-07:00</published><updated>2010-07-30T12:33:53.536-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="alias context path"/><category scheme="http://www.blogger.com/atom/ns#" term="custom context path"/><category scheme="http://www.blogger.com/atom/ns#" term="multiple context path"/><category scheme="http://www.blogger.com/atom/ns#" term="tomcat 5 6"/><category scheme="http://www.blogger.com/atom/ns#" term="user defined context path"/><title type='text'>Tomcat: How to add user defined context path to access an application</title><content type='html'>&lt;div style=&quot;color: #232323; font: 15.0px Helvetica; line-height: 20.0px; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;In tomcat (5.x and above), the context path of an application defaults to application name where application war file is exploded. For example if application is named &lt;b&gt;examples&lt;/b&gt; (ex: located in &lt;span style=&quot;direction: ltr; unicode-bidi: embed;&quot;&gt;webapps/examples) then by default url to access is: &lt;a href=&quot;http://localhost:8080/api/examples/&quot;&gt;&lt;span style=&quot;color: #053df5; direction: ltr; text-decoration: underline; unicode-bidi: embed;&quot;&gt;http://localhost:8080/&lt;/span&gt;&lt;span style=&quot;color: #053df5; direction: ltr; unicode-bidi: embed;&quot;&gt;&lt;b&gt;examples/&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;color: #366388; direction: ltr; unicode-bidi: embed;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black; direction: ltr; unicode-bidi: embed;&quot;&gt;(assuming you have deployed to localhost)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: #366388; font: 15.0px Helvetica; line-height: 20.0px; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 18.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #232323; font: 15.0px Helvetica; line-height: 20.0px; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 18.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Specifying one or more additional context path to map to the same application:&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: #232323; font: 15.0px Helvetica; line-height: 20.0px; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 18.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #232323; font: 15.0px Helvetica; line-height: 20.0px; margin: 0.0px 0.0px 0.0px 0.0px;&quot;&gt;To specify additional path to access an application, we need to add&amp;nbsp;&amp;lt;Context&amp;gt; element under&amp;nbsp;conf/server.xml like below:&lt;/div&gt;&lt;code&gt;&lt;br /&gt;
&amp;lt;Context &lt;b&gt;path=&quot;/api/examples&quot; docBase=&quot;examples&quot;&lt;/b&gt;&amp;gt;&lt;br /&gt;
&lt;br/&gt;&lt;br /&gt;
&amp;lt;!-- Default set of monitored resources --&amp;gt;&lt;br /&gt;
&lt;br/&gt;&lt;br /&gt;
&amp;lt;WatchedResource&amp;gt;WEB-INF/web.xml&amp;lt;/WatchedResource&amp;gt;&lt;br /&gt;
&lt;br/&gt;&lt;br /&gt;
&amp;lt;/Context&amp;gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;div style=&quot;color: #232323; font: 15.0px Helvetica; line-height: 20.0px; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 18.0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: black; font-family: &#39;Lucida Grande&#39;; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px; white-space: pre-wrap;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #232323; font-family: Helvetica; font-size: 15px; white-space: normal;&quot;&gt;With above we defined a new path &lt;b&gt;&quot;/api/examples&quot;&lt;/b&gt; to access the same examples application&amp;nbsp;as specified in docBase. So now examples application can also be accessed as:&amp;nbsp;&lt;a href=&quot;http://localhost:8080/api/examples/&quot;&gt;&lt;span style=&quot;color: #053df5; text-decoration: underline;&quot;&gt;http://localhost:8080/&lt;/span&gt;&lt;span style=&quot;color: #053df5;&quot;&gt;&lt;b&gt;api/examples/&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: #366388; font: 15.0px Helvetica; line-height: 20.0px; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 18.0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #232323; font: 15.0px Helvetica; line-height: 20.0px; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 18.0px;&quot;&gt;For more information about context configuration check out &lt;a href=&quot;http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Context_Parameters&quot;&gt;&lt;span style=&quot;color: #053df5;&quot;&gt;tomcat context&lt;/span&gt;&lt;/a&gt; documentation.&lt;/div&gt;&lt;a href=&quot;http://feedshark.brainbliss.com/&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;a href=&quot;http://feedshark.brainbliss.com&quot; id=&quot;BD46221593&quot;&gt;Promote your blog&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://riteshadval.blogspot.com/feeds/5008584350200921853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://riteshadval.blogspot.com/2010/07/tomcat-how-to-add-user-defined-context.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/5008584350200921853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3144090126613685403/posts/default/5008584350200921853'/><link rel='alternate' type='text/html' href='http://riteshadval.blogspot.com/2010/07/tomcat-how-to-add-user-defined-context.html' title='Tomcat: How to add user defined context path to access an application'/><author><name>Ritesh Adval</name><uri>http://www.blogger.com/profile/10076379427411435269</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>