<?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: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-5724163953511764578</id><updated>2012-01-11T13:07:05.342-08:00</updated><category term='Practise.ExtJs'/><category term='JEE ShoppingCart Example'/><category term='Inventions'/><category term='JPA'/><category term='JBoss'/><category term='Cloud Computing'/><category term='C'/><category term='Machine Structures'/><category term='Probability'/><category term='Math'/><category term='Javascript and ExtJS'/><category term='JEE'/><category term='Design Patterns'/><category term='Web'/><title type='text'>My Bread Basket</title><subtitle type='html'>My thoughts, thoughts running out of me when I am in sleep, relaxing or hopping from tree to tree. The blog also contains others thoughts on the technology!!!!</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>92</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-6640510457503836085</id><published>2010-05-22T04:24:00.000-07:00</published><updated>2010-05-22T04:49:13.581-07:00</updated><title type='text'>All you need to know about java annotations - brief</title><content type='html'>&lt;span style="font-weight: bold;"&gt;1) What are annotations?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   Annotations are tags (@) that you insert into your source code which acts as information to an external tool that process them.&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;Example: &lt;/span&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;   @Log(logLevel="Debug")&lt;br /&gt;   public void someAPI(){...}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;@Log is an annotation here which informs the external tool which processes @Log to start logging in debug mode when someAPI method is executed.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2) Why do we need Annotations?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   Some of the uses of annotations could be&lt;br /&gt;#) Provide information to the tools so that the tools can generate auxiliary files such as deployment descriptors.&lt;br /&gt;#) Provide information to the tools so that the tools can automatically generate code for testing, logging, transaction, security etc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;3) How of Annotations?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#) Defining Annotations - &lt;/span&gt;&lt;br /&gt;   Considering the above log example, this is how @Log can be defined&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;import java.lang.annotations.*;&lt;br /&gt;&lt;br /&gt;@Target (ElementType.METHOD)&lt;br /&gt;@Retention (RetentionPolicy.RUNTIME)&lt;br /&gt;public @interface Log&lt;br /&gt;{&lt;br /&gt;   String logLevel() default "info";&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The &lt;span style="font-weight: bold;"&gt;@Target&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;@Retention&lt;/span&gt; are annotations provided to another annotations and hence called meta annotations.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;@Target&lt;/span&gt; annotation helps in specifying the target element on which @Log annotation can be used. In this example the METHOD is used as the target which indicates that @Log is applicable to only methods in a class.&lt;br /&gt;&lt;br /&gt;You can add annotations to the following items&lt;br /&gt;# Packages&lt;br /&gt;# Classes (including enums)&lt;br /&gt;# Interfaces (including annotation interfaces)&lt;br /&gt;# Methods&lt;br /&gt;# Constructors&lt;br /&gt;# Instance Fields&lt;br /&gt;# Local Variables&lt;br /&gt;# Parameter Variables&lt;br /&gt;&lt;br /&gt;Example: -&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;public @interface BugReport&lt;br /&gt;{&lt;br /&gt;   enum Status {FIXED, NOTABUG};&lt;br /&gt;   boolean showStopper() default false;&lt;br /&gt;   String assignedTo() default "[None]";&lt;br /&gt;   Status status() default Status.UNCONFIRMED;&lt;br /&gt;   Reference ref() default @reference;&lt;br /&gt;   String[] reportedBy();&lt;br /&gt;   Class testCase() default Void.class&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and annotation looks like this&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;@BugReport(showStoppre=true, assignedTo="xyz", testCase=TestClass.class,&lt;br /&gt;          status=BugReport.Status.CONFIRMED, ...)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;@Retention&lt;/span&gt; meta annotation specifies how long an annotation is retained. You can specify atmost one of the following values.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;SOURCE &lt;/span&gt;- Annotatoins are not included in class file.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;CLASS &lt;/span&gt;- Annotations are included in class file, but the virtual machine need not load them.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;RUNTIME &lt;/span&gt;- Annotations are included in class file and loaded by the virtual machine. They are available throu the refelection API.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;4) Annotation Processing&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Annotation can be processed at three levels&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#) In the source code [SOURCE]&lt;/span&gt;&lt;br /&gt;   You process annotation at the source level if you want to produce other source files, such as XML descriptor files, stubs/skeletons, remote interfaces etc. For source level annotations u need to parse java source file. Java provides an annotation process tool (&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/apt/GettingStarted.html"&gt;apt&lt;/a&gt;) which processes annotations in source files and feeds them to annotation processor that you supply.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#) In bytecode files [CLASS]&lt;/span&gt;&lt;br /&gt;   If you want to process annotations at the  bytecode level then you need to analyze class files. &lt;a href="http://jakarta.apache.org/bcel/"&gt;BCEL&lt;/a&gt; is the library which helps in processing class files and modifying class files. This type of processing can be used to modify class files to add byte codes for logging etc.&lt;br /&gt;You can run a bytecode engineering tool in two modes.You can simply write a tool that&lt;br /&gt;reads a class file, processes the annotations, injects the bytecodes, and writes the modified class file. Alternatively, you can defer the bytecode engineering until load time, when the class loader loads the class.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#) At Runtime, in the virtual machine [RUNTIME]&lt;/span&gt;&lt;br /&gt;   This is the easiest way of processing an annotation which can be done by reflection api's at runtime.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-6640510457503836085?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/6640510457503836085/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2010/05/all-you-need-to-know-about-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6640510457503836085'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6640510457503836085'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2010/05/all-you-need-to-know-about-java.html' title='All you need to know about java annotations - brief'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-2957235511760661932</id><published>2010-03-13T01:42:00.000-08:00</published><updated>2010-03-13T01:44:05.062-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Two Entities to One Table mapping</title><content type='html'>The picture below shows how two entities can be mapped to one single table -&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_-1g-DDEOJz0/S5teRsPWrlI/AAAAAAAAAko/5db9-uiLFWQ/s1600-h/TwoEntitiesAndOneTable.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 346px;" src="http://2.bp.blogspot.com/_-1g-DDEOJz0/S5teRsPWrlI/AAAAAAAAAko/5db9-uiLFWQ/s400/TwoEntitiesAndOneTable.PNG" alt="" id="BLOGGER_PHOTO_ID_5448051832261094994" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-2957235511760661932?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/2957235511760661932/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2010/03/two-entities-to-one-table-mapping.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2957235511760661932'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2957235511760661932'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2010/03/two-entities-to-one-table-mapping.html' title='Two Entities to One Table mapping'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_-1g-DDEOJz0/S5teRsPWrlI/AAAAAAAAAko/5db9-uiLFWQ/s72-c/TwoEntitiesAndOneTable.PNG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-7503198056087860897</id><published>2010-03-13T01:11:00.000-08:00</published><updated>2010-03-13T01:12:26.905-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Two Tables to One Entity mapping</title><content type='html'>The picture below shows how two table can be mapped to one single entity -&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_-1g-DDEOJz0/S5tW4tAOM5I/AAAAAAAAAkg/tSQcigCseOg/s1600-h/TwoTableOneEntity.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 346px;" src="http://2.bp.blogspot.com/_-1g-DDEOJz0/S5tW4tAOM5I/AAAAAAAAAkg/tSQcigCseOg/s400/TwoTableOneEntity.PNG" alt="" id="BLOGGER_PHOTO_ID_5448043706387936146" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-7503198056087860897?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/7503198056087860897/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2010/03/two-tables-to-one-entity-mapping.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7503198056087860897'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7503198056087860897'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2010/03/two-tables-to-one-entity-mapping.html' title='Two Tables to One Entity mapping'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_-1g-DDEOJz0/S5tW4tAOM5I/AAAAAAAAAkg/tSQcigCseOg/s72-c/TwoTableOneEntity.PNG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-6408217428334426984</id><published>2010-03-13T00:31:00.000-08:00</published><updated>2010-03-13T00:35:08.730-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Entity Relationship Cascade</title><content type='html'>The table below shows the Entity Manager API used for cascading when you want to save entity relationship&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/S5tN7CF3a6I/AAAAAAAAAkY/qf2J_7FKvpY/s1600-h/Cascade.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 580px; height: 249px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/S5tN7CF3a6I/AAAAAAAAAkY/qf2J_7FKvpY/s400/Cascade.PNG" alt="" id="BLOGGER_PHOTO_ID_5448033850803841954" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-6408217428334426984?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/6408217428334426984/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2010/03/entity-relationship-cascade.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6408217428334426984'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6408217428334426984'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2010/03/entity-relationship-cascade.html' title='Entity Relationship Cascade'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_-1g-DDEOJz0/S5tN7CF3a6I/AAAAAAAAAkY/qf2J_7FKvpY/s72-c/Cascade.PNG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-4977844237932977161</id><published>2010-03-12T23:43:00.000-08:00</published><updated>2010-03-12T23:58:18.208-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Entity Manager Merge() Vs Persist()</title><content type='html'>Like every one knows Persist is used to create a new entry and merge is normally used to update an already existing entry but the confusion begins when we realize that merge can also be used to create a new entry as well.&lt;br /&gt;&lt;br /&gt;So the question is why do we have persist when merge can do both update and create entries?&lt;br /&gt;&lt;br /&gt;Here it goes -&lt;br /&gt;&lt;br /&gt;1) &lt;span style="font-weight: bold;"&gt;Performance and Memory&lt;/span&gt;&lt;br /&gt;           Persist will just dump the entity into the database but merge will have to first figure out if this entry exists in the database for it to make out if it is a create or a update scenario. Secondly merge will copy the passed object and will save the copied object into the database, so if the entity relationship is complex this copy procedure is more time consuming and a lil memory intensive.&lt;br /&gt;&lt;br /&gt;2) &lt;span style="font-weight: bold;"&gt;RBAC&lt;/span&gt;&lt;br /&gt;          Some roles can only update few entries and may not be allowed to create new entries or viceversa, so to have a seggregation we have seperate apis for persist and merge.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-4977844237932977161?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/4977844237932977161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2010/03/entity-manager-merge-vs-persist.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/4977844237932977161'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/4977844237932977161'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2010/03/entity-manager-merge-vs-persist.html' title='Entity Manager Merge() Vs Persist()'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-42902191118944900</id><published>2010-02-28T09:11:00.000-08:00</published><updated>2010-02-28T09:29:07.967-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JEE ShoppingCart Example'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Flush, Persist and Merge</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Flush&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There are two flush modes -&lt;br /&gt;1) Auto&lt;br /&gt;2) Commit&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Auto flush&lt;/span&gt; is the default which flushes [persists] the entities in the persistence context at the transaction commit time and also on every query executed with in a transaction. Exception to this is find() because if we want to find an entity which is modified then the find will return back the entity that is present in the persistence context. But if we execute a query then query doesnt return the whole entity but returns some fields as a list and this is the reason that thepersistence context is flushed during query execution.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Commit mode&lt;/span&gt; will flush the persistence context when the transaction commits.&lt;br /&gt;&lt;br /&gt;We can force flush by calling the flush method on the entity manager to flush the persistence context.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Persist and Merge&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Merge can persist but persist cannot merge.&lt;br /&gt;When an entity is persisted and before even the transaction is not commited, we can get the primary key if the primary key is autogenerated using table strategy.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-42902191118944900?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/42902191118944900/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2010/02/flush-persist-and-merge.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/42902191118944900'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/42902191118944900'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2010/02/flush-persist-and-merge.html' title='Flush, Persist and Merge'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-6569957491620820097</id><published>2010-02-28T06:53:00.000-08:00</published><updated>2010-02-28T07:37:01.774-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JEE ShoppingCart Example'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Persistence Context and Entity Manager</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Persistence Context&lt;/span&gt; is nothing but a bag that holds entities. Entity Manager contains persistence context and manages [creates, updates, deletes] the entities that are part of that persistence context. This is shown in the following figure&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/S4qGgDw4lAI/AAAAAAAAAjc/UQi7eR0t-Ss/s1600-h/PC1.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 191px; height: 247px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/S4qGgDw4lAI/AAAAAAAAAjc/UQi7eR0t-Ss/s400/PC1.PNG" alt="" id="BLOGGER_PHOTO_ID_5443310984954942466" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The life cycle of persistence context depends on whether it is transaction-scoped or extended persistence context.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Transaction-Scoped Persistence Context -&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Transaction begins when the bean method is invoked and it ends when the method returns or completes. Similarly transaction-scoped persistence context follows the transaction and is created when the transaction begins and ends after the transaction commits or rolls back.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_-1g-DDEOJz0/S4qKImwbKbI/AAAAAAAAAjk/WAu754Kvzdw/s1600-h/TSPC.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 357px; height: 192px;" src="http://2.bp.blogspot.com/_-1g-DDEOJz0/S4qKImwbKbI/AAAAAAAAAjk/WAu754Kvzdw/s400/TSPC.PNG" alt="" id="BLOGGER_PHOTO_ID_5443314980077906354" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Extended Persistence Context -&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This type of Persistence Context is independent of transaction i.e., the creation or destruction of persistence context is not dependent on transaction begin or transaction end as in the case of transaction-scoped persistence context. Persistence Context will be created when the statefull session bean is created and it is destroyed when the stateful session bean is destroyed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_-1g-DDEOJz0/S4qLSyPIQ1I/AAAAAAAAAjs/bF1Sy4tP7XA/s1600-h/ExtendedPC.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 357px; height: 192px;" src="http://4.bp.blogspot.com/_-1g-DDEOJz0/S4qLSyPIQ1I/AAAAAAAAAjs/bF1Sy4tP7XA/s400/ExtendedPC.PNG" alt="" id="BLOGGER_PHOTO_ID_5443316254469800786" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Note:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There can be many instances of Entity Manager referring to the same single instance of persistence context if all these entity managers are part of the same transaction i.e., entity manager instances part of different ejb's invoked by an ejb which starts and ends the transaction. Please refer the picture below -&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_-1g-DDEOJz0/S4qNX1Akk9I/AAAAAAAAAj0/5_yWcPzBjy4/s1600-h/PC2.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 240px;" src="http://4.bp.blogspot.com/_-1g-DDEOJz0/S4qNX1Akk9I/AAAAAAAAAj0/5_yWcPzBjy4/s400/PC2.PNG" alt="" id="BLOGGER_PHOTO_ID_5443318540136649682" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-6569957491620820097?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/6569957491620820097/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2010/02/persistence-context-and-entity-manager.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6569957491620820097'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6569957491620820097'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2010/02/persistence-context-and-entity-manager.html' title='Persistence Context and Entity Manager'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_-1g-DDEOJz0/S4qGgDw4lAI/AAAAAAAAAjc/UQi7eR0t-Ss/s72-c/PC1.PNG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-7069556224153238723</id><published>2009-11-25T09:33:00.000-08:00</published><updated>2009-11-25T09:46:02.875-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>Reusable component template as given in the extjs.com documentation</title><content type='html'>&lt;h1&gt; &lt;span class="mw-headline"&gt; A Re-usable Template&lt;/span&gt;&lt;/h1&gt; &lt;p&gt;The following is a template (based on the template posted by Jozef Sakalos in mjlecomte's &lt;a linkindex="45" href="http://extjs.com/forum/showthread.php?t=28085" class="external text" title="http://extjs.com/forum/showthread.php?t=28085" rel="nofollow"&gt;forum post&lt;/a&gt;) that you can use as a starting-point when extending Ext.Component classes &lt;/p&gt; &lt;pre name="source" class="javascript"&gt;MyComponent = Ext.&lt;span class="me1"&gt;extend&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;Ext.&lt;span class="me1"&gt;some&lt;/span&gt;.&lt;span class="me1"&gt;component&lt;/span&gt;, &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// Prototype Defaults, can be overridden by user's config object&lt;/span&gt;&lt;br /&gt;propA: &lt;span class="nu0"&gt;1&lt;/span&gt;,&lt;br /&gt;&lt;br /&gt;initComponent: &lt;span class="kw2"&gt;function&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// Called during component initialization&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// Config object has already been applied to 'this' so properties can &lt;/span&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// be overriden here or new properties (e.g. items, tools, buttons) &lt;/span&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// can be added, eg:&lt;/span&gt;&lt;br /&gt;   Ext.&lt;span class="me1"&gt;apply&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw1"&gt;this&lt;/span&gt;, &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;       propA: &lt;span class="nu0"&gt;3&lt;/span&gt;&lt;br /&gt;   &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// Before parent code&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// Call parent (required)&lt;/span&gt;&lt;br /&gt;   MyComponent.&lt;span class="me1"&gt;superclass&lt;/span&gt;.&lt;span class="me1"&gt;initComponent&lt;/span&gt;.&lt;span class="me1"&gt;apply&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw1"&gt;this&lt;/span&gt;, arguments&lt;span class="br0"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// After parent code&lt;/span&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// e.g. install event handlers on rendered component&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;,&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;// Override other inherited methods &lt;/span&gt;&lt;br /&gt;onRender: &lt;span class="kw2"&gt;function&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// Before parent code&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// Call parent (required)&lt;/span&gt;&lt;br /&gt;   MyComponent.&lt;span class="me1"&gt;superclass&lt;/span&gt;.&lt;span class="me1"&gt;onRender&lt;/span&gt;.&lt;span class="me1"&gt;apply&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw1"&gt;this&lt;/span&gt;, arguments&lt;span class="br0"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span class="co1"&gt;// After parent code&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;// register xtype to allow for lazy initialization&lt;/span&gt;&lt;br /&gt;Ext.&lt;span class="me1"&gt;reg&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;'mycomponentxtype'&lt;/span&gt;, MyComponent&lt;span class="br0"&gt;)&lt;/span&gt;;&lt;/pre&gt; &lt;p&gt;As an enlightening example, if you used the above class via either of the following: &lt;/p&gt; &lt;pre name="source" class="javascript"&gt;&lt;span class="kw2"&gt;var&lt;/span&gt; myComponent = &lt;span class="kw2"&gt;new&lt;/span&gt; MyComponent&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;propA: &lt;span class="nu0"&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span class="co1"&gt;// Or lazily:&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;..&lt;br /&gt;&lt;span class="me1"&gt;items&lt;/span&gt;: &lt;span class="br0"&gt;{&lt;/span&gt;xtype: &lt;span class="st0"&gt;'mycomponentxtype'&lt;/span&gt;, propA: &lt;span class="nu0"&gt;2&lt;/span&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;..&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;then the property &lt;b&gt;propA&lt;/b&gt; would have been set three times, to the values 1, 2, and 3 consecutively. If you follow the code (and the comments) through you will see that value starts as 1 (Prototype default) and then is set to 2 by the user's config object, and finally is overridden in initComponent and set to 3. Hopefully this example gives you a bit of insight into the order in which the code is executed (not in the order that you read it from start to finish!). &lt;/p&gt;&lt;p&gt;Because components nest other components, here's a quick way to grab the top-most component. &lt;/p&gt; &lt;pre class="source source-javascript"&gt;&lt;span class="kw2"&gt;var&lt;/span&gt; topCmp = &lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw2"&gt;function&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;o&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;span class="kw1"&gt;while&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;o.&lt;span class="me1"&gt;ownerCt&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;o=o.&lt;span class="me1"&gt;ownerCt&lt;/span&gt;&lt;span class="br0"&gt;}&lt;/span&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; o;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw1"&gt;this&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;;&lt;/pre&gt; &lt;a name="Closing_Remarks" id="Closing_Remarks"&gt;&lt;/a&gt;&lt;h1&gt; &lt;span class="mw-headline"&gt;More clearer and specific example&lt;/span&gt;&lt;/h1&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;Application.PersonnelGrid = Ext.extend(Ext.grid.GridPanel, {&lt;br /&gt;     border:false&lt;br /&gt;    ,initComponent:function() {&lt;br /&gt;        Ext.apply(this, {&lt;br /&gt;             store:new Ext.data.Store({...})&lt;br /&gt;            ,columns:[{...}, {...}]&lt;br /&gt;            ,plugins:[...]&lt;br /&gt;            ,viewConfig:{forceFit:true}&lt;br /&gt;            ,tbar:[...]&lt;br /&gt;            ,bbar:[...]&lt;br /&gt;        });&lt;br /&gt;&lt;br /&gt;        Application.PersonnelGrid.superclass.initComponent.apply(this, arguments);&lt;br /&gt;    } // eo function initComponent&lt;br /&gt;&lt;br /&gt;    ,onRender:function() {&lt;br /&gt;        this.store.load();&lt;br /&gt;&lt;br /&gt;        Application.PersonnelGrid.superclass.onRender.apply(this, arguments);&lt;br /&gt;    } // eo function onRender&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;Ext.reg('personnelgrid', Application.PersonnelGrid);&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-7069556224153238723?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/7069556224153238723/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/reusable-component-template-as-given-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7069556224153238723'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7069556224153238723'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/reusable-component-template-as-given-in.html' title='Reusable component template as given in the extjs.com documentation'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-5623697253269354631</id><published>2009-11-24T10:32:00.000-08:00</published><updated>2009-11-24T10:42:45.795-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Optimistic Concurrency Control by enabling versioning in Hibernate</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Note:&lt;br /&gt;The following article is an excerpt from the great book - &lt;span style="color: rgb(51, 204, 0);"&gt;"Java Persistence with Hibernate"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Choosing an isolation level&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Developers (ourselves included) are often unsure what transaction isolation level&lt;br /&gt;to use in a production application. Too great a degree of isolation harms scalability&lt;br /&gt;of a highly concurrent application. Insufficient isolation may cause subtle,&lt;br /&gt;unreproduceable bugs in an application that you’ll never discover until the system&lt;br /&gt;is working under heavy load.&lt;br /&gt;&lt;br /&gt;Note that we refer to optimistic locking (with versioning) in the following explanation,&lt;br /&gt;a concept explained later in this chapter. You may want to skip this section&lt;br /&gt;and come back when it’s time to make the decision for an isolation level in your&lt;br /&gt;application. Picking the correct isolation level is, after all, highly dependent on&lt;br /&gt;your particular scenario. Read the following discussion as recommendations, not&lt;br /&gt;carved in stone.&lt;br /&gt;&lt;br /&gt;Hibernate tries hard to be as transparent as possible regarding transactional&lt;br /&gt;semantics of the database. Nevertheless, caching and optimistic locking affect&lt;br /&gt;these semantics. What is a sensible database isolation level to choose in a Hibernate&lt;br /&gt;application?&lt;br /&gt;&lt;br /&gt;First, eliminate the read uncommitted isolation level. It’s extremely dangerous to&lt;br /&gt;use one transaction’s uncommitted changes in a different transaction. The rollback&lt;br /&gt;or failure of one transaction will affect other concurrent transactions. Rollback&lt;br /&gt;of the first transaction could bring other transactions down with it, or&lt;br /&gt;perhaps even cause them to leave the database in an incorrect state. It’s even possible&lt;br /&gt;that changes made by a transaction that ends up being rolled back could be&lt;br /&gt;committed anyway, because they could be read and then propagated by another&lt;br /&gt;transaction that is successful!&lt;br /&gt;&lt;br /&gt;Secondly, most applications don’t need serializable isolation (phantom reads&lt;br /&gt;aren’t usually problematic), and this isolation level tends to scale poorly. Few&lt;br /&gt;existing applications use serializable isolation in production, but rather rely on&lt;br /&gt;pessimistic locks (see next sections) that effectively force a serialized execution of&lt;br /&gt;operations in certain situations.&lt;br /&gt;&lt;br /&gt;This leaves you a choice between read committed and repeatable read. Let’s first&lt;br /&gt;consider repeatable read. This isolation level eliminates the possibility that one&lt;br /&gt;transaction can overwrite changes made by another concurrent transaction (the&lt;br /&gt;second lost updates problem) if all data access is performed in a single atomic&lt;br /&gt;database transaction. A read lock held by a transaction prevents any write lock a&lt;br /&gt;concurrent transaction may wish to obtain. This is an important issue, but&lt;br /&gt;enabling repeatable read isn’t the only way to resolve it.&lt;br /&gt;&lt;br /&gt;Let’s assume you’re using versioned data, something that Hibernate can do for&lt;br /&gt;you automatically. The combination of the (mandatory) persistence context&lt;br /&gt;cache and versioning already gives you most of the nice features of repeatable&lt;br /&gt;read isolation. In particular, versioning prevents the second lost updates problem,&lt;br /&gt;and the persistence context cache also ensures that the state of the persistent&lt;br /&gt;instances loaded by one transaction is isolated from changes made by other transactions.&lt;br /&gt;So, read-committed isolation for all database transactions is acceptable if&lt;br /&gt;you use versioned data.&lt;br /&gt;&lt;br /&gt;Repeatable read provides more reproducibility for query result sets (only for&lt;br /&gt;the duration of the database transaction); but because phantom reads are still&lt;br /&gt;possible, that doesn’t appear to have much value. You can obtain a repeatable-&lt;br /&gt;read guarantee explicitly in Hibernate for a particular transaction and piece&lt;br /&gt;of data (with a pessimistic lock).&lt;br /&gt;Setting the transaction isolation level allows you to choose a good default locking&lt;br /&gt;strategy for all your database transactions. How do you set the isolation level?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Setting an isolation level&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Every JDBC connection to a database is in the default isolation level of the DBMS—&lt;br /&gt;usually read committed or repeatable read. You can change this default in the&lt;br /&gt;DBMS configuration. You may also set the transaction isolation for JDBC connections&lt;br /&gt;on the application side, with a Hibernate configuration option:&lt;br /&gt;hibernate.connection.isolation = 4&lt;br /&gt;Hibernate sets this isolation level on every JDBC connection obtained from a&lt;br /&gt;connection pool before starting a transaction. The sensible values for this option&lt;br /&gt;are as follows (you may also find them as constants in java.sql.Connection):&lt;br /&gt;■ 1—Read uncommitted isolation&lt;br /&gt;■ 2—Read committed isolation&lt;br /&gt;■ 4—Repeatable read isolation&lt;br /&gt;■ 8—Serializable isolation&lt;br /&gt;&lt;br /&gt;Note that Hibernate never changes the isolation level of connections obtained&lt;br /&gt;from an application server-provided database connection in a managed environment!&lt;br /&gt;You can change the default isolation using the configuration of your application&lt;br /&gt;server. (The same is true if you use a stand-alone JTA implementation.)&lt;br /&gt;As you can see, setting the isolation level is a global option that affects all connections&lt;br /&gt;and transactions. From time to time, it’s useful to specify a more restrictive&lt;br /&gt;lock for a particular transaction. Hibernate and Java Persistence rely on&lt;br /&gt;optimistic concurrency control, and both allow you to obtain additional locking&lt;br /&gt;guarantees with version checking and pessimistic locking.&lt;br /&gt;&lt;br /&gt;An optimistic approach always assumes that everything will be OK and that conflicting&lt;br /&gt;data modifications are rare. Optimistic concurrency control raises an&lt;br /&gt;error only at the end of a unit of work, when data is written. Multiuser applications&lt;br /&gt;usually default to optimistic concurrency control and database connections&lt;br /&gt;with a read-committed isolation level. Additional isolation guarantees are&lt;br /&gt;obtained only when appropriate; for example, when a repeatable read is required.&lt;br /&gt;This approach guarantees the best performance and scalability.&lt;br /&gt;Understanding the optimistic strategy&lt;br /&gt;To understand optimistic concurrency control, imagine that two transactions read&lt;br /&gt;a particular object from the database, and both modify it. Thanks to the read-committed&lt;br /&gt;isolation level of the database connection, neither transaction will run into any dirty reads. However, reads are still nonrepeatable, and updates may also be&lt;br /&gt;lost. This is a problem you’ll face when you think about conversations, which are&lt;br /&gt;atomic transactions from the point of view of your users. Look at figure 10.6.&lt;br /&gt;Let’s assume that two users select the same piece of data at the same time. The&lt;br /&gt;user in conversation A submits changes first, and the conversation ends with a successful&lt;br /&gt;commit of the second transaction. Some time later (maybe only a second),&lt;br /&gt;the user in conversation B submits changes. This second transaction also commits&lt;br /&gt;successfully. The changes made in conversation A have been lost, and (potentially&lt;br /&gt;worse) modifications of data committed in conversation B may have been based&lt;br /&gt;on stale information.&lt;br /&gt;&lt;br /&gt;You have three choices for how to deal with lost updates in these second transactions&lt;br /&gt;in the conversations:&lt;br /&gt;■ Last commit wins—Both transactions commit successfully, and the second&lt;br /&gt;commit overwrites the changes of the first. No error message is shown.&lt;br /&gt;■ First commit wins—The transaction of conversation A is committed, and the&lt;br /&gt;user committing the transaction in conversation B gets an error message.&lt;br /&gt;The user must restart the conversation by retrieving fresh data and go&lt;br /&gt;through all steps of the conversation again with nonstale data.&lt;br /&gt;■ Merge conflicting updates—The first modification is committed, and the transaction&lt;br /&gt;in conversation B aborts with an error message when it’s committed.&lt;br /&gt;The user of the failed conversation B may however apply changes selectively,&lt;br /&gt;instead of going through all the work in the conversation again.&lt;br /&gt;&lt;br /&gt;If you don’t enable optimistic concurrency control, and by default it isn’t enabled,&lt;br /&gt;your application runs with a last commit wins strategy. In practice, this issue of lost&lt;br /&gt;updates is frustrating for application users, because they may see all their work&lt;br /&gt;lost without an error message.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_-1g-DDEOJz0/SwwoKO2ZdEI/AAAAAAAAAic/GjlmPb3eL8Q/s1600/SecondLostUpdate.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 153px;" src="http://4.bp.blogspot.com/_-1g-DDEOJz0/SwwoKO2ZdEI/AAAAAAAAAic/GjlmPb3eL8Q/s400/SecondLostUpdate.JPG" alt="" id="BLOGGER_PHOTO_ID_5407741408815182914" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Figure 10.6&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Conversation B overwrites&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;changes made by conversation A.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Obviously, first commit wins is much more attractive. If the application user of&lt;br /&gt;conversation B commits, he gets an error message that reads, Somebody already committed&lt;br /&gt;modifications to the data you’re about to commit. You’ve been working with stale&lt;br /&gt;data. Please restart the conversation with fresh data. It’s your responsibility to design&lt;br /&gt;and write the application to produce this error message and to direct the user to&lt;br /&gt;the beginning of the conversation. Hibernate and Java Persistence help you with&lt;br /&gt;automatic optimistic locking, so that you get an exception whenever a transaction&lt;br /&gt;tries to commit an object that has a conflicting updated state in the database.&lt;br /&gt;Merge conflicting changes, is a variation of first commit wins. Instead of displaying&lt;br /&gt;an error message that forces the user to go back all the way, you offer a dialog that&lt;br /&gt;allows the user to merge conflicting changes manually. This is the best strategy&lt;br /&gt;because no work is lost and application users are less frustrated by optimistic concurrency&lt;br /&gt;failures. However, providing a dialog to merge changes is much more&lt;br /&gt;time-consuming for you as a developer than showing an error message and forcing&lt;br /&gt;the user to repeat all the work. We’ll leave it up to you whether you want to use&lt;br /&gt;this strategy.&lt;br /&gt;&lt;br /&gt;Optimistic concurrency control can be implemented many ways. Hibernate&lt;br /&gt;works with automatic versioning.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Enabling versioning in Hibernate&lt;/span&gt;&lt;br /&gt;Hibernate provides automatic versioning. Each entity instance has a version,&lt;br /&gt;which can be a number or a timestamp. Hibernate increments an object’s version&lt;br /&gt;when it’s modified, compares versions automatically, and throws an exception if a&lt;br /&gt;conflict is detected. Consequently, you add this version property to all your persistent&lt;br /&gt;entity classes to enable optimistic locking:&lt;br /&gt;public class Item {&lt;br /&gt;...&lt;br /&gt;private int version;&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;You can also add a getter method; however, version numbers must not be modified&lt;br /&gt;by the application. The &amp;lt;version&gt; property mapping in XML must be placed&lt;br /&gt;immediately after the identifier property mapping:&lt;br /&gt;&amp;lt;class name="Item" table="ITEM"&gt;&lt;br /&gt;&amp;lt;id .../&gt;&lt;br /&gt;&amp;lt;version name="version" access="field" column="OBJ_VERSION"/&gt;&lt;br /&gt;...&lt;br /&gt;&amp;lt;/class&gt;&lt;br /&gt;&lt;br /&gt;The version number is just a counter value—it doesn’t have any useful semantic&lt;br /&gt;value. The additional column on the entity table is used by your Hibernate application.&lt;br /&gt;Keep in mind that all other applications that access the same database can&lt;br /&gt;(and probably should) also implement optimistic versioning and utilize the same&lt;br /&gt;version column. Sometimes a timestamp is preferred (or exists):&lt;br /&gt;public class Item {&lt;br /&gt;...&lt;br /&gt;private Date lastUpdated;&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;&amp;lt;class name="Item" table="ITEM"&gt;&lt;br /&gt;&amp;lt;id .../&gt;&lt;br /&gt;&amp;lt;timestamp name="lastUpdated"&lt;br /&gt;access="field"&lt;br /&gt;column="LAST_UPDATED"/&gt;&lt;br /&gt;...&lt;br /&gt;&amp;lt;/class&gt;&lt;br /&gt;In theory, a timestamp is slightly less safe, because two concurrent transactions&lt;br /&gt;may both load and update the same item in the same millisecond; in practice,&lt;br /&gt;this won’t occur because a JVM usually doesn’t have millisecond accuracy (you&lt;br /&gt;should check your JVM and operating system documentation for the guaranteed&lt;br /&gt;precision).&lt;br /&gt;&lt;br /&gt;Furthermore, retrieving the current time from the JVM isn’t necessarily safe in&lt;br /&gt;a clustered environment, where nodes may not be time synchronized. You can&lt;br /&gt;switch to retrieval of the current time from the database machine with the&lt;br /&gt;source="db" attribute on the &lt;timestamp&gt; mapping. Not all Hibernate SQL dialects&lt;br /&gt;support this (check the source of your configured dialect), and there is&lt;br /&gt;always the overhead of hitting the database for every increment.&lt;br /&gt;We recommend that new projects rely on versioning with version numbers, not&lt;br /&gt;timestamps.&lt;br /&gt;&lt;br /&gt;Optimistic locking with versioning is enabled as soon as you add a &lt;version&gt;&lt;br /&gt;or a &amp;lt;timestamp&gt; property to a persistent class mapping. There is no other switch.&lt;br /&gt;How does Hibernate use the version to detect a conflict?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Automatic management of versions&lt;/span&gt;&lt;br /&gt;Every DML operation that involves the now versioned Item objects includes a version&lt;br /&gt;check. For example, assume that in a unit of work you load an Item from the&lt;br /&gt;database with version 1. You then modify one of its value-typed properties, such as&lt;br /&gt;the price of the Item. When the persistence context is flushed, Hibernate detects&lt;br /&gt;that modification and increments the version of the Item to 2. It then executes&lt;br /&gt;the SQL UPDATE to make this modification permanent in the database:&lt;br /&gt;update ITEM set INITIAL_PRICE='12.99', OBJ_VERSION=2&lt;br /&gt;where ITEM_ID=123 and OBJ_VERSION=1&lt;br /&gt;If another concurrent unit of work updated and committed the same row, the&lt;br /&gt;OBJ_VERSION column no longer contains the value 1, and the row isn’t updated.&lt;br /&gt;Hibernate checks the row count for this statement as returned by the JDBC&lt;br /&gt;driver—which in this case is the number of rows updated, zero—and throws a&lt;br /&gt;StaleObjectStateException. The state that was present when you loaded the&lt;br /&gt;Item is no longer present in the database at flush-time; hence, you’re working&lt;br /&gt;with stale data and have to notify the application user. You can catch this exception&lt;br /&gt;and display an error message or a dialog that helps the user restart a conversation&lt;br /&gt;with the application.&lt;br /&gt;What modifications trigger the increment of an entity’s version? Hibernate&lt;br /&gt;increments the version number (or the timestamp) whenever an entity instance is&lt;br /&gt;dirty. This includes all dirty value-typed properties of the entity, no matter if&lt;br /&gt;they’re single-valued, components, or collections. Think about the relationship&lt;br /&gt;between User and BillingDetails, a one-to-many entity association: If a Credit-&lt;br /&gt;Card is modified, the version of the related User isn’t incremented. If you add or&lt;br /&gt;remove a CreditCard (or BankAccount) from the collection of billing details, the&lt;br /&gt;version of the User is incremented.&lt;br /&gt;If you want to disable automatic increment for a particular value-typed property&lt;br /&gt;or collection, map it with the optimistic-lock="false" attribute. The&lt;br /&gt;inverse attribute makes no difference here. Even the version of an owner of an&lt;br /&gt;inverse collection is updated if an element is added or removed from the&lt;br /&gt;inverse collection.&lt;br /&gt;As you can see, Hibernate makes it incredibly easy to manage versions for optimistic&lt;br /&gt;concurrency control. If you’re working with a legacy database schema or&lt;br /&gt;existing Java classes, it may be impossible to introduce a version or timestamp&lt;br /&gt;property and column. Hibernate has an alternative strategy for you.&lt;br /&gt;&lt;/version&gt;&lt;/timestamp&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-5623697253269354631?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/5623697253269354631/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/optimistic-concurrency-control-by.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5623697253269354631'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5623697253269354631'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/optimistic-concurrency-control-by.html' title='Optimistic Concurrency Control by enabling versioning in Hibernate'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_-1g-DDEOJz0/SwwoKO2ZdEI/AAAAAAAAAic/GjlmPb3eL8Q/s72-c/SecondLostUpdate.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-128676958070596229</id><published>2009-11-24T07:31:00.000-08:00</published><updated>2009-11-24T07:48:24.313-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Transaction Isolation Levels</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Dirty Read&lt;/span&gt;&lt;br /&gt;A dirty read occurs if a one transaction reads changes made by another transaction&lt;br /&gt;that has not yet been committed. This is dangerous, because the changes made by&lt;br /&gt;the other transaction may later be rolled back, and invalid data may be written by&lt;br /&gt;the first transaction, as shown in the figure&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_-1g-DDEOJz0/Swv9eC8MQ8I/AAAAAAAAAiE/efpWZwhwZVQ/s1600/DirtyRead.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 322px; height: 400px;" src="http://2.bp.blogspot.com/_-1g-DDEOJz0/Swv9eC8MQ8I/AAAAAAAAAiE/efpWZwhwZVQ/s400/DirtyRead.JPG" alt="" id="BLOGGER_PHOTO_ID_5407694470215648194" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Non-repeatable Read&lt;/span&gt;&lt;br /&gt;An unrepeatable read occurs if a transaction reads a row twice and reads different&lt;br /&gt;state each time. For example, another transaction may have written to the row&lt;br /&gt;and committed between the two reads as shown in the figure&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/Swv9etT9bEI/AAAAAAAAAiM/ZXf24y2GNS8/s1600/NonRepeatedRead.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 322px; height: 400px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/Swv9etT9bEI/AAAAAAAAAiM/ZXf24y2GNS8/s400/NonRepeatedRead.JPG" alt="" id="BLOGGER_PHOTO_ID_5407694481589627970" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Phantom Read&lt;/span&gt;&lt;br /&gt;A phantom read is said to occur when a transaction executes a query twice, and&lt;br /&gt;the second result set includes rows that weren’t visible in the first result set or rows&lt;br /&gt;that have been deleted. (It need not necessarily be exactly the same query.) This&lt;br /&gt;situation is caused by another transaction inserting or deleting rows between the&lt;br /&gt;execution of the two queries as shown in the figure&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/Swv9e80EjXI/AAAAAAAAAiU/Oh31lUJcih8/s1600/RepeatableRead.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 393px; height: 400px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/Swv9e80EjXI/AAAAAAAAAiU/Oh31lUJcih8/s400/RepeatableRead.JPG" alt="" id="BLOGGER_PHOTO_ID_5407694485750844786" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Lost update&lt;/span&gt;&lt;br /&gt;A lost update occurs if two transactions both update a row and then the second&lt;br /&gt;transaction aborts, causing both changes to be lost. This occurs in systems that&lt;br /&gt;don’t implement locking.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Transaction Isolation Levels&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Read Uncommitted&lt;/span&gt;&lt;br /&gt;A system that permits dirty reads but not lost updates is said to operate in&lt;br /&gt;read uncommitted isolation. One transaction may not write to a row if another&lt;br /&gt;uncommitted transaction has already written to it. Any transaction may read&lt;br /&gt;any row, however.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Read Committed&lt;/span&gt;&lt;br /&gt;A system that permits unrepeatable reads but not dirty reads is said to implement&lt;br /&gt;read committed transaction isolation. This may be achieved by using&lt;br /&gt;shared read locks and exclusive write locks. Reading transactions don’t&lt;br /&gt;block other transactions from accessing a row. However, an uncommitted&lt;br /&gt;writing transaction blocks all other transactions from accessing the row.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Repeatable Read&lt;/span&gt;&lt;br /&gt;A system operating in repeatable read isolation mode permits neither unrepeatable&lt;br /&gt;reads nor dirty reads. Phantom reads may occur. Reading transactions&lt;br /&gt;block writing transactions (but not other reading transactions), and&lt;br /&gt;writing transactions block all other transactions.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Serializable&lt;/span&gt;&lt;br /&gt;Serializable provides the strictest transaction isolation. This isolation level&lt;br /&gt;emulates serial transaction execution, as if transactions were executed one&lt;br /&gt;after another, serially, rather than concurrently. Serializability may not be&lt;br /&gt;implemented using only row-level locks. There must instead be some other&lt;br /&gt;mechanism that prevents a newly inserted row from becoming visible to a&lt;br /&gt;transaction that has already executed a query that would return the row.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-128676958070596229?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/128676958070596229/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/transaction-isolation-levels.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/128676958070596229'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/128676958070596229'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/transaction-isolation-levels.html' title='Transaction Isolation Levels'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_-1g-DDEOJz0/Swv9eC8MQ8I/AAAAAAAAAiE/efpWZwhwZVQ/s72-c/DirtyRead.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-8209573533805613834</id><published>2009-11-22T09:21:00.000-08:00</published><updated>2009-11-22T09:37:00.997-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JEE ShoppingCart Example'/><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>Managing User Preferences - Personalize the User Space</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Managing User Preferences&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;User does many things once he logins into the application to personalize his space.&lt;br /&gt;Some of the many things will be setting the window sizes, positioning the windows, hiding or showing widgets etc etc.&lt;br /&gt;&lt;br /&gt;Here I will briefly go through how to manage user preferences in ext js.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt; Initialize the state provider&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt; Ext.state.Manager.setProvider(new HttpProvider());&lt;/span&gt;&lt;br /&gt;In this case we are initializing the manager with the Http state provider persisting the state to the database.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Initialize the scheduler&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Scheduler in this case will schedule the task for persisting the state to the database.&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;delayedTask = new Ext.util.DelayedTask(this.persistState, this);&lt;/span&gt;&lt;br /&gt;Here DelayedTask is used as the scheduler.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Initialize the state manager&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Initialize the state manager by loading any state persisted before by reading it from the database via HttpProvider.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Start the scheduler&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;delayedTask.delay(1000);&lt;/span&gt;&lt;br /&gt;Here we are asking the scheduler to poll the state manager once in 1000 milli seconds to know if there are any changes in the state to be persisted.&lt;br /&gt;The callback api, in this case, the persistState api will be called if there are any changes to the state.&lt;br /&gt;&lt;br /&gt;persistState api may look like this,&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;function persistState() {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;if(!stateDirty) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;    delayedTask.delay(1000); &lt;span style="color: rgb(51, 51, 255);"&gt;// If state not dirty delay the scheduler for 1000 milliseconds more    &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;delayedTask.cancel(); &lt;span style="color: rgb(51, 51, 255);"&gt;// stop the scheduler and submit the state to the server asking it to persist to the database.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;submitState();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Override the set method of the provider&lt;/span&gt;&lt;br /&gt;Set method of the provider is used to set the state [user preference like window size, x &amp;amp; y position etc] to the state manager.&lt;br /&gt;&lt;br /&gt;Override the set method of the Provider to set the stateDirty flag to true and start the scheduler which was cancelled when the state was submitted to the server.&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;stateDirty = true; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;delayedTask.delay(1000);&lt;br /&gt;superclass.set();&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-8209573533805613834?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/8209573533805613834/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/managing-user-preferences-personalize.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8209573533805613834'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8209573533805613834'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/managing-user-preferences-personalize.html' title='Managing User Preferences - Personalize the User Space'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-3616300868532716881</id><published>2009-11-15T06:08:00.000-08:00</published><updated>2009-11-22T09:38:35.116-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JEE'/><category scheme='http://www.blogger.com/atom/ns#' term='JEE ShoppingCart Example'/><title type='text'>Shopping Cart: Project set up &amp; Login Screen</title><content type='html'>&lt;span style="font-weight: bold;"&gt;1) Create new project&lt;/span&gt;&lt;br /&gt;Project is the collection of modules.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2) Create Modules&lt;/span&gt;&lt;br /&gt;Module is the collection of facets.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPN77Ls-I/AAAAAAAAAhs/BMRBL0fF4qo/s1600-h/Project.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 232px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPN77Ls-I/AAAAAAAAAhs/BMRBL0fF4qo/s400/Project.JPG" alt="" id="BLOGGER_PHOTO_ID_5404336284943234018" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;3) There are lots of facets but 4 facets are quiet important to make a note of&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Web, EJB, JPA and JEE facets.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPN2yjZoI/AAAAAAAAAhk/GRvvdnDfo7w/s1600-h/Modules%26Facets.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 294px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPN2yjZoI/AAAAAAAAAhk/GRvvdnDfo7w/s400/Modules%26Facets.JPG" alt="" id="BLOGGER_PHOTO_ID_5404336283564861058" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;4) Write a web.xml for your application in the web facet.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_-1g-DDEOJz0/SwAPW1pIk6I/AAAAAAAAAh8/9hZTy90O4wk/s1600-h/Web.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 232px;" src="http://2.bp.blogspot.com/_-1g-DDEOJz0/SwAPW1pIk6I/AAAAAAAAAh8/9hZTy90O4wk/s400/Web.JPG" alt="" id="BLOGGER_PHOTO_ID_5404336437875741602" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;5) Configure an authenticator valve in context.xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_-1g-DDEOJz0/SwAPODNIp9I/AAAAAAAAAh0/VxALP_ICfnc/s1600-h/Valve.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 232px;" src="http://3.bp.blogspot.com/_-1g-DDEOJz0/SwAPODNIp9I/AAAAAAAAAh0/VxALP_ICfnc/s400/Valve.JPG" alt="" id="BLOGGER_PHOTO_ID_5404336286897579986" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;6) Write a redirect servlet&lt;/span&gt;&lt;br /&gt;&lt;pre name="code" class="java"&gt;&lt;br /&gt;package com.shoppingcart.security.login;&lt;br /&gt;&lt;br /&gt;import javax.servlet.http.HttpServlet;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.RequestDispatcher;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.util.Enumeration;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class RedirectServlet extends HttpServlet&lt;br /&gt;{&lt;br /&gt;  protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException&lt;br /&gt;  {&lt;br /&gt;      final String svlpath = httpServletRequest.getServletPath();&lt;br /&gt;      Enumeration en = httpServletRequest.getAttributeNames();&lt;br /&gt;      String actualReqPathKey = "javax.servlet.forward.servlet_path";&lt;br /&gt;      String actualReqPath = (String)httpServletRequest.getAttribute(actualReqPathKey);&lt;br /&gt;&lt;br /&gt;      String pageName = actualReqPath;&lt;br /&gt;&lt;br /&gt;      if ("/unsecured/login".equals(svlpath)) {&lt;br /&gt;          if("/index.jsp".equals(actualReqPath)) {&lt;br /&gt;              pageName = "/Login.jsp";&lt;br /&gt;          }&lt;br /&gt;      }&lt;br /&gt;      else {&lt;br /&gt;          throw new ServletException("RedirectServlet: operation '" + svlpath + "' not supported!");&lt;br /&gt;      }&lt;br /&gt;      redirectToLogin(httpServletRequest, httpServletResponse, pageName);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  private void redirectToLogin(HttpServletRequest request, HttpServletResponse response, String pageName) throws IOException, ServletException&lt;br /&gt;  {&lt;br /&gt;      RequestDispatcher dispatcher = this.getServletContext().getContext("/shoppingcart").getRequestDispatcher(pageName);&lt;br /&gt;      if (dispatcher != null) {&lt;br /&gt;          response.setContentType("text/html");&lt;br /&gt;          dispatcher.include(request, response);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;7) Mention about the module in application.xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAO6COe6kI/AAAAAAAAAhM/vz6v4HbdCZM/s1600-h/Application.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 232px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAO6COe6kI/AAAAAAAAAhM/vz6v4HbdCZM/s400/Application.JPG" alt="" id="BLOGGER_PHOTO_ID_5404335943037413954" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;8) Configure JBoss from the ide.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPNNL-lVI/AAAAAAAAAhU/3tbbKS0HtIA/s1600-h/JBoss1.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 308px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPNNL-lVI/AAAAAAAAAhU/3tbbKS0HtIA/s400/JBoss1.JPG" alt="" id="BLOGGER_PHOTO_ID_5404336272397210962" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPNsDMAXI/AAAAAAAAAhc/HGzIZPSOV9w/s1600-h/JBoss2.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 308px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPNsDMAXI/AAAAAAAAAhc/HGzIZPSOV9w/s400/JBoss2.JPG" alt="" id="BLOGGER_PHOTO_ID_5404336280681841010" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;9) Compile the application, deploy and run jboss.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;How does it all work&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When the user points the browser to &lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;localhost:8080/shoppingcart &lt;/span&gt;for the first time, the jboss server figures out from the &lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;web.xml&lt;/span&gt; configuration  that this resource cannot be accessed publicly by everyone and that it needs to be secured and only allowed roles can access it. To authenticate the jboss server will use the &lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;FormAuthenticator &lt;/span&gt;which is configured as the valve in &lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;context.xml&lt;/span&gt;. The FormAuthenticator will interpret the request and changes the url to the one mentioned in web.xml,  in this case it is &lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;/unsecured/login&lt;/span&gt; and redirects to &lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;RedirectServlet&lt;/span&gt;. The RedirectServlet will dispatch the request to &lt;span style="font-weight: bold; color: rgb(0, 153, 0);"&gt;Login.jsp&lt;/span&gt; and hence the Login.jsp is sent back to the browser.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-3616300868532716881?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/3616300868532716881/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/shopping-cart-project-set-up-login.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3616300868532716881'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3616300868532716881'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/11/shopping-cart-project-set-up-login.html' title='Shopping Cart: Project set up &amp; Login Screen'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_-1g-DDEOJz0/SwAPN77Ls-I/AAAAAAAAAhs/BMRBL0fF4qo/s72-c/Project.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-8038310669767068830</id><published>2009-10-31T11:04:00.000-07:00</published><updated>2009-10-31T11:14:26.973-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='Probability'/><title type='text'>Probability 6</title><content type='html'>&lt;span style="color: rgb(51, 204, 0); font-weight: bold;"&gt;Mixing multiple probabilities&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Given a box of 10 coins out of which 9 coins are fair and one coin has both sides heads, what is the probability of getting all heads on picking a coin from the bag and flipping it 5 times.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There are two outcomes possible if we pick a coin from the bag.&lt;br /&gt;The first outcome would be that we pick a normal coin and its probability would be 9/10 --- (1).&lt;br /&gt;The second outcome would be that we pick a coin having both sided heads, whose probability is 1/10 --- (2).&lt;br /&gt;&lt;br /&gt;Now after this,&lt;br /&gt;Given a normal coin what is the probability of getting all 5 heads is, as we know it to be (1/2)*(1/2)*(1/2)*(1/2)*(1/2) = 1/32 --- (3).&lt;br /&gt;&lt;br /&gt;Given a both sided heads coin, the probability of getting all 5 heads is 1 --- (4).&lt;br /&gt;&lt;br /&gt;From (1), (2), (3) &amp;amp; (4) we get the&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;P(getting all 5 heads) = (P(picking the normal coin)*P(getting all heads provided its normal coin))+(P(Picking an unfair coin)*P(getting all heads provided its unfair coin))&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;= (1/32)*(9/10) + 1*(1/10) = 9/320 + 1/10 = &lt;span style="font-weight: bold; color: rgb(255, 0, 0);"&gt;41/320&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-8038310669767068830?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/8038310669767068830/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-6.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8038310669767068830'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8038310669767068830'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-6.html' title='Probability 6'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-3121171894637606436</id><published>2009-10-31T10:18:00.000-07:00</published><updated>2009-10-31T10:27:02.516-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='Probability'/><title type='text'>Probability 5</title><content type='html'>What is the probability of getting a 7 when two dice is thrown.&lt;br /&gt;&lt;br /&gt;The total number of outcomes throwing 2 dice would be 36 as shown in the figure below.&lt;br /&gt;As you can see in the figure, the number of times we get 7 is 6.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_-1g-DDEOJz0/SuxzTHxXezI/AAAAAAAAAhE/jpgd9El-TIE/s1600-h/ProbDice.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 215px;" src="http://2.bp.blogspot.com/_-1g-DDEOJz0/SuxzTHxXezI/AAAAAAAAAhE/jpgd9El-TIE/s400/ProbDice.JPG" alt="" id="BLOGGER_PHOTO_ID_5398816825651198770" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Therefore, the P(7) = 6/36 = 1/6.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-3121171894637606436?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/3121171894637606436/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-5.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3121171894637606436'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3121171894637606436'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-5.html' title='Probability 5'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_-1g-DDEOJz0/SuxzTHxXezI/AAAAAAAAAhE/jpgd9El-TIE/s72-c/ProbDice.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-3379576420127158357</id><published>2009-10-28T10:45:00.000-07:00</published><updated>2009-10-28T10:52:27.854-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='Probability'/><title type='text'>Probability 4</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Example:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;---------&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0); font-weight: bold;"&gt;The probability of me making a shot in basketball is 80%, I have 3 shots and we need 1 pt to win the match. What is the probability of we winning the match?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;P(we winning) = P(me making atleast one shot) = 1 - P(me making no shots at all)&lt;br /&gt;&lt;br /&gt;P(me making no shots at all) = 0.2 * 0.2 * 0.2 = 0.008 = 0.8%&lt;br /&gt;&lt;br /&gt;P(we winning) = 1-0.008 = 99.2%&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-3379576420127158357?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/3379576420127158357/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-4.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3379576420127158357'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3379576420127158357'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-4.html' title='Probability 4'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-7589983652331623459</id><published>2009-10-27T10:37:00.000-07:00</published><updated>2009-10-27T10:40:58.651-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='Probability'/><title type='text'>Probability 3</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Arrangements [Permutations] meeting Probability.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;1) Find the probability of getting only one heads in 5 flips&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Outcomes are&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;TTTT&lt;span style="color: rgb(255, 0, 0);"&gt;H &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;TTT&lt;span style="color: rgb(255, 0, 0);"&gt;H&lt;/span&gt;T&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;TT&lt;span style="color: rgb(255, 0, 0);"&gt;H&lt;/span&gt;TT&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;T&lt;span style="color: rgb(255, 0, 0);"&gt;H&lt;/span&gt;TTT&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;H&lt;/span&gt;TTTT&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The probability of occurrence of each of these outcomes is 1/32.&lt;br /&gt;Since there are 5 such arrangements the total probability becomes 5/32.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0); font-weight: bold;"&gt;2) Find the probability of not getting exactly one heads in 5 flips&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is exactly equal to all the outcomes except the above which results to 1-5/32 = 27/32&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-7589983652331623459?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/7589983652331623459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7589983652331623459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7589983652331623459'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-3.html' title='Probability 3'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-2539932904447515210</id><published>2009-10-26T11:21:00.000-07:00</published><updated>2009-10-27T10:48:04.140-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='Probability'/><title type='text'>Probability 2</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Probability Tree&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Probability Tree is a very good tool in finding the probability of an occurrence of an event provided -&lt;br /&gt;1) The outcomes or circumstances of an event are very small in number&lt;br /&gt;2) The experiment should not be repeated large number of times&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Note:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0); font-weight: bold;"&gt;The depth of the tree is the number of times the experiment is repeated.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 51); font-weight: bold;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Example:&lt;/span&gt; Flipping coins 5 times.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0); font-weight: bold;"&gt;The width of the tree is the count of all possible outcomes of an event.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 51); font-weight: bold;"&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Example:&lt;/span&gt; H &amp;amp; T are the 2 outcome of flipping the coin.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Lets take an example and solve the probability using Probability Tree&lt;br /&gt;&lt;br /&gt;Find the probability of two heads occuring on two consecutive flips of coins, i.e., P(HH)&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_-1g-DDEOJz0/SuXpz4ElPwI/AAAAAAAAAgc/6OuIiYN036g/s1600-h/ProbabilityTree.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 274px;" src="http://4.bp.blogspot.com/_-1g-DDEOJz0/SuXpz4ElPwI/AAAAAAAAAgc/6OuIiYN036g/s400/ProbabilityTree.JPG" alt="" id="BLOGGER_PHOTO_ID_5396976805907611394" border="0" /&gt;&lt;/a&gt;From the above probability tree the total out comes or circumstances of flipping coin twice is 4. Out of the 4 outcomes, the total number of times we get HH is 1, so the P(HH) = 1/4.&lt;br /&gt;&lt;br /&gt;Another way to look at it is the probability of first flip leading to heads is 1/2, and this remains the same even during the second flip because both heads and tails are equally probable and they are not mutually exclusive so it leads to 1/2 * 1/2 = 1/4.&lt;br /&gt;&lt;br /&gt;Note we multiply the probabilities because the events are not mutually exclusive.&lt;br /&gt;&lt;br /&gt;Similary P(1H, 1T) = 2/4 = 1/2&lt;br /&gt;&lt;br /&gt;P(HHHHH) = 1/2*1/2*1/2*1/2*1/2 = 1/32&lt;br /&gt;&lt;br /&gt;P(Out of 7 flips, heads should not come even once) = p(TTTTTTT) = 1/128&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-2539932904447515210?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/2539932904447515210/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2539932904447515210'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2539932904447515210'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-2.html' title='Probability 2'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_-1g-DDEOJz0/SuXpz4ElPwI/AAAAAAAAAgc/6OuIiYN036g/s72-c/ProbabilityTree.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-1274057105886571185</id><published>2009-10-25T11:15:00.000-07:00</published><updated>2009-10-27T10:45:16.763-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='Probability'/><title type='text'>Probability 1</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Probability&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;-------------&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Two ways of looking at probability are:&lt;br /&gt;&lt;br /&gt;1) &lt;span style="color: rgb(51, 204, 0); font-weight: bold;"&gt;Repeated experiments possible [Frequency Test]&lt;/span&gt;&lt;br /&gt;  Perform an experiment repeatedly and identify wht percent of times the experiment has&lt;br /&gt;  behaved the way u want it to behave.&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;Example&lt;/span&gt;: Flipping a coin hundred times to know the probability of occurrence of heads.&lt;br /&gt;&lt;br /&gt;2) &lt;span style="color: rgb(51, 204, 0); font-weight: bold;"&gt;Repeated experiments not possible&lt;/span&gt;&lt;br /&gt;  Based on the data that you have, how strongly you believe that the experiment will behave&lt;br /&gt;  the way you want it to behave.&lt;br /&gt;  &lt;span style="font-weight: bold;"&gt;Example&lt;/span&gt;: What are the chances that its going to rain today.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Definition of Probability&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;-----------------------------&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;P(x) =  number of events in which x is true&lt;br /&gt;           ------------------------------------&lt;br /&gt;           total number of events            or all possible outcomes of an event or all possible circumstances of an event.&lt;br /&gt;&lt;br /&gt;In case of flipping a coin, the total number of circumstances or outcome of flipping a coin is 2, it can be either heads or tails.&lt;br /&gt;  &lt;br /&gt;assuming that the occurence of all the events are equally probable, i.e no single event should occur more number of times than other events.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Examples:&lt;br /&gt;---------&lt;br /&gt;&lt;br /&gt;P(Occurence of Head on flipping a coin) = 1 / 2 = 50%&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-1274057105886571185?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/1274057105886571185/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/1274057105886571185'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/1274057105886571185'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/probability-1.html' title='Probability 1'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-297372584350550777</id><published>2009-10-18T04:27:00.000-07:00</published><updated>2009-10-18T04:28:30.153-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JBoss'/><title type='text'>Determining the master node in a JBoss Cluster</title><content type='html'>&lt;h2&gt;&lt;br /&gt;&lt;/h2&gt;&lt;h2&gt;Thanks to the original author and the &lt;a href="http://javabeans.asia/2008/05/08/jboss_clustering_hasingleton_service.html"&gt;source&lt;/a&gt;&lt;br /&gt;&lt;/h2&gt;&lt;h2&gt;How to determine which node in the cluster is the master node?&lt;/h2&gt;     Have you ever dealt with clustered singleton service? How to determine which cluster node is the master? Well, if I am the current node, I can simply ask whether I am the master or not. But what if I already know that the current node is not the master, and I want to determine which node among other nodes in the cluster is the master?&lt;br /&gt;&lt;br /&gt;First I would like to give brief summary about &lt;em&gt;HASingleton&lt;/em&gt; service (HA stands for High Availability).&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Summary:&lt;/strong&gt;&lt;br /&gt;HASingleton service, is a service that is deployed on every node in a cluster, but runs only on one node, while the other nodes remain passive. The node that the service runs on, is the master node.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;How does JBoss selects the master node? &lt;/strong&gt;&lt;br /&gt;Well the first node in a cluster will become master node. If existing master node will leave the cluster as a result of a shutdown for example, another node is selected as master from the remaining nodes.&lt;br /&gt;&lt;br /&gt;Master node can control which tasks will get executed, and how many times. HASingletons also have the ability to share a memory state across clustered partition. Something like caching ...&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br /&gt;Lets assume that I have a service bean that extends from &lt;em&gt;HASingletonSupport&lt;/em&gt; class. &lt;em&gt;HASingletonSupport &lt;/em&gt;in its turn extends from &lt;em&gt;HAServiceMBeanSupport&lt;/em&gt;&lt;br /&gt;and implements two interfaces: &lt;em&gt;HASingletonMBean&lt;/em&gt; and &lt;em&gt;HASingleton. &lt;/em&gt;All of them give me those wonderful APIs that can tell me whether the current node is the master or not, what the status of my cluster, how many nodes etc. etc.&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-j" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span class="keyword"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;class&lt;/span&gt;&lt;span&gt; MyHAService &lt;/span&gt;&lt;span class="keyword"&gt;extends&lt;/span&gt;&lt;span&gt; HASingletonSupport &lt;/span&gt;&lt;span class="keyword"&gt;implements&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;        MyHAServiceMBean {  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;&lt;span class="keyword"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;static&lt;/span&gt;&lt;span&gt; Logger logger =   &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        Logger.getLogger(MyHAService.&lt;span class="keyword"&gt;class&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; &lt;span class="keyword"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;void&lt;/span&gt;&lt;span&gt; startService() &lt;/span&gt;&lt;span class="keyword"&gt;throws&lt;/span&gt;&lt;span&gt; Exception {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;        logger.info(&lt;span class="string"&gt;" *** STARTED MY SINGLETON SERVICE *** "&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        &lt;span class="keyword"&gt;super&lt;/span&gt;&lt;span&gt;.startService();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; }  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; &lt;span class="keyword"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;void&lt;/span&gt;&lt;span&gt; stopService() &lt;/span&gt;&lt;span class="keyword"&gt;throws&lt;/span&gt;&lt;span&gt; Exception {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        logger.info(&lt;span class="string"&gt;" *** STOPPED MY SINGLETON SERVICE *** "&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;        &lt;span class="keyword"&gt;super&lt;/span&gt;&lt;span&gt;.stopService();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; }  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span class="keyword"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;boolean&lt;/span&gt;&lt;span&gt; isMasterNode() {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;        &lt;span class="keyword"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;super&lt;/span&gt;&lt;span&gt;.isMasterNode();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;}  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;     &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;&lt;span class="keyword"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;void&lt;/span&gt;&lt;span&gt; startSingleton() {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        logger.info(&lt;span class="string"&gt;" *** CURRENT NODE IP:"&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;                + &lt;span class="keyword"&gt;this&lt;/span&gt;&lt;span&gt;.getPartition().getClusterNode()  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;            .getIpAddress().getHostAddress() +   &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;            &lt;span class="string"&gt;" ELECTED AS A MASTER NODE *** "&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;}  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;      &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;&lt;span class="keyword"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;void&lt;/span&gt;&lt;span&gt; stopSingleton() {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        logger.info(&lt;span class="string"&gt;" *** CURRENT NODE IP:"&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;                + &lt;span class="keyword"&gt;this&lt;/span&gt;&lt;span&gt;.getPartition().getClusterNode()  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;            .getIpAddress().getHostAddress()  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;                + &lt;span class="string"&gt;" STOPPED ACTING AS A MASTER NODE *** "&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;}  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;&lt;span class="keyword"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;void&lt;/span&gt;&lt;span&gt; partitionTopologyChanged(List newReplicants, &lt;/span&gt;&lt;span class="keyword"&gt;int&lt;/span&gt;&lt;span&gt; newViewID) {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        logger.info(&lt;span class="string"&gt;" *** TOPOLOGY CHANGE STARTING *** "&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;        &lt;span class="keyword"&gt;super&lt;/span&gt;&lt;span&gt;.partitionTopologyChanged(newReplicants, newViewID);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;   }  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;}  &lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" class="java:nocontrols:firstline[1]" name="code"&gt;public class MyHAService extends HASingletonSupport implements&lt;br /&gt;      MyHAServiceMBean {&lt;br /&gt;&lt;br /&gt;private static Logger logger =&lt;br /&gt;  Logger.getLogger(MyHAService.class);&lt;br /&gt;&lt;br /&gt;public void startService() throws Exception {&lt;br /&gt;      logger.info(" *** STARTED MY SINGLETON SERVICE *** ");&lt;br /&gt;      super.startService();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void stopService() throws Exception {&lt;br /&gt;      logger.info(" *** STOPPED MY SINGLETON SERVICE *** ");&lt;br /&gt;      super.stopService();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public boolean isMasterNode() {&lt;br /&gt;      return super.isMasterNode();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public void startSingleton() {&lt;br /&gt;      logger.info(" *** CURRENT NODE IP:"&lt;br /&gt;              + this.getPartition().getClusterNode()&lt;br /&gt;   .getIpAddress().getHostAddress() +&lt;br /&gt;   " ELECTED AS A MASTER NODE *** ");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public void stopSingleton() {&lt;br /&gt;      logger.info(" *** CURRENT NODE IP:"&lt;br /&gt;              + this.getPartition().getClusterNode()&lt;br /&gt;   .getIpAddress().getHostAddress()&lt;br /&gt;              + " STOPPED ACTING AS A MASTER NODE *** ");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public void partitionTopologyChanged(List newReplicants, int newViewID) {&lt;br /&gt;      logger.info(" *** TOPOLOGY CHANGE STARTING *** ");&lt;br /&gt;      super.partitionTopologyChanged(newReplicants, newViewID);&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt; &lt;em&gt;startSingleton()&lt;/em&gt; - invoked when the current node elected as a master.&lt;br /&gt;&lt;em&gt;stopSingleton()&lt;/em&gt; - invoked when the current node stops acting as a master.&lt;br /&gt;&lt;em&gt;partitionTopologyChanged()&lt;/em&gt; - invoked when new node joins or leaves the cluster.&lt;br /&gt;&lt;br /&gt;As i mentioned before, I have the ability to know whether the current node is the master node, by calling &lt;em&gt;isMasterNode(). &lt;/em&gt;The method will return true if the node is master and false if its not.&lt;br /&gt;&lt;br /&gt;In case I already know that the current node is not the master, I can ask the clustered partition (the cluster) which node is the master. For example I can request the current view of my cluster.&lt;br /&gt;&lt;br /&gt;The implementation can be similar to the method below which you can have inside your service bean: &lt;div class="dp-highlighter"&gt;&lt;ol class="dp-j" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span class="keyword"&gt;private&lt;/span&gt;&lt;span&gt; String getMasterSocket() {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        HAPartition partition = &lt;span class="keyword"&gt;this&lt;/span&gt;&lt;span&gt;.getPartition();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        &lt;span class="keyword"&gt;if&lt;/span&gt;&lt;span&gt; (partition != &lt;/span&gt;&lt;span class="keyword"&gt;null&lt;/span&gt;&lt;span&gt;) {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;            &lt;span class="keyword"&gt;if&lt;/span&gt;&lt;span&gt; (partition.getCurrentView() != &lt;/span&gt;&lt;span class="keyword"&gt;null&lt;/span&gt;&lt;span&gt;) {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;                &lt;span class="keyword"&gt;return&lt;/span&gt;&lt;span&gt; partition.getCurrentView().get(&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span&gt;).toString();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;            } &lt;span class="keyword"&gt;else&lt;/span&gt;&lt;span&gt; {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;                &lt;span class="keyword"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;null&lt;/span&gt;&lt;span&gt;;  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;            }  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        } &lt;span class="keyword"&gt;else&lt;/span&gt;&lt;span&gt; {  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;            &lt;span class="keyword"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span class="keyword"&gt;null&lt;/span&gt;&lt;span&gt;;  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;        }  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;    }  &lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" class="java:nocontrols:firstline[1]" name="code"&gt;private String getMasterSocket() {&lt;br /&gt;&lt;br /&gt;      HAPartition partition = this.getPartition();&lt;br /&gt;&lt;br /&gt;      if (partition != null) {&lt;br /&gt;&lt;br /&gt;          if (partition.getCurrentView() != null) {&lt;br /&gt;              return partition.getCurrentView().get(0).toString();&lt;br /&gt;&lt;br /&gt;          } else {&lt;br /&gt;              return null;&lt;br /&gt;          }&lt;br /&gt;      } else {&lt;br /&gt;          return null;&lt;br /&gt;      }&lt;br /&gt;  }&lt;/pre&gt; The method above will return me a string contains port and ip of the master node, for example:&lt;br /&gt;&lt;pre class="codeSample"&gt;192.168.62.12:1099&lt;br /&gt;&lt;/pre&gt; The &lt;em&gt;HAPartition&lt;/em&gt; service maintains across cluster a registry of nodes in a view order. Now, keep in mind that an order of the nodes in the view, does not necessary reflect the order nodes have joined the cluster.&lt;br /&gt;&lt;br /&gt;So the first node in the view as you can see beloew, will be the master node.&lt;br /&gt;Simple as that. &lt;pre class="codeSample"&gt;return partition.getCurrentView().get(0).toString();&lt;/pre&gt; &lt;strong&gt;Please note&lt;/strong&gt;:&lt;br /&gt;Method &lt;em&gt;getPartition()&lt;/em&gt; may return null, if &lt;em&gt;super.startService()&lt;/em&gt; hasnt been called. Have a look at implementation of &lt;a set="yes" linkindex="284" href="http://docs.jboss.org/jbossas/javadoc/4.0.2/org/jboss/ha/jmx/HAServiceMBeanSupport.java.html" target="_blank"&gt;HAServiceMBeanSupport&lt;/a&gt; and my other post &lt;a set="yes" linkindex="285" href="http://javabeans.asia/2008/05/14/jboss_clustering_how_many_nodes_in_the_cluster.html"&gt;JBoss Clustering - How many nodes in the cluster?&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-297372584350550777?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/297372584350550777/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/determining-master-node-in-jboss.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/297372584350550777'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/297372584350550777'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/determining-master-node-in-jboss.html' title='Determining the master node in a JBoss Cluster'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-4617446036010590574</id><published>2009-10-18T04:24:00.000-07:00</published><updated>2009-10-18T04:25:57.384-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JBoss'/><title type='text'>Finding the number of cluster nodes in JBoss</title><content type='html'>&lt;h2&gt;&lt;br /&gt;&lt;/h2&gt;&lt;h2&gt;Thanks to the Original Author and &lt;a href="http://javabeans.asia/2008/05/14/jboss_clustering_how_many_nodes_in_the_cluster.html"&gt;source&lt;/a&gt;&lt;br /&gt;&lt;/h2&gt;&lt;h2&gt;How to find out how many nodes in the cluster&lt;/h2&gt;         If you want to know how many nodes there are in the current cluster partition, all you have to do is to ask &lt;em&gt;HAPartition&lt;/em&gt; for the node list. &lt;em&gt;HAPartition&lt;/em&gt; represents your cluster partition, and it contains all the information you need to know about your cluster and the nodes: their host names, IPs, position in the cluster view.&lt;br /&gt;&lt;br /&gt;Lets assume you have a service bean that extends from &lt;em&gt;HASingletonSupport&lt;/em&gt;. &lt;em&gt;HASingletonSupport &lt;/em&gt;in its turn extends from &lt;em&gt;HAServiceMBeanSupport.&lt;br /&gt;&lt;br /&gt;&lt;/em&gt;&lt;em&gt;HAServiceMBeanSupport &lt;/em&gt;is the one who gives you access&lt;em&gt; to HAPartition &lt;/em&gt;object.&lt;br /&gt;&lt;br /&gt;The code to request for HAPartition object and node list that you see below , you can put somewhere in your service bean: &lt;pre class="codeSample"&gt;HAPartition partition = getPartition();&lt;br /&gt;ClusterNode[] nodes = partition.getClusterNodes();&lt;br /&gt;System.out.println(nodes.length);&lt;br /&gt;&lt;/pre&gt; &lt;em&gt;ClusterNode&lt;/em&gt; object represents your node in the cluster. It contains information about node's host name, its internet address and a few more things. &lt;em&gt;getClusterNodes()&lt;/em&gt;, returns to you an array contains as many &lt;em&gt;ClusterNode&lt;/em&gt; objects as you have currently in your cluster. So by getting the value of array length, you will know how many nodes your cluster has.&lt;br /&gt;&lt;br /&gt;Another way, is to do practically the same, but to request from a HAPartition a current view of your cluster: &lt;pre class="codeSample"&gt;HAPartition partition = getPartition();&lt;br /&gt;Vector v = partition.getCurrentView();&lt;br /&gt;&lt;br /&gt;System.out.println(partition.getCurrentView().size());&lt;br /&gt;&lt;br /&gt;for (Object o : v) {&lt;br /&gt;  System.out.println(o.toString());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt; The view, which is a Vector contains information about node sockets. When printed, it will return to you a String representation of node ip + port:  xxx.xxx.xxx.xxx:port. Also by printing size of the Vector, you will get number of nodes in the cluster.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Important note&lt;/strong&gt;:&lt;br /&gt;I noticed there is some delay happening from the time when node leaves the cluster to the time when HAPartition returns an updated view. In another words - after node has left the cluster and topology change has occurred, the HAPartition may return to you an old view still containing the dead node. So be careful.&lt;br /&gt;&lt;br /&gt;Also, &lt;em&gt;getPartition()&lt;/em&gt; may return null, if &lt;em&gt;super.startService()&lt;/em&gt; hasnt been called. Have a look at implementation of &lt;a set="yes" linkindex="284" href="http://docs.jboss.org/jbossas/javadoc/4.0.2/org/jboss/ha/jmx/HAServiceMBeanSupport.java.html" target="_blank"&gt;HAServiceMBeanSupport&lt;/a&gt; and my other post &lt;a set="yes" linkindex="285" href="http://javabeans.asia/2008/05/08/jboss_clustering_hasingleton_service.html"&gt;JBoss Clustering - HASingleton service&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-4617446036010590574?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/4617446036010590574/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/finding-number-of-cluster-nodes-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/4617446036010590574'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/4617446036010590574'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/finding-number-of-cluster-nodes-in.html' title='Finding the number of cluster nodes in JBoss'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-5875760372048811020</id><published>2009-10-18T03:38:00.000-07:00</published><updated>2009-10-18T03:42:12.155-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JBoss'/><title type='text'>JBoss Clustering</title><content type='html'>&lt;h3&gt;Thanks to the original author and &lt;a href="http://onjava.com/pub/a/onjava/2003/08/20/jboss_clustering.html?page=1"&gt;Source&lt;/a&gt;&lt;br /&gt;&lt;/h3&gt;&lt;h3&gt;Singleton Service&lt;/h3&gt;  &lt;p&gt;A clustered singleton service is deployed on multiple nodes in a cluster but runs on only one of the nodes.  The node running the singleton service is typically called the master node.  When the master fails, another master is selected from the remaining nodes and the service is restarted on the new master.&lt;/p&gt; &lt;!-- sidebar begins --&gt; &lt;!-- don't move sidebars --&gt; &lt;!-- sidebar ends --&gt;   &lt;p&gt; &lt;img src="http://onjava.com/onjava/2003/08/20/graphics/clustered-singleton-diagram.png" alt="clustered singleton diagram" /&gt;&lt;br /&gt;&lt;em&gt;Figure 1. Clustered singleton service&lt;/em&gt;&lt;/p&gt;  &lt;p&gt; Many times, an application needs to ensure that a certain task is run exactly once. Thus, only one of the nodes in a cluster should execute the task. The other nodes should knowingly remain passive.  Examples of singleton tasks include: &lt;/p&gt;  &lt;!-- &lt;csperl file="set_book_cover_image"&gt; --&gt;  &lt;!-- sidebar begins --&gt;&lt;!-- sidebar ends --&gt;   &lt;ul&gt;&lt;li&gt;&lt;b&gt;Sending email to the system administrator when the system is brought up or taken down.&lt;/b&gt; This notification does not take into account how many nodes are in the cluster. As long as at least one node is active, the system is up. When all nodes are down, the system is down.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Database schema validation upon startup.&lt;/b&gt; When a database-driven application is brought up, it is a good practice for the middle tier servers to verify whether the version of the business logic they implement matches the database schema.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Sending recurring notifications to system users.&lt;/b&gt; For example, a calendar application might send out email prior to each instance of a scheduled recurring meeting.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Load balancing of queued tasks.&lt;/b&gt; It's popular to use a single coordinator that distributes tasks among nodes in a cluster.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Fault tolerance.&lt;/b&gt; If an application uses a distributed cache, it is common to designate a single master node responsible for maintaining a current copy of distributed states. The other nodes make requests of the current master node. When the master fails, another node takes over its responsibilities.&lt;/li&gt;&lt;/ul&gt;  &lt;p&gt; While it is fairly easy to implement such singleton tasks in a single VM, the solution will usually not work immediately in a clustered environment. Even in the simple case of a task activated upon startup on one of the nodes in a two-node cluster, several problems must be addressed: &lt;/p&gt;  &lt;ul&gt;&lt;li&gt;When the application is started simultaneously on both nodes, which VM should run the singleton task?&lt;/li&gt;&lt;li&gt;When the application is started on one node and then started later on another, how does the second node know not to run the singleton task again?&lt;/li&gt;&lt;li&gt;When the node that started the task fails, how does another node know to resume the task?&lt;/li&gt;&lt;li&gt;When the node that started the task fails, but later recovers, how do we ensure that the task remains running on only one of the nodes?&lt;/li&gt;&lt;/ul&gt;  &lt;p&gt; The logic to solve these problems is unlikely to be included in the design of a single-VM solution. However, a solution can be found to address the case at hand and it can be patched on the startup task.  This is an acceptable approach for a few startup tasks and two-node clusters.  &lt;/p&gt;  &lt;p&gt; As the application grows and becomes more successful, more startup tasks may be necessary. The application may also need to scale to more than two nodes. The clustered singleton problem can quickly become mind-boggling for larger clusters, where the different node startup scenarios are far more difficult to enumerate than in the two-node case. Another complicating factor is communication efficiency.  While two nodes can directly connect to each other and negotiate, 10 nodes will have to establish 45 total connections to use the same technique.  &lt;/p&gt;  &lt;p&gt; This is where JBoss comes in handy. It eliminates most of the complexity and allows application developers to focus on building singleton services regardless of the cluster topology.  &lt;/p&gt;  &lt;p&gt; We will illustrate how the JBoss clustered singleton facility works with an example.  First, we will need a service archive descriptor. Let's use the one that ships with JBoss under &lt;i&gt;server/all/farm/cluster-examples-service.xml&lt;/i&gt;.  The following is an excerpt: &lt;/p&gt;  &lt;pre&gt;&lt;code&gt;&amp;lt;!--    | This MBean is an example of a cluster Singleton    --&gt;&lt;br /&gt;&amp;lt;mbean code="org.jboss.ha.singleton.examples.HASingletonMBeanExample"&lt;br /&gt;         name="jboss.examples:service=HASingletonMBeanExample"&gt;&lt;br /&gt;&amp;lt;/mbean&gt;&lt;br /&gt;&amp;lt;!- - --&gt;&lt;br /&gt;  &amp;lt;!--    | This is a singleton controller which works similarly to the&lt;br /&gt;          | SchedulerProvider (when a MBean target is used)    --&gt;&lt;br /&gt;  &amp;lt;mbean code="org.jboss.ha.singleton.HASingletonController"&lt;br /&gt;         name="jboss.examples:service=HASingletonMBeanExample-HASingletonController"&gt;&lt;br /&gt;     &amp;lt;depends&gt;jboss:service=DefaultPartition&amp;lt;/depends&gt;&lt;br /&gt;     &amp;lt;depends&gt;jboss.examples:service=HASingletonMBeanExample&amp;lt;/depends&gt;&lt;br /&gt;     &amp;lt;attribute name="TargetName"&gt;jboss:service=HASingletonMBeanExample&amp;lt;/attribute&gt;&lt;br /&gt;     &amp;lt;attribute name="TargetStartMethod"&gt;startSingleton&amp;lt;/attribute&gt;&lt;br /&gt;     &amp;lt;attribute name="TargetStopMethod"&gt;stopSingleton&amp;lt;/attribute&gt;&lt;br /&gt;  &amp;lt;/mbean&gt;&lt;br /&gt;&amp;lt;!- - --&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;  &lt;p&gt; This file declares two MBeans, &lt;code&gt;HASingletonMBeanExample&lt;/code&gt; and &lt;code&gt;HASingletonController&lt;/code&gt;.  The first one is a singleton service that contains the custom code.  It is a simple JavaBean with the following source code: &lt;/p&gt;  &lt;pre&gt;&lt;code&gt;public class HASingletonMBeanExample&lt;br /&gt; implements HASingletonMBeanExampleMBean {&lt;br /&gt;&lt;br /&gt;   private boolean isMasterNode = false;&lt;br /&gt;&lt;br /&gt;   public void startSingleton() {&lt;br /&gt;       isMasterNode = true;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public boolean isMasterNode() {&lt;br /&gt;       return isMasterNode; &lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void stopSingleton() {&lt;br /&gt;       isMasterNode = false; &lt;br /&gt;   }&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;  &lt;p&gt; All of the custom logic for this particular singleton service is contained within this class.  Our example is not too useful; it simply indicates, via the &lt;code&gt;isMasterNode&lt;/code&gt; member variable, whether the master node is running the singleton.  This value will be true only on the one node in the cluster where it is deployed.  &lt;/p&gt;  &lt;p&gt; &lt;code&gt;HASingletonMBeanExampleMBean&lt;/code&gt; exposes this variable as an MBean attribute. It also exposes &lt;code&gt;startSingleton()&lt;/code&gt; and &lt;code&gt;stopSingleton()&lt;/code&gt; as managed MBean operations. These methods control the lifecycle of the singleton service. JBoss invokes them automatically when a new master node is elected.  &lt;/p&gt;  &lt;p&gt; How does JBoss control the singleton lifecycle throughout the cluster? The answer to this question is in the MBean declarations.  Notice that the &lt;code&gt;HASingletonMBeanExample-HASingletonController&lt;/code&gt; MBean also takes the name of the sample singleton MBean and its start and stop methods.  &lt;/p&gt;  &lt;p&gt; On each node in the cluster where these MBeans are deployed, the controller will work with all of the other controllers with the same MBean name deployed in the same cluster partition to oversee the lifecycle of the singleton. The controllers are responsible for tracking the cluster topology. Their job is to elect the master node of the singleton upon startup, as well as to elect a new master should the current one fail or shut down. In the latter case, when the master node shuts down gracefully, the controllers will wait for the singleton to stop before starting another instance on the new master node.  &lt;/p&gt;  &lt;p&gt; A singleton service is scoped in a certain cluster partition via its controller. Notice that, in the declaration above, the controller MBean depends on the MBean service &lt;code&gt;DefaultPartition&lt;/code&gt;. If the partition where the singleton should run is different than the default, its name can be provided to the controller via the MBean attribute &lt;code&gt;PartitionName&lt;/code&gt;. &lt;/p&gt;  &lt;p&gt; Clustered singletons are usually deployed via the JBoss farming service. To test this example, just drop the service file above in the &lt;i&gt;server/all/farm&lt;/i&gt; directory.  You should be able to see the following in the JBoss JMX web console: &lt;/p&gt;  &lt;p&gt; &lt;img src="http://onjava.com/onjava/2003/08/20/graphics/jmx-hasingleton-controller.png" alt="JMX Console, HASingletonController" /&gt;&lt;br /&gt;&lt;em&gt;Figure 2. Controller MBean view. The &lt;code&gt;MasterNode&lt;/code&gt; attribute will have value &lt;code&gt;True&lt;/code&gt; on only one of the nodes.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;img src="http://onjava.com/onjava/2003/08/20/graphics/jmx-hasingleton-sample-singleton.png" alt="JMX Console, HASingletonMBeanExample" /&gt;&lt;br /&gt;&lt;em&gt;Figure 3. Sample singleton MBean view. The &lt;code&gt;MasterNode&lt;/code&gt; attribute will have the same value as the &lt;code&gt;MasterNode&lt;/code&gt; attribute on the controller MBean.&lt;/em&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-5875760372048811020?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/5875760372048811020/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/jboss-clustering.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5875760372048811020'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5875760372048811020'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/jboss-clustering.html' title='JBoss Clustering'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-2533648804660252908</id><published>2009-10-17T23:57:00.000-07:00</published><updated>2009-10-18T00:46:11.220-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JBoss'/><title type='text'>Brief on JBoss EAR Deployment</title><content type='html'>&lt;span style="font-weight: bold;"&gt;EAR Deployment Process:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1) The EAR starts getting deployed and &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;EARDeployer &lt;/span&gt;is the main class that does that.&lt;br /&gt;&lt;br /&gt;2) All the dependent jars are scanned and deployed if not deployed. &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;MainDeployer &lt;/span&gt;is the main class that does this.&lt;br /&gt;&lt;br /&gt;3) The Queue Sevice and Topic Service will be started if there are any topics or queues defined in the Application. &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;TopicService &lt;/span&gt;and &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;QueueService &lt;/span&gt;are the main class that does this.&lt;br /&gt;&lt;br /&gt;4) EJB3Deployment begins, which deploys Enterprise beans and Persistence Units in the application or module. &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;EJB3Deployer &lt;/span&gt;iterates through the ear to find all the jars which has persistence units and enterprise beans for deployment.&lt;br /&gt;&lt;br /&gt;    a) If a persistence unit is detected, first an corresponding MDB will be created and registered into JMXServer. Similarly if an EJB is detected, a corresponding MDB is created and registered into JMX. &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;JmxKernelAbstraction &lt;/span&gt;is the main class that does this.&lt;br /&gt;&lt;br /&gt;   b) Once PersistenceUnitMDB is registered, the service is started to create all the relevant tables n EntityManager relationships etc into db and &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;PersistenceUnitDeployment &lt;/span&gt;is the class that does this.&lt;br /&gt;&lt;br /&gt;   c) Similarly once EJBMDB is registered, the service is started using &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;EJBContainer &lt;/span&gt;class.&lt;br /&gt;&lt;br /&gt;5) Once EJB Deployment is done, &lt;span style="font-weight: bold; color: rgb(51, 204, 0);"&gt;TomcatDeployer &lt;/span&gt;will deploy the web application if any in the particular module being scanned.&lt;br /&gt;&lt;br /&gt;6) Cluster Session Management is started for this particular application, &lt;span style="font-weight: bold;"&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;JBossCacheManager&lt;/span&gt; &lt;/span&gt;is the one responsible for this.&lt;br /&gt;&lt;br /&gt;7) Once all the above steps are done, the application can have application specific database initializers, inventory trackers, etc started.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Before the EAR Deployment begins, all the services starting from Transaction Service, Timer Service, UDDI, WebService, etc would have started.&lt;br /&gt;All the JBoss related Webapps like jmx-console, web-console will start. Once all these JBoss goes ahead with the EAR Deployment.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-2533648804660252908?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/2533648804660252908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/brief-on-jboss-ear-deployment.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2533648804660252908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2533648804660252908'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/brief-on-jboss-ear-deployment.html' title='Brief on JBoss EAR Deployment'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-5033505949496012584</id><published>2009-10-17T04:44:00.000-07:00</published><updated>2009-10-17T04:47:28.699-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JBoss'/><title type='text'>JBoss Lookup</title><content type='html'>Thanks to the original author.&lt;br /&gt;You can find the original source &lt;a href="http://jaitechwriteups.blogspot.com/2007/10/why-do-i-get-namenotfoundexception.html"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Many a times when you are doing a lookup in the JNDI tree, you see javax.naming.NameNotFoundException. A simple code that does the lookup will look something like:&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-j" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;Context ctx = &lt;/span&gt;&lt;span class="keyword"&gt;new&lt;/span&gt;&lt;span&gt; InitialContext();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;Object obj = ctx.lookup(&lt;span class="string"&gt;"somepath/somename"&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="java"&gt;Context ctx = new InitialContext(); Object obj = ctx.lookup("somepath/somename"); &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This code just looks up the JNDI tree to get an object bound by the name "somepath/somename". Looks simple. However, chances are that you might even see this exception:&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-j" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;javax.naming.NameNotFoundException: &lt;b&gt;somepath&lt;/b&gt; not bound  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at org.jnp.server.NamingServer.getBinding(NamingServer.java:&lt;span class="number"&gt;529&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at org.jnp.server.NamingServer.getBinding(NamingServer.java:&lt;span class="number"&gt;537&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at org.jnp.server.NamingServer.getObject(NamingServer.java:&lt;span class="number"&gt;543&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at org.jnp.server.NamingServer.lookup(NamingServer.java:&lt;span class="number"&gt;267&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:&lt;span class="number"&gt;39&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:&lt;span class="number"&gt;25&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at java.lang.reflect.Method.invoke(Method.java:&lt;span class="number"&gt;585&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:&lt;span class="number"&gt;294&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at sun.rmi.transport.Transport$&lt;span class="number"&gt;1&lt;/span&gt;&lt;span&gt;.run(Transport.java:&lt;/span&gt;&lt;span class="number"&gt;153&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at java.security.AccessController.doPrivileged(Native Method)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at sun.rmi.transport.Transport.serviceCall(Transport.java:&lt;span class="number"&gt;149&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:&lt;span class="number"&gt;460&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:&lt;span class="number"&gt;701&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at java.lang.Thread.run(Thread.java:&lt;span class="number"&gt;595&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:&lt;span class="number"&gt;247&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:&lt;span class="number"&gt;223&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:&lt;span class="number"&gt;126&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:&lt;span class="number"&gt;625&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt; at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:&lt;span class="number"&gt;587&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt; at javax.naming.InitialContext.lookup(InitialContext.java:&lt;span class="number"&gt;351&lt;/span&gt;&lt;span&gt;)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="java"&gt;javax.naming.NameNotFoundException: &lt;b&gt;somepath&lt;/b&gt; not bound  at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)  at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)  at org.jnp.server.NamingServer.getObject(NamingServer.java:543)  at org.jnp.server.NamingServer.lookup(NamingServer.java:267)  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  at java.lang.reflect.Method.invoke(Method.java:585)  at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)  at sun.rmi.transport.Transport$1.run(Transport.java:153)  at java.security.AccessController.doPrivileged(Native Method)  at sun.rmi.transport.Transport.serviceCall(Transport.java:149)  at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)  at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)  at java.lang.Thread.run(Thread.java:595)  at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)  at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)  at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)  at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)  at javax.naming.InitialContext.lookup(InitialContext.java:351) &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Look closely at the stacktrace. It shows that while looking up the JNDI tree it could not find the jndi name "somepath" (this name may vary). The reason is simple, the JNDI tree does not have any object bound by this name.&lt;br /&gt;&lt;br /&gt;To quote the javadocs of this exception "This exception is thrown when a component of the name cannot be resolved because it is not bound."&lt;br /&gt;&lt;br /&gt;So how do i know, what's the name to which my object is bound? Each application server, usually provides a JNDI view which can be used to see the contents of the JNDI tree. If you know what object you are looking for (ex: the name of the bean), then you can traverse this JNDI tree to see what name it is bound to. The JNDI view is specific to every application server.&lt;br /&gt;&lt;br /&gt;To give an example, JBoss provides its JDNI tree view, through the JMX console. Here are the steps, one has to follow to check the JNDI tree contents on JBoss:&lt;br /&gt;&lt;br /&gt;- Go to http://&lt;&gt;:&lt;&gt;/jmx-console (Ex: http://localhost:8080/jmx-console)&lt;br /&gt;- Search for service=JNDIView on the jmx-console page&lt;br /&gt;- Click on that link&lt;br /&gt;- On the page that comes up click on the &lt;i&gt;Invoke&lt;/i&gt; button beside the &lt;i&gt;list()&lt;/i&gt; method&lt;br /&gt;- The page that comes up will show the contents of the JNDI tree.&lt;br /&gt;&lt;br /&gt;Here's an sample of how the output looks like(just a small part of the entire output):&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-xml" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;java: Namespace  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- SecurityProxyFactory (class: org.jboss.security.SubjectSecurityProxyFactory)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- DefaultJMSProvider (class: org.jboss.jms.jndi.JNDIProviderAdapter)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- comp (class: javax.naming.Context)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- JmsXA (class: org.jboss.resource.adapter.jms.JmsConnectionFactoryImpl)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- jaas (class: javax.naming.Context)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  |   +- dukesbank (class: org.jboss.security.plugins.SecurityDomainContext)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   +- HsqlDbRealm (class: org.jboss.security.plugins.SecurityDomainContext)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  |   +- jbossmq (class: org.jboss.security.plugins.SecurityDomainContext)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   +- JmsXARealm (class: org.jboss.security.plugins.SecurityDomainContext)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;   &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;Global JNDI Namespace  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- ebankTxController (proxy: $Proxy79 implements interface com.sun.ebank.ejb.tx.TxControllerHome,interface javax.ejb.Handle)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- ebankAccountController (proxy: $Proxy75 implements interface com.sun.ebank.ejb.account.AccountControllerHome,interface javax.ejb.Handle)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- TopicConnectionFactory (class: org.jboss.naming.LinkRefPair)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- jmx (class: org.jnp.interfaces.NamingContext)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  |   +- invoker (class: org.jnp.interfaces.NamingContext)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   |   +- RMIAdaptor (proxy: $Proxy48 implements interface org.jboss.jmx.adaptor.rmi.RMIAdaptor,interface org.jboss.jmx.adaptor.rmi.RMIAdaptorExt)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  |   +- rmi (class: org.jnp.interfaces.NamingContext)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   |   +- RMIAdaptor[link -&lt;span class="tag"&gt;&gt;&lt;/span&gt;&lt;span&gt; jmx/invoker/RMIAdaptor] (class: javax.naming.LinkRef)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- HTTPXAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- ebankCustomer (proxy: $Proxy67 implements interface com.sun.ebank.ejb.customer.LocalCustomerHome)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- UserTransactionSessionFactory (proxy: $Proxy14 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- ebankCustomerController (proxy: $Proxy77 implements interface com.sun.ebank.ejb.customer.CustomerControllerHome,interface javax.ejb.Handle)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- HTTPConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- TransactionSynchronizationRegistry (class: com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- ebankAccount (proxy: $Proxy68 implements interface com.sun.ebank.ejb.account.LocalAccountHome)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- UILXAConnectionFactory[link -&lt;span class="tag"&gt;&gt;&lt;/span&gt;&lt;span&gt; XAConnectionFactory] (class: javax.naming.LinkRef)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  +- UIL2XAConnectionFactory[link -&lt;span class="tag"&gt;&gt;&lt;/span&gt;&lt;span&gt; XAConnectionFactory] (class: javax.naming.LinkRef)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  +- queue (class: org.jnp.interfaces.NamingContext)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   +- A (class: org.jboss.mq.SpyQueue)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  |   +- testQueue (class: org.jboss.mq.SpyQueue)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   +- ex (class: org.jboss.mq.SpyQueue)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  |   +- DLQ (class: org.jboss.mq.SpyQueue)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   +- D (class: org.jboss.mq.SpyQueue)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;  |   +- C (class: org.jboss.mq.SpyQueue)  &lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   +- B (class: org.jboss.mq.SpyQueue)  &lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="xml"&gt;java: Namespace    +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)   +- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)   +- SecurityProxyFactory (class: org.jboss.security.SubjectSecurityProxyFactory)   +- DefaultJMSProvider (class: org.jboss.jms.jndi.JNDIProviderAdapter)   +- comp (class: javax.naming.Context)   +- JmsXA (class: org.jboss.resource.adapter.jms.JmsConnectionFactoryImpl)   +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)   +- jaas (class: javax.naming.Context)   |   +- dukesbank (class: org.jboss.security.plugins.SecurityDomainContext)   |   +- HsqlDbRealm (class: org.jboss.security.plugins.SecurityDomainContext)   |   +- jbossmq (class: org.jboss.security.plugins.SecurityDomainContext)   |   +- JmsXARealm (class: org.jboss.security.plugins.SecurityDomainContext)    Global JNDI Namespace    +- ebankTxController (proxy: $Proxy79 implements interface com.sun.ebank.ejb.tx.TxControllerHome,interface javax.ejb.Handle)   +- ebankAccountController (proxy: $Proxy75 implements interface com.sun.ebank.ejb.account.AccountControllerHome,interface javax.ejb.Handle)   +- TopicConnectionFactory (class: org.jboss.naming.LinkRefPair)   +- jmx (class: org.jnp.interfaces.NamingContext)   |   +- invoker (class: org.jnp.interfaces.NamingContext)   |   |   +- RMIAdaptor (proxy: $Proxy48 implements interface org.jboss.jmx.adaptor.rmi.RMIAdaptor,interface org.jboss.jmx.adaptor.rmi.RMIAdaptorExt)   |   +- rmi (class: org.jnp.interfaces.NamingContext)   |   |   +- RMIAdaptor[link -&gt; jmx/invoker/RMIAdaptor] (class: javax.naming.LinkRef)   +- HTTPXAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)   +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)   +- ebankCustomer (proxy: $Proxy67 implements interface com.sun.ebank.ejb.customer.LocalCustomerHome)   +- UserTransactionSessionFactory (proxy: $Proxy14 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)   +- ebankCustomerController (proxy: $Proxy77 implements interface com.sun.ebank.ejb.customer.CustomerControllerHome,interface javax.ejb.Handle)   +- HTTPConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)   +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)   +- TransactionSynchronizationRegistry (class: com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)   +- ebankAccount (proxy: $Proxy68 implements interface com.sun.ebank.ejb.account.LocalAccountHome)   +- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)   +- UILXAConnectionFactory[link -&gt; XAConnectionFactory] (class: javax.naming.LinkRef)   +- UIL2XAConnectionFactory[link -&gt; XAConnectionFactory] (class: javax.naming.LinkRef)   +- queue (class: org.jnp.interfaces.NamingContext)   |   +- A (class: org.jboss.mq.SpyQueue)   |   +- testQueue (class: org.jboss.mq.SpyQueue)   |   +- ex (class: org.jboss.mq.SpyQueue)   |   +- DLQ (class: org.jboss.mq.SpyQueue)   |   +- D (class: org.jboss.mq.SpyQueue)   |   +- C (class: org.jboss.mq.SpyQueue)   |   +- B (class: org.jboss.mq.SpyQueue) &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Let's see what this tells us. Let's consider the Global JNDI Namespace first. It contains (among other things) the following:&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-xml" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;+- ebankTxController (proxy: $Proxy79 implements interface com.sun.ebank.ejb.tx.TxControllerHome,interface javax.ejb.Handle)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="xml"&gt;+- ebankTxController (proxy: $Proxy79 implements interface com.sun.ebank.ejb.tx.TxControllerHome,interface javax.ejb.Handle) &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This tells me that an object which implements com.sun.ebank.ejb.tx.TxControllerHome and javax.ejb.Handle interfaces is bound to the JNDI tree by the jndi-name "ebankTxController". So if at all i have to lookup this object, my lookup code would be something like:&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;div class="bar"&gt;&lt;div class="tools"&gt;&lt;a href="http://jaitechwriteups.blogspot.com/2007/10/why-do-i-get-namenotfoundexception.html#" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;"&gt;view plain&lt;/a&gt;&lt;a href="http://jaitechwriteups.blogspot.com/2007/10/why-do-i-get-namenotfoundexception.html#" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;"&gt;copy to clipboard&lt;/a&gt;&lt;a href="http://jaitechwriteups.blogspot.com/2007/10/why-do-i-get-namenotfoundexception.html#" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;"&gt;print&lt;/a&gt;&lt;a href="http://jaitechwriteups.blogspot.com/2007/10/why-do-i-get-namenotfoundexception.html#" onclick="dp.sh.Toolbar.Command('About',this);return false;"&gt;?&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;ol class="dp-j" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;Context ctx = &lt;/span&gt;&lt;span class="keyword"&gt;new&lt;/span&gt;&lt;span&gt; InitialContext();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;ctx.lookup(&lt;span class="string"&gt;"ebankTxController"&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="java"&gt;Context ctx = new InitialContext(); ctx.lookup("ebankTxController"); &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Similarly in the same Global JDNI Namespace, we see :&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-xml" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;+- queue (class: org.jnp.interfaces.NamingContext)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;  |   +- A (class: org.jboss.mq.SpyQueue)  &lt;/span&gt;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span&gt;    &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="xml"&gt;+- queue (class: org.jnp.interfaces.NamingContext)   |   +- A (class: org.jboss.mq.SpyQueue)    &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Make note of the nesting of the names here. This tells me that an object of type org.jboss.mq.SpyQueue is bound by the name "A under the path queue". So your lookup for this object should look like:&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-j" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;Context ctx = &lt;/span&gt;&lt;span class="keyword"&gt;new&lt;/span&gt;&lt;span&gt; InitialContext();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;ctx.lookup(&lt;span class="string"&gt;"queue/A"&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="java"&gt;Context ctx = new InitialContext(); ctx.lookup("queue/A"); &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now let's move on to the java: namespace in the JNDI tree view above. The difference between a Global JNDI namespace and the java: namespace is that, the object bound in the java: namespace can be looked-up ONLY by clients within the SAME JVM. Whereas, in case of Global JNDI namespace, the objects bound in this namespace can be looked-up by clients, even if they are not in the same JVM as the server. One would ask, how does this matter? Consider a standalone java program(client) which tries to lookup some object on the server (running in its own JVM). Whenever a standalone client is started (using the java command), a new JVM is instantiated. As a result, the server (which is started in its own JVM) and the client are running on different JVMs. Effectively, the client will NOT be able to lookup objects bound in the java: namespace of the server. However, the client can lookup the objects present in the Global JNDI namespace of the server.&lt;br /&gt;&lt;br /&gt;So, why are we discussing these details, in a topic which was meant to explain the NameNotFoundException? Let's consider the java: namespace output above. There's a&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-xml" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;+- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="xml"&gt;+- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource) &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This tells me that there's an object bound to the name DefaultDS in the java: namespace. So my lookup code would be:&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter"&gt;&lt;ol class="dp-j" start="1"&gt;&lt;li class="alt"&gt;&lt;span&gt;&lt;span&gt;Context ctx = &lt;/span&gt;&lt;span class="keyword"&gt;new&lt;/span&gt;&lt;span&gt; InitialContext();  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span&gt;ctx.lookup(&lt;span class="string"&gt;"java:/DefaultDS"&lt;/span&gt;&lt;span&gt;);  &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;pre style="display: none;" name="code" class="java"&gt;Context ctx = new InitialContext(); ctx.lookup("java:/DefaultDS"); &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As explained above, this code is going to return you the object, if this piece of code runs in the same JVM as the server. However, if this piece of code is run from a client in different JVM (maybe a standalone client), then it's going to run into NameNotFoundException. The reason i explained the java: and the Global JNDI namespace is that, sometimes people are surprised that even though the JNDI view shows that the object is bound in the java: namespace(with the same name as the one they pass to the lookup method), they still run into NameNotFoundException. The probable reason might be, the client is in a different JVM.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-5033505949496012584?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/5033505949496012584/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/jboss-lookup.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5033505949496012584'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5033505949496012584'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/jboss-lookup.html' title='JBoss Lookup'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-8438983699083948359</id><published>2009-10-01T22:38:00.000-07:00</published><updated>2009-10-02T00:05:05.926-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Practise.ExtJs'/><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>All you need to know about Javascript Inheritance</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Classical Inheritance&lt;/span&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;&amp;lt;html&gt;&lt;br /&gt;&amp;lt;script&gt;&lt;br /&gt;function Person(name)&lt;br /&gt;{&lt;br /&gt;   this.name = name;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Person.prototype.getName = function() {&lt;br /&gt;   return this.name;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function Author(name, book)&lt;br /&gt;{&lt;br /&gt;   Person.call(this, name);&lt;br /&gt;   this.book = book;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Author.prototype = new Person();&lt;br /&gt;&lt;br /&gt;Author.prototype.getBook = function() {&lt;br /&gt;   return this.book;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;var simpson = new Author("Simpson", "The Big Fat Book");&lt;br /&gt;alert(simpson.getName());&lt;br /&gt;alert(simpson.getBook());&lt;br /&gt;&lt;br /&gt;function Me()&lt;br /&gt;{&lt;br /&gt;   Author.call(this, "Vinuth", "GentleMenz Arena");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Me.prototype = new Author();&lt;br /&gt;&lt;br /&gt;Me.prototype.getBook = function() {&lt;br /&gt;   return "MyBreadBasket";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;var me = new Me();&lt;br /&gt;alert(me.getName() + " - " + me.getBook());&lt;br /&gt;&amp;lt;/script&gt;&lt;br /&gt;&amp;lt;/html&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;How of Classical Inheritance&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1) Define a function as a constructor,&lt;br /&gt;for e.g., &lt;span style="font-weight: bold;"&gt;Person&lt;/span&gt;, &lt;span style="font-weight: bold;"&gt;Author &lt;/span&gt;and &lt;span style="font-weight: bold;"&gt;Me &lt;/span&gt;shown above.&lt;br /&gt;&lt;br /&gt;2) Constructors initializes the member variables and methods using the "&lt;span style="font-weight: bold;"&gt;this&lt;/span&gt;" keyword as shown in Person and Author constructors.&lt;br /&gt;&lt;br /&gt;3) Define the complete structure by adding methods.&lt;br /&gt;In the above example Person is defined as having a method called &lt;span style="font-weight: bold;"&gt;getName&lt;/span&gt;.&lt;br /&gt;In javascript &lt;span style="font-weight: bold;"&gt;prototype &lt;/span&gt;keyword is used extensively for this as shown below.&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;Person.prototype.getName = function() {&lt;br /&gt; return this.name;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;4) Extend Author from Person and this is done using two steps.&lt;br /&gt;a) In the constructor of Author call the Person's constructor like &lt;span style="font-weight: bold;"&gt;Person.call(this,...);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    "this"&lt;/span&gt; as the parameter to the call method says that you are invoking Person in the Author's context,&lt;br /&gt;   i.e., "this" here refers to Author's Object.&lt;br /&gt;b) &lt;span style="font-weight: bold;"&gt;Prototype chaining:&lt;/span&gt;&lt;br /&gt; It is a way to define the hierarchy of objects.&lt;br /&gt; Here Author's prototype is linked to Person meaning Author is an extension of Person or Author is of type Person.&lt;br /&gt;    This is done using &lt;span style="font-weight: bold;"&gt;Author.prototype = new Person();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;5) Creating the objects is as simple as "&lt;span style="font-weight: bold;"&gt;var author = new Author();&lt;/span&gt;" and calling a method of its super class will be like "&lt;span style="font-weight: bold;"&gt;author.getName();&lt;/span&gt;"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Prototypal Inheritance&lt;/span&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;&amp;lt;html&gt;&lt;br /&gt;&amp;lt;script&gt;&lt;br /&gt;&lt;br /&gt;var Person = {&lt;br /&gt; name: "Vinuth",&lt;br /&gt; getName: function() {&lt;br /&gt;     return this.name;&lt;br /&gt; }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;alert(Person.getName());&lt;br /&gt;&lt;br /&gt;function clone(obj)&lt;br /&gt;{&lt;br /&gt; function F(){}&lt;br /&gt; F.prototype = obj;&lt;br /&gt; return new F();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;var Author = clone(Person);&lt;br /&gt;&lt;br /&gt;Author.setName = function(name) {&lt;br /&gt;this.name = name;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;alert(Author.getName());&lt;br /&gt;Author.setName("Chet");&lt;br /&gt;alert(Author.getName());&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/script&gt;&lt;br /&gt;&amp;lt;/html&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;All about Prototypal Inheritance&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1) Most importantly there is no use of keyword "&lt;span style="font-weight: bold;"&gt;function&lt;/span&gt;" for defining the classes.&lt;br /&gt;There is no concept of classes.&lt;br /&gt;The beauty of Prototypal language is that everything is an object and there is no class concept at all.&lt;br /&gt;As you can see in the above example Person is not a class but its an object and it is defined using the &lt;span style="font-weight: bold;"&gt;configuration object.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Configuration objects&lt;/span&gt; are defined as &lt;span style="font-weight: bold;"&gt;{...}&lt;/span&gt; within the flower brackets &lt;span style="font-weight: bold;"&gt;"{}"&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;2) Since there is no concept of classes and everything in Prototypal language is object, we do not use the keyword "&lt;span style="font-weight: bold;"&gt;new&lt;/span&gt;" at all.&lt;br /&gt;&lt;br /&gt;3) extends is achieved by using prototype keyword as shown in the clone() method above.&lt;br /&gt;&lt;br /&gt;4) Any objects can be extended as easily as just defining a method or property on the object directly as follows&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;Author.setName = function(name) {&lt;br /&gt; this.name = name;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-8438983699083948359?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/8438983699083948359/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/all-you-need-to-know-about-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8438983699083948359'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8438983699083948359'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/10/all-you-need-to-know-about-javascript.html' title='All you need to know about Javascript Inheritance'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-3114998451984904839</id><published>2009-09-27T23:03:00.000-07:00</published><updated>2009-09-27T23:08:20.204-07:00</updated><title type='text'>Private Members in Javascript</title><content type='html'>I just had a glance at this wonderful article on Private Members in Javascript, its really worth a read.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Objects&lt;/h2&gt;  &lt;p&gt;JavaScript is fundamentally about &lt;i&gt;objects&lt;/i&gt;. Arrays are objects. Functions are objects. Objects are objects. So what are objects? Objects are collections of name-value pairs. The names are strings, and the values are strings, numbers, booleans, and objects (including arrays and functions). Objects are usually implemented as hashtables so values can be retrieved quickly.&lt;/p&gt;  &lt;p&gt;If a value is a function, we can consider it a &lt;i&gt;method&lt;/i&gt;. When a method of an object is invoked, the &lt;tt&gt;this&lt;/tt&gt; variable is set to the object. The method can then access the instance variables through the &lt;tt&gt;this&lt;/tt&gt; variable.&lt;/p&gt;  &lt;p&gt;Objects can be produced by &lt;i&gt;constructors&lt;/i&gt;, which are functions which initialize objects. Constructors provide the features that classes provide in other languages, including static variables and methods.&lt;/p&gt;  &lt;h2&gt;Public&lt;/h2&gt;  &lt;p&gt;The members of an object are all &lt;i&gt;public&lt;/i&gt; members. Any function can access, modify, or delete those members, or add new members. There are two main ways of putting members in a new object:&lt;/p&gt;  &lt;h3&gt;In the constructor&lt;/h3&gt;  &lt;p&gt;This technique is usually used to initialize public instance variables. The constructor's &lt;tt&gt;this&lt;/tt&gt; variable is used to add members to the object.&lt;/p&gt;  &lt;blockquote&gt;&lt;pre&gt;function Container(param) {&lt;br /&gt;  this.member = param;&lt;br /&gt;}&lt;/pre&gt;&lt;/blockquote&gt;  &lt;p&gt;So, if we construct a new object&lt;/p&gt;  &lt;blockquote&gt;&lt;pre&gt;var myContainer = new Container('abc');&lt;/pre&gt;&lt;/blockquote&gt;  &lt;p&gt;then &lt;tt&gt;myContainer.member&lt;/tt&gt; contains &lt;tt&gt;'abc'&lt;/tt&gt;.&lt;/p&gt;  &lt;h3&gt;In the prototype&lt;/h3&gt;  &lt;p&gt;This technique is usually used to add public methods. When a member is sought and it isn't found in the object itself, then it is taken from the object's constructor's &lt;tt&gt;prototype&lt;/tt&gt; member. The prototype mechanism is used for inheritance. It also conserves memory. To add a method to all objects made by a constructor, add a function to the constructor's &lt;tt&gt;prototype&lt;/tt&gt;:&lt;/p&gt;  &lt;blockquote&gt;&lt;pre&gt;Container.prototype.stamp = function (string) {&lt;br /&gt;  return this.member + string;&lt;br /&gt;}&lt;/pre&gt;&lt;/blockquote&gt;  &lt;p&gt;So, we can invoke the method&lt;/p&gt;  &lt;blockquote&gt;&lt;pre&gt;myContainer.stamp('def')&lt;/pre&gt;&lt;/blockquote&gt;  &lt;p&gt;which produces &lt;tt&gt;'abcdef'&lt;/tt&gt;.&lt;/p&gt;  &lt;h2&gt;Private&lt;/h2&gt;  &lt;p&gt;&lt;i&gt;Private&lt;/i&gt; members are made by the constructor. Ordinary &lt;tt&gt;var&lt;/tt&gt;s and parameters of the constructor becomes the private members.&lt;/p&gt;  &lt;blockquote&gt;&lt;pre name="code" class="javascript"&gt;function Container(param) {&lt;br /&gt;  this.member = param;&lt;br /&gt;  var secret = 3;&lt;br /&gt;  var that = this;&lt;br /&gt;}&lt;/pre&gt;&lt;/blockquote&gt;  &lt;p&gt;This constructor makes three private instance variables: &lt;tt&gt;param&lt;/tt&gt;, &lt;tt&gt;secret&lt;/tt&gt;, and &lt;tt&gt;that&lt;/tt&gt;. They are attached to the object, but they are not accessible to the outside, nor are they accessible to the object's own public methods. They are accessible to private methods. Private methods are inner functions of the constructor.&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre name="code" class="javascript"&gt;function Container(param) {&lt;br /&gt;&lt;br /&gt;  function dec() {&lt;br /&gt;      if (secret &gt; 0) {&lt;br /&gt;          secret -= 1;&lt;br /&gt;          return true;&lt;br /&gt;      } else {&lt;br /&gt;          return false;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  this.member = param;&lt;br /&gt;  var secret = 3;&lt;br /&gt;  var that = this;&lt;br /&gt;}&lt;/pre&gt; &lt;/blockquote&gt;  &lt;p&gt;The private method &lt;tt&gt;dec&lt;/tt&gt; examines the &lt;tt&gt;secret&lt;/tt&gt; instance variable. If it is greater than zero, it decrements &lt;tt&gt;secret&lt;/tt&gt; and returns &lt;tt&gt;true&lt;/tt&gt;. Otherwise it returns &lt;tt&gt;false&lt;/tt&gt;. It can be used to make this object limited to three uses.&lt;/p&gt;  &lt;p&gt;By convention, we make a private &lt;tt&gt;that&lt;/tt&gt; parameter. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes &lt;tt&gt;this&lt;/tt&gt; to be set incorrectly for inner functions.&lt;/p&gt;  &lt;p&gt;Private methods cannot be called by public methods. To make private methods useful, we need to introduce a privileged method.&lt;/p&gt;  &lt;h2&gt;Privileged&lt;/h2&gt;  &lt;p&gt;A &lt;i&gt;privileged&lt;/i&gt; method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.&lt;/p&gt;  &lt;p&gt;Privileged methods are assigned with &lt;tt&gt;this&lt;/tt&gt; within the constructor.&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre name="code" class="javascript"&gt;function Container(param) {&lt;br /&gt;&lt;br /&gt;  function dec() {&lt;br /&gt;      if (secret &gt; 0) {&lt;br /&gt;          secret -= 1;&lt;br /&gt;          return true;&lt;br /&gt;      } else {&lt;br /&gt;          return false;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  this.member = param;&lt;br /&gt;  var secret = 3;&lt;br /&gt;  var that = this;&lt;br /&gt;&lt;br /&gt;  this.service = function () {&lt;br /&gt;      if (dec()) {&lt;br /&gt;          return that.member;&lt;br /&gt;      } else {&lt;br /&gt;          return null;&lt;br /&gt;      }&lt;br /&gt;  };&lt;br /&gt;}&lt;/pre&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;tt&gt;service&lt;/tt&gt; is a privileged method. Calling &lt;tt&gt;myContainer.service()&lt;/tt&gt; will return &lt;tt&gt;'abc'&lt;/tt&gt; the first three times it is called. After that, it will return &lt;tt&gt;null&lt;/tt&gt;. &lt;tt&gt;service&lt;/tt&gt; calls the private &lt;tt&gt;dec&lt;/tt&gt; method which accesses the private &lt;tt&gt;secret&lt;/tt&gt; variable. &lt;tt&gt;service&lt;/tt&gt; is available to other objects and methods, but it does not allow direct access to the private members.&lt;/p&gt;  &lt;h2&gt;Closures&lt;/h2&gt;  &lt;p&gt;This pattern of public, private, and privileged members is possible because JavaScript has &lt;i&gt;closures&lt;/i&gt;. What this means is that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned. This is an extremely powerful property of the language. There is no book currently available on JavaScript programming that shows how to exploit it. Most don't even mention it.&lt;/p&gt;  &lt;p&gt;Private and privileged members can only be made when an object is constructed. Public members can be added at any time.&lt;/p&gt;  &lt;h2&gt;Patterns&lt;/h2&gt;  &lt;h3&gt;Public&lt;/h3&gt;  &lt;blockquote&gt;&lt;tt&gt;function &lt;/tt&gt;&lt;i&gt;Constructor&lt;/i&gt;&lt;tt&gt;(&lt;/tt&gt;...&lt;tt&gt;)    {&lt;/tt&gt;     &lt;blockquote&gt;&lt;tt&gt;this.&lt;/tt&gt;&lt;i&gt;membername&lt;/i&gt;&lt;tt&gt; =       &lt;/tt&gt;&lt;i&gt;value&lt;/i&gt;&lt;tt&gt;;&lt;/tt&gt;&lt;/blockquote&gt;     &lt;p&gt;}&lt;br /&gt;  &lt;i&gt;Constructor&lt;/i&gt;&lt;tt&gt;.prototype.&lt;/tt&gt;&lt;i&gt;membername&lt;/i&gt;&lt;tt&gt; =    &lt;/tt&gt;&lt;i&gt;value&lt;/i&gt;&lt;tt&gt;;&lt;/tt&gt;&lt;/p&gt;&lt;/blockquote&gt;  &lt;h3&gt;Private&lt;/h3&gt;  &lt;blockquote&gt;&lt;tt&gt;function &lt;/tt&gt;&lt;i&gt;Constructor&lt;/i&gt;&lt;tt&gt;(&lt;/tt&gt;...&lt;tt&gt;)    {&lt;/tt&gt;     &lt;blockquote&gt;&lt;tt&gt;var that = this;&lt;br /&gt;     var &lt;/tt&gt;&lt;i&gt;membername&lt;/i&gt;&lt;tt&gt; = &lt;/tt&gt;&lt;i&gt;value&lt;/i&gt;&lt;tt&gt;;&lt;/tt&gt;       &lt;p&gt;&lt;tt&gt;function &lt;/tt&gt;&lt;i&gt;membername&lt;/i&gt;&lt;tt&gt;(&lt;/tt&gt;...&lt;tt&gt;) {&lt;/tt&gt;...&lt;tt&gt;}&lt;/tt&gt;&lt;/p&gt;   &lt;/blockquote&gt;   &lt;p&gt;&lt;tt&gt;}&lt;/tt&gt;&lt;/p&gt;   &lt;p&gt;Note: The function statement&lt;/p&gt;   &lt;blockquote&gt;     &lt;p&gt;&lt;tt&gt;function       &lt;/tt&gt;&lt;i&gt;membername&lt;/i&gt;&lt;tt&gt;(&lt;/tt&gt;...&lt;tt&gt;) {&lt;/tt&gt;...&lt;tt&gt;}&lt;/tt&gt;&lt;/p&gt;   &lt;/blockquote&gt;   &lt;p&gt;is shorthand for&lt;/p&gt;   &lt;blockquote&gt;     &lt;p&gt;&lt;tt&gt;var &lt;/tt&gt;&lt;i&gt;membername&lt;/i&gt;&lt;tt&gt; = function &lt;/tt&gt;&lt;i&gt;membername&lt;/i&gt;&lt;tt&gt;(&lt;/tt&gt;...&lt;tt&gt;)     {&lt;/tt&gt;...&lt;tt&gt;};&lt;/tt&gt;&lt;/p&gt;   &lt;/blockquote&gt; &lt;/blockquote&gt;  &lt;h3&gt;Privileged&lt;/h3&gt;  &lt;blockquote&gt;&lt;tt&gt;function&lt;/tt&gt; &lt;i&gt;Constructor&lt;/i&gt;&lt;tt&gt;(&lt;/tt&gt;...&lt;tt&gt;)    {&lt;/tt&gt;     &lt;blockquote&gt;&lt;tt&gt;this.&lt;/tt&gt;&lt;i&gt;membername&lt;/i&gt;&lt;tt&gt; = function (&lt;/tt&gt;...&lt;tt&gt;) {&lt;/tt&gt;...&lt;tt&gt;};&lt;/tt&gt;&lt;/blockquote&gt;     &lt;p&gt;&lt;tt&gt;}&lt;/tt&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;a href="http://javascript.crockford.com/private.html"&gt;Original Source&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-3114998451984904839?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/3114998451984904839/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/private-members-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3114998451984904839'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3114998451984904839'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/private-members-in-javascript.html' title='Private Members in Javascript'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-888886021098837043</id><published>2009-09-27T10:01:00.000-07:00</published><updated>2009-09-27T10:03:04.449-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>Prototype Based Programming</title><content type='html'>&lt;p&gt;&lt;b&gt;Prototype-based programming&lt;/b&gt; is a style of &lt;a href="http://en.wikipedia.org/wiki/Object-oriented_programming" title="Object-oriented programming"&gt;object-oriented programming&lt;/a&gt; in which &lt;a href="http://en.wikipedia.org/wiki/Class_%28programming%29" title="Class (programming)" class="mw-redirect"&gt;classes&lt;/a&gt; are not present, and behavior reuse (known as &lt;a href="http://en.wikipedia.org/wiki/Inheritance_%28programming%29" title="Inheritance (programming)" class="mw-redirect"&gt;inheritance&lt;/a&gt; in class-based languages) is performed via a process of &lt;a href="http://en.wikipedia.org/wiki/Cloning_%28programming%29" title="Cloning (programming)"&gt;cloning&lt;/a&gt; existing &lt;a href="http://en.wikipedia.org/wiki/Object_%28programming%29" title="Object (programming)" class="mw-redirect"&gt;objects&lt;/a&gt; that serve as &lt;a href="http://en.wikipedia.org/wiki/Prototype" title="Prototype"&gt;prototypes&lt;/a&gt;. This model can also be known as &lt;i&gt;class-less&lt;/i&gt;, &lt;i&gt;prototype-oriented&lt;/i&gt; or &lt;i&gt;instance-based&lt;/i&gt; programming.&lt;/p&gt; &lt;p&gt;The original (and most canonical) example of a prototype-based language is the programming language &lt;a href="http://en.wikipedia.org/wiki/Self_%28programming_language%29" title="Self (programming language)"&gt;Self&lt;/a&gt; developed by &lt;a href="http://en.wikipedia.org/wiki/David_Ungar" title="David Ungar"&gt;David Ungar&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Randall_Smith" title="Randall Smith"&gt;Randall Smith&lt;/a&gt;. However, the classless programming style has recently grown increasingly popular, and has been adopted for the &lt;a href="http://en.wikipedia.org/wiki/Programming_language" title="Programming language"&gt;programming languages&lt;/a&gt; &lt;a href="http://en.wikipedia.org/wiki/JavaScript" title="JavaScript"&gt;JavaScript&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Cecil_%28programming_language%29" title="Cecil (programming language)"&gt;Cecil&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/NewtonScript" title="NewtonScript"&gt;NewtonScript&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Io_%28programming_language%29" title="Io (programming language)"&gt;Io&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/MOO_%28programming_language%29" title="MOO (programming language)"&gt;MOO&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/REBOL" title="REBOL"&gt;REBOL&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Lisaac" title="Lisaac"&gt;Lisaac&lt;/a&gt; and several others.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h2&gt;&lt;span class="mw-headline" id="Comparison_with_class-based_models"&gt;Comparison with class-based models&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;With class-based languages, the structure of &lt;a href="http://en.wikipedia.org/wiki/Object_%28computer_science%29" title="Object (computer science)"&gt;objects&lt;/a&gt; is specified in programmer-defined types called &lt;a href="http://en.wikipedia.org/wiki/Class_%28computer_science%29" title="Class (computer science)"&gt;classes&lt;/a&gt;. While classes define the type of data and functionality that objects will have, &lt;a href="http://en.wikipedia.org/wiki/Instance" title="Instance" class="mw-redirect"&gt;instances&lt;/a&gt; are "usable" objects based on the patterns of a particular class. In this model, classes act as collections of behavior (&lt;a href="http://en.wikipedia.org/wiki/Method_%28computer_science%29" title="Method (computer science)"&gt;methods&lt;/a&gt;) and structure that are the same for all instances, whereas instances carry the objects' data. The role distinction is thus primarily based on a distinction between structure and behavior on the one hand, and &lt;a href="http://en.wikipedia.org/wiki/State_%28computer_science%29" title="State (computer science)"&gt;state&lt;/a&gt; on the other.&lt;/p&gt; &lt;p&gt;Advocates of prototype-based programming often argue that class-based languages encourage a model of development that focuses first on the taxonomy and relationships between classes. In contrast, prototype-based programming is seen as encouraging the programmer to focus on the behavior of some set of examples and only later worry about classifying these objects into archetypal objects that are later used in a fashion similar to classes. As such, many prototype-based systems encourage the alteration of prototypes during &lt;a href="http://en.wikipedia.org/wiki/Run_time_%28computing%29" title="Run time (computing)"&gt;runtime&lt;/a&gt;, whereas only very few class-based object-oriented systems (such as the first dynamic object-oriented system, &lt;a href="http://en.wikipedia.org/wiki/Smalltalk" title="Smalltalk"&gt;Smalltalk&lt;/a&gt;) allow classes to be altered during the execution of a program.&lt;/p&gt; &lt;p&gt;While the vast majority of prototype-based systems are based around interpreted and &lt;a href="http://en.wikipedia.org/wiki/Dynamically_typed" title="Dynamically typed" class="mw-redirect"&gt;dynamically typed&lt;/a&gt; programming languages, it is important to point out that &lt;a href="http://en.wikipedia.org/wiki/Statically_typed" title="Statically typed" class="mw-redirect"&gt;statically typed&lt;/a&gt; systems based around prototypes are technically feasible. The &lt;a href="http://en.wikipedia.org/wiki/Omega_programming_language" title="Omega programming language" class="mw-redirect"&gt;Omega programming language&lt;/a&gt; discussed in &lt;i&gt;Prototype-Based Programming&lt;/i&gt; &lt;sup id="cite_ref-0" class="reference"&gt;&lt;a href="http://en.wikipedia.org/wiki/Prototype-based_programming#cite_note-0"&gt;&lt;span&gt;[&lt;/span&gt;1&lt;span&gt;]&lt;/span&gt;&lt;/a&gt;&lt;/sup&gt; is an example of such a system, though according to Omega's website even Omega is not exclusively static but rather its "compiler may choose to use static binding where this is possible and may improve the efficiency of a program."&lt;/p&gt; &lt;h2&gt;&lt;span class="editsection"&gt;&lt;/span&gt;&lt;span class="mw-headline" id="Object_construction"&gt;Object construction&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;In class-based languages a new instance is constructed through the class's &lt;a href="http://en.wikipedia.org/wiki/Constructor_%28computer_science%29" title="Constructor (computer science)" class="mw-redirect"&gt;constructor&lt;/a&gt; and an optional set of constructor &lt;a href="http://en.wikipedia.org/wiki/Parameter_%28computer_science%29" title="Parameter (computer science)"&gt;arguments&lt;/a&gt;. The resulting instance is modeled on the layout and behavior dictated by the chosen class.&lt;/p&gt; &lt;p&gt;In prototype-based systems there are two methods of constructing new objects, through &lt;i&gt;cloning&lt;/i&gt; of an existing object, and through &lt;i&gt;&lt;a href="http://en.wikipedia.org/wiki/Ex_nihilo" title="Ex nihilo"&gt;ex nihilo&lt;/a&gt;&lt;/i&gt; ("from nothing") object creation. While most systems support a variety of cloning, &lt;i&gt;ex nihilo&lt;/i&gt; object creation is not as prominent.&lt;sup id="cite_ref-1" class="reference"&gt;&lt;a href="http://en.wikipedia.org/wiki/Prototype-based_programming#cite_note-1"&gt;&lt;span&gt;[&lt;/span&gt;2&lt;span&gt;]&lt;/span&gt;&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt; &lt;p&gt;Systems that support &lt;i&gt;ex nihilo&lt;/i&gt; object creation allow new objects to be created from scratch without cloning from an existing prototype. Such systems provide a special syntax for specifying the properties and behaviors of new objects without referencing existing objects. In many prototype languages, there is often a basic &lt;i&gt;Object&lt;/i&gt; prototype that carries commonly needed methods and is used as a master prototype for all other objects. One useful aspect of &lt;i&gt;ex nihilo&lt;/i&gt; object creation is to ensure that a new object's slot names do not have &lt;a href="http://en.wikipedia.org/wiki/Namespace_%28computer_science%29" title="Namespace (computer science)"&gt;namespace&lt;/a&gt; collisions with the top-level &lt;i&gt;Object&lt;/i&gt; object. (In the Mozilla &lt;a href="http://en.wikipedia.org/wiki/JavaScript" title="JavaScript"&gt;JavaScript&lt;/a&gt; implementation, one can accomplish this by setting a newly constructed object's &lt;i&gt;__proto__&lt;/i&gt; property to null.)&lt;/p&gt; &lt;p&gt;&lt;i&gt;Cloning&lt;/i&gt; refers to a process whereby a new object is constructed by copying the behavior of an existing object (its prototype). The new object then carries all the qualities of the original. From this point on, the new object can be modified. In some systems the resulting child object maintains an explicit link (via &lt;a href="http://en.wikipedia.org/wiki/Delegation_%28programming%29" title="Delegation (programming)"&gt;&lt;i&gt;delegation&lt;/i&gt;&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/w/index.php?title=Resemblance_%28programing%29&amp;amp;action=edit&amp;amp;redlink=1" class="new" title="Resemblance (programing) (page does not exist)"&gt;&lt;i&gt;resemblance&lt;/i&gt;&lt;/a&gt;) to its prototype, and changes in the prototype cause corresponding changes to be apparent in its clone. Other systems, such as the &lt;a href="http://en.wikipedia.org/wiki/Forth_%28programming_language%29" title="Forth (programming language)"&gt;Forth&lt;/a&gt;-like programming language &lt;a href="http://en.wikipedia.org/w/index.php?title=Kevo_%28programming_language%29&amp;amp;action=edit&amp;amp;redlink=1" class="new" title="Kevo (programming language) (page does not exist)"&gt;Kevo&lt;/a&gt;, do not propagate change from the prototype in this fashion, and instead follow a more &lt;i&gt;concatenative&lt;/i&gt; model where changes in cloned objects do not automatically propagate across descendants.&lt;sup id="cite_ref-2" class="reference"&gt;&lt;a href="http://en.wikipedia.org/wiki/Prototype-based_programming#cite_note-2"&gt;&lt;span&gt;[&lt;/span&gt;3&lt;span&gt;]&lt;/span&gt;&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt; &lt;div dir="ltr" class="mw-geshi" style="text-align: left;"&gt; &lt;div class="javascript source-javascript" style="font-family: monospace;"&gt; &lt;pre class="de1"&gt;&lt;span class="co1"&gt;//Example of true prototypal inheritance style in JavaScript.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//"ex nihilo" object creation employing the literal object notation {}.&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;var&lt;/span&gt; foo &lt;span class="sy0"&gt;=&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt;one&lt;span class="sy0"&gt;:&lt;/span&gt; 1&lt;span class="sy0"&gt;,&lt;/span&gt; two&lt;span class="sy0"&gt;:&lt;/span&gt; 2&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//another "ex nihilo" object.&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;var&lt;/span&gt; bar &lt;span class="sy0"&gt;=&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt;three&lt;span class="sy0"&gt;:&lt;/span&gt; 3&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Gecko and Webkit JavaScript engines can directly manipulate the internal prototype link.&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//For the sake of simplicity, let's just pretend that the following line works regardless of the engine used:&lt;/span&gt;&lt;br /&gt;bar.__proto__ &lt;span class="sy0"&gt;=&lt;/span&gt; foo&lt;span class="sy0"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;// bar is now the child of foo.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//If we try to access foo's properties from bar from now on, we'll succeed. &lt;/span&gt;&lt;br /&gt;bar.&lt;span class="me1"&gt;one&lt;/span&gt; &lt;span class="co1"&gt;//resolves to 1.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//The child objects properties are also accessible.&lt;/span&gt;&lt;br /&gt;bar.&lt;span class="me1"&gt;three&lt;/span&gt; &lt;span class="co1"&gt;//resolves 3.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt; &lt;/div&gt; &lt;h2&gt;&lt;span class="editsection"&gt;&lt;/span&gt;&lt;span class="mw-headline" id="Delegation"&gt;Delegation&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;In prototype-based languages that use &lt;i&gt;delegation&lt;/i&gt;, the language runtime is capable of &lt;a href="http://en.wikipedia.org/wiki/Dynamic_dispatch" title="Dynamic dispatch"&gt;dispatching&lt;/a&gt; the correct method or finding the right piece of data simply by following a series of delegation pointers (from object to its prototype) until a match is found. All that is required to establish this behavior-sharing between objects is the delegation pointer. Unlike the relationship between class and instance in class-based object-oriented languages, the relationship between the prototype and its offshoots does not require that the child object have a memory or structural similarity to the prototype beyond this link. As such, the child object can continue to be modified and amended over time without rearranging the structure of its associated prototype as in class-based systems. It is also important to note that not only data but also methods can be added or changed. For this reason, most prototype-based languages refer to both data and methods as "slots".&lt;/p&gt;&lt;h2&gt;&lt;span class="mw-headline" id="Criticism"&gt;Criticism&lt;/span&gt;&lt;/h2&gt;&lt;p&gt;Advocates of class-based object models who criticize prototype-based systems often have concerns that could be seen as similar to those concerns that proponents of static type systems for programming languages have of dynamic type systems (see &lt;a href="http://en.wikipedia.org/wiki/Datatype" title="Datatype" class="mw-redirect"&gt;Datatype&lt;/a&gt;). Usually, such concerns involve: &lt;a href="http://en.wikipedia.org/wiki/Correctness" title="Correctness"&gt;correctness&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Safety" title="Safety"&gt;safety&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Predictability" title="Predictability"&gt;predictability&lt;/a&gt;, and &lt;a href="http://en.wikipedia.org/wiki/Algorithmic_efficiency" title="Algorithmic efficiency"&gt;efficiency&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;On the first three points, classes are often seen as analogous to types (in most statically typed object-oriented languages they serve that role) and are proposed to provide contractual guarantees to their instances, and to users of their instances, that they will behave in some given fashion.&lt;/p&gt; &lt;p&gt;On the last point, efficiency, the declaration of classes simplifies many &lt;a href="http://en.wikipedia.org/wiki/Compiler" title="Compiler"&gt;compiler&lt;/a&gt; optimizations that allow developing efficient method and instance variable lookup. For the &lt;a href="http://en.wikipedia.org/wiki/Self_%28programming_language%29" title="Self (programming language)"&gt;Self&lt;/a&gt; language, much development time was spent on developing, compiling, and interpreting techniques to improve the performance of prototype-based systems versus class-based systems. For example, the &lt;a href="http://en.wikipedia.org/wiki/Lisaac" title="Lisaac"&gt;Lisaac&lt;/a&gt; compiler produces code almost as fast as C. Tests have been run with an MPEG-2 codec written in Lisaac, copied from a C version. These tests show the Lisaac version is 1.9% slower than the C version with 37% fewer lines of code&lt;sup id="cite_ref-4" class="reference"&gt;&lt;a href="http://en.wikipedia.org/wiki/Prototype-based_programming#cite_note-4"&gt;&lt;span&gt;[&lt;/span&gt;5&lt;span&gt;]&lt;/span&gt;&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt; &lt;p&gt;The most common criticism made against prototype-based languages is that the community of &lt;a href="http://en.wikipedia.org/wiki/Software_developer" title="Software developer"&gt;software developers&lt;/a&gt; is not familiar with them, despite the popularity and market permeation of &lt;a href="http://en.wikipedia.org/wiki/JavaScript" title="JavaScript"&gt;JavaScript&lt;/a&gt;. This knowledge level of prototype based systems seems to be changing with the proliferation of &lt;a href="http://en.wikipedia.org/wiki/JavaScript_framework" title="JavaScript framework" class="mw-redirect"&gt;JavaScript frameworks&lt;/a&gt; and increases in the complex use of &lt;a href="http://en.wikipedia.org/wiki/JavaScript" title="JavaScript"&gt;JavaScript&lt;/a&gt; as "&lt;a href="http://en.wikipedia.org/wiki/Web_2.0" title="Web 2.0"&gt;Web 2.0&lt;/a&gt;" matures.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-888886021098837043?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/888886021098837043/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/prototype-based-programming.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/888886021098837043'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/888886021098837043'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/prototype-based-programming.html' title='Prototype Based Programming'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-2105724996131336555</id><published>2009-09-27T07:54:00.000-07:00</published><updated>2009-09-27T08:25:27.791-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>Scope in Javascript</title><content type='html'>Scope is one of the foundational aspects of the JavaScript language, and probably the one I’ve struggled with the most when building complex programs. I can’t count the number of times I’ve lost track of what the this keyword refers to after passing control around from function to function, and I’ve often found myself contorting my code in all sorts of confusing ways, trying to retain some semblance of sanity in my understanding of which variables were accessible where.&lt;br /&gt;&lt;br /&gt;  In typical object-oriented programming, we need a way of identifying and referring to the object that we’re currently working with. this serves the purpose admirably, providing our objects the ability to examine themselves, and point at their own properties.&lt;br /&gt;&lt;br /&gt;This article will tackle the problem head-on, outlining definitions of context and scope, examining two JavaScript methods that allow us to manipulate context, and concluding with a deep dive into an effective solution to ninety percent of the problems I’ve run into.&lt;br /&gt;Where Am I? And Who Are You?&lt;br /&gt;&lt;br /&gt;Every bit of your JavaScript program is executed in one execution context or another. You can think of these contexts as your code’s neighborhood, giving each line an understanding of where it comes from, and who its friends and neighbors are. As it turns out, this is important information, as JavaScript societies have fairly strict rules about who can associate with whom; execution contexts are better thought of as gated communities than as open subdivisions.&lt;br /&gt;&lt;br /&gt;We can refer to these social boundaries generally as scope, and they’re important enough to be codified in each neighborhood’s charter, which we’ll refer to as the context’s scope chain. Code within a particular neighborhood can only access variables listed on its scope chain, and prefers interaction with locals to associations outside its neighborhood.&lt;br /&gt;&lt;br /&gt;Practically speaking, evaluating a function establishes a distinct execution context that appends its local scope to the scope chain it was defined within. JavaScript resolves identifiers within a particular context by climbing up the scope chain, moving locally to globally. This means that local variables with the same name as variables higher up on the scope chain take precedence, which makes sense: If my good friends are talking together about “Mike West,” it’s pretty clear that they’re talking about me, not about the bluegrass singer or the Duke professor, even though the latter two are (arguably) better known.&lt;br /&gt;&lt;br /&gt;Let’s walk through some example code to explore the implications:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;&amp;lt;script type="text/javascript"&gt;&lt;br /&gt;var ima_celebrity = "Everyone can see me! I'm famous!",&lt;br /&gt;the_president = "I'm the decider!";&lt;br /&gt;&lt;br /&gt;function pleasantville() {&lt;br /&gt;var the_mayor = "I rule Pleasantville with an iron fist!",&lt;br /&gt; ima_celebrity = "All my neighbors know who I am!";&lt;br /&gt;&lt;br /&gt;function lonely_house() {&lt;br /&gt; var agoraphobic = "I fear the day star!",&lt;br /&gt;  a_cat = "Meow.";&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&amp;lt;/script&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Our global star, ima_celebrity, is recognized by everyone. She’s politically active, talking with the_president on a fairly frequent basis, and incredibly friendly; she’ll sign autographs and answer questions for anyone she runs into. That said, she doesn’t have a whole lot of personal contact with her fans. She’s pretty sure they exist and that they probably have lives of their own somewhere, but she certainly doesn’t know what they’re doing, or even their names.&lt;br /&gt;&lt;br /&gt;Inside pleasantville, the_mayor is a well-known face. She’s always walking the streets of her town, chatting up her constituents, shaking hands, and kissing babies. As pleasantville is a big, important neighborhood, she’s got a big red phone in her office, giving her a direct line to the president (or at least a top aide) 24 hours a day, 7 days a week. She’s seen lonely_house up on a hill at the outskirts of town, but never really worried about who lives inside.&lt;br /&gt;&lt;br /&gt;That lonely_house is a world unto itself. The agoraphobic stays inside most of the time, playing solitaire and feeding a_cat. He’s called the_mayor a few times to ask about local noise regulations, and even wrote ima_celebrity (Pleasantville’s ima_celebrity, that is) some fan mail after seeing her on the local news.&lt;br /&gt;this? What’s that?&lt;br /&gt;&lt;br /&gt;In addition to establishing a scope chain, each execution context offers a keyword named this. In its most common usage, this serves as an identity function, providing our neighborhoods a way of referring to themselves. We can’t always rely on that behavior, however: Depending on how we get into a particular neighborhood, this might mean something else entirely. In fact, how we get into the neighborhood is itself exactly what this generally refers to. Four scenarios deserve special attention:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;  * Calling an Object’s Method&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    In typical object-oriented programming, we need a way of identifying and referring to the object that we’re currently working with. this serves the purpose admirably, providing our objects the ability to examine themselves, and point at their own properties.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;     &amp;lt;script type="text/javascript"&gt;&lt;br /&gt;      var deep_thought = {&lt;br /&gt;       the_answer: 42,&lt;br /&gt;       ask_question: function () {&lt;br /&gt;        return this.the_answer;&lt;br /&gt;       }&lt;br /&gt;      };&lt;br /&gt;   &lt;br /&gt;      var the_meaning = deep_thought.ask_question();&lt;br /&gt;     &amp;lt;/script&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;    This example builds an object named deep_thought, sets its the_answer property to 42, and creates an ask_question method. When deep_thought.ask_question() is executed, JavaScript establishes an execution context for the function call, setting this to the object referenced by whatever came before the last ”.”, in this case: deep_thought. The method can then look in the mirror via this to examine its own properties, returning the value stored in this.the_answer: 42.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;  * Constructor&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    Likewise, when defining a function to be used as a constructor with the new keyword, this can be used to refer to the object being created. Let’s rewrite the example above to reflect that scenario:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;     &amp;lt;script type="text/javascript"&gt;&lt;br /&gt;      function BigComputer(answer) {&lt;br /&gt;       this.the_answer = answer;&lt;br /&gt;       this.ask_question = function () {&lt;br /&gt;        return this.the_answer;&lt;br /&gt;       }&lt;br /&gt;      }&lt;br /&gt;   &lt;br /&gt;      var deep_thought = new BigComputer(42);&lt;br /&gt;      var the_meaning = deep_thought.ask_question();&lt;br /&gt;     &amp;lt;/script&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;    Instead of explicitly creating the deep_thought object, we’ll write a function to create BigComputer objects, and instantiate deep_thought as an instance variable via the new keyword. When new BigComputer() is executed, a completely new object is created transparently in the background. BigComputer is called, and its this keyword is set to reference that new object. The function can set properties and methods on this, which is transparently returned at the end of BigComputer’s execution.&lt;br /&gt;&lt;br /&gt;    Notice, though, that deep_thought.the_question() still works just as it did before. What’s going on there? Why does this mean something different inside the_question than it does inside BigComputer? Put simply, we entered BigComputer via new, so this meant “the new object.” On the other hand, we entered the_question via deep_thought, so while we’re executing that method, this means “whatever deep_thought refers to”. this is not read from the scope chain as other variables are, but instead is reset on a context by context basis.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;  * Function Call&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    What if we just call a normal, everyday function without any of this fancy object stuff? What does this mean in that scenario?&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;     &amp;lt;script type="text/javascript"&gt;&lt;br /&gt;      function test_this() {&lt;br /&gt;       return this;&lt;br /&gt;      }&lt;br /&gt;      var i_wonder_what_this_is = test_this();&lt;br /&gt;     &amp;lt;/script&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;    In this case, we weren’t provided a context by new, nor were we given a context in the form of an object to piggyback off of. Here, this defaults to reference the most global thing it can: for web pages, this is the window object.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;  * Event Handler&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    For a more complicated twist on the normal function call, let’s say that we’re using a function to handle an onclick event. What does this mean when the event triggers our function’s execution? Unfortunately, there’s not a simple answer to this question.&lt;br /&gt;&lt;br /&gt;    If we write the event handler inline, this refers to the global window object:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;     &amp;lt;script type="text/javascript"&gt;&lt;br /&gt;      function click_handler() {&lt;br /&gt;       alert(this); // alerts the window object&lt;br /&gt;      }&lt;br /&gt;     &amp;lt;/script&gt;&lt;br /&gt;     ...&lt;br /&gt;     &amp;lt;button id='thebutton' onclick='click_handler()'&gt;Click me!&amp;lt;/button&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;    However, when we add an event handler via JavaScript, this refers to the DOM element that generated the event. (Note: The event handling shown here is short and readable, but otherwise poor. Please use a real addEvent function instead.):&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;     &amp;lt;script type="text/javascript"&gt;&lt;br /&gt;      function click_handler() {&lt;br /&gt;       alert(this); // alerts the button DOM node&lt;br /&gt;      }&lt;br /&gt;   &lt;br /&gt;      function addhandler() {&lt;br /&gt;       document.getElementById('thebutton').onclick = click_handler;&lt;br /&gt;      }&lt;br /&gt;   &lt;br /&gt;      window.onload = addhandler;&lt;br /&gt;     &amp;lt;/script&gt;&lt;br /&gt;     ...&lt;br /&gt;     &amp;lt;button id='thebutton'&gt;Click me!&amp;lt;/button&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Complications&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let’s run with that last example for a moment longer. What if instead of running click_handler, we wanted to ask deep_thought a question every time we clicked the button? The code for that seems pretty straightforward; we might try this:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;&amp;lt;script type="text/javascript"&gt;&lt;br /&gt;function BigComputer(answer) {&lt;br /&gt;this.the_answer = answer;&lt;br /&gt;this.ask_question = function () {&lt;br /&gt; alert(this.the_answer);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function addhandler() {&lt;br /&gt;var deep_thought = new BigComputer(42),&lt;br /&gt; the_button = document.getElementById('thebutton');&lt;br /&gt;&lt;br /&gt;the_button.onclick = deep_thought.ask_question;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;window.onload = addhandler;&lt;br /&gt;&amp;lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Perfect, right? We click on the button, deep_thought.ask_question is executed, and we get back “42.” So why is the browser giving us undefined instead? What did we do wrong?&lt;br /&gt;&lt;br /&gt;The problem is simply this: We’ve passed off a reference to the ask_question method, which, when executed as an event handler, runs in a different context than when it’s executed as an object method. In short, the this keyword in ask_question is pointing at the DOM element that generated the event, not at a BigComputer object. The DOM element doesn’t have a the_answer property, so we’re getting back undefined instead of “42.” setTimeout exhibits similar behavior, delaying the execution of a function while at the same time moving it out into a global context.&lt;br /&gt;&lt;br /&gt;This issue crops up all over the place in our programs, and it’s a terribly difficult problem to debug without keeping careful track of what’s going on in all the corners of your program, especially if your object has properties that do exist on DOM elements or the window object.&lt;br /&gt;Manipulating Context With .apply() and .call()&lt;br /&gt;&lt;br /&gt;We really do want to be able to ask deep_thought a question when we click the button, and more generally, we do want to be able to call object methods in their native context when responding to things like events and setTimeout calls. Two little-known JavaScript methods, apply and call, indirectly enable this functionality by allowing us to manually override the default value of this when we execute a function call. Let’s look at call first:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;&amp;lt;script type="text/javascript"&gt;&lt;br /&gt;var first_object = {&lt;br /&gt;num: 42&lt;br /&gt;};&lt;br /&gt;var second_object = {&lt;br /&gt;num: 24&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;function multiply(mult) {&lt;br /&gt;return this.num * mult;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;multiply.call(first_object, 5); // returns 42 * 5&lt;br /&gt;multiply.call(second_object, 5); // returns 24 * 5&lt;br /&gt;&amp;lt;/script&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In this example, we first define two objects, first_object and second_object, each with a num property. Then we define a multiply function that accepts a single argument, and returns the product of that argument, and the num property of its this object. If we called that function by itself, the answer returned would almost certainly be undefined, since the global window object doesn’t have a num property unless we explicitly set one. We need some way of telling multiply what its this keyword ought refer to; the call method of the multiply function is exactly what we’re looking for.&lt;br /&gt;&lt;br /&gt;The first argument to call defines what this means inside the executed function. The remaining arguments to call are passed into the executed function, just as if you’d called it yourself. So, when multiply.call(first_object, 5) is executed, the multiply function is called, 5 is passed in as the first argument, and the this keyword is set to refer to object first_object. Likewise, when multiply.call(second_object, 5) is executed, the multiply function is called, 5 is passed in as the first argument, and the this keyword is set to refer to object second_object.&lt;br /&gt;&lt;br /&gt;apply works in exactly the same way as call, but allows you to wrap up the arguments to the called function in an array, which can be quite useful when programatically generating function calls. Replicating the functionality we just talked about using apply is trivial:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;&amp;lt;script type="text/javascript"&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;multiply.apply(first_object, [5]); // returns 42 * 5&lt;br /&gt;multiply.apply(second_object, [5]); // returns 24 * 5&lt;br /&gt;&amp;lt;/script&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;apply and call are very useful on their own, and well worth keeping around in your toolkit, but they only get us halfway to solving the problem of context shifts for event handlers. It’s easy to think that we could solve the problem by simply using call to shift the meaning of this when we set up the handler:&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;function addhandler() {&lt;br /&gt;var deep_thought = new BigComputer(42),&lt;br /&gt;the_button = document.getElementById('thebutton');&lt;br /&gt;&lt;br /&gt;the_button.onclick = deep_thought.ask_question.call(deep_thought);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The problem with this line of reasoning is simple: call executes the function immediately. Instead of providing a function reference to the onclick handler, we’re giving it the result of an executed function. We need to exploit another feature of JavaScript to really solve this problem.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;The Beauty of .bind()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I’m not a huge fan of the Prototype JavaScript framework, but I am very much impressed with the quality of its code as a whole. In particular, one simple addition it makes to the Function object has had a hugely positive impact on my ability to manage the context in which function calls execute: bind performs the same general task as call, altering the context in which a function executes. The difference is that bind returns a function reference that can be used later, rather than the result of an immediate execution that we get with call.&lt;br /&gt;&lt;br /&gt;If we simplify the bind function a bit to get at the key concepts, we can insert it into the multiplication example we discussed earlier to really dig into how it works; it’s quite an elegant solution:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;&amp;lt;script type="text/javascript"&gt;&lt;br /&gt;var first_object = {&lt;br /&gt;num: 42&lt;br /&gt;};&lt;br /&gt;var second_object = {&lt;br /&gt;num: 24&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;function multiply(mult) {&lt;br /&gt;return this.num * mult;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Function.prototype.bind = function(obj) {&lt;br /&gt;var method = this,&lt;br /&gt; temp = function() {&lt;br /&gt;  return method.apply(obj, arguments);&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;return temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;var first_multiply = multiply.bind(first_object);&lt;br /&gt;first_multiply(5); // returns 42 * 5&lt;br /&gt;&lt;br /&gt;var second_multiply = multiply.bind(second_object);&lt;br /&gt;second_multiply(5); // returns 24 * 5&lt;br /&gt;&amp;lt;/script&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;First, we define first_object, second_object, and the multiply function, just as before. With those taken care of, we move on to creating a bind method on the Function object’s prototype, which has the effect of making bind available for all functions in our program. When multiply.bind(first_object) is called, JavaScript creates an execution context for the bind method, setting this to the multiply function, and setting the first argument, obj, to reference first_object. So far, so good.&lt;br /&gt;&lt;br /&gt;The real genius of this solution is the creation of method, set equal to this (the multiply function itself). When the anonymous function is created on the next line, method is accessible via its scope chain, as is obj (this couldn’t be used here, because when the newly created function is executed, this will be overwritten by a new, local context). This alias to this makes it possible to use apply to execute the multiply function, passing in obj to ensure that the context is set correctly. In computer-science-speak, temp is a closure that, when returned at the end of the bind call, can be used in any context whatsoever to execute multiply in the context of first_object.&lt;br /&gt;&lt;br /&gt;This is exactly what we need for the event handler and setTimeout scenarios discussed above. The following code solves that problem completely, binding the deep_thought.ask_question method to the deep_thought context, so that it executes correctly whenever the event is triggered:&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="javascript"&gt;&lt;br /&gt;function addhandler() {&lt;br /&gt;var deep_thought = new BigComputer(42),&lt;br /&gt;the_button = document.getElementById('thebutton');&lt;br /&gt;&lt;br /&gt;the_button.onclick = deep_thought.ask_question.bind(deep_thought);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Beautiful.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;References&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;     * &lt;a href="http://www.jibbering.com/faq/faq_notes/closures.html"&gt;JavaScript Closures&lt;/a&gt; is the best resource on the net for a thorough discussion of closures: what they do, how they do it, and how to use them without going insane.&lt;br /&gt;  * The Protype JavaScript Framework is full of little nuggets like bind. The version available here not only allows the binding of a particular this value, but also of some or all of a function’s arguments, which comes in handy all too often.&lt;br /&gt;  * &lt;a href="http://javascript.crockford.com/"&gt;Douglas Crockford’s JavaScript essays&lt;/a&gt; are excellent resources for both basic and advanced JavaScript programmers. The man knows what he’s talking about, and explains difficult concepts in an easy-to-grasp manner.&lt;br /&gt;  * &lt;a href="http://digital-web.com/articles/variable_scope_for_new_programmers/"&gt;Variable Scope for New Programmers&lt;/a&gt; is a good article if you'd like more discussion of scope from a beginner's perspective. Written by Jonathan Snook, and published in this very magazine at the end of last year, it's still an informative and useful read.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.digital-web.com/articles/scope_in_javascript/"&gt;Original Source&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-2105724996131336555?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/2105724996131336555/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/scope-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2105724996131336555'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2105724996131336555'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/scope-in-javascript.html' title='Scope in Javascript'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-6409543728261742667</id><published>2009-09-26T04:44:00.001-07:00</published><updated>2009-09-27T08:10:55.714-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Practise.ExtJs'/><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>First ExtJs program</title><content type='html'>&lt;pre name="code" class="xml"&gt;&lt;br /&gt;&amp;lt;html&gt;&lt;br /&gt;&amp;lt;head&gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;!--&lt;br /&gt;       The following css and scripts are needed for any ext js programming.&lt;br /&gt;       ext-all.css is the css file, ext-base.js is the core ext js file&lt;br /&gt;       which helps in AJAX Communication, DOM Manipulation, Event Management.&lt;br /&gt;       ext-all-debug.js contains the ui components, data services, remoting, Drag &amp;amp; Drop and Utilities&lt;br /&gt;&lt;br /&gt;   --&gt;&lt;br /&gt;   &amp;lt;link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" /&gt;&lt;br /&gt;   &amp;lt;script type="text/javascript" src="../adapter/ext/ext-base.js"&gt;&amp;lt;/script&gt;&lt;br /&gt;   &amp;lt;script type="text/javascript" src="../ext-all-debug.js"&gt;&amp;lt;/script&gt;&lt;br /&gt;   &amp;lt;script type="text/javascript"&gt;&lt;br /&gt;       Ext.BLANK_IMAGE_URL = '../resources/images/default/s.gif';&lt;br /&gt;   &amp;lt;/script&gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;script type="text/javascript"&gt;&lt;br /&gt;       function buildWindow() {&lt;br /&gt;           var win = new Ext.Window(&lt;br /&gt;&lt;br /&gt;           //Configuration Node for the Window&lt;br /&gt;           {&lt;br /&gt;               id : 'myWindow',&lt;br /&gt;               title : 'My first Ext JS Window',&lt;br /&gt;               width : 300,&lt;br /&gt;               height : 150,&lt;br /&gt;               layout : 'fit',&lt;br /&gt;           });&lt;br /&gt;           win.show(); // 3&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       //Entry point for any ext js code&lt;br /&gt;       Ext.onReady(buildWindow);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;/script&gt;&lt;br /&gt;&amp;lt;/head&gt;&lt;br /&gt;&amp;lt;body&gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;script type='text/javascript'&gt;&lt;br /&gt;       function highlightWindow() {&lt;br /&gt;           var win = Ext.getCmp('myWindow');&lt;br /&gt;           var winBody = win.body;&lt;br /&gt;           winBody.highlight();&lt;br /&gt;       }&lt;br /&gt;       highlightWindow.defer(1000);&lt;br /&gt;   &amp;lt;/script&gt;&lt;br /&gt;&amp;lt;/body&gt;&lt;br /&gt;&amp;lt;/html&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-6409543728261742667?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/6409543728261742667/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/first-extjs-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6409543728261742667'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6409543728261742667'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/first-extjs-program.html' title='First ExtJs program'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-4196760678224796966</id><published>2009-09-12T05:57:00.000-07:00</published><updated>2009-09-12T05:58:48.736-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Saving (detached) entities</title><content type='html'>&lt;p&gt;Saving an entity in JPA is simple, right? We just pass the object we want to persist to &lt;a set="yes" linkindex="139" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#persist%28java.lang.Object%29"&gt;&lt;tt&gt;EntityManager.persist&lt;/tt&gt;&lt;/a&gt;. It all seems to work quite well until we run into the dreaded &lt;a set="yes" linkindex="140" href="http://www.google.com/search?q=%22detached+entity+passed+to+persist%22"&gt;"detached entity passed to persist"&lt;/a&gt; message. Or a similar message when we use a different JPA provider than the Hibernate EntityManager.&lt;br /&gt;&lt;span id="more-914"&gt;&lt;/span&gt;&lt;br /&gt;So what is that detached entity the message talks about? A detached entity (a.k.a. a detached object) is an object that has the same ID as an entity in the persistence store but that is no longer part of a persistence context (the scope of an &lt;tt&gt;EntityManager&lt;/tt&gt; session). The two most common causes for this are:&lt;/p&gt; &lt;ul&gt;&lt;li&gt;The EntityManager from which the object was retrieved has been &lt;a set="yes" linkindex="141" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#close%28%29"&gt;closed&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;The object was received from outside of our application, e.g. as part of a form submission, a remoting protocol such as &lt;a set="yes" linkindex="142" href="http://hessian.caucho.com/"&gt;Hessian&lt;/a&gt;, or through a &lt;a set="yes" linkindex="143" href="http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=lcconfig_2.html"&gt;BlazeDS AMF Channel&lt;/a&gt; from a Flex client.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The contract for persist (see section 3.2.1 of the &lt;a set="yes" linkindex="144" href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html"&gt;JPA 1.0 spec&lt;/a&gt;) explicitly states that an &lt;tt&gt;EntityExistsException&lt;/tt&gt; is thrown by the persist method when the object passed in is a detached entity. Or any other &lt;a set="yes" linkindex="145" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/PersistenceException.html"&gt;&lt;tt&gt;PersistenceException&lt;/tt&gt;&lt;/a&gt; when the persistence context is &lt;a set="yes" linkindex="146" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#flush%28%29"&gt;flushed&lt;/a&gt; or the transaction is &lt;a set="yes" linkindex="147" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityTransaction.html#commit%28%29"&gt;committed&lt;/a&gt;. Note that it is &lt;em&gt;not&lt;/em&gt; a problem to persist the same object twice within one transaction. The second invocation will just be ignored, although the persist operation might be cascaded to any associations of the entity that were added since the first invocation. Apart from that latter consideration there is no need to invoke &lt;tt&gt;EntityManager.persist&lt;/tt&gt; on an already persisted entity because any changes will automatically be saved at flush or commit time.&lt;/p&gt; &lt;h3&gt;saveOrUpdate vs. merge&lt;/h3&gt; &lt;p&gt;Those of you that have worked with plain Hibernate will probably have grown quite accustomed to using the &lt;a set="yes" linkindex="148" href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#saveOrUpdate%28java.lang.Object%29"&gt;&lt;tt&gt;Session.saveOrUpdate&lt;/tt&gt;&lt;/a&gt; method to save entities. The &lt;tt&gt;saveOrUpdate&lt;/tt&gt; method figures out whether the object is new or has already been saved before. In the first case the entity is &lt;a linkindex="149" href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#save%28java.lang.Object%29"&gt;saved&lt;/a&gt;, in the latter case it is &lt;a set="yes" linkindex="150" href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#update%28java.lang.Object%29"&gt;updated&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;When switching from Hibernate to JPA &lt;a set="yes" linkindex="151" href="http://forum.springframework.org/showthread.php?t=36064"&gt;a lot of people&lt;/a&gt; &lt;a set="yes" linkindex="152" href="http://www.experts-exchange.com/Programming/Languages/Java/Q_23941563.html"&gt;are dismayed to find&lt;/a&gt; &lt;a set="yes" linkindex="153" href="http://forums.java.net/jive/thread.jspa?threadID=28896"&gt;that method missing&lt;/a&gt;. The closest alternative seems to be the &lt;a set="yes" linkindex="154" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#merge%28T%29"&gt;&lt;tt&gt;EntityManager.merge&lt;/tt&gt;&lt;/a&gt; method, but there is a big difference that has important implications. The &lt;tt&gt;Session.saveOrUpdate&lt;/tt&gt; method, and its cousin &lt;tt&gt;Session.update&lt;/tt&gt;, attach the passed entity to the persistence context while &lt;tt&gt;EntityManager.merge&lt;/tt&gt; method copies the state of the passed object to the persistent entity with the same identifier and then return a reference to that persistent entity. The object passed is &lt;em&gt;not&lt;/em&gt; attached to the persistence context.&lt;/p&gt; &lt;p&gt;That means that after invoking &lt;tt&gt;EntityManager.merge&lt;/tt&gt;, we have to use the entity reference returned from that method in place of the original object passed in. This is unlike the the way one can simply invoke &lt;tt&gt;EntityManager.persist&lt;/tt&gt; on an object (even multiple times as mentioned above!) to save it and continue to use the original object. Hibernate's &lt;tt&gt;Session.saveOrUpdate&lt;/tt&gt; &lt;em&gt;does&lt;/em&gt; share that nice behaviour with &lt;tt&gt;EntityManager.persist&lt;/tt&gt; (or rather &lt;tt&gt;Session.save&lt;/tt&gt;) even when updating, but it has one big drawback; if an entity with the same ID as the one we are trying to update, i.e. reattach, is already part of the persistence context, a &lt;a set="yes" linkindex="155" href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/NonUniqueObjectException.html"&gt;&lt;tt&gt;NonUniqueObjectException&lt;/tt&gt;&lt;/a&gt; is thrown. And figuring out what piece of code persisted (or merged or retrieved) that other entity is harder than figuring out why we get a "detached entity passed to persist" message.&lt;/p&gt; &lt;h3&gt;Putting it all together&lt;/h3&gt; &lt;p&gt;So let's examine the three possible cases and what the different methods do:&lt;/p&gt; &lt;table&gt; &lt;tbody&gt;&lt;tr valign="top"&gt; &lt;th&gt;Scenario&lt;/th&gt; &lt;th&gt;&lt;tt&gt;EntityManager.persist&lt;/tt&gt;&lt;/th&gt; &lt;th&gt;&lt;tt&gt;EntityManager.merge&lt;/tt&gt;&lt;/th&gt; &lt;th&gt;&lt;tt&gt;SessionManager.saveOrUpdate&lt;/tt&gt;&lt;/th&gt; &lt;/tr&gt; &lt;tr valign="top"&gt; &lt;th&gt;Object passed was never persisted&lt;/th&gt; &lt;td&gt;1. Object added to persistence context as new entity&lt;br /&gt;2. New entity inserted into database at flush/commit&lt;/td&gt; &lt;td&gt;1. State copied to new entity.&lt;br /&gt;2. New entity added to persistence context&lt;br /&gt;3. New entity inserted into database at flush/commit&lt;br /&gt;4. New entity returned&lt;/td&gt; &lt;td&gt;1. Object added to persistence context as new entity&lt;br /&gt;2. New entity inserted into database at flush/commit&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="top"&gt; &lt;th&gt;Object was previously persisted, but not loaded in this persistence context&lt;/th&gt; &lt;td&gt;1. &lt;tt&gt;EntityExistsException&lt;/tt&gt; thrown (or a &lt;tt&gt;PersistenceException&lt;/tt&gt; at flush/commit)&lt;/td&gt; &lt;td&gt;2. Existing entity loaded.&lt;br /&gt;2. State copied from object to loaded entity&lt;br /&gt;3. Loaded entity updated in database at flush/commit&lt;br /&gt;4. Loaded entity returned&lt;/td&gt; &lt;td&gt;1. Object added to persistence context&lt;br /&gt;2. Loaded entity updated in database at flush/commit&lt;/td&gt; &lt;/tr&gt; &lt;tr valign="top"&gt; &lt;th&gt;Object was previously persisted and already loaded in this persistence context&lt;/th&gt; &lt;td&gt;1. &lt;tt&gt;EntityExistsException&lt;/tt&gt; thrown (or a &lt;tt&gt;PersistenceException&lt;/tt&gt; at flush or commit time)&lt;/td&gt; &lt;td&gt;1. State from object copied to loaded entity&lt;br /&gt;2. Loaded entity updated in database at flush/commit&lt;br /&gt;3. Loaded entity returned&lt;/td&gt; &lt;td&gt;1. &lt;tt&gt;NonUniqueObjectException&lt;/tt&gt; thrown&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;Looking at that table one may begin to understand why the &lt;tt&gt;saveOrUpdate&lt;/tt&gt; method never became a part of the JPA specification and why the JSR members instead choose to go with the &lt;tt&gt;merge&lt;/tt&gt; method. BTW, you can find a different angle on the &lt;tt&gt;saveOrUpdate&lt;/tt&gt; vs. &lt;tt&gt;merge&lt;/tt&gt; problem in &lt;a set="yes" linkindex="156" href="http://www.stevideter.com/2008/12/07/saveorupdate-versus-merge-in-hibernate/"&gt;Stevi Deter's blog about the subject&lt;/a&gt;.&lt;/p&gt; &lt;h3&gt;The problem with merge&lt;/h3&gt; &lt;p&gt;Before we continue, we need to discuss one disadvantage of the way &lt;tt&gt;EntityManager.merge&lt;/tt&gt; works; it can easily break bidirectional associations. Consider the example with the &lt;tt&gt;Order&lt;/tt&gt; and &lt;tt&gt;OrderLine&lt;/tt&gt; classes from &lt;a set="yes" linkindex="157" href="http://blog.xebia.com/2009/03/16/jpa-implementation-patterns-bidirectional-assocations/"&gt;the previous blog in this series&lt;/a&gt;. If an updated &lt;tt&gt;OrderLine&lt;/tt&gt; object is received from a web front end (or from a Hessian client, or a Flex application, etc.) the &lt;tt&gt;order&lt;/tt&gt; field might be set to &lt;tt&gt;null&lt;/tt&gt;. If that object is then merged with an already loaded entity, the &lt;tt&gt;order&lt;/tt&gt; field of that entity is set to &lt;tt&gt;null&lt;/tt&gt;. But it won't be removed from the &lt;tt&gt;orderLines&lt;/tt&gt; set of the &lt;tt&gt;Order&lt;/tt&gt; it used to refer to, thereby breaking the invariant that every element in an &lt;tt&gt;Order&lt;/tt&gt;'s &lt;tt&gt;orderLines&lt;/tt&gt; set has its &lt;tt&gt;order&lt;/tt&gt; field set to point back at that &lt;tt&gt;Order&lt;/tt&gt;.&lt;/p&gt; &lt;p&gt;In this case, or other cases where the simplistic way &lt;tt&gt;EntityManager.merge&lt;/tt&gt; copies the object state into the loaded entity causes problems, we can fall back to the &lt;em&gt;DIY merge pattern&lt;/em&gt;. Instead of invoking &lt;tt&gt;EntityManager.merge&lt;/tt&gt; we invoke &lt;a set="yes" linkindex="158" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#find%28java.lang.Class,%20java.lang.Object%29"&gt;&lt;tt&gt;EntityManager.find&lt;/tt&gt;&lt;/a&gt; to find the existing entity and copy over the state ourselves. If &lt;tt&gt;EntityManager.find&lt;/tt&gt; returns &lt;tt&gt;null&lt;/tt&gt; we can decide whether to persist the received object or throw an exception. Applied to the &lt;tt&gt;Order&lt;/tt&gt; class this pattern could be implemented like this:&lt;/p&gt; &lt;pre class="java"&gt;&lt;br /&gt; Order existingOrder = dao.&lt;span style="color: rgb(0, 102, 0);"&gt;findById&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;receivedOrder.&lt;span style="color: rgb(0, 102, 0);"&gt;getId&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;existingOrder == &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;  dao.&lt;span style="color: rgb(0, 102, 0);"&gt;persist&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;receivedOrder&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;  existingOrder.&lt;span style="color: rgb(0, 102, 0);"&gt;setCustomerName&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;receivedOrder.&lt;span style="color: rgb(0, 102, 0);"&gt;getCustomerName&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  existingOrder.&lt;span style="color: rgb(0, 102, 0);"&gt;setDate&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;receivedOrder.&lt;span style="color: rgb(0, 102, 0);"&gt;getDate&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;/pre&gt; &lt;h3&gt;The pattern&lt;/h3&gt; &lt;p&gt;So where does all this leave us? The rule of thumb I stick to is this:&lt;/p&gt; &lt;ul&gt;&lt;li&gt;When and only when (and preferably where) we create a new entity, invoke &lt;tt&gt;EntityManager.persist&lt;/tt&gt; to save it. This makes perfect sense when we view our &lt;a set="yes" linkindex="159" href="http://domaindrivendesign.org/discussion/messageboardarchive/Repositories.html"&gt;domain access objects as collections&lt;/a&gt;. I call this the &lt;em&gt;persist-on-new pattern&lt;/em&gt;.&lt;/li&gt;&lt;li&gt;When updating an existing entity, we do not invoke any &lt;tt&gt;EntityManager&lt;/tt&gt; method; the JPA provider will automatically update the database at flush or commit time.&lt;/li&gt;&lt;li&gt;When we receive an updated version of an existing simple entity (an entity with no references to other entities) from outside of our application and want to save the new state, we invoke &lt;tt&gt;EntityManager.merge&lt;/tt&gt; to copy that state into the persistence context. Because of the way merging works, we can also do this if we are unsure whether the object has been already persisted.&lt;/li&gt;&lt;li&gt;When we need more control over the merging process, we use the &lt;em&gt;DIY merge pattern&lt;/em&gt;.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;em&gt;I hope this blog gives you some pointers on how to save entities and how to work with detached entities. We'll get back to detached entities when we discuss Data Transfer Objects in a later blog. But next week we'll handle a number of common entity retrieval pattern first. In the meantime your feedback is welcome. What are your JPA patterns?&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;&lt;a href="http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/"&gt;Original Source&lt;/a&gt;&lt;br /&gt;&lt;/em&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-4196760678224796966?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/4196760678224796966/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/saving-detached-entities.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/4196760678224796966'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/4196760678224796966'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/saving-detached-entities.html' title='Saving (detached) entities'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-2130921371608186002</id><published>2009-09-12T03:48:00.000-07:00</published><updated>2009-09-12T04:34:56.528-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>Bidirectional assocations</title><content type='html'>&lt;p&gt;JPA offers the &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/OneToMany.html"&gt;&lt;tt&gt;@OneToMany&lt;/tt&gt;&lt;/a&gt;, &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/ManyToOne.html"&gt;&lt;tt&gt;@ManyToOne&lt;/tt&gt;&lt;/a&gt;, &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/OneToOne.html"&gt;&lt;tt&gt;@OneToOne&lt;/tt&gt;&lt;/a&gt;, and &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/ManyToMany.html"&gt;&lt;tt&gt;@ManyToMany&lt;/tt&gt;&lt;/a&gt; annotations to map associations between objects. While EJB 2.x offered &lt;a href="http://java.sun.com/developer/technicalArticles/ebeans/EJB20CMP/"&gt;container managed relationships&lt;/a&gt; to manage these associations, and especially to keep bidirectional associations in sync, JPA leaves more up to the developer.&lt;br /&gt;&lt;span id="more-904"&gt;&lt;/span&gt;&lt;/p&gt; &lt;h3&gt;The setup&lt;/h3&gt; &lt;p&gt;Let's start by expanding the &lt;tt&gt;Order&lt;/tt&gt; example from &lt;a href="http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/"&gt;the previous blog&lt;/a&gt; with an &lt;tt&gt;OrderLine&lt;/tt&gt; object. It has an id, a description, a price, and a reference to the order that contains it:&lt;br /&gt;&lt;a name="OrderLine"&gt;&lt;/a&gt; &lt;/p&gt;&lt;pre class="java"&gt;@&lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AEntity+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;Entity&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; OrderLine &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;@Id&lt;br /&gt;@GeneratedValue&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;int&lt;/span&gt; id;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;private&lt;/span&gt; &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AString+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;String&lt;/span&gt;&lt;/a&gt; description;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;int&lt;/span&gt; price;&lt;br /&gt;&lt;br /&gt;@ManyToOne&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;private&lt;/span&gt; Order order;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;int&lt;/span&gt; getId&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; id; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; setId&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(153, 51, 51);"&gt;int&lt;/span&gt; id&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;id&lt;/span&gt; = id; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AString+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;String&lt;/span&gt;&lt;/a&gt; getDescription&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; description; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; setDescription&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AString+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;String&lt;/span&gt;&lt;/a&gt; description&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;description&lt;/span&gt; = description; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;int&lt;/span&gt; getPrice&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; price; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; setPrice&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(153, 51, 51);"&gt;int&lt;/span&gt; price&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;price&lt;/span&gt; = price; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; Order getOrder&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; order; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; setOrder&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;Order order&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;order&lt;/span&gt; = order; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;Using the generic DAO pattern, we quickly get ourselves a very basic &lt;tt&gt;OrderLineDao&lt;/tt&gt; interface and an implementation:&lt;/p&gt; &lt;pre class="java"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;interface&lt;/span&gt; OrderLineDao &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;extends&lt;/span&gt; Dao&amp;lt;Integer, OrderLine&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; List&amp;lt;OrderLine&gt; findOrderLinesByOrder&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;Order o&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; JpaOrderLineDao &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;extends&lt;/span&gt; JpaDao&amp;lt;Integer, OrderLine&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;implements&lt;/span&gt;&lt;br /&gt; OrderLineDao &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; List&amp;lt;OrderLine&gt; findOrderLinesByOrder&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;Order o&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; Query q = entityManager.&lt;span style="color: rgb(0, 102, 0);"&gt;createQuery&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"SELECT e FROM "&lt;/span&gt;&lt;br /&gt;   + entityClass.&lt;span style="color: rgb(0, 102, 0);"&gt;getName&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; + &lt;span style="color: rgb(255, 0, 0);"&gt;" e WHERE order = :o "&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; q.&lt;span style="color: rgb(0, 102, 0);"&gt;setParameter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"o"&lt;/span&gt;, o&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;List&amp;lt;OrderLine&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; q.&lt;span style="color: rgb(0, 102, 0);"&gt;getResultList&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;We can use this DAO to add a orderline to an order, or to find all the order lines for an order:&lt;/p&gt; &lt;pre class="java"&gt; OrderLine line = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; OrderLine&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;line.&lt;span style="color: rgb(0, 102, 0);"&gt;setDescription&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"Java Persistence with Hibernate"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;line.&lt;span style="color: rgb(0, 102, 0);"&gt;setPrice&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;5999&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;line.&lt;span style="color: rgb(0, 102, 0);"&gt;setOrder&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;o&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;orderLineDao.&lt;span style="color: rgb(0, 102, 0);"&gt;persist&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;Collection&amp;lt;OrderLine&gt; lines = orderLineDao.&lt;span style="color: rgb(0, 102, 0);"&gt;findOrderLinesByOrder&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;o&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt; &lt;h3&gt;Mo associations, mo problems&lt;/h3&gt; &lt;p&gt;All this is pretty straight forward, but it gets interesting when we make this association bidirectional. Let's add an &lt;tt&gt;orderLines&lt;/tt&gt; field to our &lt;tt&gt;Order&lt;/tt&gt; object and include a naïve implementation of the getter/setter pair:&lt;/p&gt; &lt;pre class="java"&gt; @OneToMany&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;mappedBy = &lt;span style="color: rgb(255, 0, 0);"&gt;"order"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;private&lt;/span&gt; Set&amp;lt;OrderLine&gt; orderLines = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; HashSet&amp;lt;OrderLine&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; Set&amp;lt;OrderLine&gt; getOrderLines&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; orderLines; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; setOrderLines&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;Set&amp;lt;&lt;br /&gt;OrderLine&gt; orderLines&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;orderLines&lt;/span&gt; = orderLines; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;The &lt;tt&gt;mappedBy&lt;/tt&gt; field on the &lt;tt&gt;@OneToMany&lt;/tt&gt; annotation tells JPA that this is the reverse side of an association and, instead of mapping this field directly to a database column, it can look at &lt;tt&gt;order&lt;/tt&gt; field of a &lt;tt&gt;OrderLine&lt;/tt&gt; object to know with which &lt;tt&gt;Order&lt;/tt&gt; object it goes.&lt;/p&gt; &lt;p&gt;So without changing the underlying database we can now retrieve the orderlines for an order like this:&lt;/p&gt; &lt;pre class="java"&gt; Collection&amp;lt;&lt;br /&gt;OrderLine&gt; lines = o.&lt;span style="color: rgb(0, 102, 0);"&gt;getOrderLines&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt; &lt;p&gt;No more need to access the &lt;tt&gt;OrderLineDao&lt;/tt&gt;. &lt;img src="http://blog.xebia.com/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley" /&gt; &lt;/p&gt; &lt;p&gt;But there is a catch! While container managed relationships (CMR) as defined by EJB 2.x made sure that adding an &lt;tt&gt;OrderLine&lt;/tt&gt; object to the &lt;tt&gt;orderLines&lt;/tt&gt; property of an &lt;tt&gt;Order&lt;/tt&gt; &lt;em&gt;also&lt;/em&gt; sets the &lt;tt&gt;order&lt;/tt&gt; property on that &lt;tt&gt;OrderLine&lt;/tt&gt; (and vice versa), JPA (being a POJO framework) performs no such magic. This is actually a good thing because it makes our domain objects usable outside of a JPA container, which means you can test them more easily and use them when they have not been persisted (yet). But it &lt;a href="http://maba.wordpress.com/2004/08/13/ejb-container-managed-vs-pojo-relationships/"&gt;can also be confusing for people that were used to EJB 2.x CMR behaviour&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;If you run the examples above in separate transactions, you will find that they run correctly. But if you run them within one transaction like the code below does, you will find that the item list while be empty:&lt;/p&gt; &lt;pre class="java"&gt; Order o = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Order&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;o.&lt;span style="color: rgb(0, 102, 0);"&gt;setCustomerName&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"Mary Jackson"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;o.&lt;span style="color: rgb(0, 102, 0);"&gt;setDate&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3ADate+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;Date&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;OrderLine line = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; OrderLine&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;line.&lt;span style="color: rgb(0, 102, 0);"&gt;setDescription&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"Java Persistence with Hibernate"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;line.&lt;span style="color: rgb(0, 102, 0);"&gt;setPrice&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;5999&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;line.&lt;span style="color: rgb(0, 102, 0);"&gt;setOrder&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;o&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3ASystem+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;System&lt;/span&gt;&lt;/a&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;out&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;println&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"Items ordered by "&lt;/span&gt; + o.&lt;span style="color: rgb(0, 102, 0);"&gt;getCustomerName&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; + &lt;span style="color: rgb(255, 0, 0);"&gt;": "&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;Collection&amp;lt;OrderLine&gt; lines = o.&lt;span style="color: rgb(0, 102, 0);"&gt;getOrderLines&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;for&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;OrderLine each : lines&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3ASystem+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;System&lt;/span&gt;&lt;/a&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;out&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;println&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;each.&lt;span style="color: rgb(0, 102, 0);"&gt;getId&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; + &lt;span style="color: rgb(255, 0, 0);"&gt;": "&lt;/span&gt; + each.&lt;span style="color: rgb(0, 102, 0);"&gt;getDescription&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;   + &lt;span style="color: rgb(255, 0, 0);"&gt;" at $"&lt;/span&gt; + each.&lt;span style="color: rgb(0, 102, 0);"&gt;getPrice&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;This can be fixed by adding the following line before the first &lt;tt&gt;System.out.println&lt;/tt&gt; statement:&lt;/p&gt; &lt;pre class="java"&gt; o.&lt;span style="color: rgb(0, 102, 0);"&gt;getOrderLines&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;add&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt; &lt;h3&gt;Fixing and fixing...&lt;/h3&gt; &lt;p&gt;It works, but it's not very pretty. It breaks the abstraction and it's brittle as it depends on the user of our domain objects to correctly invoke these setters and adders. We can fix this by moving that invocation into the definition of &lt;tt&gt;OrderLine.setOrder(Order)&lt;/tt&gt;:&lt;/p&gt; &lt;pre class="java"&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; setOrder&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;Order order&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;order&lt;/span&gt; = order;&lt;br /&gt; order.&lt;span style="color: rgb(0, 102, 0);"&gt;getOrderLines&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;add&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;When can do even better by encapsulating the &lt;tt&gt;orderLines&lt;/tt&gt; property of the &lt;tt&gt;Order&lt;/tt&gt; object in a better manner:&lt;/p&gt; &lt;pre class="java"&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; Set&amp;lt;OrderLine&gt; getOrderLines&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; orderLines; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; addOrderLine&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;OrderLine line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; orderLines.&lt;span style="color: rgb(0, 102, 0);"&gt;add&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;And then we can redefine &lt;tt&gt;OrderLine.setOrder(Order)&lt;/tt&gt; as follows:&lt;/p&gt; &lt;pre class="java"&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; setOrder&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;Order order&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;order&lt;/span&gt; = order;&lt;br /&gt; order.&lt;span style="color: rgb(0, 102, 0);"&gt;addOrderLine&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;Still with me? I hope so, but if you're not, please try it out and see for yourself.&lt;/p&gt; &lt;p&gt;Now another problem pops up. What if someone directly invokes the &lt;tt&gt;Order.addOrderLine(OrderLine)&lt;/tt&gt; method? The &lt;tt&gt;OrderLine&lt;/tt&gt; will be added to the &lt;tt&gt;orderLines&lt;/tt&gt; collection, but its &lt;tt&gt;order&lt;/tt&gt; property will not point to the order it belongs. Modifying &lt;tt&gt;Order.addOrderLine(OrderLine)&lt;/tt&gt; like below will not work because it will cause an infinite loop with &lt;tt&gt;addOrderLine&lt;/tt&gt; invoking &lt;tt&gt;setOrder&lt;/tt&gt; invoking &lt;tt&gt;addOrderLine&lt;/tt&gt; invoking &lt;tt&gt;setOrder&lt;/tt&gt; etc.:&lt;/p&gt; &lt;pre class="java"&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; addOrderLine&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;OrderLine line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; orderLines.&lt;span style="color: rgb(0, 102, 0);"&gt;add&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; line.&lt;span style="color: rgb(0, 102, 0);"&gt;setOrder&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;This problem can be solved by introducing an &lt;tt&gt;Order.internalAddOrderLine(OrderLine)&lt;/tt&gt; method that only adds the line to the collection, but does not invoke &lt;tt&gt;line.setOrder(this)&lt;/tt&gt;. This method will then be invoked from &lt;tt&gt;OrderLine.setOrder(Order)&lt;/tt&gt; and not cause an infinite loop. Users of the &lt;tt&gt;Order&lt;/tt&gt; class should invoke &lt;tt&gt;Order.addOrderLine(OrderLine)&lt;/tt&gt;. &lt;/p&gt; &lt;p&gt;&lt;a name="thepattern"&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3&gt;The pattern&lt;/h3&gt; &lt;p&gt;Taking this idea to its logical conclusion we end up with these methods for the &lt;tt&gt;OrderLine&lt;/tt&gt; class:&lt;/p&gt; &lt;pre class="java"&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; Order getOrder&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; order; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; setOrder&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;Order order&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;order&lt;/span&gt; != &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;order&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;internalRemoveOrderLine&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;order&lt;/span&gt; = order;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;order != &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; order.&lt;span style="color: rgb(0, 102, 0);"&gt;internalAddOrderLine&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;And these methods for the &lt;tt&gt;Order&lt;/tt&gt; class:&lt;/p&gt; &lt;pre class="java"&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; Set&amp;lt;OrderLine&gt; getOrderLines&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3ACollections+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;Collections&lt;/span&gt;&lt;/a&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;unmodifiableSet&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;orderLines&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; addOrderLine&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;OrderLine line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; line.&lt;span style="color: rgb(0, 102, 0);"&gt;setOrder&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; removeOrderLine&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;OrderLine line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; line.&lt;span style="color: rgb(0, 102, 0);"&gt;setOrder&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; internalAddOrderLine&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;OrderLine line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; orderLines.&lt;span style="color: rgb(0, 102, 0);"&gt;add&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(153, 51, 51);"&gt;void&lt;/span&gt; internalRemoveOrderLine&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;OrderLine line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; orderLines.&lt;span style="color: rgb(0, 102, 0);"&gt;remove&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;line&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;These methods provide a POJO-based implementation of the CMR logic that was built into EJB 2.x. With the typical POJOish advantages of being easier to understand, test, and maintain.&lt;/p&gt; &lt;p&gt;Of course there are a number of variations on this theme:&lt;/p&gt; &lt;ul&gt;&lt;li&gt;If &lt;tt&gt;Order&lt;/tt&gt; and &lt;tt&gt;OrderLine&lt;/tt&gt; are in the same package, you can give the &lt;tt&gt;internal...&lt;/tt&gt; methods package scope to prevent them from being invoked by accident. (This is where C++'s &lt;a href="http://en.wikipedia.org/wiki/Friend_class"&gt;friend class&lt;/a&gt; concept would come in handy. Then again, let's not go there. &lt;img src="http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /&gt;  ).&lt;/li&gt;&lt;li&gt;You can do away with the &lt;tt&gt;removeOrderLine&lt;/tt&gt; and &lt;tt&gt;internalRemoveOrderLine&lt;/tt&gt; methods if order lines will never be removed from an order.&lt;/li&gt;&lt;li&gt;You can move the responsibility for managing the bidirectional association from the &lt;tt&gt;OrderLine.setOrder(Order)&lt;/tt&gt; method to the &lt;tt&gt;Order&lt;/tt&gt; class, basically flipping the idea around. But that would mean spreading the logic over the &lt;tt&gt;addOrderLine&lt;/tt&gt; and &lt;tt&gt;removeOrderLine&lt;/tt&gt; methods.&lt;/li&gt;&lt;li&gt;Instead of, or in addition to, using &lt;tt&gt;Collections.singletonSet&lt;/tt&gt; to make the &lt;tt&gt;orderLine&lt;/tt&gt; set read-only at run-time, you can also use generic types to make it read-only at compile-time: &lt;pre class="java"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;public&lt;/span&gt; Set&amp;lt;&lt;br /&gt;? &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;extends&lt;/span&gt; OrderLine&gt; getOrderLines&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;return&lt;/span&gt; &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=allinurl%3ACollections+java.sun.com&amp;amp;bntI=I%27m%20Feeling%20Lucky"&gt;&lt;span style="color: rgb(170, 170, 221); font-weight: bold;"&gt;Collections&lt;/span&gt;&lt;/a&gt;.&lt;span style="color: rgb(0, 102, 0);"&gt;unmodifiableSet&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;orderLines&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;But this makes it harder to mock these objects with a mocking framework such as &lt;a href="http://www.easymock.org/"&gt;EasyMock&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;There are also some things to consider when using this pattern:&lt;/p&gt; &lt;ul&gt;&lt;li&gt;Adding an &lt;tt&gt;OrderLine&lt;/tt&gt; to an &lt;tt&gt;Order&lt;/tt&gt; does not automatically persist it. You'll need to also invoke the persist method on its DAO (or the EntityManager) to do that. Or you can set the &lt;tt&gt;cascade&lt;/tt&gt; property of the &lt;tt&gt;@OneToMany&lt;/tt&gt; annotation on the &lt;tt&gt;Order.orderLines&lt;/tt&gt; property to &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/CascadeType.html#PERSIST"&gt;&lt;tt&gt;CascadeType.PERSIST&lt;/tt&gt;&lt;/a&gt; (at least) to achieve that. More on this when we discuss the &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#persist%28java.lang.Object%29"&gt;&lt;tt&gt;EntityManager.persist&lt;/tt&gt;&lt;/a&gt; method.&lt;/li&gt;&lt;li&gt;Bidirectional associations do not play well with the &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#merge%28T%29"&gt;&lt;tt&gt;EntityManager.merge&lt;/tt&gt;&lt;/a&gt; method. We will discuss this when we get to the subject of detached objects.&lt;/li&gt;&lt;li&gt;When an entity that is part of a bidirectional associated is (about to be) removed, it should also be removed from the other end of the association. This will also come up when we talk about the &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#remove%28java.lang.Object%29"&gt;&lt;tt&gt;EntityManager.remove&lt;/tt&gt;&lt;/a&gt; method.&lt;/li&gt;&lt;li&gt;The pattern above only works when using field access (instead of property/method access) to let your JPA provider populate your entities. Field access is used when the &lt;a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/Id.html"&gt;&lt;tt&gt;@Id&lt;/tt&gt;&lt;/a&gt; annotation of your entity is placed on the corresponding field as opposed of the corresponding getter. Whether to prefer field access or property/method access is a contentious issue to which I will return in a later blog.&lt;/li&gt;&lt;li&gt;And last but not least; while this pattern may be a technically sound POJO-based implementation of managed associations, you can argue why you need all those getters and setters. Why would you need to be able to use both &lt;tt&gt;Order.addOrderLine(OrderLine)&lt;/tt&gt; and &lt;tt&gt;OrderLine.setOrder(Order)&lt;/tt&gt; to achieve the same result? Doing away with one of these could make our code simpler. See for example &lt;a href="http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html"&gt;James Holub's article on getters and setters&lt;/a&gt;. Then again, we've found that this pattern gives developers that use these domain objects the flexibility to associate them as they wish.&lt;/li&gt;&lt;/ul&gt;&lt;a href="http://blog.xebia.com/2009/03/16/jpa-implementation-patterns-bidirectional-assocations/"&gt;Original Source&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-2130921371608186002?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/2130921371608186002/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/bidirectional-assocations.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2130921371608186002'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/2130921371608186002'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/bidirectional-assocations.html' title='Bidirectional assocations'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-8836678508356985018</id><published>2009-09-08T11:01:00.000-07:00</published><updated>2009-09-08T11:11:27.088-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JBoss'/><title type='text'>Advanced JBoss Class Loading</title><content type='html'>&lt;h2&gt;&lt;span&gt;Advanced JBoss Class Loading&lt;/span&gt;&lt;/h2&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h3&gt;&lt;span&gt;Introduction&lt;/span&gt;&lt;/h3&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;One of the main concerns of a developer writing hot re-deployable JBoss applications is to understand how JBoss class loading works. Within the internals of the class loading mechanism lies the answer to questions like&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;ul&gt;&lt;li type="ul"&gt;&lt;p&gt;What happens if I pack a newer version of an utility library with my application, while an older version of the same library lingers somewhere in the server's lib directory?&lt;/p&gt;&lt;/li&gt;&lt;li type="ul"&gt;&lt;p&gt;How can I use two different versions of the same utility library, simultaneously, within the same instance of the application server?&lt;/p&gt;&lt;/li&gt;&lt;li type="ul"&gt;&lt;p&gt;What version of an utility class I am currently using?&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;or even the most fundamental of them all&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;ul&gt;&lt;li type="ul"&gt;&lt;p&gt;Why do I need to mess with all this class loading stuff anyway?&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;This article tries to provide the reader with the knowledge required to answer these questions. It will start by trying to answer the last one, and then it will present several often encountered use cases and explain the behavior of the JBoss class loading mechanism when faced with those situations.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h3&gt;&lt;span&gt;The Need for Class Loaders and Class Loading Management&lt;/span&gt;&lt;/h3&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h4&gt;&lt;span&gt;Class Namespace Isolation&lt;/span&gt;&lt;/h4&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;An application server should ideally give its deployed applications the freedom to use whatever utility library and whatever version of the library they see fit, regardless of the presence of concurrent applications that want to use the same library. This is mandated by the J2EE specifications, which calls it &lt;em&gt;class namespace isolation&lt;/em&gt; (Java EE 5 Specifications, Section EE.8.4). The fact that different applications load their classes in different class name spaces or &lt;em&gt;class loading domains&lt;/em&gt; allow them to do just that: run whatever class version they like, oblivious to the fact that their neighbors use the same class.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Java doesn't provide the formal notion of class version. So how is it possible to implement a class loading domain? The runtime identity of a class in Java 2 is defined by the &lt;em&gt;fully qualified class name&lt;/em&gt; and its &lt;em&gt;defining class loader&lt;/em&gt;. This means that the same class, loaded by two different class loaders, is seen by the Virtual Machine as two completely different types.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;span&gt;If you like history, you probably know that this wasn't always the case. In Java 1.1 the runtime identity of a class was defined only by its fully qualified class name. That made Vijay Saraswat declare in 1997 that "[Java is not type-safe|&lt;/span&gt;&lt;a class="jive-link-external-small" href="http://matrix.research.att.com/vj/bug.html"&gt;http://matrix.research.att.com/vj/bug.html&lt;/a&gt;&lt;span&gt;]" and Sheng Liang and Gilad Bracha fixed it by strengthening the type system to include a class's defining class loader in addition to the name of the class to fully define the type. This is good and ... not so good. It is good because now its not possible anymore that a rogue class loader would re-define your "java.lang.String" class. The VM will detect that and throw a ClassCastException. It is also good because now it is possible to have class loading domains within the same VM. Not so good however, is the fact that passing an object instance by reference between two class loading domains is not possible. Doing so results in the dreaded ClassCastException. If you would like to know more details about how this happens, please &lt;/span&gt;&lt;a class="jive-link-wiki-small" href="http://www.jboss.org/community/docs/DOC-9287"&gt;follow this link&lt;/a&gt;. Not being able to pass an Object by reference means you have to fall back on serialization and serialization means performance degradation.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h4&gt;&lt;span&gt;Hot Redeployment&lt;/span&gt;&lt;/h4&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Returning to application servers, the need for class loaders becomes probably obvious: this is how an application server implements class namespace isolation: each application gets its own class loader at deployment, and hence, its own "version" of classes.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;To extend this even more, it would be nice if we could re-deploy an application (i.e. instantiate a newer version of a class), at run-time, without necessarily bringing down the VM and re-starting it to reload the class. That would mean 24x7 uptime. Java 4 doesn't intrinsically support the concept of hot re-deployment. Once a dependent class reference was added to the runtime constant pool of a class, it is not possible to drop that reference anymore.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;However, it is possible to trick the server (or the VM) into doing this. If application A interacts with application B, but doesn't have any direct references to the B classes, and B changes, let's say a newer and better version becomes available, it is possible to create a new class loader, load the new B classes and have the invocation bus (the application server) route all invocations from A to the new B classes. This way, A deals with a new version of B without even knowing it. If the application server is careful to drop all explicit references to the old B classes, they will eventually be garbage collected and the old B will eventually disappear from the system.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h4&gt;&lt;span&gt;Sharing Classes&lt;/span&gt;&lt;/h4&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Isolating class loading domains is nice. Our applications will run happily and safe. But very slowly, when it comes to interacting with each other. This is because each interaction involves passing arguments by value, which means serialization, which means overhead.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;We're sometimes (actually quite often) faced with the situation where in we would like to allow applications to share classes. We know precisely, for example, that in our environment, application A and B, otherwise independent, will always use the same version of the utility library and doing so, they could pass references among themselves without any problem. The added benefit in this case is that the invocations will be faster, given the fact serialization is cut out.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;One word of caution though. In this situation, if we hot redeploy the utility library, we also must re-deploy the application A and B: the current A and B classes used direct references to the utility classes, so they're tainted forever, they won't ever be able to use the new utility classes.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;JBoss makes possible for applications to share classes. JBoss 3.x does that by default. JBoss 4.0 does this for the "standard" configuration, but maintains class namespace isolation between applications for its "default" configuration. JBoss 4.0.1 reverts to the 3.x convention.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h4&gt;&lt;span&gt;Class Repositories or How JBoss Class Loading Works&lt;/span&gt;&lt;/h4&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;JBoss makes sharing classes possible by introducing the concept of &lt;em&gt;class loader repository&lt;/em&gt;. The central piece of the class loading mechanism is the &lt;span style="font-family:courier new;"&gt;org.jboss.mx.loading.UnifiedClassLoader3&lt;/span&gt; (UCL). &lt;span style="font-family:courier new;"&gt;UnifiedClassLoader3&lt;/span&gt; extends &lt;span style="font-family:courier new;"&gt;URLClassLoader&lt;/span&gt;. Each UCL is associated with a shared repository of classes and resources, usually an instance of &lt;span style="font-family:courier new;"&gt;org.jboss.mx.loading.UnifiedLoaderRepository3&lt;/span&gt;. Every UCL is associated with a single instance of &lt;span style="font-family:courier new;"&gt;UnifiedLoaderRepository3&lt;/span&gt;, but a repository can have multiple UCLs. A UCL may have multiple URLs associated with it for class and resource loading. By default, there is a single &lt;span style="font-family:courier new;"&gt;UnifiedLoaderRepository3&lt;/span&gt; shared across all UCL instances. UCLs form a single flat class namespace.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;The class loader parent for each UCL is a &lt;span style="font-family:courier new;"&gt;NoAnnotationClassLoader&lt;/span&gt; instance. The &lt;span style="font-family:courier new;"&gt;NoAnnotationClassLoader&lt;/span&gt; extends &lt;span style="font-family:courier new;"&gt;URLClassLoader&lt;/span&gt;. A singleton &lt;span style="font-family:courier new;"&gt;NoAnnotationClassLoader&lt;/span&gt; instance is created during the server's boot process and its job is to define classes available in the &lt;span style="font-family:courier new;"&gt;$JBOSS_HOME/lib&lt;/span&gt; libraries (ex: &lt;span style="font-family:courier new;"&gt;commons-logging.jar&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;concurrent.jar&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;dom4j.jar&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;jboss-common.jar&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;jboss-jmx.jar&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;jboss-system.jar&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;log4j-boot.jar&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;xercesImpl.jar&lt;/span&gt;, etc.). The &lt;span style="font-family:courier new;"&gt;NoAnnotationClassLoader&lt;/span&gt;'s parent is the system class loader (&lt;span style="font-family:courier new;"&gt;sun.misc.Launcher$AppClassLoader&lt;/span&gt;).&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4696/JBossClassLoading_pre1.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4696/-1--1/JBossClassLoading_pre1.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;When a new UCL is created and associated with the repository, it contributes to the repository a map of packages it can potentially serve classes from. It doesn't add any class to the repository's class cache yet, because nobody has requested any class at this stage. The repository just walks through the class loader's URL to see what packages that UCL is capable of handling. So, the UCL just declares that it can potentially serve classes from the packages that are present in its classpath.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;When requested to load a class, a UCL &lt;em&gt;overrides&lt;/em&gt; the standard Java2 class loading model by first trying to load a class from its associated repository's cache. If it doesn't find it there, it delegates the task of loading the class to the &lt;em&gt;first&lt;/em&gt; UCL associated with the repository that declared it can load that class. The order in which the UCLs have been added to the repository becomes important, because this is what defines "first" in this context. If no "available" UCL is found, the initiating UCL falls back to the standard Java2 parent delegation. This explains why you are still able to use "java.lang.String", for example.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;At the end of this process, if no class definition is found in the bootstrap libraries, in the $JBOSS_HOME/lib libraries nor among the libraries associated with the repository's UCL, the UCL throws a &lt;span style="font-family:courier new;"&gt;ClassNotFoundException&lt;/span&gt;. However, if one of the pair UCL is able to load the class, the class will be added to the repository's class cache and from this moment on, it will be returned to any UCL requesting it.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Even if the Java bootstrap packages or $JAVA_HOME/lib packages are not added to the repository's package map, the classes belonging to those packages can be loaded through the process described above and they are added to the repository too. This explains why you'll find "java.lang.String" in the repository.&lt;/p&gt;Note: Package Map is nothing but ...&lt;br /&gt;&lt;br /&gt;&lt;pre id="Classes"&gt;&lt;span id="FormalComment"&gt;/** A HashMap&amp;lt;String, Set&lt;/span&gt;&lt;span&gt;&lt;span id="FormalComment"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span id="FormalComment"&gt;UCL&gt;&gt; of package names to the set of&lt;br /&gt;&lt;a name="111"&gt;&lt;/a&gt;    * ClassLoaders which have classes in the package.&lt;br /&gt;&lt;a name="112"&gt;&lt;/a&gt;    * Access synchronized via this.packagesMap monitor.&lt;br /&gt;&lt;a name="113"&gt;&lt;/a&gt;    */&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Class sharing can be turned off. J2EE-style class namespace isolation is available. You get an "isolated" application by &lt;em&gt;scoping&lt;/em&gt; the application's deployment. At the JBoss class loading management system level, scoping translates into creating a child repository. A scoped application still can load the classes present in the classpaths of the UCLs or the root repository. Depending on whether repository's "Java2ParentDelegation" flag is turned on or off, a scoped application even has access to the &lt;em&gt;class instances&lt;/em&gt; available in the root repository's cache. However, sibling child repositories can never share classes.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4691/JBossClassLoading_pre2.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4691/-1--1/JBossClassLoading_pre2.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Note: Even if an &lt;span style="font-family:courier new;"&gt;HierarchicalLoaderRepository3$NoParentClassLoader&lt;/span&gt; instance has its parent set to be an instance of &lt;span style="font-family:courier new;"&gt;NoAnnotationURLClassLoader&lt;/span&gt;, as represented above, the &lt;span style="font-family:courier new;"&gt;NoParentClassLoader&lt;/span&gt; implementation of &lt;span style="font-family:courier new;"&gt;loadClass()&lt;/span&gt; always throws a &lt;span style="font-family:courier new;"&gt;ClassNotFoundException&lt;/span&gt;  to force the UCL to only load from its URLs. We will look closer at how &lt;span style="font-family:courier new;"&gt;NoParentClassLoader&lt;/span&gt; works and how a scoped application loads a class available in the system's bootstrap libraries when we present the Cases 3 and 4, below.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h3&gt;&lt;span&gt;Real World Scenarios&lt;/span&gt;&lt;/h3&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;We will explore the complex interactions presented above based on concrete use cases.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;We start by assuming that we want to deploy our own application (be it a JBoss service, a complex enterprise archive or a simple stateless session bean), and this application relies on an external library. For simplicity, we could assume that the utility library contains only a single class, &lt;span style="font-family:courier new;"&gt;org.useful.Utility&lt;/span&gt;. The &lt;span style="font-family:courier new;"&gt;Utility&lt;/span&gt; class can be packed together with the application classes inside the application archive, or it could be packed in its own archive, &lt;span style="font-family:courier new;"&gt;utility.jar&lt;/span&gt;. We could also assume that we always use the JBoss' &lt;span style="font-family:courier new;"&gt;default&lt;/span&gt; configuration.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Our hypothetical application consists of a single class, &lt;span style="font-family:courier new;"&gt;org.pkg1.A&lt;/span&gt;. We will consider several common situations:&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h3&gt;&lt;span&gt;Case 1. The &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Utility.class&lt;/span&gt;&lt;span&gt; is present in the application's archive, but nowhere else on the server.&lt;/span&gt;&lt;/h3&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;The short story: The current UCL will become the defining class loader of the class, and the class will be added to the repository's class cache. The details of the process are presented below.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4688/JBossClassLoading1.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4688/-1--1/JBossClassLoading1.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;First time the application needs to use a strong-typed &lt;span style="font-family:courier new;"&gt;Utility&lt;/span&gt; reference, the VM asks the current UCL to load the class. The UCL tries to get the class from the repository's cache (1). If it is found, the class is returned and the process stops right here. If the class is not found, the UCL queries the repository for UCLs capable to load classes from the package the unknown class is part of (3). Being the single UCL able to define the class, the control returns to it and load manager calls &lt;span style="font-family:courier new;"&gt;loadClassLocally()&lt;/span&gt; on it (4). &lt;span style="font-family:courier new;"&gt;loadClassLocally()&lt;/span&gt; first tries to call &lt;span style="font-family:courier new;"&gt;super.loadClass()&lt;/span&gt; (5), which ends by involving the &lt;span style="font-family:courier new;"&gt;NoAnnotationClassLoader&lt;/span&gt; in the loading process. If the class is present in the bootstrap libraries or $JBOSS_HOME/lib (the URLs associated with the &lt;span style="font-family:courier new;"&gt;NoAnnotationClassLoader&lt;/span&gt; instance), it is loaded. Otherwise, the class is loaded from the URLs associated with the current UCL. Finally, the class is added to the repository's class cache (6).&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;This is the configuration of the UnifiedLoaderRepository after the class loading takes place.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4694/JBossClassLoading1_Post.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4694/-1--1/JBossClassLoading1_Post.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h3&gt;&lt;span&gt;Case 2. The &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Utility.class&lt;/span&gt;&lt;span&gt; is present both in the application's archive AND &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;server/default/lib&lt;/span&gt;&lt;span&gt;. The deployment is non-scoped.&lt;/span&gt;&lt;/h3&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;The short story: The version of the class available in &lt;span style="font-family:courier new;"&gt;server/default/lib/utility.jar&lt;/span&gt; will be used by the new deployment. The version of the class packed with the deployment will be ignored.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4690/JBossClassLoading2.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4690/-1--1/JBossClassLoading2.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;The key element here is that when &lt;span style="font-family:courier new;"&gt;getPackageClassLoaders()&lt;/span&gt; is invoked on the repository, the method calls returns &lt;em&gt;two&lt;/em&gt; potential classloaders that can load &lt;span style="font-family:courier new;"&gt;org.useful.Utility&lt;/span&gt;: UCL0 and UCL1. The UCL0 is chosen, because it was added to the repository &lt;em&gt;before&lt;/em&gt; UCL1 and it will be used to load &lt;span style="font-family:courier new;"&gt;org.useful.Utility&lt;/span&gt;.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;This is the configuration of the UnifiedLoaderRepository after the class loading takes place.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4697/JBossClassLoading2_Post.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4697/-1--1/JBossClassLoading2_Post.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h3&gt;&lt;span&gt;Case 3. The &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Utility.class&lt;/span&gt;&lt;span&gt; is present both in the application's archive AND &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;server/default/lib&lt;/span&gt;&lt;span&gt;. The deployment is scoped and &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Java2ParentDelegation&lt;/span&gt;&lt;span&gt; is turned off (default).&lt;/span&gt;&lt;/h3&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;The short story: The utility class is loaded from the application's archive.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4698/JBossClassLoading3.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4698/-1--1/JBossClassLoading3.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Because Java2ParentDelegation is turned off by default, the Step (1.1) is never executed, &lt;span style="font-family:courier new;"&gt;parentRepository.getCachedClass()&lt;/span&gt; never gets called, so the UCL doesn't have access to the repository's &lt;em&gt;cached&lt;/em&gt; classes.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Within the scope of the call to &lt;span style="font-family:courier new;"&gt;getPackageClassLoaders()&lt;/span&gt; at Step 3, the child repository also calls &lt;span style="font-family:courier new;"&gt;getPackageClassLoaders()&lt;/span&gt; on its parent, and also includes into the returned class loader set a UCL (constructed on the spot and associated to the child repository) that has among its ancestors an instance of &lt;span style="font-family:courier new;"&gt;NoAnnotationURLClassLoader&lt;/span&gt;, which ultimately can reach the system class loader. Why is that? Remember that the UCL's parent, &lt;span style="font-family:courier new;"&gt;HierarchicalLoaderRepository3$NoParentClassLoader&lt;/span&gt;, overrides &lt;span style="font-family:courier new;"&gt;loadClass()&lt;/span&gt; to always throw a &lt;span style="font-family:courier new;"&gt;ClassNotFoundException&lt;/span&gt;, thus forcing the UCL to only load from its URLs. If the UCL relies only on its class loader parent to load bootstrap classes, it will throw &lt;span style="font-family:courier new;"&gt;ClassNotFoundException&lt;/span&gt; and fail when your application wants to load "java.lang.String", for example. The &lt;span style="font-family:courier new;"&gt;NoAnnotationURLClassLoader&lt;/span&gt;-delegating UCL instance included in the return set provides a way load bootstrap library classes.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Always the HierarchialLoaderRepository's class loaders take precedence over the parent's (their "order" is lower). For the case depicted above, UCL1 is the preferred class loader.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;This is the configuration of the UnifiedLoaderRepository after the class loading takes place.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4695/JBossClassLoading3_Post.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4695/-1--1/JBossClassLoading3_Post.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;h3&gt;&lt;span&gt;Case 4. The &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Utility.class&lt;/span&gt;&lt;span&gt; is present both in the application's archive AND &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;server/default/lib&lt;/span&gt;&lt;span&gt;. The deployment is scoped, but &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;java2ParentDelegation&lt;/span&gt;&lt;span&gt; is turned on.&lt;/span&gt;&lt;/h3&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4689/JBossClassLoading4.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4689/-1--1/JBossClassLoading4.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;When Java2ParentDelegation is turned on, the Step (1.1) is executed, and if a cached class is found in the parent repository, it is returned and the process stops here.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Within the scope of the call to &lt;span style="font-family:courier new;"&gt;getPackageClassLoaders()&lt;/span&gt; at Step (3), the child repository also calls &lt;span style="font-family:courier new;"&gt;getPackageClassLoaders()&lt;/span&gt; on its parent, but does not include into the returned class loader set a UCL with a parent to the system class loader. If there are no class loaders in the repository capable of handling the request ask the class loader itself in the event that its parent(s) can load the class (&lt;span style="font-family:courier new;"&gt;repository.loadClassFromClassLoader()&lt;/span&gt;)&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;The HierarchialLoaderRepository's class loaders take precedence over the parent's (their "order" is lower). For the case depicted above, UCL1 is the preferred class loader.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;This is the configuration of the UnifiedLoaderRepository after the class loading takes place.&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4693/JBossClassLoading4_Post.png"&gt;&lt;img src="http://www.jboss.org/community/servlet/JiveServlet/download/10290-37-4693/-1--1/JBossClassLoading4_Post.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;p&gt;Question: What happens if the parent delegation is true and a classloader already loaded the class in the parent repository's class cache?&lt;/p&gt;&lt;p&gt;Answer: My scoped application will use the already loaded class from the parent repository's class cache.&lt;/p&gt;&lt;p&gt;-&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;&lt;span&gt;TO_DO&lt;/span&gt;&lt;/h3&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;ul&gt;&lt;li type="ul"&gt;&lt;p&gt;Create similar use cases for WARs. Deploying a WAR involves an extra class loader (WebappClassLoader) created by the servlet container, that can be independently configured using the WAR's &lt;span style="font-family:courier new;"&gt;jboss-web.xml&lt;/span&gt;.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;ul&gt;&lt;li type="ul"&gt;&lt;p&gt;Explain how UCL3 are created, by whom and why. Explain the relationship between UCL created during an EAR deployment. Multiple UCLs are created (the one corresponding to the EAR and then for the embedded JARs, WARs, etc).&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="padding: 0px; min-height: 8pt; height: 8pt;"&gt; &lt;/p&gt;&lt;ul&gt;&lt;li type="ul"&gt;&lt;p&gt;More details on Java2ParentDelegation flag. Make it clear that it only affects the relationship between repositories not classloaders. If a class is not found in any repository, then the standard delegation model is invoked (i.e. the parent classloader is asked to find the class).&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-8836678508356985018?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/8836678508356985018/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/advanced-jboss-class-loading.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8836678508356985018'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8836678508356985018'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/09/advanced-jboss-class-loading.html' title='Advanced JBoss Class Loading'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-162287787532226731</id><published>2009-08-30T10:34:00.000-07:00</published><updated>2009-08-30T10:39:09.694-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JPA'/><title type='text'>JPA Performance: Dont Ignore the Database</title><content type='html'>&lt;h2&gt;Database Schema&lt;/h2&gt; Good Database schema design is important for performance. &lt;a set="yes" linkindex="33" target="_blank" href="http://dev.mysql.com/doc/refman/5.0/en/data-size.html"&gt;One of the most basic optimizations is to design your tables to take as little space on the disk as possible&lt;/a&gt; , this makes disk reads faster and         uses less memory for query processing.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Data Types&lt;/h3&gt; You should &lt;a set="yes" linkindex="34" target="_blank" href="http://blogs.sun.com/carolmcdonald/entry/mysql_for_developers"&gt;use             the smallest data types possible&lt;/a&gt;, especially for indexed fields. The smaller your data types, the more indexes (and data) can fit into a block of memory, the faster your queries will be.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Normalization&lt;/h3&gt; Database Normalization eliminates redundant data, which usually makes updates faster since there is less data to change. However a Normalized schema causes joins for queries, which makes queries slower, denormalization speeds retrieval. More normalized schemas are better for applications involving many transactions, less normalized are better for reporting types of applications.  You should normalize your schema first, then de-normalize later.  Applications often need to mix the approaches, for example use a partially normalized schema, and duplicate, or cache, selected columns from one table in another table. With JPA O/R mapping you can use the @Embedded annotation for denormalized columns to specify a persistent field whose &lt;a linkindex="35" target="_blank" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/Embeddable.html"&gt;@Embeddable&lt;/a&gt;         type can be stored as an intrinsic part of the owning entity and share         the identity of the entity.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/normalization.jpg" alt="" /&gt;&lt;br /&gt;&lt;h4&gt; &lt;/h4&gt; &lt;h4&gt;Database Normalization and Mapping Inheritance Hiearchies&lt;/h4&gt; The Class Inheritance hierarchy shown below will be used as an example         of JPA O/R mapping.&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/mappinginheritance.jpg" alt="" style="width: 326px; height: 267px;" /&gt;&lt;br /&gt;&lt;br /&gt;In the Single table per class mapping shown below, &lt;a linkindex="36" target="_blank" href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnbqa.html"&gt;all             classes in the hierarchy are mapped to a single table in the database&lt;/a&gt;.         This table has a discriminator column (mapped by &lt;span style="font-weight: bold;"&gt;@DiscriminatorColumn)&lt;/span&gt;, which identifies the subclass.  Advantages: This is fast for querying, no joins are required. Disadvantages:  wastage of space since all inherited fields are in every row, a &lt;a linkindex="37" target="_blank" href="http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_single_disadv"&gt;deep inheritance hierarchy will result in wide tables with many, some empty columns&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/singletableperclass.jpg" alt="" style="width: 341px; height: 212px;" /&gt;&lt;br /&gt;&lt;br /&gt;In the Joined Subclass mapping shown below, the root of the class hierarchy is represented by a single table, and each subclass has a separate table that only contains those fields specific to that subclass. This is normalized (eliminates redundant data) which is better for storage and updates. However queries cause joins which makes queries slower especially for deep hierachies, polymorphic queries and relationships.&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/joinedsubclass.jpg" alt="" style="width: 279px; height: 288px;" /&gt;&lt;br /&gt;&lt;br /&gt;In the Table per Class mapping (in JPA 2.0, optional in JPA 1.0),          every &lt;span style="font-weight: bold;"&gt;concrete&lt;/span&gt; class is mapped to a table in the database and all the inherited state is repeated in that table. This is not normlalized, inherited data is repeated which wastes space.  Queries for Entities of the same type are fast, however  polymorphic queries cause unions which are slower.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/tableperclass.jpg" alt="" style="width: 363px; height: 246px;" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Know what SQL is executed&lt;/h2&gt; You need to understand the SQL queries your application makes and evaluate their performance. Its a good idea to enable SQL logging, then go through a use case scenario to check the executed SQL.  Logging is not part of the JPA specification, With EclipseLink you can &lt;a set="yes" linkindex="38" target="_blank" href="http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging"&gt;enable logging&lt;/a&gt; of SQL by setting the following property in the persistence.xml file:&lt;br /&gt;&lt;br /&gt;&lt;table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2"&gt;     &lt;tbody&gt;         &lt;tr&gt;             &lt;td style="vertical-align: top;"&gt;&lt;properties&gt;&lt;br /&gt;               &lt;property name="eclipselink.logging.level" value="FINE"&gt;&lt;br /&gt;           &lt;/property&gt;&lt;/properties&gt;&lt;/td&gt;         &lt;/tr&gt;     &lt;/tbody&gt; &lt;/table&gt;&amp;lt;properties&gt;&lt;br /&gt;    &amp;lt;property name="eclipselink.logging.level" value="FINE"/&gt;&lt;br /&gt;&amp;lt;/properties&gt;&lt;br /&gt;&lt;br /&gt;With Hibernate you set the following property in the persistence.xml file:&lt;br /&gt;&lt;br /&gt;&lt;table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2"&gt;     &lt;tbody&gt;         &lt;tr&gt;             &lt;td style="vertical-align: top;"&gt;&lt;properties&gt;&lt;br /&gt;               &lt;property name="hibernate.show_sql" value="true"&gt;&lt;br /&gt;           &lt;/property&gt;&lt;/properties&gt;&lt;/td&gt;         &lt;/tr&gt;     &lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&amp;lt;properties&gt;&lt;br /&gt;    &amp;lt;property name="hibernate.show_sql" value="true" /&gt;&lt;br /&gt;&amp;lt;/properties&gt;&lt;br /&gt;&lt;br /&gt;Basically you want to make your queries access less data, is your application retrieving more data than it needs, are queries accessing too many rows or columns? Is the database query analyzing more rows than it needs? Watch out for the following:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;queries which execute too often to retrieve needed data&lt;/li&gt;&lt;li&gt;retrieving more data than needed&lt;/li&gt;&lt;li&gt;queries which are too slow     &lt;ul&gt;&lt;li&gt;you can use EXPLAIN to see where you should add indexes&lt;/li&gt;&lt;/ul&gt;     &lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;With MySQL you can use the &lt;a set="yes" linkindex="39" target="_blank" href="http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html"&gt;slow query log&lt;/a&gt; to see which queries are executing slowly, or you can use the &lt;a linkindex="40" target="_blank" href="http://www.mysql.com/products/enterprise/query.html"&gt;MySQL query analyzer&lt;/a&gt; to see slow queries, query execution counts, and results of EXPLAIN statements.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Understanding EXPLAIN&lt;/h3&gt; For slow queries, you can &lt;a set="yes" linkindex="41" target="_blank" href="http://blogs.sun.com/carolmcdonald/entry/mysql_for_developers"&gt;precede a SELECT statement with the keyword EXPLAIN&lt;/a&gt;  to get information about the query execution plan, which explains how it would process the SELECT,  including information about how tables are joined and in which order. This helps find missing indexes early in the development process.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/fulltablescan.jpg" alt="" style="width: 489px; height: 279px;" /&gt;&lt;br /&gt;You should index columns that are frequently used in Query WHERE, GROUP BY clauses, and columns frequently used in joins, but be aware that indexes can slow down inserts and updates.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Lazy Loading and JPA&lt;/h3&gt; With JPA many-to-one and many-to-many relationships lazy load by default, meaning they will be loaded when the entity in the relationship is accessed. Lazy loading is usually good, but if you need to access all of the "many" objects in a relationship, it will cause n+1 selects where n is the number of "many" objects.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/lazyloading.jpg" alt="" style="width: 434px; height: 212px;" /&gt;&lt;br /&gt;&lt;br /&gt;You can change the relationship to be loaded eagerly as follows :&lt;br /&gt;&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/eagerloading.jpg" alt="" style="width: 502px; height: 184px;" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;However you should be careful with eager loading which could cause SELECT statements that fetch too much data. It can cause a Cartesian product if you eagerly load entities with several related collections.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If you want to override the LAZY fetch type for specific use cases, you can use Fetch Join. For example this query would eagerly load the employee addresses:&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/joinfetch.jpg" alt="" style="width: 502px; height: 198px;" /&gt;&lt;br /&gt;In General you should lazily load relationships, test your use case scenarios, check the SQL log, and use @NameQueries with JOIN FETCH to eagerly load when needed.&lt;br /&gt;&lt;h2&gt;Partitioning &lt;/h2&gt; the main goal of partitioning is to reduce the amount of data read for particular SQL operations so that the overall response time is reduced&lt;br /&gt;&lt;br /&gt;&lt;a set="yes" linkindex="42" target="_blank" href="http://dev.mysql.com/tech-resources/articles/performance-partitioning.html"&gt;&lt;span style="font-weight: bold;"&gt;                 Vertical Partitioning&lt;/span&gt;&lt;/a&gt;  splits tables with many columns into multiple tables with fewer columns, so that only certain columns are included in a particular dataset, with each partition including all rows.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;a linkindex="43" target="_blank" href="http://dev.mysql.com/tech-resources/articles/performance-partitioning.html"&gt;                 Horizontal Partitioning&lt;/a&gt; &lt;/span&gt;segments table rows so that distinct groups of physical row-based datasets are formed. All columns defined to a table are found in each set of partitions. An example of horizontal partitioning might be a table that contains historical data being partitioned by date.&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/partitioning.jpg" alt="" style="width: 483px; height: 309px;" /&gt;&lt;br /&gt;&lt;h3&gt;Vertical Partitioning&lt;/h3&gt;&lt;br /&gt;In the example of vertical partitioning below a table that contains a number of very wide text or BLOB columns that aren't referenced often is split into two tables with the most referenced columns in one table and the seldom-referenced text or BLOB columns in another.&lt;br /&gt;&lt;br /&gt;By removing the large data columns from the table, you get a faster query response time for the more frequently accessed Customer data. Wide tables can slow down queries, so you should always ensure that all columns defined to a table are actually needed.&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/verticalpartitioning.jpg" alt="" style="width: 484px; height: 309px;" /&gt;&lt;br /&gt;The example below shows the JPA mapping for the tables above. The Customer data table with the more frequently accessed and smaller data types  is mapped to the Customer Entity, the CustomerInfo table with the less frequently accessed and larger data types is mapped to the CustomerInfo Entity with a lazily loaded one to one relationship to the Customer.&lt;br /&gt;&lt;h3&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/verticalpart2.jpg" alt="" style="width: 506px; height: 338px;" /&gt;&lt;/h3&gt;&lt;br /&gt;&lt;h3&gt;Horizontal Partitioning&lt;/h3&gt; The major forms of horizontal partitioning are by Range, Hash, Hash Key, List, and Composite.&lt;br /&gt;&lt;br /&gt;Horizontal partitioning can make queries faster because the query optimizer knows what partitions contain the data that will satisfy a particular query and will access only those necessary partitions during query execution. Horizontal Partitioning works best for large database Applications that contain a lot of query activity that targets specific ranges of database tables.&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/horizontalpartitioning.jpg" alt="" style="width: 503px; height: 320px;" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Hibernate Shards &lt;/h4&gt; Partitioning data horizontally into "Shards"  is used by &lt;a set="yes" linkindex="44" target="_blank" href="http://www.javaworld.com/podcasts/jtech/2008/072408jtech.html"&gt;google&lt;/a&gt;, &lt;a linkindex="45" target="_blank" href="http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-4696&amp;amp;yr=2009&amp;amp;track=javaee"&gt;linkedin&lt;/a&gt;, and others to give extreme scalability for very large amounts of data. &lt;a linkindex="46" target="_blank" href="http://www.infoq.com/articles/ebay-scalability-best-practices"&gt;eBay "shards" data horizontally along its primary access path.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a linkindex="47" target="_blank" href="https://www.hibernate.org/414.html"&gt;Hibernate Shards&lt;/a&gt; is a framework that is designed to encapsulate support for horizontal partitioning into the Hibernate Core.&lt;br /&gt;&lt;img src="http://blogs.sun.com/carolmcdonald/resource/sharding.jpg" alt="" style="width: 506px; height: 330px;" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Caching&lt;/h2&gt; &lt;a set="yes" linkindex="48" href="http://weblogs.java.net/blog/caroljmcdonald/archive/2009/08/jpa_caching.html"&gt;JPA Level 2 caching&lt;/a&gt; avoids database access for already loaded entities, this make reading reading frequently accessed unmodified entities faster, however it can give bad scalability for frequent or concurrently updated entities.&lt;br /&gt;&lt;br /&gt;You should configure L2 caching for entities that are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;read often&lt;/li&gt;&lt;li&gt;modified infrequently&lt;/li&gt;&lt;li&gt;Not critical if stale&lt;/li&gt;&lt;/ul&gt; You should also configure L2 (vendor specific) caching for maxElements, time to expire, refresh... &lt;h3&gt;References and More Information:&lt;/h3&gt; &lt;a linkindex="49" target="_blank" href="http://www.slideshare.net/caroljmcdonald/td09jpabestpractices2"&gt;JPA             Best Practices presentation&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="50" target="_blank" href="http://blogs.sun.com/carolmcdonald/entry/mysql_for_developers"&gt;MySQL             for Developers Article&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="51" target="_blank" href="http://www.slideshare.net/caroljmcdonald/mysqlfordevelopers"&gt;MySQL             for developers presentation&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="52" target="_blank" href="http://blogs.sun.com/carolmcdonald/entry/mysql_for_developers_screencast_and"&gt;MySQL             for developers screencast&lt;/a&gt;&lt;br /&gt;&lt;a set="yes" linkindex="53" target="_blank" href="http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-3977&amp;amp;yr=2009&amp;amp;track=javaee"&gt;Keeping             a Relational Perspective for Optimizing Java Persistence&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="54" target="_blank" href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="55" target="_blank" href="http://www.apress.com/book/view/1590596455"&gt;Pro EJB 3: Java             Persistence API&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="56" target="_blank" href="http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-5214&amp;amp;yr=2009&amp;amp;track=javaee"&gt;Java             Persistence API 2.0: What's New ?&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="57" target="_blank" href="http://oreilly.com/catalog/9780596101718/"&gt;High Performance MySQL book&lt;/a&gt;&lt;br /&gt;&lt;a set="yes" linkindex="58" target="_blank" href="http://dev.mysql.com/tech-resources/articles/pro-mysql-ch6.html"&gt;Pro MySQL, Chapter 6: Benchmarking and Profiling&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="59" target="_blank" href="http://www.manning.com/panda/"&gt;EJB 3 in Action&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="60" target="_blank" href="http://highscalability.com/sharding-hibernate-way"&gt;sharding the hibernate way&lt;/a&gt;&lt;br /&gt;&lt;a linkindex="61" target="_blank" href="http://blogs.sun.com/carolmcdonald/entry/jpa_caching"&gt;JPA Caching&lt;/a&gt;&lt;br /&gt;&lt;a set="yes" linkindex="62" target="_blank" href="http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-4407&amp;amp;yr=2009&amp;amp;track=javaee"&gt;Best Practices for Large-Scale Web Sites: Lessons from eBay&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://weblogs.java.net/blog/caroljmcdonald/archive/2009/08/28/jpa-performance-dont-ignore-database-0"&gt;Original Source&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-162287787532226731?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/162287787532226731/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/jpa-performance-dont-ignore-database.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/162287787532226731'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/162287787532226731'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/jpa-performance-dont-ignore-database.html' title='JPA Performance: Dont Ignore the Database'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-7658534175519329181</id><published>2009-08-29T08:44:00.000-07:00</published><updated>2009-08-29T08:51:14.835-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Summary of Business Tier Patterns</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_-1g-DDEOJz0/SplNHqfGY0I/AAAAAAAAAe4/RH1HOVwgpsY/s1600-h/BusinessTierPattern.JPG"&gt;&lt;img style="cursor: pointer; width: 400px; height: 226px;" src="http://4.bp.blogspot.com/_-1g-DDEOJz0/SplNHqfGY0I/AAAAAAAAAe4/RH1HOVwgpsY/s400/BusinessTierPattern.JPG" alt="" id="BLOGGER_PHOTO_ID_5375412424302551874" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Presentation Tier will access the Business Tier via Business Delegate .&lt;br /&gt;Business Delegate inturn access EJB by looking up using Service Locator.&lt;br /&gt;The EJB [Session Facade] or the WebService End Point representing the Deployment Layer communicates to Application Service Layer [Algorithms, Main Logic].&lt;br /&gt;Application Service Layer access Entities [Business Objects].&lt;br /&gt;&lt;br /&gt;The communication between Tiers happen using Transfer Objects [not shown in the figure].&lt;br /&gt;The Deployment Layer can also communicate to Business Objects directly and not via Application Service Layer depending on the Transaction Design.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-7658534175519329181?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/7658534175519329181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/summary-of-business-tier-patterns.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7658534175519329181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7658534175519329181'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/summary-of-business-tier-patterns.html' title='Summary of Business Tier Patterns'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_-1g-DDEOJz0/SplNHqfGY0I/AAAAAAAAAe4/RH1HOVwgpsY/s72-c/BusinessTierPattern.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-1274941716667424735</id><published>2009-08-27T10:22:00.000-07:00</published><updated>2009-08-27T10:23:57.211-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Inventions'/><title type='text'>How do inventors actually think of inventions</title><content type='html'>How do inventors actually think of inventions? Most of them say they first define a problem, then come up with a way to solve it. “I look for something not being done efficiently,” says Micron’s Leonard Forbes. “I tour around a lot of conferences and keep up on the literature to try to identify problems. I’ll go through different approaches. It’s not usually an ‘aha’ moment, but more a process of elimination.”&lt;br /&gt;&lt;br /&gt;&lt;p&gt;What happens next is the strange, incomprehensible part: finding the answer. Yamazaki says he gets his best ideas after dozing. “Oftentimes, I’ll fall asleep while taking the train home at the end of the day,” he says. “I wake up, and I have an inspiration.”&lt;/p&gt;               &lt;p&gt;Mark Gardner, who stopped working for the chipmaker Advanced Micro Devices in 2005, also cogitates while snoozing. “I wake up every day thinking of inventions,” he says. “I don’t know if it’s a curse or a blessing.”&lt;/p&gt;               &lt;p&gt;Other innovators don’t close their eyelids to find inspiration but know that their brains function best when they’re not trying to work on a problem. “A lot of times, you don’t come up with solutions right away,” says Akram. “I keep a problem in the back of my mind, thinking about it in different settings, adding a little here and there. Some of this thinking occurs when I’m on a plane or driving my car.” Hearing this, Akram’s colleague Warren Farnworth pipes in, “I hate to say it, but there’s something about standing in the shower, rubbing my head with shampoo, and I’ll go, Wow, why didn’t I think of that before?”&lt;/p&gt;               &lt;p&gt;Also important is an environment that encourages experimentation. All the men say they have to feel free to propose any idea, no matter how outrageous. “When chasing an invention, you have to not be very critical of suggestions,” Weder says. “You have to try not to snicker.” Even when the wildest solutions don’t work, they can spark discussion that might lead to other ideas. “There are two kinds of supervisors,” says Akram. “One says, ‘Why are you wasting our time?’ The other says, ‘This is so cool!’ ” Micron’s researchers have thrived under the latter.&lt;/p&gt;               &lt;p&gt;Then there is another, less romantic reason why these men have so many patents. All work for firms that value patents, systematically and aggressively apply for them, and reward those who win them. At Micron, the patent lawyers’ offices are next to the R&amp;amp;D lab, so engineers can stop by and quickly find out if an idea is patentable. Yamazaki and Silverbrook now run companies that essentially produce nothing but patentable technology—and they make money licensing it to manufacturers. In his 24 years at A.M.D., which relies on patentable inventions to compete with archrival Intel, Gardner figures he made more than $1 million in bonuses from his patents.&lt;/p&gt;               &lt;p&gt;Which brings up the subject of wealth. It’s tempting to think that owning hundreds of patents must be the key to riches, but it’s not necessarily true. Inventions created while working for a company usually belong to that company. The leading inventors are all well paid—their firms understand their value—but none is cruising the Aegean on his yacht or lining up to buy the Yankees. Silverbrook has the best shot at great wealth, if his printer technology takes flight.&lt;/p&gt;               &lt;p&gt;Silverbrook, Yamazaki, and Weder will continue to chase one another at the top of the list. They each have nearly twice as many patents as the fifth-ranked inventor, Micron’s Gurtej Sandhu. They are the reticent megastars of invention, each eclipsing Edison just as Barry Bonds roared past Babe Ruth. These three, especially, deserve a place not just in the popular imagination, but also in history.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-1274941716667424735?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/1274941716667424735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/how-do-inventors-actually-think-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/1274941716667424735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/1274941716667424735'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/how-do-inventors-actually-think-of.html' title='How do inventors actually think of inventions'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-7070632502447499585</id><published>2009-08-27T09:56:00.000-07:00</published><updated>2009-08-27T09:57:25.456-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Cloud Computing'/><title type='text'>Traditional SaaS vs Cloud enabled SaaS</title><content type='html'>Inspired by Gilad's &lt;a linkindex="1" href="http://cloud-silver-lining.blogspot.com/2009/08/cloud-computing-programming-model-draft.html"&gt;great summary on the Cloud Programming model&lt;/a&gt;, I try to summarize the difference that I observe between the traditional SaaS model and the "cloud-enabled SaaS model". Although cloud providers advocates zero effort is need to migrate existing applications into the cloud, it is my belief that this "strict-port" approach doesn't fully exploit the full power of cloud computing. There are a number of characteristic that cloud is different from traditional data center environment, application which design along these characteristic will take more advantages from the cloud.&lt;br /&gt;&lt;br /&gt;I believe a Cloud-enabled-Application should have the following characteristic in its fundamental design.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-size: 130%;"&gt;Latency Awareness&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Traditional SaaS App typically run within a single data center and assume low latency among server components. Now in the cloud environment that span many distant geographic locations, but the assumption of low latency cannot hold any more. We need to be “smarter” when choosing where to deploy to avoid the situation of putting frequently communicating components between far-distant locations. “Cloud-enabled SaaS app” need to be aware of latency difference and built in self-configuring and self-tuning mechanism to cope with that.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-size: 130%;"&gt;Cost Awareness&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Traditional SaaS app typically run on already purposed hardware where utilization efficiency is not a concern. Now with the “pay as you go” model, application need to pay more attention to its usage pattern and efficiency of underlying resources because it will affect the operation cost. Cloud-enabled SaaS application need to understand the cost model of different resources utilization (such as CPU cost may be very different from Bandwidth cost) and adjust their usage strategy to minimize the operation cost.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-size: 130%;"&gt;Security Awareness&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Traditional SaaS app typically run on a fully trusted data center based on perimeter security. But in the Hybrid cloud model, the perimeter being drawn is very different now. Application need to carefully select where to store its data such that sensitivity will not be leaking. This involve careful determination of storage provider or use encryption for protection.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-size: 130%;"&gt;Capitalize on Elasticity&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Traditional SaaS App is not used to large-scale growth / shrink of compute resources and typically haven’t designed well to handle how data get distributed to newly joined machines (in a growth scenario) or redistributed among remaining machines (in a shrink scenario). This ends up having a very inefficient use of network bandwidth and results in high cost and low performance. More sophisticated data distribution protocol that align with the growth and shrink dimension is needed for “Cloud-enabled SaaS app”&lt;br /&gt;&lt;br /&gt;&lt;a href="http://horicky.blogspot.com/2009/08/traditional-saas-vs-cloud-enabled-saas.html"&gt;Original Source&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-7070632502447499585?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/7070632502447499585/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/traditional-saas-vs-cloud-enabled-saas.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7070632502447499585'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7070632502447499585'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/traditional-saas-vs-cloud-enabled-saas.html' title='Traditional SaaS vs Cloud enabled SaaS'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-6954649885213349860</id><published>2009-08-23T03:15:00.000-07:00</published><updated>2009-08-23T03:19:35.312-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Summary of Presentation Tier Patterns</title><content type='html'>&lt;meta equiv="Content-Type" content="text/html; charset=utf-8"&gt;&lt;meta name="ProgId" content="Word.Document"&gt;&lt;meta name="Generator" content="Microsoft Word 11"&gt;&lt;meta name="Originator" content="Microsoft Word 11"&gt;&lt;link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cvinutht%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"&gt;&lt;o:smarttagtype namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="City"&gt;&lt;/o:smarttagtype&gt;&lt;o:smarttagtype namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="place"&gt;&lt;/o:smarttagtype&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt;  &lt;w:worddocument&gt;   &lt;w:view&gt;Normal&lt;/w:View&gt;   &lt;w:zoom&gt;0&lt;/w:Zoom&gt;   &lt;w:punctuationkerning/&gt;   &lt;w:validateagainstschemas/&gt;   &lt;w:saveifxmlinvalid&gt;false&lt;/w:SaveIfXMLInvalid&gt;   &lt;w:ignoremixedcontent&gt;false&lt;/w:IgnoreMixedContent&gt;   &lt;w:alwaysshowplaceholdertext&gt;false&lt;/w:AlwaysShowPlaceholderText&gt;   &lt;w:compatibility&gt;    &lt;w:breakwrappedtables/&gt;    &lt;w:snaptogridincell/&gt;    &lt;w:wraptextwithpunct/&gt;    &lt;w:useasianbreakrules/&gt;    &lt;w:dontgrowautofit/&gt;   &lt;/w:Compatibility&gt;   &lt;w:browserlevel&gt;MicrosoftInternetExplorer4&lt;/w:BrowserLevel&gt;  &lt;/w:WordDocument&gt; &lt;/xml&gt;&lt;![endif]--&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt;  &lt;w:latentstyles deflockedstate="false" latentstylecount="156"&gt;  &lt;/w:LatentStyles&gt; &lt;/xml&gt;&lt;![endif]--&gt;&lt;!--[if !mso]&gt;&lt;object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id="ieooui"&gt;&lt;/object&gt; &lt;style&gt; st1\:*{behavior:url(#ieooui) } &lt;/style&gt; &lt;![endif]--&gt;&lt;style&gt; &lt;!--  /* Font Definitions */  @font-face 	{font-family:Verdana; 	panose-1:2 11 6 4 3 5 4 4 2 4; 	mso-font-charset:0; 	mso-generic-font-family:swiss; 	mso-font-pitch:variable; 	mso-font-signature:536871559 0 0 0 415 0;}  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal 	{mso-style-parent:""; 	margin:0in; 	margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:12.0pt; 	font-family:"Times New Roman"; 	mso-fareast-font-family:"Times New Roman";} @page Section1 	{size:8.5in 11.0in; 	margin:1.0in 1.25in 1.0in 1.25in; 	mso-header-margin:.5in; 	mso-footer-margin:.5in; 	mso-paper-source:0;} div.Section1 	{page:Section1;}  /* List Definitions */  @list l0 	{mso-list-id:160387944; 	mso-list-type:hybrid; 	mso-list-template-ids:-375988492 67698705 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 	{mso-level-text:"%1\)"; 	mso-level-tab-stop:.5in; 	mso-level-number-position:left; 	text-indent:-.25in;} @list l1 	{mso-list-id:393897268; 	mso-list-type:hybrid; 	mso-list-template-ids:293644402 -1469808246 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l1:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l2 	{mso-list-id:426194648; 	mso-list-type:hybrid; 	mso-list-template-ids:2014112246 -1559612748 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l2:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l2:level2 	{mso-level-number-format:alpha-lower; 	mso-level-tab-stop:1.75in; 	mso-level-number-position:left; 	margin-left:1.75in; 	text-indent:-.25in;} @list l3 	{mso-list-id:498275919; 	mso-list-type:hybrid; 	mso-list-template-ids:479510778 -1603237680 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l3:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l4 	{mso-list-id:517696468; 	mso-list-type:hybrid; 	mso-list-template-ids:1524288814 874822552 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l4:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l5 	{mso-list-id:873426528; 	mso-list-type:hybrid; 	mso-list-template-ids:928406902 197293380 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l5:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l6 	{mso-list-id:1541940966; 	mso-list-type:hybrid; 	mso-list-template-ids:33569972 -220572932 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l6:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l7 	{mso-list-id:1714185016; 	mso-list-type:hybrid; 	mso-list-template-ids:713954836 1543031730 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l7:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l7:level2 	{mso-level-number-format:alpha-lower; 	mso-level-tab-stop:1.75in; 	mso-level-number-position:left; 	margin-left:1.75in; 	text-indent:-.25in;} @list l8 	{mso-list-id:1835104838; 	mso-list-type:hybrid; 	mso-list-template-ids:1098005750 1827571488 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l8:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l9 	{mso-list-id:2053453124; 	mso-list-type:hybrid; 	mso-list-template-ids:-192757162 2058749036 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l9:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} @list l9:level2 	{mso-level-number-format:alpha-lower; 	mso-level-tab-stop:1.75in; 	mso-level-number-position:left; 	margin-left:1.75in; 	text-indent:-.25in;} @list l10 	{mso-list-id:2079018171; 	mso-list-type:hybrid; 	mso-list-template-ids:620895380 -153199962 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l10:level1 	{mso-level-number-format:alpha-lower; 	mso-level-text:"%1\)"; 	mso-level-tab-stop:1.25in; 	mso-level-number-position:left; 	margin-left:1.25in; 	text-indent:-.25in;} ol 	{margin-bottom:0in;} ul 	{margin-bottom:0in;} --&gt; &lt;/style&gt;&lt;!--[if gte mso 10]&gt; &lt;style&gt;  /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin:0in; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;} &lt;/style&gt; &lt;![endif]--&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;&lt;span style=""&gt;1)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;Intercepting Filters&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to intercept and manipulate a request and a response before and after the request is processed.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;When:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Does the client have a valid session?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Does the request path violate any constraints?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;c)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Do you support the browser type of the client?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;d)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;What encoding does the client use to send the data?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;e)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Is the request stream encrypted or compressed?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Actors:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Filter Manager&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Filter Manager manages Filter Processing. It creates the FilterChain with the appropriate filters, in the correct order, and initiates processing.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Filter Chain&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;The FilterChain is an ordered collection of independent filters.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;c)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Filter&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Filter represents individual filters.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;d)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Target&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;The Target is the resource requested by the client. Target here can be a Front Controller.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;&lt;span style=""&gt;2)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;Front Controller&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want a centralized access point for presentation-tier request handling&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;When&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to avoid duplicate control logic&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to apply common logic to multiple requests.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;c)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to separate system processing logic from the view.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;d)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to centralize controlled access points into your system.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Does&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Converts the protocol specific requests into a more general form with the help of Context Object. An example of this is retrieving parameters from an HttpServletRequest instance and populating a MyRequest object that can be used independent of any servlet-related artifacts.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Validates the ContextObject for FormBased and Business Validation using the Validator provided by the ContextObject.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;c)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Delegates request to Application Controller.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;          &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;&lt;span style=""&gt;3)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;Context Object&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to avoid using protocol-specific system information outside of its relevant context. An example of this is retrieving parameters from an HttpServletRequest instance and populating a MyRequest object that can be used independent of any servlet-related artifacts. Context Object also provides a framework to validate the user data.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Actors&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;ProtocolInterface&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt; [HttpServletRequest, HttpServletResponse]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;An object that exposes protocol or tier-specific details.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;ContextFactory&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt; [RequestContextFactory, ResponseContextFactory]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;A ContextFactory creates a protocol and tier independent ContextObject.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;c)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;ContextObject&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt; [RequestContextObject, ResponseContextObject]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;ContextObject is a generic object used to share domain-neutral state throughout an application. Per Form a separate ContextObject can be created like for example StudentAdmission form can have its own ContextObject called StudentAdmissionContextObject which has its own logic for validating form.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;&lt;span style=""&gt;4)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;Application Controller&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to centralize and modularize action and view management.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Does&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;First, the incoming request must be resolved to an action that services the request. This is called action management.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Second, the appropriate view is located and dispatched. This is called view management.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Actors&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Client&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Client invokes the application controller. In the presentation tier, a FrontController or an InterceptingFilter typically fulfill this role.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;ApplicationController&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Uses Mapper to resolve an incoming request to the appropriate action and view, to which it delegates or dispatches.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;c)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Mapper&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Uses a Map to translate an incoming request into the appropriate action [Command] and view [ViewFactory].&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;d)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Map&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Holds references to handles that represent target resources [Command or ViewFactory]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;e)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Target&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;A resource that helps fulfill a particular request, including commands, views and style sheets.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;&lt;span style=""&gt;5)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;CommandProcessing&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to perform core request handling and invoke business logic before control is passed to the view.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Actors&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;ApplicationController&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Manages choosing the appropriate action [Command] to fulfill the request.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;CommandProcessor&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Helps in processing the Command.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;c)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Command&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Command performs an action for a request. It invokes the business method on the BusinessDelegate.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;d)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;BusinessDelegate&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;BusinessDelegate acts as the façade which hides the details of invoking the BusinessService.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;e)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;BusinessService&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;BusinessService is the &lt;st1:place st="on"&gt;&lt;st1:city st="on"&gt;Enterprise&lt;/st1:city&gt;&lt;/st1:place&gt; Beans normally Session Beans which does our task and returns the data in the form of the PresentationModel used by the view to render.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;f)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;      &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;PresentationModel&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;PresentationModel is the data returned by the BusinessService for the use by the view.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;&lt;span style=""&gt;6)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 18pt;"&gt;ViewFactory&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Helps in creating View&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Does&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;View Preparation&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;The view preparation phase involves request handling, action management, and view management. A request is resolved to a specific action, that action is invoked and the appropriate view is identified, with the request then being dispatched to that view.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;View Creation&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Subsequently, during view creation, the view retrieves content from its model, using helpers to retrieve and adapt the model state. Often extracted and adapted from TransferObject, this content is inserted, formatted, and converted into the static template text of the view to generate a dynamic response.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 0.5in;"&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Actors&lt;/span&gt;&lt;/b&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;a)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;ViewHelper&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;It helps in separating a view from its processing logic. This can be in the form of JSPTags, Helper Pojos etc.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;b)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;ViewTemplate&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Template used in creating view.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;c)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;CompositeView&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;You want to build a view from modular, atomic component parts that are combined to create a composite whole, while managing the content and the layout independently.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in; text-indent: -0.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;span style=""&gt;d)&lt;span style="font-family: &amp;quot;Times New Roman&amp;quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;!--[endif]--&gt;&lt;b style=""&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;PresentationModel&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-left: 1.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;Presentation Model is the data used by the view.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Verdana;"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-6954649885213349860?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/6954649885213349860/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/summary-of-presentation-tier-patterns.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6954649885213349860'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6954649885213349860'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/summary-of-presentation-tier-patterns.html' title='Summary of Presentation Tier Patterns'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-5008683516340592121</id><published>2009-08-14T23:49:00.000-07:00</published><updated>2009-08-14T23:54:11.336-07:00</updated><title type='text'>ACID Properties</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Atomicity&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;First, a transaction needs to be atomic (or all-or-nothing], meaning that it executes&lt;br /&gt;completely or not at all. There must not be any possibility that only part&lt;br /&gt;of a transaction program is executed.&lt;br /&gt;For example, suppose we have a transaction program that moves $100 from&lt;br /&gt;account A to account B. It takes $100 out of account A and adds it to account&lt;br /&gt;B. When this runs as a transaction, it has to be atomic—either both or neither&lt;br /&gt;of the updates execute. It must not be possible for it to execute one of the&lt;br /&gt;updates and not the other.&lt;br /&gt;The TP system guarantees atomicity through database mechanisms that&lt;br /&gt;track the execution of the transaction. If the transaction program should fail&lt;br /&gt;for some reason before it completes its work, the TP system will undo the&lt;br /&gt;effects of any updates that the transaction program has already done. Only if it&lt;br /&gt;gets to the very end and performs all of its updates will the TP system allow&lt;br /&gt;the updates to become a permanent part of the database.&lt;br /&gt;By using the atomicity property, we can write a transaction program that&lt;br /&gt;emulates an atomic business transaction, such as a bank account withdrawal,&lt;br /&gt;a flight reservation, or a sale of stock shares. Each of these business actions&lt;br /&gt;requires updating multiple data items. By implementing the business action&lt;br /&gt;by a transaction, we ensure that either all of the updates are performed or&lt;br /&gt;none are. Furthermore, atomicity ensures the database is returned to a known&lt;br /&gt;state following a failure, reducing the requirement for manual intervention&lt;br /&gt;during restart. The successful completion&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;of a transaction is called commit. The failure of&lt;br /&gt;a transaction is called abort.&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;Consistency&lt;/span&gt;&lt;br /&gt;A second property of transactions is consistency—a transaction program&lt;br /&gt;should maintain the consistency of the database. That is, if you execute the&lt;br /&gt;transaction all by itself on a database that's initially consistent, then when the&lt;br /&gt;transaction finishes executing the database is again consistent.&lt;br /&gt;&lt;br /&gt;By consistent, we mean "internally consistent." In database terms, this&lt;br /&gt;means that the database satisfies all of its integrity constraints. There are&lt;br /&gt;many possible kinds of integrity constraints, such as the following:&lt;br /&gt;• All primary key values are unique (e.g., no two employee records have&lt;br /&gt;the same employee number).&lt;br /&gt;• The database has referential integrity, meaning that records only reference&lt;br /&gt;objects that exist (e.g., the Part record and Customer record that are&lt;br /&gt;referenced by an Order record really exist).&lt;br /&gt;• Certain predicates hold (e.g., the sum of expenses in each department is&lt;br /&gt;less than or equal to the department's budget).&lt;br /&gt;Ensuring that transactions maintain the consistency of the database is good&lt;br /&gt;programming practice. However, unlike atomicity, isolation, and durability,&lt;br /&gt;consistency is a responsibility shared between transaction programs and the&lt;br /&gt;TP system that executes those programs. That is, a TP system ensures that a&lt;br /&gt;set of transactions is atomic, isolated, and durable, whether or not they are&lt;br /&gt;programmed to preserve consistency. Thus, strictly speaking, the ACID test&lt;br /&gt;for transaction systems is a bit too strong, because the TP system does its part&lt;br /&gt;for C only by guaranteeing AID. It's the application programmer's responsibility&lt;br /&gt;to ensure the transaction program preserves consistency.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Isolation&lt;/span&gt;&lt;br /&gt;The third property of a transaction is isolation. We say that a set of transactions&lt;br /&gt;is isolated if the effect of the system running them is the same as if it ran&lt;br /&gt;them one at a time. The technical definition of isolation is serializability. An&lt;br /&gt;execution is serializable (meaning isolated) if its effect is the same as running&lt;br /&gt;the transactions serially, one after the next, in sequence, with no overlap in&lt;br /&gt;executing any two of them. This has the same effect as running the transactions&lt;br /&gt;one at a time.&lt;br /&gt;A classic example of a nonisolated execution is a banking system, where&lt;br /&gt;two transactions each try to withdraw the last $100 in an account. If both&lt;br /&gt;transactions read the account balance before either of them updates it, then&lt;br /&gt;both transactions will determine there's enough money to satisfy their&lt;br /&gt;requests, and both will withdraw the last $100. Clearly, this is the wrong&lt;br /&gt;result. Moreover, it isn't a serializable result. In a serial execution, only the&lt;br /&gt;first transaction to execute would be able to withdraw the last $100. The second&lt;br /&gt;one would find an empty account.&lt;br /&gt;Notice that isolation is different from atomicity. In the example, both&lt;br /&gt;transactions executed completely, so they were atomic. However, they were&lt;br /&gt;not isolated and therefore produced undesirable behavior.&lt;br /&gt;If the execution is serializable, then from the point of view of an end user&lt;br /&gt;who submits a request to run a transaction, the system looks like a standalone&lt;br /&gt;system that's running that transaction all by itself. Between the time he&lt;br /&gt;or she runs two transactions, other transactions from other users may run. But&lt;br /&gt;&lt;br /&gt;during the period that the system is processing that one user's transaction, it&lt;br /&gt;appears to the user that the system is doing no other work. But this is only an&lt;br /&gt;illusion. It's too inefficient for the system to actually run transactions serially&lt;br /&gt;because there is lots of internal parallelism in the system that must be&lt;br /&gt;exploited by running transactions concurrently.&lt;br /&gt;If each transaction preserves consistency, then any serial execution (i.e.,&lt;br /&gt;sequence) of such transactions preserves consistency. Since each serializable&lt;br /&gt;execution is equivalent to a serial execution, a serializable execution of the&lt;br /&gt;transactions will preserve database consistency, too. It is the combination of&lt;br /&gt;transaction consistency and isolation that ensures that executions of sets of&lt;br /&gt;transactions preserve database consistency.&lt;br /&gt;The database typically places locks on data accessed by each transaction.&lt;br /&gt;The effect of placing the locks is to make the execution appear to be serial. In&lt;br /&gt;fact, internally, the system is running transactions in parallel, but through&lt;br /&gt;this locking mechanism the system gives the illusion that the transactions are&lt;br /&gt;running serially, one after the next. In Chapter 6 on locking, we will describe&lt;br /&gt;those mechanisms in more detail and present the rather subtle argument why&lt;br /&gt;locking actually produces serializable executions.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Durability&lt;/span&gt;&lt;br /&gt;The fourth property of a transaction is durability. Durability means that when&lt;br /&gt;a transaction completes executing, all of its updates are stored on a type of&lt;br /&gt;storage, typically disk storage, that will survive the failure of the TP system.&lt;br /&gt;So even if the transaction program or operating system fails, once the transaction&lt;br /&gt;has committed, its results are durably stored on a disk and can be found&lt;br /&gt;there after the system recovers from the failure.&lt;br /&gt;Durability is important because each transaction is usually providing a service&lt;br /&gt;that amounts to a contract between its users and the enterprise that is&lt;br /&gt;providing the service. For example, if you're moving money from one account&lt;br /&gt;to another, once you get a reply from the transaction saying that it executed,&lt;br /&gt;you really expect that the result is permanent. It's a legal agreement between&lt;br /&gt;the user and the system that the money has been moved between these two&lt;br /&gt;accounts. So it's essential that the transaction actually makes sure that the&lt;br /&gt;updates are stored on some nonvolatile device, typically disk storage in&lt;br /&gt;today's technology, to ensure that the updates cannot possibly be lost after the&lt;br /&gt;transaction finishes executing. Moreover, the durability of the result often&lt;br /&gt;must be maintained for a long period. For example, some tax regulations allow&lt;br /&gt;audits that can take place years after the transactions were completed.&lt;br /&gt;The durability property is usually obtained via a mechanism that starts by&lt;br /&gt;having the TP system write a copy of all the transaction's updates to a log file&lt;br /&gt;while the transaction program is running. When the transaction program&lt;br /&gt;issues the commit operation, the system first ensures that all the records written&lt;br /&gt;to the log file are out on disk, and then returns to the transaction program,&lt;br /&gt;indicating that the transaction has indeed committed and that the results are&lt;br /&gt;durable. The updates may be written to the database right away, or they may&lt;br /&gt;&lt;br /&gt;be written a little later. However, if the system fails after the transaction commits&lt;br /&gt;and before the updates go to the database, then after the system recovers&lt;br /&gt;from the failure it must repair the database. To do this, it rereads the log and&lt;br /&gt;checks that each update by a committed transaction actually made it to the&lt;br /&gt;database. If not, it reapplies the update to the database. When this recovery&lt;br /&gt;activity is complete, the system resumes normal operation. Thus, any new&lt;br /&gt;transaction will read a database state that includes all committed updates. We&lt;br /&gt;describe log-based recovery algorithms in Chapter 8.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-5008683516340592121?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/5008683516340592121/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/acid-properties.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5008683516340592121'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5008683516340592121'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/acid-properties.html' title='ACID Properties'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-5016033830142444627</id><published>2009-08-14T10:00:00.000-07:00</published><updated>2009-08-14T10:01:07.014-07:00</updated><title type='text'>Modularity by Example</title><content type='html'>&lt;p&gt;There are lots of benefits to modularity, some of which I discussed when introducing &lt;a set="yes" linkindex="42" href="http://techdistrict.kirkk.com/2009/08/05/modularity-patterns/"&gt;modularity patterns&lt;/a&gt;. But here’s a simple example, which serves as a prelude to some upcoming posts explaining a few of the patterns.&lt;/p&gt; &lt;p&gt;&lt;a set="yes" linkindex="43" href="http://techdistrict.kirkk.com/wp-content/uploads/2009/08/all2.png"&gt;&lt;img src="http://techdistrict.kirkk.com/wp-content/uploads/2009/08/all2.png" alt="" width="296" height="220" /&gt;&lt;/a&gt;In the diagram at right (click to enlarge), the top left quadrant shows a sample system with a relatively complex class structure. When change occurs within a single class, shown in red in the bottom left quadrant, understanding the impact of change is difficult. It appears possible that it can propagate to any class dependent on the class highlighted in red.  Assessing the impact of change requires that we analyze the complete class structure. The ripple effect appears significant, and change instills fear.&lt;/p&gt; &lt;p&gt;But if the system is modular with classes allocated to these modules, as shown in the bottom right quadrant, then understanding the impact of change can be isolated to a discrete set of modules. And this makes it much easier to identify which modules contain classes that might also change, as shown in the top right quadrant. Change is isolated to classes within modules that are dependent on the module containing the class that is changing.&lt;/p&gt; &lt;p&gt;This is a simple example, but it serves as evidence of the need for modular architecture, and illustrates one reason why modularity is so important. Modularity makes understanding the system easier. It makes maintaining the system easier. And it makes reusing system modules much more likely. As systems grow in size and complexity, it’s imperative that we design more modular software. That means we need a module system for the Java platform. It means that module system shouldn’t be shielded from enterprise developers. And it means we need to understand the patterns that are going to provide the guidance necessary in helping us design more modular software.&lt;/p&gt;&lt;p&gt;&lt;em&gt;From &lt;a linkindex="44" href="http://techdistrict.kirkk.com/"&gt;http://techdistrict.kirkk.com&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-5016033830142444627?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/5016033830142444627/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/modularity-by-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5016033830142444627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/5016033830142444627'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/modularity-by-example.html' title='Modularity by Example'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-623864318733502048</id><published>2009-08-14T09:03:00.000-07:00</published><updated>2009-08-14T09:04:21.561-07:00</updated><title type='text'>Presentation Model Pattern</title><content type='html'>&lt;p&gt;&lt;b&gt;Presentation Model&lt;/b&gt; is a design pattern used to build desktop GUIs with a clean separation of concerns. One of the most important advantages of this pattern is the easy creation of unit tests that can focus on the application logic without touching UI components. In fact, this is a goal that many other patterns have failed to accomplish. Read the &lt;a set="yes" linkindex="43" href="http://home1.codecomics.com/article-5"&gt;advantages of the Presentation Model pattern&lt;/a&gt; for more details.&lt;/p&gt;  &lt;p&gt;The first characteristic of the Presentation Model pattern that you have to understand is how its classes (often referred to as layers) are organized. &lt;b&gt;Figure 1&lt;/b&gt; shows its structure, which details the following classes: &lt;b&gt;view&lt;/b&gt;, &lt;b&gt;presentation model&lt;/b&gt; and &lt;b&gt;service layer&lt;/b&gt;. Before explaining each of them, pay special attention to the references (arrows) and notice that the class that holds the state and logic of the GUI (presentation model class) does not have a reference to the view. What happens is exactly the opposite: the view that has a reference to this class.&lt;/p&gt;  &lt;p&gt;Let's understand the role of each of these classes in the pattern:&lt;/p&gt;  &lt;ul&gt;&lt;li&gt;&lt;b&gt;View&lt;/b&gt;: Simple class that contains the UI components. The displayed data reflects the state kept by the Presentation Model class.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Presentation Model:&lt;/b&gt; Class that holds the state of the GUI and its logic. The code operates on data and variables in memory and is unaware of the existence of UI components and presentation formats.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Service Layer&lt;/b&gt;: Class that represents a channel between the Presentation Model and the outer world, where different types of service might exist (e.g., EJBs, databases, etc.). What lies beyond this layer is not seen by the Presentation Model class.&lt;/li&gt;&lt;/ul&gt;  &lt;div style="margin: 0.5em;" align="center"&gt; &lt;img src="http://www.componenthouse.com/images/articles/presentationModel.png" /&gt;&lt;br /&gt;&lt;b&gt;Figure 1: Three layers of the pattern.&lt;/b&gt; &lt;/div&gt;  &lt;p&gt;The Presentation Model class has no idea about views. It simply holds the state of the GUI and fires events when this state changes. Moreover, it is possible to attach more than one view to a presentation model instance, which leads to diferent visualizations of the same data.&lt;/p&gt;  &lt;p&gt;The separation into decoupled layers and different responsibilities brings important advantages to the GUI implementation and maintenance. When we work this way, the layers become thinner and Ö consequently Ö more simple, which increases the understandability of the code. Besides that, the operations with models and business rules happen only in the presentation model layer, where the unit tests can reach efficiently.&lt;/p&gt;  &lt;p&gt;The Presentation Model pattern applied without a framework requires some synchronization code between the layers (see the articles on the JGoodies Binding API).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-623864318733502048?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/623864318733502048/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/presentation-model-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/623864318733502048'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/623864318733502048'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/presentation-model-pattern.html' title='Presentation Model Pattern'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-1441189035337497554</id><published>2009-08-10T04:14:00.000-07:00</published><updated>2009-08-10T04:17:40.134-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>Inheritence in Javascript</title><content type='html'>&lt;b&gt;Example of Inheritence in Javascript:&lt;/b&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;function &lt;span class="Apple-style-span"  style="color:#33CCFF;"&gt;Person&lt;/span&gt;(name) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;this.name = name;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;this.printName = function() {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;alert("Name is "+this.name);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;function &lt;span class="Apple-style-span"  style="color:#009900;"&gt;Employee&lt;/span&gt;(name, sal) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;b&gt;Person.call(this, name);&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;this.sal = sal;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;this.printSalary = function() {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;alert("Salary is "+this.sal);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;function &lt;span class="Apple-style-span"  style="color:#FF0000;"&gt;NonFunctionalEmployee&lt;/span&gt;(name, sal, hours) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;b&gt;Employee.call(this, name, sal);&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;this.hours = hours;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;this.printHours = function() {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;alert("Number of hours of work "+this.hours);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;var nonFunctionalEmp = new NonFunctionalEmployee("XYZ", 3333, 12);&lt;/div&gt;&lt;div&gt;nonFunctionalEmp.printName();&lt;/div&gt;&lt;div&gt;nonFunctionalEmp.printSalary();&lt;/div&gt;&lt;div&gt;nonFunctionalEmp.printHours();&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;call(this) is the main api which helps in inheritence.&lt;/div&gt;&lt;div&gt;The "this" parameter specifies the context in which the call() should get executed.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-1441189035337497554?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/1441189035337497554/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/inheritence-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/1441189035337497554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/1441189035337497554'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/inheritence-in-javascript.html' title='Inheritence in Javascript'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-7290871110120170379</id><published>2009-08-09T10:42:00.000-07:00</published><updated>2009-08-09T10:44:03.879-07:00</updated><title type='text'>Choosing a Web Development Framework</title><content type='html'>Here is a list which helps in choosing the web development framework -   &lt;ol&gt;&lt;li&gt;&lt;strong&gt;Support for MVC&lt;/strong&gt; (most frameworks have pretty decent support for that now)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Extensible MVC&lt;/strong&gt; (need to be able to extend the way controllers and views operate. Some frameworks do it by convention and limit you to a set of popular conventions.)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Allows you to build your domain in isolation.&lt;/strong&gt; (I want my domain model to be completely decoupled from any web technologies, persistence, etc... Just a plain OO domain model)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Gives you very flexible persistence options.&lt;/strong&gt; (I might decide to use a fully featured ORM (ActiveRecord is not fully featured), or I might want to use SQL, or heck, I might want to do both for efficiency or to scratch a morning itch, who cares about the reason, please let me choose. Oh, and one more thing, what if I don't want to use a SQL database at all? I want to use a native XML store or better yet a key/value store. Even if it's just to piss someone off, I want to do this and one should be able to accomplish that pretty easily. I'm not asking for a mapper for these stores, simply just don't make your framework bound to some relational store though making the work of turning this dependency off a 5 hour chore.)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Supports AJAX&lt;/strong&gt; (I should be able to easily render JSON or XML views, without much plumbing or lots of mappings and annotations. The authentications and forms support should also expose some form of ajax compliant interfaces, so that forms and authentication can be done using ajax if I choose so. Be able to easily parse submitted data into some data structure and validate/synchronize it with the domain model.)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Bindings&lt;/strong&gt; (All frameworks have some sort of bindings. Some of them are limiting. I don't want to create command objects to just simply bind the data and than synch with my domain model. If I have domain object graph(s), I should be able to bind it in the view layer. Bindings should be customizable. In Spring MVC for example, you can only bind one input control to a field or set of fields in the command object, but what if I want to bind 3 input fields that collectively represent one field in the domain object, I'm out of luck, unless I use javascript to first serialize those input fields into one field. That really sucks.)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Support RESTful, stateless, and other web concepts in a straightforward way.&lt;/strong&gt; (I want to be able to configure every part of HTTP and the web and make the application work, look, interact in my way that's compatible with the web, not your way. Some component based frameworks make that harder that it should be, like the fact that they are inherently stateful by default. Some make it hard to support RESTful or custom URI schemes, because they transfer state through URL rewriting. All of these problems don't exist in some frameworks, like Rails, Spring MVC, Grails, etc..., so I know it's possible.)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Validations&lt;/strong&gt; (Most have fully fledged validation support, but I can't say that it can't be made a bit easier. I do like Spring's flexible validation support.)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Forms&lt;/strong&gt; (This is a big one. Can you provide a flexible way of creating forms and layouts. I mean seriously, we're developing forms today the same way we've developed forms 15 years ago. Every other aspect of development has moved on, but we're still doing bullshit html form controls. XForms is a way out, but no browser support and pretty hard to integrate support from vendors like Orbeon and Chiba makes the standard useless. Can we either embrace it or come up with something else. Am I the only one that gets an anxiety attack every time I think about creating yet another interactive form that doesn't do anything much differently than the form I created 4 months ago for a different project, though I either have to copy and paste all the cruft or start from the absolute scratch. Wow, that's sad IMO.)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Scalability&lt;/strong&gt;. I know this one again is not up to the framework, but as I mentioned before, the framework can make it easier or harder to achieve. For example, inherently stateful frameworks that require either session affinity or replication of session state, make it very hard to horizontally scale. Yes, I know you can scale with replications tools available out there, but any synchronous replications is not linearly scalable. So any such frameworks makes it harder. There are many other criteria that can make a framework more scalable than others, but in general, statelessness, stability, and speed makes it viable for faster scalability tunes.)&lt;/li&gt;&lt;/ol&gt;&lt;a href="http://www.ilyasterin.com/blog/2009/07/choosing-a-web-development-frameworktoolkit.html"&gt;Original Source&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-7290871110120170379?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/7290871110120170379/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/choosing-web-development-framework.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7290871110120170379'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7290871110120170379'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/choosing-web-development-framework.html' title='Choosing a Web Development Framework'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-7347842387981342660</id><published>2009-08-08T07:52:00.000-07:00</published><updated>2009-08-08T08:03:09.139-07:00</updated><title type='text'>Load Balancing Web Applications</title><content type='html'>&lt;p&gt; This article offers an overview of several approaches to load balancing on Web application server clusters. A cluster is a group of servers running a Web application simultaneously, appearing to the world as if it were a single server. To balance server load, the system distributes requests to different nodes within the server cluster, with the goal of optimizing system performance. This results in higher availability and scalability -- necessities in an enterprise, Web-based application. &lt;/p&gt;  &lt;p&gt;High availability can be defined as redundancy. If one server cannot handle a request, can other servers in the cluster handle it? In a highly available system, if a single Web server fails, then another server takes over, as transparently as possible, to process the request. &lt;/p&gt;         &lt;p&gt;Scalability is an application's ability to support a growing number of users. If it takes an application 10 milliseconds(ms) to respond to one request, how long does it take to respond to 10,000 concurrent requests? Infinite scalability would allow it to respond to those in 10 ms; in the real world, it's somwhere between 10 ms and a logjam. Scalability is a measure of a range of factors, including the number of simultaneous users a cluster can support and the time it takes to process a request. &lt;/p&gt;          &lt;p&gt;Of the many methods available to balance a server load, the main two are:&lt;/p&gt;   &lt;ul&gt;&lt;li&gt;DNS round robin&lt;/li&gt; and &lt;li&gt;Hardware load balancers.&lt;/li&gt;&lt;/ul&gt;    &lt;h3&gt;DNS Round Robin&lt;/h3&gt;  &lt;p&gt;As most readers of ONJava probably know, the Domain Name Server (DNS) database maps host names to their IP addresses.&lt;/p&gt;  &lt;p&gt;&lt;img src="http://onjava.com/onjava/2001/09/26/graphics/Figure1.gif" alt="Diagram." width="466" border="0" height="497" /&gt;&lt;/p&gt;  &lt;p&gt;When you enter a URL in a browser (say,  &lt;code&gt;www.loadbalancedsite.com&lt;/code&gt;), the browser sends a request to the DNS asking it to return the IP address of the site. This is called the DNS lookup. After the Web browser gets the IP address for that site, it contacts the site using the IP address, and displays the page for you.&lt;/p&gt;  &lt;p&gt;The DNS server generally contains a single IP address mapped to a particular site name. In our fictional example, our site www.loadbalancedsite.com maps to the IP address 203.24.23.3&lt;/p&gt;  &lt;p&gt;To balance server loads using DNS, the DNS server maintains several different IP addresses for a site name. The multiple IP addresses represent the machines in the cluster, all of which map to the same single logical site name. Using our example, www.loadbalancedsite.com could be hosted on three machines in a cluster with the following IP addresses:&lt;/p&gt;    &lt;blockquote&gt; &lt;p&gt; 203.34.23.3&lt;br /&gt;203.34.23.4&lt;br /&gt;203.34.23.5&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In this case, the DNS server contains the following mappings:&lt;/p&gt;  &lt;blockquote&gt; &lt;pre&gt;&lt;code&gt;www.loadbalancedsite.com  203.34.23.3&lt;br /&gt;www.loadbalancedsite.com  203.34.23.4&lt;br /&gt;www.loadbalancedsite.com  203.34.23.5&lt;/code&gt;&lt;/pre&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;img src="http://onjava.com/onjava/2001/09/26/graphics/Figure2.gif" alt="Diagram." width="468" border="0" height="360" /&gt;&lt;/p&gt;  &lt;p&gt; When the first request arrives at the DNS server, it returns the IP address 203.34.23.3, the first machine. On the second request, it returns the second IP address: 203.34.23.4. And so on. On the fourth request, the first IP address is returned again. &lt;/p&gt;  &lt;p&gt;Using the above DNS round robin, all of the requests to the particular site have been evenly distributed among all of the machines in the cluster. Therefore, with the DNS round robin method of load balancing, all of the nodes in the cluster are exposed to the net.&lt;/p&gt;  &lt;h3&gt;Advantages of DNS Round Robin&lt;/h3&gt;  &lt;p&gt;The main advantages of DNS round robin are that it's cheap and easy:&lt;/p&gt;  &lt;ul&gt;&lt;li&gt;&lt;p&gt;&lt;b&gt;Inexpensive and easy to set up.&lt;/b&gt; The system administrator only needs to make a few changes in the DNS server to  support round robin, and many of the newer DNS servers already include support.  It doesn't require any code change to the Web application; in fact, Web applications aren't aware of the load-balancing scheme in front of it.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;b&gt;Simplicity.&lt;/b&gt; It does not require any networking experts to set up  or debug the system in case a problem arises.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;     &lt;h3&gt;Disadvantages of DNS Round Robin&lt;/h3&gt;  &lt;p&gt;Two main disadvantages of this software-based method of load balancing are that it  offers no real support for server affinity and doesn't support high availability.&lt;/p&gt;  &lt;ul&gt;&lt;li&gt;&lt;p&gt;&lt;b&gt;No support for &lt;a set="yes" linkindex="20" href="http://www-4.ibm.com/software/webservers/appserv/doc/v35/ae/infocenter/was/07010603.html"&gt;server affinity&lt;/a&gt;&lt;/b&gt;. Server affinity is a load-balancing system's ability to manage a user's requests, either to a specific server or any server, depending on whether session information is maintained on the server or at an underlying, database level. &lt;/p&gt;  &lt;p&gt; Without server affinity, DNS round robin relies on one of three methods devised to maintain session control or user identity to requests coming in over HTTP, which is a stateless protocol. &lt;/p&gt;    &lt;ul type="disc"&gt;&lt;li&gt;cookies&lt;/li&gt;&lt;li&gt;hidden fields&lt;/li&gt;&lt;li&gt;URL rewriting&lt;/li&gt;&lt;/ul&gt;  &lt;p&gt; When a user makes a first request, the Web server returns a text-based token uniquely identifying that user. Subsequent requests include this token using either cookies, URL rewriting, or hidden fields, allowing the server to appear to maintain a session between client and server. When a user establishes a session with one server, all subsequent requests usually go to the same server. &lt;/p&gt;  &lt;p&gt; The problem is that the browser caches that server's IP address. Once the cache expires, the browser makes another request to the DNS server for the IP address associated with the domain name. If the DNS server returns a differnt IP address, that of another server in the cluster, the session information is lost. &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt; &lt;b&gt;No support for high availability.&lt;/b&gt;  Consider a cluster of &lt;i&gt;n&lt;/i&gt; nodes. If a node goes down, then every &lt;i&gt;n&lt;/i&gt;th request to the DNS server directs you to the dead node. An advanced router solves this problem by checking nodes at regular intervals, detecting failed nodes and removing them from the list, so no requests go to them. However, the problem still exists if the node is up but the Web application running on the node goes down. &lt;/p&gt;  &lt;p&gt;Changes to the cluster take time to propagate through the rest of the Internet. One reason is that many large organizations -- ISPs, corporations, agencies -- cache their DNS requests to reduce network traffic and request time. When a user within these organizations makes a DNS request, it's checked against the cache's list of DNS names mapped to IP addresses. If it finds an entry, it returns the IP address to the user. If an entry is not found in its local cache, the ISP sends this DNS request to the DNS server and caches response. &lt;/p&gt;  &lt;p&gt; When a cached entry expires, the ISP updates its local database by contacting other DNS servers. When your list of servers changes, it can take a while for the cached entries on other organizations' networks to expire and look for the updated list of servers. During that period, a client can still attempt to hit the downed server node, if that client's ISP still has an entry pointing to it. In such a case, some users of that ISP couldn't access your site on their first attempt, even if your cluster has redundant servers up and running. &lt;/p&gt;  &lt;p&gt; This is a bigger problem when removing a node than when adding one. When you drop a node, a user may be trying to hit a non-existing server. When you add one, that server may just be under-utilized until its IP address propogates to all the DNS servers. &lt;/p&gt;  &lt;!-- sidebar begins --&gt; &lt;table style="width: 8px; height: 83px;" align="right" border="0" cellpadding="4" cellspacing="8"&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td valign="top" bgcolor="#efefef"&gt; &lt;p class="smalltext"&gt;&lt;br /&gt;&lt;/p&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;!-- sidebar ends --&gt;   &lt;p&gt; Although this method tries to balance the number of users on each server, it doesn't necessarily balance the server load. Some users could demand a higher load of activity during their session than users on another server, and this methodology cannot guard against that inequity. &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h3&gt;Hardware Load Balancers&lt;/h3&gt;  &lt;p&gt;Hardware load balancers solve many of the problems faced by the round robin software solution through virtual IP addresses. The load balancer shows a single (virtual) IP address to the outside world, which maps to the addresses of each machine in the cluster. So, in a way, the load balancer exposes the IP address of the entire cluster to the world.&lt;/p&gt;  &lt;br /&gt;&lt;!--ONJava MPU Ad --&gt;  &lt;div style="float: right; width: 300px; padding-left: 15px;"&gt; &lt;!--JavaScript Tag // Tag for network 5159: TechVertical // Website: OR - ONJava // Page: ONJava // Placement: ros_300x250 (425855) // created at: Oct 9, 2008 5:48:41 PM--&gt; &lt;script language="javascript"&gt;&lt;!-- document.write('&lt;scr'+'ipt language="javascript1.1" src="http://adserver.adtechus.com/addyn/3.0/5159/425855/0/170/ADTECH;loc=100;target=_blank;key=key1+key2+key3+key4;grp=[group];misc='+new Date().getTime()+'"&gt;&lt;/scri'+'pt&gt;'); //--&gt; &lt;/script&gt;&lt;script language="javascript1.1" src="http://adserver.adtechus.com/addyn/3.0/5159/425855/0/170/ADTECH;loc=100;target=_blank;key=key1+key2+key3+key4;grp=%5Bgroup%5D;misc=1249744136372"&gt;&lt;/script&gt;  &lt;script type="text/javascript" src="http://ad.afy11.net/srad.js?azId=5585507"&gt; &lt;/script&gt;&lt;script src="http://ad.afy11.net/ad?asId=5585507&amp;amp;sd=2x300x250&amp;amp;ct=15&amp;amp;enc=1&amp;amp;sf=0&amp;amp;sfd=0&amp;amp;ynw=0&amp;amp;anw=1&amp;amp;rand=22219588&amp;amp;rk1=81348601&amp;amp;rk2=1249744133.215&amp;amp;pt=0"&gt;&lt;/script&gt;&lt;!-- This space is unknown. Contact Adify CS to resolve this issue. --&gt;  &lt;div&gt;  &lt;/div&gt;    &lt;noscript&gt;&lt;/noscript&gt;&lt;/div&gt; &lt;br /&gt; &lt;p&gt;&lt;img src="http://onjava.com/onjava/2001/09/26/graphics/Figure3.gif" alt="Diagram." width="441" border="0" height="315" /&gt;&lt;/p&gt;  &lt;p&gt;When a request comes to the load balancer, it rewrites the request's header to point to other machines in the cluster. If a machine is removed from the cluster, the request doesn't run the risk of hitting a dead server, since all of the machines in the cluster appear to have the same IP address. This address remains the same even if a node in the cluster is down. Moreover, cached DNS entries around the Internet aren't a problem. When a response is returned, the client sees it coming from the hardware load balancer machine. In other words, the client is dealing with a single machine, the hardware load balancer.&lt;/p&gt;  &lt;!-- sidebar begins --&gt;&lt;!-- sidebar ends --&gt;    &lt;h3&gt;Advantages of Hardware Load Balancers&lt;/h3&gt;  &lt;p&gt; &lt;/p&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt; &lt;b&gt;Server affinity.&lt;/b&gt; The hardware load balancer reads the cookies or URL readings on each request made by the client. Based on this information, it can rewrite the header information and send the request to the appropriate node in the cluster, where its session is maintained. &lt;/p&gt; &lt;!-- sidebar begins --&gt; &lt;!-- don't move sidebars --&gt; &lt;!-- sidebar ends --&gt;  &lt;p&gt; Hardware load balancers can provide server affinity in HTTP communication, but not through a secure channel, such as HTTPS. In a secure channel, the messages are SSL-encrypted, and this prevents the load balancer from reading the session information. &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt; &lt;b&gt;High Availability Through Failover.&lt;/b&gt; Failover  happens when one node in a cluster cannot process a  request and redirects it to another.  There are two types of failover: &lt;/p&gt;  &lt;ul type="disc"&gt;&lt;li&gt; &lt;b&gt;Request Level Failover.&lt;/b&gt;  When one node in a cluster cannot process a request (often because it's down), it passes it along to another node.  &lt;/li&gt;&lt;li&gt; &lt;b&gt;Transparent Session Failover.&lt;/b&gt; When an invocation fails, it's transparently routed to another  node in the cluster to complete the execution.  &lt;/li&gt;&lt;/ul&gt;   &lt;p&gt; Hardware load balancers provide request-level failover; when the load balancer detects that a particular node has gone down, it redirects all subsequent requests to that dead node to another active node in the cluster. However, any session information on the dead node will be lost when requests are redirected to a new node.&lt;/p&gt;  &lt;p&gt; Transparent session failover requires execution knowledge for a single process in a node, since the hardware load balancer can only detect network-level problems, not errors. In the execution process of a single node, hardware load balancers do not provide transparent session failover. To achieve transparent session failover, the nodes in the cluster must collaborate among each other and have something like a shared memory area or a common database where all the session data is stored. Therefore, if a node in the cluster has a problem, a session can continue in another node. &lt;/p&gt;  &lt;/li&gt;&lt;li&gt; &lt;b&gt;Metrics.&lt;/b&gt; Since all requests to a Web application  must pass through the load-balancing system, the system can determine  the number of active sessions, the number of active sessions connected in  any instance, response times, peak load times, the number of sessions during  peak load, the number of sessions during minimum load, and more. All this  audit information is used to fine tune the entire system for optimal  performance.  &lt;/li&gt;&lt;/ul&gt;  &lt;h3&gt;Disadvantages of Hardware Load Balancers&lt;/h3&gt;  &lt;p&gt;The drawbacks to the hardware route are the costs, the complexity of setting up, and the vulnerability to a single point of failure. Since all requests pass through a single hardware load balancer, the failure of that piece of hardware sinks the entire site. &lt;/p&gt;   &lt;h3&gt;Load Balancing HTTPS Requests&lt;/h3&gt;  &lt;p&gt; As mentioned above, it's difficult to load balance and maintain session information of requests that come in over HTTPS, as they're encrypted. The hardware load balancer cannot redirect requests based on the information in the header, cookies, or URL readings. There are two options to solve this problem:&lt;/p&gt;  &lt;ul&gt;&lt;li&gt;Web server proxies&lt;/li&gt;&lt;li&gt;Hardware SSL decoders.&lt;/li&gt;&lt;/ul&gt;  &lt;h3&gt;Implementing Web Server Proxies&lt;/h3&gt;  &lt;p&gt;A Web server proxy that sits in front of a cluster of Web servers takes all requests and decrypts them. Then it redirects them to the appropriate node, based on header information in the header, cookies, and URL readings.&lt;/p&gt;  &lt;img src="http://onjava.com/onjava/2001/09/26/graphics/Figure4.gif" alt="Diagram." width="410" border="0" height="324" /&gt;  &lt;p&gt; The advantages of Web server proxies are that they offer a way to get server affinity for SSL-encrypted messages, without any extra hardware. But extensive SSL processing puts an extra load on the proxy. &lt;/p&gt; &lt;p&gt; &lt;b&gt;Apache and Tomcat.&lt;/b&gt; In many serving systems, Apache and Tomcat servers work together to handle all HTTP requests. Apache handles the request for static pages (including HTML, JPEG, and GIF files), while Tomcat handles requests for dynamic pages (JSPs or servlets). Tomcat servers can also handle static pages, but in combined systems, they're usually set up to handle dynamic requests. &lt;/p&gt;  &lt;p&gt;&lt;img src="http://onjava.com/onjava/2001/09/26/graphics/Figure5.gif" alt="Diagram." width="450" border="0" height="104" /&gt;&lt;/p&gt;  &lt;p&gt; You can also configure Apache and Tomcat to handle HTTPS requests and to balance loads. To achieve this, you run multiple instances of Tomcat servers on one or more machines. If all of the Tomcat servers are running on one machine, they should be configured to listen on different ports. To implement load balancing, you create a special type of Tomcat instance, called a Tomcat Worker. &lt;/p&gt;  &lt;img src="http://onjava.com/onjava/2001/09/26/graphics/Figure6.gif" alt="Diagram." width="462" border="0" height="258" /&gt;  &lt;p&gt;As shown in the illustration, the Apache Web server receives HTTP and HTTPS requests from clients. If the request is HTTPS, the Apache Web server decrypts the request and sends it to a Web server adapter, which in turn sends the request to the Tomcat Worker, which contains a load-balancing algorithm. Similar to the Web server proxy, this algorithm balances the load among Tomcat instances. &lt;/p&gt;  &lt;h3&gt;Hardware SSL Decoder&lt;/h3&gt;  &lt;p&gt;Finally, we should mention that there are hardware devices capable of decoding SSL requests. A complete description of them is beyond the scope of this article, but briefly, they sit in front of the hardware load balancer, allowing it to decrypt information in cookies, headers and URLs.&lt;/p&gt;  &lt;img src="http://onjava.com/onjava/2001/09/26/graphics/Figure7.gif" alt="Diagram." width="436" border="0" height="315" /&gt;   &lt;p&gt; These hardware SSL decoders are faster than Web server proxies and are highly scalable. But as with most hardware solutions, they cost more and are complicated to set up and configure.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-7347842387981342660?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/7347842387981342660/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/load-balancing-web-applications.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7347842387981342660'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7347842387981342660'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/load-balancing-web-applications.html' title='Load Balancing Web Applications'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-3680340437695765436</id><published>2009-08-08T01:10:00.000-07:00</published><updated>2009-08-08T01:15:33.657-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>Javascript: Static Methods and Variables</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Static Methods and Attributes&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Applying the lesson of scope and closures from earlier in the chapter can lead to a way to&lt;br /&gt;create static members, which can be both private and publicly accessible. Most methods&lt;br /&gt;and attributes interact with an instance of a class; static members interact with the class&lt;br /&gt;itself. Another way of putting it is to say that static members operate on the class-level instead&lt;br /&gt;of the instance-level; there is only one copy of each static member. As you will see later in&lt;br /&gt;this section, static members are called directly off of the class object.&lt;br /&gt;Here is the Book class with static attributes and methods:&lt;br /&gt;var Book = (function() {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Private static attributes.&lt;br /&gt;var numOfBooks = 0;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Private static method.&lt;br /&gt;function checkIsbn(isbn) {&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Return the constructor.&lt;br /&gt;return function(newIsbn, newTitle, newAuthor) { // implements Publication&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Private attributes.&lt;br /&gt;var isbn, title, author;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Privileged methods.&lt;br /&gt;this.getIsbn = function() {&lt;br /&gt;return isbn;&lt;br /&gt;};&lt;br /&gt;this.setIsbn = function(newIsbn) {&lt;br /&gt;if(!checkIsbn(newIsbn)) throw new Error('Book: Invalid ISBN.');&lt;br /&gt;isbn = newIsbn;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;this.getTitle = function() {&lt;br /&gt;return title;&lt;br /&gt;};&lt;br /&gt;this.setTitle = function(newTitle) {&lt;br /&gt;title = newTitle || 'No title specified';&lt;br /&gt;};&lt;br /&gt;this.getAuthor = function() {&lt;br /&gt;return author;&lt;br /&gt;};&lt;br /&gt;this.setAuthor = function(newAuthor) {&lt;br /&gt;author = newAuthor || 'No author specified';&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Constructor code.&lt;br /&gt;numOfBooks++; // Keep track of how many Books have been instantiated&lt;br /&gt;// with the private static attribute.&lt;br /&gt;if(numOfBooks &gt; 50) throw new Error('Book: Only 50 instances of Book can be '&lt;br /&gt;+ 'created.');&lt;br /&gt;this.setIsbn(newIsbn);&lt;br /&gt;this.setTitle(newTitle);&lt;br /&gt;this.setAuthor(newAuthor);&lt;br /&gt;}&lt;br /&gt;})();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Public static method.&lt;br /&gt;Book.convertToTitleCase = function(inputString) {&lt;br /&gt;...&lt;br /&gt;};&lt;br /&gt;// Public, non-privileged methods.&lt;br /&gt;Book.prototype = {&lt;br /&gt;display: function() {&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;This is similar to the class created earlier in the chapter in the “Private Members Through&lt;br /&gt;Closures” section, with a couple of key differences. Private and privileged members are still&lt;br /&gt;declared within the constructor, using var and this respectively, but the constructor is changed&lt;br /&gt;from a normal function to a nested function that gets returned to the variable Book. This makes&lt;br /&gt;it possible to create a closure where you can declare private static members. The empty parentheses&lt;br /&gt;after the function declaration are extremely important. They serve to execute that&lt;br /&gt;function immediately, as soon as the code is loaded (not when the Book constructor is called).&lt;br /&gt;The result of that execution is another function, which is returned and set to be the Book constructor.&lt;br /&gt;When Book is instantiated, this inner function is what gets called; the outer function is&lt;br /&gt;used only to create a closure, within which you can put private static members.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this example, the checkIsbn method is static because there is no point in creating a new&lt;br /&gt;copy of it for each instance of Book. There is also a static attribute called numOfBooks, which allows&lt;br /&gt;you to keep track of how many times the Book constructor has been called. In this example, we&lt;br /&gt;use that attribute to limit the constructor to creating only 50 instances.&lt;br /&gt;These private static members can be accessed from within the constructor, which means&lt;br /&gt;that any private or privileged function has access to them. They have a distinct advantage over&lt;br /&gt;these other methods in that they are only stored in memory once. Since they are declared outside&lt;br /&gt;of the constructor, they do not have access to any of the private attributes, and as such, are&lt;br /&gt;not privileged; private methods can call private static methods, but not the other way around.&lt;br /&gt;A rule of thumb for deciding whether a private method should be static is to see whether it&lt;br /&gt;needs to access any of the instance data. If it does not need access, making the method static&lt;br /&gt;is more efficient (in terms of memory use) because only a copy is ever created.&lt;br /&gt;Public static members are much easier to create. They are simply created directly off of&lt;br /&gt;the constructor, as with the previous method convertToTitleCase. This means you are essentially&lt;br /&gt;using the constructor as a namespace.&lt;br /&gt;■Note In JavaScript, everything except for variables of the three primitive types is an object (and even&lt;br /&gt;those primitives are automatically wrapped by objects when needed). This means that functions are also&lt;br /&gt;objects. Since objects are essentially hash tables, you can add members at any time. The end result of this is&lt;br /&gt;that functions can have attributes and methods just like any other object, and they can be added whenever&lt;br /&gt;you want.&lt;br /&gt;All public static methods could just as easily be declared as separate functions, but it is useful&lt;br /&gt;to bundle related behaviors together in one place. They are useful for tasks that are related to&lt;br /&gt;the class as a whole and not to any particular instance of it. They don’t directly depend on any of&lt;br /&gt;the data contained within the instances.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Another Example:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;var StaticTest = (function()&lt;br /&gt;{&lt;br /&gt;    var count = 0; //Static Variable&lt;br /&gt;&lt;br /&gt;    return function() { //Constructor&lt;br /&gt;&lt;br /&gt;        this.incrementCount = function() { //public privileged function&lt;br /&gt;            count++;&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;        this.printCount = function() { //public privileged function&lt;br /&gt;            alert("The count is: " + count);&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;})();&lt;br /&gt;&lt;br /&gt;//Static Method&lt;br /&gt;StaticTest.printCount = function() {&lt;br /&gt;&lt;br /&gt;    alert("The count is not accessible in static method [printCount()]");&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;new StaticTest().incrementCount(); //increments count to 1&lt;br /&gt;new StaticTest().printCount(); //prints count as 1&lt;br /&gt;new StaticTest().incrementCount(); //increments count to 2&lt;br /&gt;new StaticTest().printCount(); //prints count as 1&lt;br /&gt;StaticTest.printCount(); //prints The count is not accessible in static method [printCount()]&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Source: &lt;span style="font-style: italic;"&gt;Pro Javascript Design Patterns&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-3680340437695765436?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/3680340437695765436/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/javascript-static-methods-and-variables.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3680340437695765436'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/3680340437695765436'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/javascript-static-methods-and-variables.html' title='Javascript: Static Methods and Variables'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-6562499115315034200</id><published>2009-08-07T23:48:00.000-07:00</published><updated>2009-08-07T23:57:23.557-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>Javascript: Private Members through Closures</title><content type='html'>Back to the problem at hand: you need to create a variable that can only be accessed internally.&lt;br /&gt;A closure seems to be a perfect fit because it allows you to create variables that are accessible&lt;br /&gt;only to certain functions and are preserved in between those function calls. To create private&lt;br /&gt;attributes, you define variables in the scope of your constructor function. These attributes will&lt;br /&gt;be accessible to all functions defined within this scope, including privileged methods:&lt;br /&gt;var Book = function(newIsbn, newTitle, newAuthor) { // implements Publication&lt;br /&gt;// Private attributes.&lt;br /&gt;var isbn, title, author;&lt;br /&gt;// Private method.&lt;br /&gt;function checkIsbn(isbn) {&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;// Privileged methods.&lt;br /&gt;this.getIsbn = function() {&lt;br /&gt;return isbn;&lt;br /&gt;};&lt;br /&gt;this.setIsbn = function(newIsbn) {&lt;br /&gt;if(!checkIsbn(newIsbn)) throw new Error('Book: Invalid ISBN.');&lt;br /&gt;isbn = newIsbn;&lt;br /&gt;};&lt;br /&gt;this.getTitle = function() {&lt;br /&gt;return title;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;this.setTitle = function(newTitle) {&lt;br /&gt;title = newTitle || 'No title specified';&lt;br /&gt;};&lt;br /&gt;this.getAuthor = function() {&lt;br /&gt;return author;&lt;br /&gt;};&lt;br /&gt;this.setAuthor = function(newAuthor) {&lt;br /&gt;author = newAuthor || 'No author specified';&lt;br /&gt;};&lt;br /&gt;// Constructor code.&lt;br /&gt;this.setIsbn(newIsbn);&lt;br /&gt;this.setTitle(newTitle);&lt;br /&gt;this.setAuthor(newAuthor);&lt;br /&gt;};&lt;br /&gt;// Public, non-privileged methods.&lt;br /&gt;Book.prototype = {&lt;br /&gt;display: function() {&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;So how is this different from the other patterns we’ve covered so far? In the other Book&lt;br /&gt;examples, we always created and referred to the attributes using the this keyword. In this&lt;br /&gt;example, we declared these variables using var. That means they will only exist within the Book&lt;br /&gt;constructor. We also declare the checkIsbn function in the same way, making it a private method.&lt;br /&gt;Any method that needs to access these variables and functions need only be declared&lt;br /&gt;within Book. These are called privileged methods because they are public but have access to&lt;br /&gt;private attributes and methods. The this keyword is used in front of these privileged functions&lt;br /&gt;to make them publicly accessible. Because these methods are defined within the Book constructor’s&lt;br /&gt;scope, they can access the private attributes. They are not referred to using this because&lt;br /&gt;they aren’t public. All of the accessor and mutator methods have been changed to refer to the&lt;br /&gt;attributes directly, without this.&lt;br /&gt;Any public method that does not need direct access to private attributes can be declared&lt;br /&gt;normally in the Book.prototype. An example of one of these methods is display; it doesn’t&lt;br /&gt;need direct access to any of the private attributes because it can just call getIsbn or getTitle.&lt;br /&gt;It’s a good idea to make a method privileged only if it needs direct access to the private members.&lt;br /&gt;Having too many privileged methods can cause memory problems because new copies&lt;br /&gt;of all privileged methods are created for each instance.&lt;br /&gt;With this pattern, you can create objects that have true private attributes. It is impossible&lt;br /&gt;for other programmers to create an instance of Book and directly access any of the data. You&lt;br /&gt;can tightly control what gets set because they are forced to go through the mutator methods.&lt;br /&gt;This pattern solves all of the problems with the other patterns, but it introduces a few drawbacks&lt;br /&gt;of its own. In the fully exposed object pattern, all methods are created off of the prototype,&lt;br /&gt;which means there is only one copy of each in memory, no matter how many instances you create.&lt;br /&gt;In this pattern, you create a new copy of every private and privileged method each time a new&lt;br /&gt;&lt;br /&gt;object is instantiated. This has the potential to use more memory than the other patterns, so it&lt;br /&gt;should only be used when you require true private members. This pattern is also hard to subclass.&lt;br /&gt;The new inherited class will not have access to any of the superclass’s private attributes or methods.&lt;br /&gt;It is said that “inheritance breaks encapsulation” because in most languages, the subclass has&lt;br /&gt;access to all of the private attributes and methods of the superclass. In JavaScript, this is not the&lt;br /&gt;case. If you are creating a class that might be subclassed later, it is best to stick to one of the fully&lt;br /&gt;exposed patterns.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Summary&lt;/span&gt;&lt;br /&gt;1) To declare private variables dont use this keyword, instead use var with in the constructor.&lt;br /&gt;2) The same applies to the private functions as well like checkIsbn() function above.&lt;br /&gt;3) To declare public privileged functions [functions which are public and which can access private variables] use this keyword and define it with in the constructor like setIsbn(), getIsbn() api's above.&lt;br /&gt;4) To declare public non-privileged functions [functions which are public and which cannot access private variables] use prototype to define.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Source:&lt;/span&gt; &lt;span style="font-style: italic; font-weight: bold;"&gt;&lt;br /&gt;               Pro Javascript Design Patterns&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-6562499115315034200?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/6562499115315034200/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/javascript-private-members-through.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6562499115315034200'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/6562499115315034200'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/javascript-private-members-through.html' title='Javascript: Private Members through Closures'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-9142573005332021488</id><published>2009-08-07T12:20:00.000-07:00</published><updated>2009-08-07T12:22:04.037-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>Interfaces in Javascript</title><content type='html'>Here is an example of our Interface class and comment combination:&lt;br /&gt;// Interfaces.&lt;br /&gt;var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);&lt;br /&gt;var FormItem = new Interface('FormItem', ['save']);&lt;br /&gt;// CompositeForm class&lt;br /&gt;var CompositeForm = function(id, method, action) { // implements Composite, FormItem&lt;br /&gt;...&lt;br /&gt;};&lt;br /&gt;...&lt;br /&gt;function addForm(formInstance) {&lt;br /&gt;Interface.ensureImplements(formInstance, Composite, FormItem);&lt;br /&gt;// This function will throw an error if a required method is not implemented,&lt;br /&gt;// halting execution of the function.&lt;br /&gt;// All code beneath this line will be executed only if the checks pass.&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;Interface.ensureImplements provides a strict check. If a problem is found, an error will be&lt;br /&gt;thrown, which can either be caught and handled or allowed to halt execution. Either way, the&lt;br /&gt;programmer will know immediately that there is a problem and where to go to fix it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The Interface Class&lt;/span&gt;&lt;br /&gt;The following is the Interface class that we use throughout the book:&lt;br /&gt;// Constructor.&lt;br /&gt;var Interface = function(name, methods) {&lt;br /&gt;if(arguments.length != 2) {&lt;br /&gt;throw new Error("Interface constructor called with " + arguments.length +&lt;br /&gt;"arguments, but expected exactly 2.");&lt;br /&gt;}&lt;br /&gt;this.name = name;&lt;br /&gt;this.methods = [];&lt;br /&gt;for(var i = 0, len = methods.length; i &lt; len; i++) {&lt;br /&gt;if(typeof methods[i] !== 'string') {&lt;br /&gt;throw new Error("Interface constructor expects method names to be "&lt;br /&gt;+ "passed in as a string.");&lt;br /&gt;}&lt;br /&gt;this.methods.push(methods[i]);&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;// Static class method.&lt;br /&gt;Interface.ensureImplements = function(object) {&lt;br /&gt;if(arguments.length &lt; 2) {&lt;br /&gt;throw new Error("Function Interface.ensureImplements called with " +&lt;br /&gt;arguments.length + "arguments, but expected at least 2.");&lt;br /&gt;}&lt;br /&gt;for(var i = 1, len = arguments.length; i &lt; len; i++) {&lt;br /&gt;var interface = arguments[i];&lt;br /&gt;if(interface.constructor !== Interface) {&lt;br /&gt;throw new Error("Function Interface.ensureImplements expects arguments"&lt;br /&gt;+ "two and above to be instances of Interface.");&lt;br /&gt;}&lt;br /&gt;for(var j = 0, methodsLen = interface.methods.length; j &lt; methodsLen; j++) {&lt;br /&gt;var method = interface.methods[j];&lt;br /&gt;if(!object[method] || typeof object[method] !== 'function') {&lt;br /&gt;throw new Error("Function Interface.ensureImplements: object "&lt;br /&gt;+ "does not implement the " + interface.name&lt;br /&gt;+ " interface. Method " + method + " was not found.");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As you can see, it is very strict about the arguments given to each method and will throw&lt;br /&gt;an error if any check doesn’t pass. This is done intentionally, so that if you receive no errors,&lt;br /&gt;you can be certain the interface is correctly declared and implemented.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-9142573005332021488?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/9142573005332021488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/interfaces-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/9142573005332021488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/9142573005332021488'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/interfaces-in-javascript.html' title='Interfaces in Javascript'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-7914581017823673125</id><published>2009-08-07T10:54:00.000-07:00</published><updated>2009-08-07T10:57:49.921-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript and ExtJS'/><title type='text'>The Flexibility of JavaScript</title><content type='html'>&lt;span style="font-weight: bold;"&gt;The Flexibility of JavaScript&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;One of the most powerful features of the language is its flexibility. As a JavaScript programmer,&lt;br /&gt;you can make your programs as simple or as complex as you wish them to be. The language&lt;br /&gt;also allows several different programming styles. You can write your code in the functional style&lt;br /&gt;or in the slightly more complex object-oriented style. It also lets you write relatively complex&lt;br /&gt;programs without knowing anything at all about functional or object-oriented programming;&lt;br /&gt;you can be productive in this language just by writing simple functions. This may be one of the&lt;br /&gt;reasons that some people see JavaScript as a toy, but we see it as a good thing. It allows programmers&lt;br /&gt;to accomplish useful tasks with a very small, easy-to-learn subset of the language. It also&lt;br /&gt;means that JavaScript scales up as you become amore advanced programmer.&lt;br /&gt;JavaScript allows you to emulate patterns and idioms found in other languages. It even&lt;br /&gt;creates a few of its own. It provides all the same object-oriented features as the more traditional&lt;br /&gt;server-side languages.&lt;br /&gt;Let’s take a quick look at a few different ways you can organize code to accomplish one&lt;br /&gt;task: starting and stopping an animation. It’s OK if you don’t understand these examples; all of&lt;br /&gt;the patterns and techniques we use here are explained throughout the book. For now, you can&lt;br /&gt;view this section as a practical example of the different ways a task can be accomplished in&lt;br /&gt;JavaScript.&lt;br /&gt;&lt;br /&gt;If you’re coming from a procedural background, you might just do the following:&lt;br /&gt;/* Start and stop animations using functions. */&lt;br /&gt;function startAnimation() {&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;function stopAnimation() {&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;This approach is very simple, but it doesn’t allow you to create animation objects, which&lt;br /&gt;can store state and have methods that act only on this internal state. This next piece of code&lt;br /&gt;defines a class that lets you create such objects:&lt;br /&gt;/* Anim class. */&lt;br /&gt;var Anim = function() {&lt;br /&gt;...&lt;br /&gt;};&lt;br /&gt;Anim.prototype.start = function() {&lt;br /&gt;...&lt;br /&gt;};&lt;br /&gt;Anim.prototype.stop = function() {&lt;br /&gt;...&lt;br /&gt;};&lt;br /&gt;/* Usage. */&lt;br /&gt;var myAnim = new Anim();&lt;br /&gt;myAnim.start();&lt;br /&gt;...&lt;br /&gt;myAnim.stop();&lt;br /&gt;This defines a new class called Anim and assigns two methods to the class’s prototype&lt;br /&gt;property. We cover this technique in detail in Chapter 3. If you prefer to create classes encapsulated&lt;br /&gt;in one declaration, you might instead write the following:&lt;br /&gt;/* Anim class, with a slightly different syntax for declaring methods. */&lt;br /&gt;var Anim = function() {&lt;br /&gt;...&lt;br /&gt;};&lt;br /&gt;Anim.prototype = {&lt;br /&gt;start: function() {&lt;br /&gt;...&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;stop: function() {&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;This may look a little more familiar to classical object-oriented programmers who are used&lt;br /&gt;to seeing a class declaration with the method declarations nested within it. If you’ve used this&lt;br /&gt;style before, you might want to give this next example a try. Again, don’t worry if there are parts&lt;br /&gt;of the code you don’t understand:&lt;br /&gt;/* Add a method to the Function object that can be used to declare methods. */&lt;br /&gt;Function.prototype.method = function(name, fn) {&lt;br /&gt;this.prototype[name] = fn;&lt;br /&gt;};&lt;br /&gt;/* Anim class, with methods created using a convenience method. */&lt;br /&gt;var Anim = function() {&lt;br /&gt;...&lt;br /&gt;};&lt;br /&gt;Anim.method('start', function() {&lt;br /&gt;...&lt;br /&gt;});&lt;br /&gt;Anim.method('stop', function() {&lt;br /&gt;...&lt;br /&gt;});&lt;br /&gt;Function.prototype.method allows you to add new methods to classes. It takes two arguments.&lt;br /&gt;The first is a string to use as the name of the new method, and the second is a function&lt;br /&gt;that will be added under that name.&lt;br /&gt;You can take this a step further by modifying Function.prototype.method to allow it to be&lt;br /&gt;chained. To do this, you simply return this after creating each method. We devote Chapter 6&lt;br /&gt;to chaining:&lt;br /&gt;/* This version allows the calls to be chained. */&lt;br /&gt;Function.prototype.method = function(name, fn) {&lt;br /&gt;this.prototype[name] = fn;&lt;br /&gt;return this;&lt;br /&gt;};&lt;br /&gt;/* Anim class, with methods created using a convenience method and chaining. */&lt;br /&gt;var Anim = function() {&lt;br /&gt;...&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Anim.&lt;br /&gt;method('start', function() {&lt;br /&gt;...&lt;br /&gt;}).&lt;br /&gt;method('stop', function() {&lt;br /&gt;...&lt;br /&gt;});&lt;br /&gt;You have just seen five different ways to accomplish the same task, each using a slightly&lt;br /&gt;different style. Depending on your background, you may find one more appealing than another.&lt;br /&gt;This is fine; JavaScript allows you to work in the style that is most appropriate for the project at&lt;br /&gt;hand. Each style has different characteristics with respect to code size, efficiency, and performance.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;A Loosely Typed Language&lt;/span&gt;&lt;br /&gt;In JavaScript, you do not declare a type when defining a variable. However, this does not mean&lt;br /&gt;that variables are not typed. Depending on what data it contains, a variable can have one of&lt;br /&gt;several types. There are three primitive types: booleans, numbers, and strings (JavaScript differs&lt;br /&gt;from most other mainstream languages in that it treats integers and floats as the same type).&lt;br /&gt;There are functions, which contain executable code. There are objects, which are composite&lt;br /&gt;datatypes (an array is a specialized object, which contains an ordered collection of values).&lt;br /&gt;Lastly, there are the null and undefined datatypes. Primitive datatypes are passed by value,&lt;br /&gt;while all other datatypes are passed by reference. This can cause some unexpected side effects&lt;br /&gt;if you aren’t aware of it.&lt;br /&gt;As in other loosely typed languages, a variable can change its type, depending on what&lt;br /&gt;value is assigned to it. The primitive datatypes can also be cast from one type to another. The&lt;br /&gt;toString method converts a number or boolean to a string. The parseFloat and parseInt functions&lt;br /&gt;convert strings to numbers. Double negation casts a string or a number to a boolean:&lt;br /&gt;var bool = !!num;&lt;br /&gt;Loosely typed variables provide a great deal of flexibility. Because JavaScript converts type&lt;br /&gt;as needed, for the most part, you won’t have to worry about type errors.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Functions As First-Class Objects&lt;/span&gt;&lt;br /&gt;In JavaScript, functions are first-class objects. They can be stored in variables, passed into other&lt;br /&gt;functions as arguments, passed out of functions as return values, and constructed at run-time.&lt;br /&gt;These features provide a great deal of flexibility and expressiveness when dealing with functions.&lt;br /&gt;As you will see throughout the book, these features are the foundation around which you will&lt;br /&gt;build a classically object-oriented framework.&lt;br /&gt;You can create anonymous functions, which are functions created using the function()&lt;br /&gt;{ ... } syntax. They are not given names, but they can be assigned to variables. Here is an&lt;br /&gt;example of an anonymous function:&lt;br /&gt;&lt;br /&gt;/* An anonymous function, executed immediately. */&lt;br /&gt;(function() {&lt;br /&gt;var foo = 10;&lt;br /&gt;var bar = 2;&lt;br /&gt;alert(foo * bar);&lt;br /&gt;})();&lt;br /&gt;This function is defined and executed without ever being assigned to a variable. The pair&lt;br /&gt;of parentheses at the end of the declaration execute the function immediately. They are empty&lt;br /&gt;here, but that doesn’t have to be the case:&lt;br /&gt;/* An anonymous function with arguments. */&lt;br /&gt;(function(foo, bar) {&lt;br /&gt;alert(foo * bar);&lt;br /&gt;})(10, 2);&lt;br /&gt;This anonymous function is equivalent to the first one. Instead of using var to declare the&lt;br /&gt;inner variables, you can pass them in as arguments. You can also return a value from this function.&lt;br /&gt;This value can be assigned to a variable:&lt;br /&gt;/* An anonymous function that returns a value. */&lt;br /&gt;var baz = (function(foo, bar) {&lt;br /&gt;return foo * bar;&lt;br /&gt;})(10, 2);&lt;br /&gt;// baz will equal 20.&lt;br /&gt;The most interesting use of the anonymous function is to create a closure. A closure is&lt;br /&gt;a protected variable space, created by using nested functions. JavaScript has function-level scope.&lt;br /&gt;This means that a variable defined within a function is not accessible outside of it. JavaScript is&lt;br /&gt;also lexically scoped, which means that functions run in the scope they are defined in, not the&lt;br /&gt;scope they are executed in. These two facts can be combined to allow you to protect variables by&lt;br /&gt;wrapping them in an anonymous function. You can use this to create private variables for classes:&lt;br /&gt;/* An anonymous function used as a closure. */&lt;br /&gt;var baz;&lt;br /&gt;(function() {&lt;br /&gt;var foo = 10;&lt;br /&gt;var bar = 2;&lt;br /&gt;baz = function() {&lt;br /&gt;return foo * bar;&lt;br /&gt;};&lt;br /&gt;})();&lt;br /&gt;&lt;br /&gt;baz(); // baz can access foo and bar, even though it is executed outside of the&lt;br /&gt;// anonymous function.&lt;br /&gt;The variables foo and bar are defined only within the anonymous function. Because the&lt;br /&gt;function baz was defined within that closure, it will have access to those two variables, even&lt;br /&gt;after the closure has finished executing. This is a complex topic, and one that we touch upon&lt;br /&gt;throughout the book. We explain this technique in much greater detail in Chapter 3, when we&lt;br /&gt;discuss encapsulation.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The Mutability of Objects&lt;/span&gt;&lt;br /&gt;In JavaScript, everything is an object (except for the three primitive datatypes, and even they&lt;br /&gt;are automatically wrapped with objects when needed). Furthermore, all objects are mutable.&lt;br /&gt;These two facts mean you can use some techniques that wouldn’t be allowed in most other&lt;br /&gt;languages, such as giving attributes to functions:&lt;br /&gt;function displayError(message) {&lt;br /&gt;displayError.numTimesExecuted++;&lt;br /&gt;alert(message);&lt;br /&gt;};&lt;br /&gt;displayError.numTimesExecuted = 0;&lt;br /&gt;It also means you can modify classes after they have been defined and objects after they&lt;br /&gt;have been instantiated:&lt;br /&gt;/* Class Person. */&lt;br /&gt;function Person(name, age) {&lt;br /&gt;this.name = name;&lt;br /&gt;this.age = age;&lt;br /&gt;}&lt;br /&gt;Person.prototype = {&lt;br /&gt;getName: function() {&lt;br /&gt;return this.name;&lt;br /&gt;},&lt;br /&gt;getAge: function() {&lt;br /&gt;return this.age;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/* Instantiate the class. */&lt;br /&gt;var alice = new Person('Alice', 93);&lt;br /&gt;var bill = new Person('Bill', 30);&lt;br /&gt;/* Modify the class. */&lt;br /&gt;&lt;br /&gt;Person.prototype.getGreeting = function() {&lt;br /&gt;return 'Hi ' + this.getName() + '!';&lt;br /&gt;};&lt;br /&gt;/* Modify a specific instance. */&lt;br /&gt;alice.displayGreeting = function() {&lt;br /&gt;alert(this.getGreeting());&lt;br /&gt;}&lt;br /&gt;In this example, the getGreeting method is added to the class after the two instances are&lt;br /&gt;created, but these two instances still get the method, due to the way the prototype object works.&lt;br /&gt;alice also gets the displayGreeting method, but no other instance does.&lt;br /&gt;Related to object mutability is the concept of introspection. You can examine any object at&lt;br /&gt;run-time to see what attributes and methods it contains. You can also use this information to&lt;br /&gt;instantiate classes and execute methods dynamically, without knowing their names at development&lt;br /&gt;time (this is known as reflection). These are important techniques for dynamic scripting&lt;br /&gt;and are features that static languages (such as C++) lack.&lt;br /&gt;Most of the techniques that we use in this book to emulate traditional object-oriented&lt;br /&gt;features rely on object mutability and reflection. It may be strange to see this if you are used to&lt;br /&gt;languages like C++ or Java, where an object can’t be extended once it is instantiated and classes&lt;br /&gt;can’t be modified after they are declared. In JavaScript, everything can be modified at run-time.&lt;br /&gt;This is an enormously powerful tool and allows you to do things that are not possible in those&lt;br /&gt;other languages. It does have a downside, though. It isn’t possible to define a class with a particular&lt;br /&gt;set of methods and be sure that those methods are still intact later on. This is part of&lt;br /&gt;the reason why type checking is done so rarely in JavaScript. We cover this in Chapter 2 when&lt;br /&gt;we talk about duck typing and interface checking.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Inheritance&lt;/span&gt;&lt;br /&gt;Inheritance is not as straightforward in JavaScript as in other object-oriented languages. JavaScript&lt;br /&gt;uses object-based (prototypal) inheritance; this can be used to emulate class-based (classical)&lt;br /&gt;inheritance. You can use either style in your code, and we cover both styles in this book. Often&lt;br /&gt;one of the two will better suit the particular task at hand. Each style also has different performance&lt;br /&gt;characteristics, which can be an important factor in deciding which to use.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-7914581017823673125?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/7914581017823673125/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/flexibility-of-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7914581017823673125'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/7914581017823673125'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/flexibility-of-javascript.html' title='The Flexibility of JavaScript'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-8393462566041497004</id><published>2009-08-07T10:14:00.000-07:00</published><updated>2009-08-07T10:15:14.112-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Web'/><title type='text'>CSS Shorthand - 2</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_-1g-DDEOJz0/SnxhCi0AfuI/AAAAAAAAAew/G1v2_eM18og/s1600-h/CSSSH.JPG"&gt;&lt;img style="cursor: pointer; width: 400px; height: 250px;" src="http://1.bp.blogspot.com/_-1g-DDEOJz0/SnxhCi0AfuI/AAAAAAAAAew/G1v2_eM18og/s400/CSSSH.JPG" alt="" id="BLOGGER_PHOTO_ID_5367271552251363042" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-8393462566041497004?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/8393462566041497004/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/css-shorthand-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8393462566041497004'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/8393462566041497004'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/css-shorthand-2.html' title='CSS Shorthand - 2'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_-1g-DDEOJz0/SnxhCi0AfuI/AAAAAAAAAew/G1v2_eM18og/s72-c/CSSSH.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-9080148768496330752</id><published>2009-08-07T10:09:00.000-07:00</published><updated>2009-08-07T10:11:10.342-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Web'/><title type='text'>CSS Shorthand - 1</title><content type='html'>&lt;p&gt;Ok. Let’s set the record straight. There is no official guide for each and every CSS shorthand property value. So let’s work together and put one together shall we? Ok. Straight to the business. Anytime I’ve ran into a specification (besides the confusing mess at the W3C), it turns into showing off a couple of examples and you’re supposed to be set on your way. Well well. Over the years, I’ve found quite some interesting unknown quirky facts about these shorthands… hence this Guide was born.&lt;/p&gt;&lt;h3&gt;Background&lt;/h3&gt;&lt;p&gt;Backgrounds can be tricky. Nevertheless, effective when condensed correctly. The syntax for declaring the background shorthand values are as follows:&lt;/p&gt;&lt;h3 class="code"&gt;background properties&lt;/h3&gt;&lt;pre animheight="140"&gt;&lt;code&gt;element {&lt;br /&gt; background-color: color || #hex || (rgb / % || 0-255);&lt;br /&gt; background-image:url(URI);&lt;br /&gt; background-repeat: repeat || repeat-x || repeat-y || no-repeat;&lt;br /&gt; background-position: X Y || (top||bottom||center) (left||right||center);&lt;br /&gt; background-attachment: scroll || fixed;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;span id="more-123"&gt;&lt;/span&gt;&lt;br /&gt;Believe it or not, all these properties can be combined into one single &lt;strong&gt;background&lt;/strong&gt; property as follows:&lt;/p&gt;&lt;h3 class="code"&gt;the background shorthand property&lt;/h3&gt;&lt;pre animheight="156"&gt;&lt;code&gt;element {&lt;br /&gt; background:&lt;br /&gt;   #fff&lt;br /&gt;   url(image.png)&lt;br /&gt;   no-repeat&lt;br /&gt;   20px 100px&lt;br /&gt;   fixed;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;The Unknown&lt;/h3&gt;&lt;p&gt;Often times developers find themselves wondering &lt;q&gt;What if I leave out this value or that one? How will that effect the design?&lt;/q&gt;. Good questions. &lt;/p&gt;&lt;p&gt;By default, the background property will assume the following when you &lt;strong&gt;do not&lt;/strong&gt; declare each value of the properties.&lt;/p&gt;&lt;h3 class="code"&gt;default background property values&lt;/h3&gt;&lt;pre animheight="156"&gt;&lt;code&gt;&lt;br /&gt;element {&lt;br /&gt; background-color: transparent;&lt;br /&gt; background-image: none;&lt;br /&gt; background-repeat: repeat;&lt;br /&gt; background-position: top left;&lt;br /&gt; background-attachment: scroll;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Lesson learned: &lt;/strong&gt;&lt;em&gt;be careful on what you don’t declare. By chosing to not declare a value on a shorthand property, you are explicitly declaring the above default settings.&lt;/em&gt; For example, let’s look at the following example.&lt;/p&gt;&lt;h3 class="code"&gt;background shorthand example (unexplicit)&lt;/h3&gt;&lt;pre animheight="76"&gt;&lt;code&gt;element {&lt;br /&gt; background:red url(image.png);&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This would be the same as declaring the following values:&lt;/p&gt;&lt;h3 class="code"&gt;background shorthand example (explicit)&lt;/h3&gt;&lt;pre animheight="76"&gt;&lt;code&gt;element {&lt;br /&gt; background:red url(image.png) repeat top left scroll;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Font&lt;/h3&gt;&lt;p&gt;Font is perhaps the trickiest. However, it follows the same rules as the background shorthand property. &lt;em&gt;All that you do not declare will have unexplicit values&lt;/em&gt;. Here is the font shorthand specification:&lt;/p&gt;&lt;h3 class="code"&gt;font properties&lt;/h3&gt;&lt;pre animheight="156"&gt;&lt;code&gt;element {&lt;br /&gt; font-style: normal || italic || oblique;&lt;br /&gt; font-variant:normal || small-caps;&lt;br /&gt; font-weight: normal || bold || bolder || || lighter || (100-900);&lt;br /&gt; font-size: (number+unit) || (xx-small - xx-large);&lt;br /&gt; line-height: normal || (number+unit);&lt;br /&gt; font-family:name,"more names";&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The default values for the font shorthand property are as follows:&lt;/p&gt;&lt;h3 class="code"&gt;default font property values&lt;/h3&gt;&lt;pre animheight="156"&gt;&lt;code&gt;element {&lt;br /&gt; font-style: normal;&lt;br /&gt; font-variant:normal;&lt;br /&gt; font-weight: normal;&lt;br /&gt; font-size: inherit;&lt;br /&gt; line-height: normal;&lt;br /&gt; font-family:inherit;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And of course without any further ado. The font shorthand property syntax:&lt;/p&gt;&lt;h3 class="code"&gt;the font shorthand property&lt;/h3&gt;&lt;pre animheight="172"&gt;&lt;code&gt;element {&lt;br /&gt; font:&lt;br /&gt;   normal&lt;br /&gt;   normal&lt;br /&gt;   normal&lt;br /&gt;   inhert/&lt;br /&gt;   normal&lt;br /&gt;   inherit;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here is where it gets tricky. The fact that &lt;code&gt;font-style, font-variant,&lt;/code&gt; and &lt;code&gt;font-weight&lt;/code&gt; all come “normal” out of the box, you may need to pay a little more close attention when you’re styling elements that come with default browser styles like &amp;lt;h1&gt; - &amp;lt;h6&gt; or &amp;lt;strong&gt; and &amp;lt;em&gt;. For example, styling the strong element:&lt;/p&gt;&lt;h3 class="code"&gt;strong element styled with font&lt;/h3&gt;&lt;pre animheight="76"&gt;&lt;code&gt;strong {&lt;br /&gt; font:12px verdana;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;By writing the above into your style sheet, you will be unexplicitly removing the &lt;strong&gt;font-weight:bold&lt;/strong&gt; default browser style that is applied to strong elements.&lt;/p&gt;&lt;p&gt;Last but not least (for -font- that is), a real world example:&lt;/p&gt;&lt;h3 class="code"&gt;font shorthand property example (unexplicit)&lt;/h3&gt;&lt;pre animheight="76"&gt;&lt;code&gt;p {&lt;br /&gt; font:bold 1em/1.2em georgia,"times new roman",serif;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This would be the same as declaring the following properties:&lt;/p&gt;&lt;h3 class="code"&gt;the font shorthand property (explicit)&lt;/h3&gt;&lt;pre animheight="156"&gt;&lt;code&gt;p {&lt;br /&gt; font-style:normal;&lt;br /&gt; font-variant:normal;&lt;br /&gt; font-weight:bold;&lt;br /&gt; font-size:1em;&lt;br /&gt; line-height:1.2em;&lt;br /&gt; font-family:georgia,"times new roman",serif;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="borderProperty"&gt;Border&lt;/h3&gt;&lt;p&gt;Let’s not waste time discussing the warnings. The same rules apply from here on out. This is all you need to know&lt;/p&gt;&lt;h3 class="code"&gt;border properties&lt;/h3&gt;&lt;pre animheight="108"&gt;&lt;code&gt;element {&lt;br /&gt; border-width: number+unit;&lt;br /&gt; border-style: (numerous);&lt;br /&gt; border-color: color || #hex || (rgb / % || 0-255);&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;becomes this:&lt;/p&gt;&lt;h3 class="code"&gt;the border shorthand propertie&lt;/h3&gt;&lt;pre animheight="124"&gt;&lt;code&gt;element {&lt;br /&gt; border:&lt;br /&gt;   4px&lt;br /&gt;   groove&lt;br /&gt;   linen&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Don’t ask me how that would look. The fact that “linen” is in there, things could get scary. Nevermind the matter, here is where ‘border’ gets funny.&lt;/p&gt;&lt;h3 class="code"&gt;border examples&lt;/h3&gt;&lt;pre animheight="380"&gt;&lt;code&gt;p {&lt;br /&gt; border:solid blue;&lt;br /&gt;}&lt;br /&gt;/* will create a '3px' solid blue border...&lt;br /&gt;  who knows where 3px came from?? */&lt;br /&gt;&lt;br /&gt;p {&lt;br /&gt; border:5px solid;&lt;br /&gt;}&lt;br /&gt;/* will create 5px solid 'black' border...&lt;br /&gt;   default must be black?? */&lt;br /&gt;&lt;br /&gt;p {&lt;br /&gt; border:dashed;&lt;br /&gt;}&lt;br /&gt;/* will create a '3px' dashed 'black' border...&lt;br /&gt;   3px black lines unite! */&lt;br /&gt;&lt;br /&gt;p { border:10px red; }&lt;br /&gt;p { border:10px; }&lt;br /&gt;p { border:red; }&lt;br /&gt;/* these just don't even work */&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;One thing to specially take note about declaring a border without a color, the default will be ‘black’ unless otherwise noted through an explicit or inherited ‘color’ property. See the following examples:&lt;/p&gt;&lt;h3 class="code"&gt;border color examples&lt;/h3&gt;&lt;pre animheight="268"&gt;&lt;code&gt;&lt;br /&gt;p {&lt;br /&gt; border:dotted;&lt;br /&gt; color:red;&lt;br /&gt;}&lt;br /&gt;/* will create a 3px dotted red border */&lt;br /&gt;/* ----------------------------- */&lt;br /&gt;body {&lt;br /&gt; color:blue;&lt;br /&gt;}&lt;br /&gt;body p {&lt;br /&gt; border:5px solid;&lt;br /&gt;}&lt;br /&gt;/* will create a 5px solid blue border */&lt;br /&gt;/* ----------------------------- */&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Get it? Got it. Good! (isn’t that a song?) Anyway. On with this&lt;/p&gt;&lt;h3&gt;Margin and Padding&lt;/h3&gt;&lt;p&gt;These are by far the easiest. Just think about the hands of a clock starting at noon, and follow the hour. For the sake of brevity, we’ll be working with margin (since it’s a shorter word). So for all cases of margin, the same rules apply to padding.&lt;/p&gt;&lt;h3 class="code"&gt;margin properties.&lt;/h3&gt;&lt;pre animheight="124"&gt;&lt;code&gt;element {&lt;br /&gt; margin-top: number+unit;&lt;br /&gt; margin-right: number+unit;&lt;br /&gt; margin-bottom: number+unit;&lt;br /&gt; margin-left: number+unit;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;… combined into the margin superpowers:&lt;/p&gt;&lt;h3 class="code"&gt;the margin shorthand property&lt;/h3&gt;&lt;pre animheight="92"&gt;&lt;code&gt;/* top right bottom left */&lt;br /&gt;element {&lt;br /&gt; margin: auto auto auto auto;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Of course, you may declare your margin with one, two, three, or four values. Here is how each scenario will be played out:&lt;/p&gt;&lt;h3 class="code"&gt;margin fun&lt;/h3&gt;&lt;pre animheight="300"&gt;&lt;code&gt;/* adds a 10px margin to all four sides */&lt;br /&gt;element {&lt;br /&gt; margin:10px;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* adds a 20px margin to top and bottom&lt;br /&gt;   and a 5px margin to left and right */&lt;br /&gt;element {&lt;br /&gt; margin:20px 5px;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* adds a 50px margin to top&lt;br /&gt;   and a 10px margin to left and right&lt;br /&gt;   and a 300px margin to bottom */&lt;br /&gt;element {&lt;br /&gt; margin:50px 10px 300px;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Understood? Let’s keep going. This is fun isn’t it! (whatever, you like it).&lt;/p&gt;&lt;h3&gt;Outline&lt;/h3&gt;&lt;p&gt;Quite frankly, this property has dropped off the existence of the design radar. Mainly because of lack of browsers supporting this CSS 2.1 standard (yep, it’s an actual property), but nonetheless, it too has a shorthand property. This property follows the exact same (or same exact - they mean the same thing) specification as the ‘border’ shorthand property. But, for purposes of this being a Guide, it must be here. So:&lt;/p&gt;&lt;h3 class="code"&gt;outline properties&lt;/h3&gt;&lt;pre animheight="108"&gt;&lt;code&gt;element {&lt;br /&gt; outline-width: number+unit;&lt;br /&gt; outline-style: (numerous);&lt;br /&gt; outline-color: color || #hex || (rgb / % || 0-255);&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Outline written as shorthand:&lt;/p&gt;&lt;h3 class="code"&gt;outline shorthand property&lt;/h3&gt;&lt;pre animheight="76"&gt;&lt;code&gt;element {&lt;br /&gt; outline:3px dotted gray;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For purposes of trying to keep things from repeating, please see the &lt;a href="http://www.dustindiaz.com/css-shorthand/#borderProperty" title="Jump to Border Properties on this page"&gt;border shorthand section&lt;/a&gt; on this document to understand the odds, ends, and quirks of the outline property.&lt;/p&gt;&lt;h3&gt;List-style&lt;/h3&gt;&lt;p&gt;This is it. The last one. It’s rarely used frequently. Hence rarely. That is why I kept it until the end (sorry, the best was first in my own opinion). Here is the list-style properties:&lt;/p&gt;&lt;h3 class="code"&gt;list-style properties&lt;/h3&gt;&lt;pre animheight="108"&gt;&lt;code&gt;element {&lt;br /&gt; list-style-type: (numerous);&lt;br /&gt; list-style-position:inside || outside;&lt;br /&gt; list-style-image:url(image.png);&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here is the defaults:&lt;/p&gt;&lt;h3 class="code"&gt;list-style property defaults&lt;/h3&gt;&lt;pre animheight="108"&gt;&lt;code&gt;element {&lt;br /&gt; list-style-type:disc;&lt;br /&gt; list-style-position:outside;&lt;br /&gt; list-style-image:none;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And for the sake of final brevity. Here is a simple example:&lt;/p&gt;&lt;h3 class="code"&gt;list-style shorthand property example&lt;/h3&gt;&lt;pre animheight="108"&gt;&lt;code&gt;ul li {&lt;br /&gt; list-style:square inside url(image.png);&lt;br /&gt;}&lt;br /&gt;/* in this particular case if image.png is not available&lt;br /&gt;   then a square will be provided as secondary */&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;That’s it!&lt;/h3&gt;&lt;p&gt;I hope this provides years and years of referencing for all your CSS shorthand needs. When CSS3 is finally outside its working draft, expect to see this guide updated as necessary.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5724163953511764578-9080148768496330752?l=mybreadbasket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mybreadbasket.blogspot.com/feeds/9080148768496330752/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/css-shorthand-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/9080148768496330752'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5724163953511764578/posts/default/9080148768496330752'/><link rel='alternate' type='text/html' href='http://mybreadbasket.blogspot.com/2009/08/css-shorthand-1.html' title='CSS Shorthand - 1'/><author><name>Vinuth</name><uri>http://www.blogger.com/profile/00768746253943108816</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5724163953511764578.post-470456753867544369</id><published>2009-08-07T08:15:00.000-07:00</published><updated>2009-08-07T08:27:21.091-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Web'/><title type='text'>CSS: Block Box, Line Box and Inline Box</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Block-Level Elements&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The short definition is that block-level elements are elements that create blocks or large groupings of text.&lt;br /&gt;&lt;br /&gt;block-level elements have some specific distinctions from inline elements:&lt;br /&gt;&lt;br /&gt;    * block-level elements generally can contain text, data, inline elements, or other block-level elements.&lt;br /&gt;    * block-level elements generally begin new lines of text.&lt;br /&gt;    * block-level elements inherit directionality information differently from inline elements.&lt;br /&gt;&lt;br /&gt;Examples:&lt;br /&gt;&lt;br /&gt;    * &amp;lt;p&gt;&amp;lt;/p&gt;&lt;br /&gt;    * &amp;lt;blockquote&gt;&amp;lt;/blockquote&gt;&lt;br /&gt;    * &amp;lt;table&gt;&amp;lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Inline Elements&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The short definition is that inline elements are elements that are found in the text of the HTML document. They are also sometimes called text level elements.&lt;br /&gt;&lt;br /&gt;Inline elements have some specific distinctions from block-level elements:&lt;br /&gt;&lt;br /&gt;    * Inline elements generally only contain text, data or other inline elements. They are usually "smaller" than block-level elements.&lt;br /&gt;    * Inline elements do not generally begin new lines of text.&lt;br /&gt;    * Inline elements inherit directionality information differently from block-level elements.&lt;br /&gt;&lt;br /&gt;Also Known As: text level elements&lt;br /&gt;Examples:&lt;br /&gt;&lt;br /&gt;    * &amp;lt;span&gt;&amp;lt;/span&gt;&lt;br /&gt;    * &amp;lt;strong&gt
