tag:blogger.com,1999:blog-3163432433672618002024-02-08T09:29:12.946+05:30The Geek's BlogNotes of a geek on Java and related technologies. Unknownnoreply@blogger.comBlogger40125tag:blogger.com,1999:blog-316343243367261800.post-25069943277607156282023-03-27T19:13:00.002+05:302023-03-27T19:13:42.376+05:30Adobe Firefly<p>Adobe Firefly is designed to help developers create custom applications more quickly and easily. The platform is built on top of Adobe's existing Experience Cloud, which provides a range of tools for managing customer data and creating personalized experiences.</p><p>One of the key features of Adobe Firefly is its ability to integrate with a wide range of other technologies and services. This means that developers can use the platform to build applications that connect with everything from social media platforms to IoT devices.</p><p>Another important aspect of Adobe Firefly is its focus on speed and efficiency. The platform includes a range of pre-built components and templates that developers can use to quickly create new applications. It also includes a range of tools for testing and debugging applications, which can help to speed up the development process.</p><p>Overall, Adobe Firefly looks like an exciting new platform for developers who are looking to create custom applications quickly and efficiently. While it's still early days for the platform, it's clear that Adobe has put a lot of thought and effort into making it as user-friendly as possible. If you're a developer looking to build custom applications, it's definitely worth checking out.</p>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-78612175556203610652018-08-21T22:06:00.000+05:302018-08-21T22:11:02.170+05:30Create AEM Project using Maven Archetype 11<div dir="ltr" style="text-align: left;" trbidi="on">
Let's start with our journey of Adobe Experience Manager project development.<br />
<br />
I am using <b>AEM 6.3, Java 1.8 and Maven 3.2</b>.<br />
<br />
Follow below steps and try to replicate the same at your end. Let know of any issues if you face in the comment section.<br />
<br />
<br />
<ol style="text-align: left;">
<li>Go to your project path and validate the commands mvn -v and java -version are pointing to the accurate installation of Maven and JDK.</li>
<li>Type below command to start: <i>"mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate -DarchetypeGroupId=com.adobe.granite.archetypes -DarchetypeArtifactId=aem-project-archetype -DarchetypeVersion=11 -DarchetypeCatalog=https://repo.adobe.com/nexus/content/groups/public/"</i></li>
<li>Once this is done downloading the dependency and starts creating folder structure, it will ask you for the required information. You can refer below information that I have given, you need to use yours based on your requirement or client need. </li>
</ol>
<blockquote class="tr_bq" style="text-align: left;">
<i>Define value for property 'groupId': : first-project-aem<br />Define value for property 'artifactId': : </i><i>first-project</i><i>-aem<br />Define value for property 'version': 1.0-SNAPSHOT: :<br />Define value for property 'package': </i><i>first-project</i><i>-aem: : com.blogspot.curiousjava<br />Define value for property 'appsFolderName': </i><i>first-project</i><i>-aem: :<br />Define value for property 'artifactName': </i><i>first-project</i><i>-aem: :<br />Define value for property 'componentGroupName': </i><i>first-project</i><i>-aem: : Set 1<br />Define value for property 'contentFolderName': </i><i>first-project</i><i>-aem: : </i><i>first-project</i><i><br />Define value for property 'cssId': </i><i>first-project</i><i>-aem: : </i><i>first-project</i><i><br />Define value for property 'packageGroup': </i><i>first-project</i><i>-aem: : </i><i>first-project</i><i><br />Define value for property 'siteName': </i><i>first-project</i><i>-aem: : curious-java-aem</i></blockquote>
Once you are done with above, it will ask you for confirmation. Press "Y" and you are ready to start your journey to AEM-6.3. You can import the project in your IDE and start working towards your project.<br />
<br />
Best of luck with your start to AEM learning, hope this helps. </div>
Utkarsh Thakkarhttp://www.blogger.com/profile/06779698818895958833noreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-68002946892797834072012-09-11T15:09:00.003+05:302021-02-24T06:07:03.633+05:30Primitive Obsession with Example<div dir="ltr" style="text-align: left;" trbidi="on">
<span xmlns=""></span><br />
<span xmlns="">Primitive Obsession is the name of a code smell that occurs when we use primitive data types to represent domain ideas. For example, we use a string to represent a message or an integer to represent an amount of money.</span><br />
<span xmlns="">For Example: Code with Primitive Obsession</span><br /><br /></div>
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #888888">// primitiveObsession.java</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">primitiveObsession</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">main</span><span style="color: #333333">(</span>String args<span style="color: #333333">[])</span> <span style="color: #333333">{</span>
Integer<span style="color: #333333">[]</span> cityPopulations <span style="color: #333333">=</span> <span style="color: #333333">{</span> <span style="color: #0000DD; font-weight: bold">13000000</span><span style="color: #333333">,</span> <span style="color: #888888">// London</span>
<span style="color: #0000DD; font-weight: bold">21903623</span><span style="color: #333333">,</span> <span style="color: #888888">// New York</span>
<span style="color: #0000DD; font-weight: bold">12570000</span><span style="color: #333333">,</span> <span style="color: #888888">// Tokyo</span>
<span style="color: #0000DD; font-weight: bold">1932763</span><span style="color: #333333">,</span> <span style="color: #888888">// Stockholm</span>
<span style="color: #0000DD; font-weight: bold">1605602</span><span style="color: #333333">,</span> <span style="color: #888888">// Barcelona</span>
<span style="color: #0000DD; font-weight: bold">4119190</span> <span style="color: #888888">// Sydney</span>
<span style="color: #333333">};</span>
<span style="color: #008800; font-weight: bold">for</span> <span style="color: #333333">(</span>Integer cityPopulation <span style="color: #333333">:</span> cityPopulations<span style="color: #333333">)</span> <span style="color: #333333">{</span>
System<span style="color: #333333">.</span><span style="color: #0000CC">out</span><span style="color: #333333">.</span><span style="color: #0000CC">println</span><span style="color: #333333">(</span>cityPopulation<span style="color: #333333">);</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">City</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">final</span> String name<span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">final</span> <span style="color: #333399; font-weight: bold">int</span> population<span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">private</span> <span style="color: #008800; font-weight: bold">final</span> Continent continent<span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">public</span> String <span style="color: #0066BB; font-weight: bold">getName</span><span style="color: #333333">()</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">return</span> name<span style="color: #333333">;</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">getPopulation</span><span style="color: #333333">()</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">return</span> population<span style="color: #333333">;</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> Continent <span style="color: #0066BB; font-weight: bold">getContinent</span><span style="color: #333333">()</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">return</span> continent<span style="color: #333333">;</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #0066BB; font-weight: bold">City</span><span style="color: #333333">(</span>String name<span style="color: #333333">,</span> <span style="color: #333399; font-weight: bold">int</span> population<span style="color: #333333">,</span> Continent continent<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">this</span><span style="color: #333333">.</span><span style="color: #0000CC">name</span> <span style="color: #333333">=</span> name<span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">this</span><span style="color: #333333">.</span><span style="color: #0000CC">population</span> <span style="color: #333333">=</span> population<span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">this</span><span style="color: #333333">.</span><span style="color: #0000CC">continent</span> <span style="color: #333333">=</span> continent<span style="color: #333333">;</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> String <span style="color: #0066BB; font-weight: bold">toString</span><span style="color: #333333">()</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">return</span> String<span style="color: #333333">.</span><span style="color: #0000CC">format</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"%s has a popluation of %s and is located in %s"</span><span style="color: #333333">,</span>
name<span style="color: #333333">,</span> population<span style="color: #333333">,</span> continent<span style="color: #333333">);</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #008800; font-weight: bold">final</span> City<span style="color: #333333">[]</span> ALL_CITIES <span style="color: #333333">=</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">new</span> <span style="color: #0066BB; font-weight: bold">City</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"London"</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">13000000</span><span style="color: #333333">,</span> Continent<span style="color: #333333">.</span><span style="color: #0000CC">EUROPE</span><span style="color: #333333">),</span>
<span style="color: #008800; font-weight: bold">new</span> <span style="color: #0066BB; font-weight: bold">City</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"New York"</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">21903623</span><span style="color: #333333">,</span> Continent<span style="color: #333333">.</span><span style="color: #0000CC">AMERICA</span><span style="color: #333333">),</span>
<span style="color: #008800; font-weight: bold">new</span> <span style="color: #0066BB; font-weight: bold">City</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"Tokyo"</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">12570000</span><span style="color: #333333">,</span> Continent<span style="color: #333333">.</span><span style="color: #0000CC">ASIA</span><span style="color: #333333">),</span>
<span style="color: #008800; font-weight: bold">new</span> <span style="color: #0066BB; font-weight: bold">City</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"Stockholm"</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">1932763</span><span style="color: #333333">,</span> Continent<span style="color: #333333">.</span><span style="color: #0000CC">EUROPE</span><span style="color: #333333">),</span>
<span style="color: #008800; font-weight: bold">new</span> <span style="color: #0066BB; font-weight: bold">City</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"Barcelona"</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">1605602</span><span style="color: #333333">,</span> Continent<span style="color: #333333">.</span><span style="color: #0000CC">EUROPE</span><span style="color: #333333">),</span>
<span style="color: #008800; font-weight: bold">new</span> <span style="color: #0066BB; font-weight: bold">City</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"Sydney"</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">4119190</span><span style="color: #333333">,</span> Continent<span style="color: #333333">.</span><span style="color: #0000CC">AUSTRALIA</span><span style="color: #333333">)</span>
<span style="color: #333333">};</span>
<span style="color: #333333">}</span>
<span style="color: #888888">//Continent.java</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">enum</span> Continent <span style="color: #333333">{</span>
AMERICA<span style="color: #333333">,</span>
EUROPE<span style="color: #333333">,</span>
AFRICA<span style="color: #333333">,</span>
ASIA<span style="color: #333333">,</span>
AUSTRALIA
<span style="color: #333333">}</span>
<span style="color: #888888">// withOutPrimitiveObsession.java</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">withOutPrimitiveObsession</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">main</span><span style="color: #333333">(</span>String args<span style="color: #333333">[])</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">for</span> <span style="color: #333333">(</span>City city <span style="color: #333333">:</span> City<span style="color: #333333">.</span><span style="color: #0000CC">ALL_CITIES</span><span style="color: #333333">)</span> <span style="color: #333333">{</span>
System<span style="color: #333333">.</span><span style="color: #0000CC">out</span><span style="color: #333333">.</span><span style="color: #0000CC">println</span><span style="color: #333333">(</span>city<span style="color: #333333">.</span><span style="color: #0000CC">toString</span><span style="color: #333333">());</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
</pre></div>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-24036603326195711882012-03-06T11:37:00.002+05:302022-07-01T20:43:51.582+05:30Factory Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<span xmlns=""></span><br />
<ul>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">This pattern is used when it must be decided at run time which one of several compatible classes is to be instantiated.</span></span></li>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">For example, the abstract <strong>Collator </strong>class's <strong>getInstance() </strong>method returns a collation object that is appropriate for the default locale, as determined by <strong>java.util.Locale.getDefault()</strong>:</span></span></li>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">Like other locale-sensitive classes, you can use the static factory method, getInstance, to obtain the appropriate Collator object for a given locale. </span></span></li>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">The following example shows how to compare two strings using the Collator for the default locale.</span></span></li>
<li><div>
<span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">Compare two strings in the default locale</span></span></div>
<ul>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">Collator myCollator = Collator.getInstance();</span></span></li>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">if( myCollator.compare("abc", "ABC") < 0 )</span></span></li>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">System.out.println("abc is less than ABC");</span></span></li>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;">else</span></span></li>
<li><span xmlns=""><span style="font-family: Georgia; font-size: 12pt;"><span class="Apple-style-span">System.out.println("abc is greater than or equal to ABC");</span></span></span></li>
</ul>
</li>
</ul>
</div>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-89181169549701178612012-03-06T10:32:00.001+05:302012-03-07T14:25:39.404+05:30Singleton Pattern<span xmlns=''><p><span style='font-size:12pt'><em><span style='color:#4f81bd'><span style='text-decoration:underline'>Lazy Initialization</span>:-</span>The instantiation of an object can be delayed until it is actually needed.<br /></em></span></p><p><span style='text-decoration:underline'><strong><em>Usage:</em></strong></span> This especially beneficial when the constructor is doing a costly job like, accessing a remote database.<br /></p><p><span style='text-decoration:underline'><strong><em>Example:<br /></em></strong></span></p><p>This code demonstrates how the Singleton pattern can be used to create a counter to provide unique sequential numbers, such as might be required for use as primary keys in a<br /></p><p>Database:<br /></p><p><br /> </p><p style='margin-left: 36pt'><span style='font-family:Courier New; font-size:10pt'><strong>Sequence.java<br /></strong></span></p><p style='margin-left: 36pt'><br /> </p><p style='margin-left: 36pt'><span style='font-family:Courier New; font-size:10pt'><strong>public class Sequence {<br /></strong></span></p><p style='margin-left: 36pt'><span style='font-family:Courier New; font-size:10pt'><strong>private static Sequence instance;<br /></strong></span></p><p style='margin-left: 36pt'><span style='font-family:Courier New; font-size:10pt'><strong>private static int counter;<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>private Sequence()<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>{<br /></strong></span></p><p style='margin-left: 108pt'><span style='font-family:Courier New; font-size:10pt'><strong>counter = 0; // May be necessary to obtain<br /></strong></span></p><p style='margin-left: 108pt'><span style='font-family:Courier New; font-size:10pt'><strong>// starting value elsewhere...<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>}<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>public static synchronized Sequence getInstance()<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>{<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>if(instance==null) // Lazy instantiation<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>{<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>instance = new Sequence();<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>}<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>return instance;<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>}<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>public static synchronized int getNext()<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>{<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>return ++counter;<br /></strong></span></p><p style='margin-left: 72pt'><span style='font-family:Courier New; font-size:10pt'><strong>}<br /></strong></span></p><p style='margin-left: 36pt'><span style='font-family:Courier New; font-size:10pt'><strong>}<br /></strong></span></p><p style='margin-left: 36pt'><br /> </p><p>Some things to note about this implementation:<br /></p><ul><li><strong>Synchronized </strong>methods are used to ensure that the class is thread-safe.<br /></li><li>This class cannot be subclassed because the constructor is <strong>private</strong>. This may or may not be a good thing depending on the resource being protected. To allow subclassing, the visibility of the constructor should be changed to <strong>protected</strong>.<span style='font-family:Arial'><br /> </span></li><li>Object serialization can cause problems; if a Singleton is serialized and then deserialized more than once, there will be multiple objects and not a singleton<br /></li></ul></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-39727914290537848342012-01-10T15:15:00.000+05:302012-01-10T15:15:18.395+05:30Design Patterns (TID)<div dir="ltr" style="text-align: left;" trbidi="on">
<span xmlns=""></span><br />
<ul>
<li><span style="font-family: Georgia; font-size: 12pt;">The pattern is an organised way of solving some specific class of problems.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">These patterns come in to the picture at analysis and high-level-design phase.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">The first step of applying one pattern to the code base is first to understand the find the vector of change in the code base.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">Next step is to isolate the things that are subject to change form the things that are not.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">That is adding a layer of abstraction to the code.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">The goal of design patterns is isolating the changes in your code.</span></li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;">Understand Inheritance and Composition as a solution to a specific class of problems.</span></div>
<ul>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Inheritance</strong>: - It allows you to express differences in behavior (that's the thing that changes) in objects that all have the same interface (that's what stays the same).</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Composition</strong>: - Composition can also be considered a pattern, since it allows you to change—dynamically or statically—the objects that implement your class, and thus the way that class works.</span></li>
</ul>
</li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;">Some principles of designing the code:</span></div>
<ul>
<li><span style="font-family: Georgia; font-size: 12pt;">Principle of least astonishment</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">Make common things easy and rare things possible</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Consistency</strong>: -</span><br /><span style="font-family: Georgia; font-size: 12pt;">the more random rules you pile onto the programmer, rules that have nothing to do with solving the problem at hand, the slower the programmer can produce.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Law of Demeter</strong>: - "Don't talk to strangers." An object should only reference itself, its attributes, and the arguments of its methods.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">Make the solution to be the most Simple.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">One abstraction per class, one class per abstraction. Might also be called <strong>Isomorphism</strong>.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Independence</strong> or <strong>Orthogonality</strong>. Express independent ideas independently. This complements Separation, Encapsulation and Variation, and is part of the Low-Coupling-High-Cohesion message.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Once and once only</strong>: Avoid duplication of logic and structure where the duplication is not accidental, ie where both pieces of code express the same intent for the same reason.</span></li>
</ul>
</li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;"><strong>Classifying Patterns</strong></span></div>
<ul>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Creational: -</strong> how an object can be created. This often involves isolating the details of object creation so your code isn't dependent on what types of objects there are and thus doesn't have to be changed when you add a new type of object. The aforementioned <em>Singleton</em> is classified as a creational pattern, and later in this book you'll see examples of <em>Factory Method</em> and <em>Prototype</em>.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Structural</strong>: designing objects to satisfy particular project constraints. These works with the way objects are connected with other objects to ensure that changes in the system don't require changes to those connections.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><strong>Behavioral</strong>: objects that handle particular types of actions within a program. These encapsulate processes that you want to perform, such as interpreting a language, fulfilling a request, moving through a sequence (as in an iterator), or implementing an algorithm. This book contains examples of the <em>Observer</em> and the <em>Visitor</em> patterns.</span></li>
</ul>
</li>
<li><span style="font-family: Georgia; font-size: 12pt;">It is required to classify the patterns in those categories so that we come across a specific type of problem then they help us to provide a solution.</span></li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;"><strong>Some basics to be kept in mind during coding</strong>.</span></div>
<ul>
<li><span style="font-family: Georgia; font-size: 12pt;">Use a Class as messenger so that u can pass the bunch as a package. Make it public so that all can access.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">Still to understand…</span><span style="font-family: Verdana; font-size: 28pt;"><br /></span><span style="font-family: Georgia; font-size: 12pt;">Collecting Parameter</span></li>
</ul>
</li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;"><strong>Object Pool pattern</strong>: -</span></div>
<ul>
<li><span style="font-family: Georgia; font-size: 12pt;">An object pool is a set of initialised objects that are kept ready to use rather than allocated or destroyed on demand.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished with an object, it returns it to the pool, rather than destroying it. It is a specific type of factory object.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">The case where it should be used: - Object pooling can offer a significant performance boost in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instances in use at any one time is low.</span></li>
</ul>
</li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;"><strong>Singleton Pattern : - </strong></span></div>
<ul>
<li><span style="font-family: Georgia; font-size: 12pt;">Possibly the simplest design pattern is the <em>singleton</em>, which is a way to provide one and only one object of a particular type. An important aspect of Singleton is that you provide a global access point, so singletons are often a solution for what you would have used a global variable for in C.</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;">Singletons can be found in java library.</span></li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;">The Code Example:</span></div>
<span style="font-family: Georgia; font-size: 12pt;"><em>//: singleton: SingletonPattern.java</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>// The Singleton design pattern: you can</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>// never instantiate more than one.</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>package singleton;</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>import junit.framework.*;</em></span><br />
<br /><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>// Since this isn't inherited from a Cloneable</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>// base class and cloneability isn't added,</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>// making it final prevents cloneability from</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>// being added through inheritance:</em></span><br />
<br /><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>final class Singleton {</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> private static Singleton s = new Singleton(47);</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>//Static as no duplicates are allowed for static methods and vars</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> private int i;</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> private Singleton(int x) { i = x; }</em></span><br />
<br /><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>//The method is static because it is accessing the static parameter so it has to be static to access it.</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> public static Singleton getReference() { </em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> return s; </em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> }</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> public int getValue() { return i; }</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> public void setValue(int x) { i = x; }</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>}</em></span><br />
<br /><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>public class SingletonPattern extends TestCase {</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> public void test() {</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>//This is possible because static methods can be called even without</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em>//object references</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> Singleton s = Singleton.getReference();</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> String result = "" + s.getValue();</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> System.out.println(result);</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> assertEquals(result, "47");</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> Singleton s2 = Singleton.getReference();</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> s2.setValue(9);</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> result = "" + s.getValue();</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> System.out.println(result);</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> assertEquals(result, "9");</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> try {</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> // Can't do this: compile-time error.</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> // Singleton s3 = (Singleton)s2.clone();</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> } catch(Exception e) {</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> throw new RuntimeException(e);</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> }</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> }</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> public static void main(String[] args) {</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> junit.textui.TestRunner.run(SingletonPattern.class);</em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em> }</em></span><br />
<em><span style="font-family: Georgia; font-size: 12pt;">} ///:~</span><span style="font-family: Arial; font-size: 10pt;"><br /></span></em></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><br /></span></li>
</ul>
</li>
</ul>
</div>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-69355687142708181142011-12-27T10:37:00.001+05:302012-01-10T15:15:51.603+05:30EJB 2.0 Drawbacks<span xmlns=''><ul><li><span style='font-family:Georgia; font-size:12pt'>Complex Products<br /></span></li><li><span style='font-family:Georgia; font-size:12pt'>Unmaintainable System<br /></span></li><li><span style='font-family:Georgia; font-size:12pt'>Non-portable, frame-work committed business components<br /></span></li><li><span style='font-family:Georgia; font-size:12pt'>Unpredictable System<br /></span></li><li><div><span style='font-family:Georgia; font-size:12pt'>Why EJB 2.0 is complex / complicated?<br /></span></div><ul><li><span style='font-family:Georgia; font-size:12pt'>Session bean - to write say hello method<br/><br/>in EJB 2.x, you need to write <br/>home interface<br/>component interface <br/>bean<br/><br/>in EJB 3.x, you need to write <br/>interface (with annotation) <br/>bean (with annotation) <br/><br/>EJB 2.x; lookup is the only way to get server object/resource<br/>EJB 3; DI (dependency injection) & lookup are the available way<br/><br/><br/>there are more points. But basically, with EJB 3 responsibility of container is heavy & responsibility of developer is light weight; so that development is less & maintenance is easy.</span></li></ul></li></ul></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-13480132627825389482011-12-12T17:28:00.001+05:302012-01-10T15:26:11.970+05:30Seam, EJB and Hibernate useful tips<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: black;"></span><span xmlns=""></span><span style="color: black;"></span><br />
<br /><span style="color: black;"> </span><br />
<span style="color: black; font-family: Trebuchet MS; font-size: 12pt;">Here are some tips I have gathered during our Seam project.<br /><br />@Create - Signals that this method should be called upon instantiation of the JavaBean.<br /><br />@Stateless - Makes an EJB3 object<br /><br />@Stateful - (SESSION) Caches in memory between server requests<br /><br />@In - These variables will automatically be set by Seam through injection of the variables labeled by the annotation. i.e<br />String zipCode; (Behind the curtain: zipCode = request.getParameter("zipCode"); )<br /><br />@Out - Same as above. This variable is also automatically set by Seam. Behind the curtain this variable is set in the HttpSession object.<br />( request.setAttribute("bestRestaurant", restaurant); )<br />What you also must remember is that if you want to use the outjected object, you only call it with its name. Not component name first.<br />i.e. If you stateful session bean is named @Name("someName") and you have a @Out String test, then when you use it in the view, you must write #{test} and not #{someName.test}<br />You can also say @Out(required = false) which means that the Object can be null. Otherwize it cannot.<br /><br />@DataModel annotation exposes an attribute of type java.util.List to the JSF page as an instance of javax.faces.model.DataModel. This allows us to use the list in a JSF with clickable links for each row. The DataModel is made available in a session context variable named the variable name.<br /><br />@PersistenceContext(type=EXTENDED)<br />EntityManager em;<br />This stateful bean has an EJB3 extended persistence context. The messages retrieved in the query remain in the managed state as long as the bean exists, so any subsequent method calls to the stateful bean can update them without needing to make any explicit call to the EntityManager.<br />Remember, its a good idea to clear the entitymanager once in a while if you do lots of searches<br /><br />@Factory("messageList")<br />public void findMessages() {<br />messageList = em.createQuery("from Message msg order by msg.datetime desc").getResultList();<br />}<br />The first time we navigate to the JSP page, there will be no value in the messageList context variable. The @Factory annotation tells Seam to create an instance of MessageManagerBean and invoke the findMessages() method to initialize the value. We call findMessages() a factory method for messages.<br /><br />@Remove<br />@Destroy<br />public void destroy() {}<br />All stateful session bean Seam components must have a method with no parameters marked @Remove that Seam uses to remove the stateful bean when the Seam context ends, and clean up any server-side state.<br /><br />@NamedQuery(name="findProeveByAddress",<br />query = "SELECT p " +<br />"FROM Proeve p, PersonAdresseAdressetype paa "<br />+ "WHERE paa.person = p.person "<br />+ "AND (lower(p.person.fornavn) like :fornavn OR lower(p.person.mellomnavn) like :fornavn) "<br />+ "AND (lower(p.person.etternavn) like :etternavn) "<br />+ "AND (lower(p.person.foedselsnummer) like :foedselsnr) "<br />+ "AND (lower(paa.adresse.adressefelt1) like :adresse) "<br />+ "AND (lower(p.kommune.navn) like :kommunenavn) "<br />+ "order by p.person.etternavn, p.avlagt desc"<br />)<br /><br />@Lob - Used for storing large string/byte objects. Becomes text or blob.<br /><br />Join Fetch - Makes the fetch eager. Be careful using join fetch. What I mean with that is that you should only use fetch when you are going to call methods<br />on the object being fetched. Otherwize it will be a performance issue.<br /><br />@Enumerated(EnumType.STRING) - Makes the enum as string in the database<br /><br />@Observer("proeveSearchDirty")<br />public void clear() {<br />proeveSokList = null;<br />}<br />With the observer annotation you can call on methods by giving it a name from other seam components.<br />For instance you can raise event which will call the observer method after an persist is made<br /><br />@RaiseEvent("proeveSearchDirty")<br />public String persist() {<br />return super.persist();<br />}<br /><br />If you want to override the default "Successfully created/updated/removed" from Seam and your EntityHome objects, then you can either override the methods<br />in the Home class. i.e<br />@Override<br />public String getCreatedMessage() {<br />return "Saved";<br />}<br /><br />@Override<br />public String getUpdatedMessage() {<br />return "Updated";<br />}<br /><br />@Override<br />public String getDeletedMessage() {<br />return "Removed";<br />}<br />Or you can put it in the messages.properties file with the notation:<br />*EntityName*.created. For instance:<br />Adresse_created=Lagret<br />Adresse_updated=Oppdatert<br />Adresse_deleted=Slettet<br />The messages.properties will be the default even if you have overrided the methods in your Home objects. </span></div>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-86464567208071954792011-12-02T15:43:00.001+05:302012-01-10T15:16:14.827+05:30Hibernate Introduction<span xmlns=''><p><span style='font-family:Georgia; font-size:12pt'>Hibernate is a Object relational mapping framework. What it will help us is in saving the objects into the relational world.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>Employee.java<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>public class Employee {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>private String Name;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>private int Emp_Id;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>private String Designation;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>private String Reportee;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>private int Access_Carad_No;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public String getName() {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> return Name;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public void setName(String name) {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> Name = name;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public int getEmp_Id() {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> return Emp_Id;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public void setEmp_Id(int emp_Id) {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> Emp_Id = emp_Id;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public String getDesignation() {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> return Designation;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public void setDesignation(String designation) {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> Designation = designation;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public String getReportee() {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> return Reportee;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public void setReportee(String reportee) {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> Reportee = reportee;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public int getAccess_Carad_No() {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> return Access_Carad_No;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> public void setAccess_Carad_No(int access_Carad_No) {<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> Access_Carad_No = access_Carad_No;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><br /> </p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em>}<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'>Now we have the domain class definition with us. Now we will put a mapping file which will map this class to the data model in the database. We are not creating the table in the database as we will use hibernate to generate the data model. Though in real life application you would have your data model already created and placed in the database.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>Employee.hbm.xml<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><?xml version="1.0"?><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><!DOCTYPE hibernate-mapping PUBLIC<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> "-//Hibernate/Hibernate Mapping DTD 3.0//EN"<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><hibernate-mapping> <br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><class name="Employee.java" table="Employee" schema="hiberpract"><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><id name="Emp_Id" column="EMP_ID"><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><generator class="increment"></generator><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em></id><br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em><property name="Name" column="NAME"></property><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><property name="Designation" column="DESIGNATION"></property><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><property name="Reportee" column="REPORTEE"></property><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><property name="Access_Carad_No" column="ACCCESS_CARD_NO"></property><br /></em></span></p><p><br /> </p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em></class><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em></hibernate-mapping><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'>Now we will provide hibernate the details to connect to the database. Similar to JDBC coding, the connection details and drivers need to be provided to Hibernate. This is done in a XML file<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>Hibernate.cfg.xml<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><?xml version='1.0' encoding='utf-8'?><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><!DOCTYPE hibernate-configuration PUBLIC<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>"-//Hibernate/Hibernate Configuration DTD 3.0//EN"<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em><<span style='background-color:lightgrey'>hibernate-configuration</span>><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <session-factory><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <!-- Database connection settings --><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="connection.driver_class">com.mysql.jdbc.Driver </property><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="connection.url">jdbc:mysql://<span style='text-decoration:underline'>localhost</span>:3306/<span style='text-decoration:underline'>Hiberpract </span></property><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="connection.username">root</property><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="connection.password">root</property><br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> <!-- JDBC connection pool (use the built-in) --><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="connection.pool_size">1<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> </property><br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> <!-- SQL dialect - This tells the SQL <span style='text-decoration:underline'>grammer</span> to be used --><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="dialect">org.hibernate.dialect.HSQLDialect<br /></em></span></p><p style='margin-left: 36pt'><span style='font-family:Georgia; font-size:12pt'><em> </property><br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> <!-- Enable Hibernate's automatic session context management --><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="current_session_context_class">thread</property><br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> <!-- Disable the second-level cache --><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider<br /></em></span></p><p style='margin-left: 36pt'><span style='font-family:Georgia; font-size:12pt'><em> </property><br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> <!-- Log out all the <span style='text-decoration:underline'>sql</span> that <span style='text-decoration:underline'>hibernate</span> is issuing to <span style='text-decoration:underline'>datbase</span>.<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> This is very useful for debugging --><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="show_sql">true</property><br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> <!-- Create the table looking at class and mapping. Very useful in development<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> Use validate in production environments. --><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="hbm2ddl.auto">create</property><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <property name="format_sql">true</property><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <!-- Mapping file. --><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> <mapping resource="Employee.hbm.xml"/><br /></em></span></p><p> <br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> </session-factory><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> </hibernate-configuration><br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'>Now we will write a utility class which will start the session factory of hibernate, if it is not started and will return the Session factory. This is the most used pattern of boot strapping Hibernate. Session factory contains all the configuration information at runtime. A Session is retrieve from the Session factory. A session is a light weight object which provide services to interact with database. You can think of Session like JDBC connection, thought it may not open the database connection if not required.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>HiberUtil.java:<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>import org.hibernate.SessionFactory;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>import org.hibernate.cfg.Configuration;<br /></em></span></p><p><br /> </p><p><br /> </p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em>public class HiberUtil <br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> {<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> private static SessionFactory sessionFactory;<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> static{<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> try{<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> //By default it will look for hibernate.cfg.xml in the class path<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> sessionFactory=new Configuration().configure().buildSessionFactory();<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }catch(Throwable ex){<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> throw new ExceptionInInitializerError(ex);<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> public static SessionFactory getSessionFactory(){<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> return sessionFactory;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> public static void shutdown(){<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> //Close caches and connection pool<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> getSessionFactory().close();<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em><br/></em>Now Let's create the base class having main method..<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>HiberBase.java<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>import org.hibernate.Session;<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>import org.hibernate.Transaction;<br /></em></span></p><p><br /> </p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em>public class HiberBase {<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> public static void main(String[] args) {<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> //Get the session from the Session factory<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> Session session = HiberUtil.getSessionFactory().openSession();<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> Transaction tx= session.beginTransaction();<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> Employee Emp = new Employee();<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> Emp.setName("Utkarsh");<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> session.save(Emp);<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> //System.out.println(studentId);<br /></em></span></p><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em> tx.commit();<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> session.close();<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em> }<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>}<br /></em></span></p><p><span style='font-family:Georgia; font-size:12pt'>In the above example we saw how to configure hibernate using XML mapping.</span></p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-13374673483215014232011-11-21T14:41:00.001+05:302012-01-10T15:29:29.274+05:30Overview of EJB and Lifecycle<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #cccccc;" xmlns=""></span><br />
<span style="color: #cccccc;"><br /></span><br />
<br />
<span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">The container is responsible for loading, activating, and in general maintaining the "life-cycle" of objects it provides. EJB have a fairly complex life-cycle. There are several kinds of EJB:</span><br />
<span style="font-family: Georgia; font-size: 12pt;"><span style="color: #cccccc;"><strong>Session Beans:</strong> These may be either stateful or stateless, and are primarily used to encapsulate business logic, carry out tasks on behalf of a client, and act as controllers or managers for other beans.</span></span><br />
<div>
</div>
<br />
<span style="font-family: Georgia; font-size: 12pt;"><span style="color: #cccccc;"><strong>Entity Beans:</strong> Entity beans represent persistent objects or business concepts that exist outside a specific application's lifetime. They are typically stored in a relational database. Entity beans can be developed using bean-managed persistence, which is implemented by the developer, or container-managed persistence, implemented by the container.</span></span><br />
<br />
<div>
<span style="color: #cccccc;"> </span></div>
<span style="color: #cccccc; font-family: Georgia; font-size: 12pt;"><strong>Message-Driven Beans</strong>: Message-driven beans listen asynchronously for Java Message Service (JMS) messages from any client or component and are used for loosely coupled, typically batch processing.</span><br />
</div>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-15315862313624446082011-11-07T12:53:00.001+05:302011-11-23T16:55:53.314+05:30Basics of Encapsulation Inheritance & Polymorphism<span xmlns=''><p><span style='font-family:Georgia; font-size:12pt'>Encapsulation:<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>Hiding the data/methods inside the object is called the Encapsulation.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>In Structured programming when u want to have a variable having accessible by some part and not by other part u don't have a mechanism to achieve the same.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>Encapsulation provides the same mechanism, if a class or a function wants to access then it should access those variables and functions from the references of the class i.e. objects.<br /></span></p><p><span style='color:black; font-family:Georgia; font-size:12pt'><strong><a name='Inheritance'/>Inheritance</strong><br /> </span></p><p><span style='color:black; font-family:Georgia; font-size:12pt'>Inheritance enables you to create a class that is similar to a previously defined class, but one that still has some of its own properties.<br /></span></p><h3><span style='color:black'><a name='Polymorphism'/>Polymorphism<br /></span></h3><p><span style='color:black; font-family:Times New Roman; font-size:13pt'>The last major feature of object-oriented programming is polymorphism. By using polymorphism, you can create new objects that perform the same functions as the base object but which perform one or more of these functions in a different way. For example, you may have a shape object that draws a circle on the screen. By using polymorphism, you can create a shape object that draws a rectangle instead. You do this by creating a new version of the method that draws the shape on the screen.<br /></span></p><p><span style='color:black; font-family:Times New Roman; font-size:13pt'>It also includes run-time polymorphism.</span></p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-77832510326818839782011-11-03T18:44:00.001+05:302012-03-07T14:25:28.058+05:30Ant Tutorial<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #cccccc;"></span><span xmlns=""></span><span style="color: #cccccc;"></span><br />
<ul>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Build.xml is the backbone file for running ANT.</span></li>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">It has one project tag and at least one target tag.</span></li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;"><span style="color: #cccccc;"><em>Project Tag</em> has three attributes</span></span></div>
<ul>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Name:- Project Name</span></li>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Default:-Default target to be called</span></li>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Basedir:- Basic Directory to do the needful.(More specifically it contains the absolute path.) </span></li>
</ul>
</li>
<li><span style="font-family: Georgia; font-size: 12pt;"><span style="color: #cccccc;">Example:<br /><em><project name="My Project" default="compile" basedir="."></em></span><em><br /></em></span></li>
<li><div>
<span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Then the <em>target tag</em></span></div>
<ul>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Name:- target name(Required)</span></li>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Depends:-if dependent on other targets..(We can even use if statement like if there exist the property then the target is dependent on other target )</span></li>
<li><span style="font-family: Georgia; font-size: 12pt;"><span style="color: #cccccc;">Description:- description.</span><strong><br /></strong></span></li>
</ul>
</li>
<li><div>
<span style="font-size: 12pt;"><span style="font-family: Georgia;"><span style="color: #cccccc;"><strong>Example</strong>:</span></span><span style="font-family: Courier New;"><strong><br /></strong></span></span></div>
<span style="color: #cccccc; font-family: Georgia; font-size: 12pt;"><em><target name="clean" description="Removing the all generated files."><br /><delete dir="${dir.build}"/><br /><delete dir="${dir.dest}"/><br /></target></em></span></li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;"><span style="color: #cccccc;">Then the </span><em><span style="color: #cccccc;">property tag.</span></em></span></div>
<ul>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Name:-name of the property</span></li>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Location:- It contains the property name.</span></li>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Value:- we can place the property value between ${"name } </span></li>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">File:- The name of the property file.</span></li>
</ul>
</li>
<li><div>
<span style="font-family: Georgia; font-size: 12pt;"><span style="color: #cccccc;"><strong>Example</strong>:</span></span></div>
<span style="color: #cccccc; font-family: Georgia; font-size: 12pt;"><em><property name="build" value="${build}"/></em></span><br />
<span style="color: #cccccc; font-family: Georgia; font-size: 12pt;"><em><property name="build" location="src"/></em></span><br />
<span style="font-family: Georgia; font-size: 12pt;"><em><span style="color: #cccccc;"><property file="build.properties"/></span><strong><br /></strong></em></span></li>
<li><span style="color: #cccccc; font-family: Georgia; font-size: 12pt;">Sample: build.xml</span></li>
</ul>
<span style="color: #cccccc; font-family: Georgia; font-size: 10pt;"><project name="My Project" default="compile" basedir="."></span><br />
<div style="margin-left: 18pt;">
<span style="color: #cccccc;"></span></div>
<pre><code><span style="color: #cccccc; font-family: Georgia;"><property name="build" value="${build}"/> </span></code></pre>
<div style="margin-left: 18pt;">
<br /><span style="color: #cccccc;"> </span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc;"></span></div>
<pre><code><span style="color: #cccccc; font-family: Georgia;"><property name="src" location="src"/> </span></code></pre>
<div style="margin-left: 18pt;">
<br /><span style="color: #cccccc;"> </span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc;"></span></div>
<pre><code><span style="color: #cccccc; font-family: Georgia;"><property file="build.properties"/> </span></code></pre>
<div style="margin-left: 18pt;">
<br /><span style="color: #cccccc;"> </span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc; font-family: Georgia; font-size: 10pt;"><project name="My Project" default="jar" basedir="."><br /><property name="dir.src" value="src"/><br /><property name="dir.build" value="build"/><br /><property name="dir.dest" value="dest"/><br /></span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc; font-family: Georgia; font-size: 10pt;"><target name="clean" description="Removing the all generated files."><br /><delete dir="${dir.build}"/><br /><delete dir="${dir.dest}"/><br /></span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc; font-family: Georgia; font-size: 10pt;"></target><br /><target name="prepare" depends="clean"><br /><mkdir dir="${dir.build}"/><br /><mkdir dir="${dir.dest}"/><br /><mkdir dir="${dir.src}"/><br /></target><br /></span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc; font-family: Georgia; font-size: 10pt;"><target name="compile" depends="prepare" description="Compilation of all source code."><br /><javac srcdir="${dir.src}" destdir="${dir.build}"/><br /></target><br /></span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc; font-family: Georgia; font-size: 10pt;"><target name="jar" depends="compile" description="Generates Sample.jar file in the dest folder"> <br /><jar jarfile="${dir.dest}/sample.jar" basedir="${dir.build}"/><br /></target><br /></project></span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc;"></span></div>
<pre><code><span style="color: #cccccc; font-family: Georgia;"><target name="buildWar" depends="init" description="build a war file"/></span></code></pre>
<div style="margin-left: 18pt;">
<br /><span style="color: #cccccc;"> </span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc;"></span></div>
<pre><code><span style="color: #cccccc; font-family: Georgia;"><target name="init" if="build"/></span></code></pre>
<div style="margin-left: 18pt;">
<br /><span style="color: #cccccc;"> </span></div>
<div style="margin-left: 18pt;">
<span style="color: #cccccc;"></span></div>
<pre><code><span style="color: #cccccc; font-family: Georgia;"><target name="init" unless="build"/></span></code></pre>
</div>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-8838877132464407172011-11-01T12:33:00.001+05:302011-11-01T12:34:45.388+05:30Hibernate Annotation tags<span xmlns=''><p>For that we need to import the ejb3.persistance.jar file so that we can use the following tags<br /></p><p><a href='http://download.oracle.com/javaee/5/api/index.html?javax/persistence/package-summary.html'>Package Summary</a><br /> </p><p><br /> </p><p><br /> </p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-51614968572350924352011-10-31T18:10:00.001+05:302011-11-01T12:34:45.388+05:30Hibernate Notes<span xmlns=''><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'><br /> </span></p><p style='text-align: justify'><br /> </p><ul><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>The Hibernate project has the structure as shown in the figure as above.<br /></span></div></li><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>We need one hibernate.cfg.xml file for hibernate configuration which handles connection pulling and other stuff like driver name, username of the database, password of the database, and the following properties..<br /></span></div></li></ul><p><span style='color:black; font-family:Georgia; font-size:12pt'>Hibernate.cfg.xml<br /></span></p><p><span style='color:#eeece1; font-family:Georgia; font-size:12pt'><?xml version='1.0' encoding='utf-8'?><br/><!DOCTYPE hibernate-configuration PUBLIC<br/>"-//Hibernate/Hibernate Configuration DTD//EN"<br/>"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><br/><br/><hibernate-configuration><br/><session-factory><br/> <property name="hibernate.connection.driver_class"><br /></span></p><p><span style='color:#eeece1; font-family:Georgia; font-size:12pt'>com.mysql.jdbc.Driver</property><br/> <property name="hibernate.connection.url"><br /></span></p><p><span style='color:#eeece1; font-family:Georgia; font-size:12pt'><strong>jdbc:mysql://localhost/hibernatetutorial</strong></property><br/> <property name="hibernate.connection.username"><strong>root</strong></property><br/> <property name="hibernate.connection.password"></property><br/> <property name="hibernate.connection.pool_size">10</property><br/> <property name="show_sql"><strong>true</strong></property><br/> <property name="dialect"><strong>org.hibernate.dialect.MySQLDialect</strong></property><br/> <property name="hibernate.hbm2ddl.auto"><strong>update</strong></property><br/> <!-- Mapping files --><br/> <mapping resource="<strong>contact.hbm.xml</strong>"/><br/></session-factory><br/></hibernate-configuration><br /></span></p><ul><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>Here the Hibernate tutorial is the database supposed to be created at local host.<br /></span></div></li><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>The dialect property says that we are suing the MySQL database.<br /></span></div></li><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>The show sql property lets us see the comments at the console which ultimately increases readability.<br /></span></div></li><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>Every Class file having the attributes have a classname.hbm.xml file which is the conf file for mapping between POJO and the database. For example we have "contact.hbm.xml" file for contact class.<br /></span></div></li><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>Hibernate accesses all the data through POJO objects and then interacts with the database. For interacting with the database it uses the HQL(Hibernate Query Language)<br /></span></div></li><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>Hibernate uses the Plain Old Java Objects (POJOs) classes to map to the database table. We can configure the variables to map to the database column.<br /></span></div></li></ul><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>Code for Contact.java:<br /></span></p><p style='text-align: justify'><span style='color:black; font-family:Georgia; font-size:12pt'><br/><span style='color:#3f5fbf'>/**<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* Java Class to map to the datbase Contact Table<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:#7f0055'><strong>public class </strong><span style='color:black'>Contact {<br/><span style='color:white'> <span style='color:#7f0055'><strong>private </strong><span style='color:black'>String firstName;<br/><span style='color:white'> <span style='color:#7f0055'><strong>private </strong><span style='color:black'>String lastName;<br/><span style='color:white'> <span style='color:#7f0055'><strong>private </strong><span style='color:black'>String email;<br/><span style='color:white'> <span style='color:#7f0055'><strong>private long </strong><span style='color:black'>id;<br/><br/><span style='color:white'> <span style='color:#3f5fbf'>/**<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* <span style='color:#7f9fbf'>@return <span style='color:#3f5fbf'>Email<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:white'> <span style='color:#7f0055'><strong>public </strong><span style='color:black'>String getEmail() {<br/><span style='color:white'> <span style='color:#7f0055'><strong>return </strong><span style='color:black'>email;<br/><span style='color:white'> <span style='color:black'>}<br/><br/><span style='color:white'> <span style='color:#3f5fbf'>/**<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* <span style='color:#7f9fbf'>@return <span style='color:#3f5fbf'>First Name<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:white'> <span style='color:#7f0055'><strong>public </strong><span style='color:black'>String getFirstName() {<br/><span style='color:white'> <span style='color:#7f0055'><strong>return </strong><span style='color:black'>firstName;<br/><span style='color:white'> <span style='color:black'>}<br/><br/><span style='color:white'> <span style='color:#3f5fbf'>/** <span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* <span style='color:#7f9fbf'>@return <span style='color:#3f5fbf'>Last name<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:white'> <span style='color:#7f0055'><strong>public </strong><span style='color:black'>String getLastName() {<br/><span style='color:white'> <span style='color:#7f0055'><strong>return </strong><span style='color:black'>lastName;<br/><span style='color:white'> <span style='color:black'>}<br/><br/><span style='color:white'> <span style='color:#3f5fbf'>/**<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* <span style='color:#7f9fbf'>@param <span style='color:#3f5fbf'>string Sets the Email<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:white'> <span style='color:#7f0055'><strong>public void </strong><span style='color:black'>setEmail(String string) {<br/><span style='color:white'> <span style='color:black'>email = string;<br/><span style='color:white'> <span style='color:black'>}<br/><br/><span style='color:white'> <span style='color:#3f5fbf'>/**<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* <span style='color:#7f9fbf'>@param <span style='color:#3f5fbf'>string Sets the First Name<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:white'> <span style='color:#7f0055'><strong>public void </strong><span style='color:black'>setFirstName(String string) {<br/><span style='color:white'> <span style='color:black'>firstName = string;<br/><span style='color:white'> <span style='color:black'>}<br/><br/><span style='color:white'> <span style='color:#3f5fbf'>/**<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* <span style='color:#7f9fbf'>@param <span style='color:#3f5fbf'>string sets the Last Name<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:white'> <span style='color:#7f0055'><strong>public void </strong><span style='color:black'>setLastName(String string) {<br/><span style='color:white'> <span style='color:black'>lastName = string;<br/><span style='color:white'> <span style='color:black'>}<br/><br/><span style='color:white'> <span style='color:#3f5fbf'>/**<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* <span style='color:#7f9fbf'>@return <span style='color:#3f5fbf'>ID Returns ID<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:white'> <span style='color:#7f0055'><strong>public long </strong><span style='color:black'>getId() {<br/><span style='color:white'> <span style='color:#7f0055'><strong>return </strong><span style='color:black'>id;<br/><span style='color:white'> <span style='color:black'>}<br/><br/><span style='color:white'> <span style='color:#3f5fbf'>/**<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>* <span style='color:#7f9fbf'>@param <span style='color:#3f5fbf'>l Sets the ID<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:white'> <span style='color:#7f0055'><strong>public void </strong><span style='color:black'>setId(<span style='color:#7f0055'><strong>long </strong><span style='color:black'>l) {<br/><span style='color:white'> <span style='color:black'>id = l;<br/><span style='color:white'> <span style='color:black'>}<br/>}<br /></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p><h1><span style='color:black; font-family:Georgia; font-size:12pt'>Mapping the Contact class with the Contact Table.<br /></span></h1><p><span style='font-family:Georgia'> Code for Contact.hbm.xml:<br /></span></p><p><span style='color:black; font-family:Arial; font-size:10pt'><?xml version="1.0"?><br/><!DOCTYPE hibernate-mapping PUBLIC <br/>"-//Hibernate/Hibernate Mapping DTD 3.0//EN"<br/>"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><br/><br/><hibernate-mapping><br/> <class name="roseindia.tutorial.hibernate.Contact" table="CONTACT"><br/> <id name="id" type="long" column="ID" ><br/> <generator class="assigned"/><br/> </id><br/><br/> <property name="firstName"><br/> <column name="FIRSTNAME" /><br/> </property><br/> <property name="lastName"><br/> <column name="LASTNAME"/><br/> </property><br/> <property name="email"><br/> <column name="EMAIL"/><br/> </property><br/> </class><br /></span></p><p><span style='color:black; font-family:Arial; font-size:10pt'></hibernate-mapping></span><span style='font-family:Georgia'><br /> </span></p><p style='text-align: justify'><br /> </p><p style='text-align: justify'><br /> </p><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>Then we need to create a hibernatetutorial database using MySQL running at localhost.<br /></span></p><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>For updating or inserting the data in the database we first need to create the session of hibernate. For that we need to use hibernate session factory which returns the session for hibernate.<br /></span></p><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>The hibernate-session is the main runtime interface between java and Hibernate.<br /></span></p><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>The most important method for this is sessionobj.save(POJOobject);.<br /></span></p><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'> The code for FirstExample.java<br /></span></p><p><span style='color:#7f0055; font-family:Georgia'><span style='font-size:12pt'><strong>import </strong><span style='color:black'>org.hibernate.Session;<br/><span style='color:#7f0055'><strong>import </strong><span style='color:black'>org.hibernate.SessionFactory;<br/><span style='color:#7f0055'><strong>import </strong><span style='color:black'>org.hibernate.cfg.Configuration;<br/><br/><br/><span style='color:#3f5fbf'>/**</span></span></span></span></span></span></span><span style='font-size:10pt'><br /> </span></span></p><p><span style='color:white; font-family:Georgia; font-size:12pt'> <span style='color:#3f5fbf'>* Hibernate example to inset data into Contact table<span style='color:black'><br/><span style='color:white'> <span style='color:#3f5fbf'>*/<span style='color:black'><br/><span style='color:#7f0055'><strong>public class </strong><span style='color:black'>FirstExample {<br/><span style='color:white'> <span style='color:#7f0055'><strong>public static void </strong><span style='color:black'>main(String[] args) {<br/><span style='color:white'> <span style='color:black'>Session session = <span style='color:#7f0055'><strong>null</strong><span style='color:black'>;<br/><br/><span style='color:white'> <span style='color:#7f0055'><strong>try</strong><span style='color:black'>{<br/><span style='color:white'> <span style='color:#3f7f5f'>// This step will read hibernate.cfg.xml <span style='color:black'><br /> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p><p><span style='color:#3f7f5f; font-family:Georgia; font-size:12pt'>and prepare hibernate for use<span style='color:black'><br/><span style='color:white'> <span style='color:black'>SessionFactory sessionFactory = <span style='color:#7f0055'><strong>new </strong><span style='color:black'><br /> </span></span></span></span></span></span></p><p><span style='color:black; font-family:Georgia; font-size:12pt'>Configuration().configure().buildSessionFactory();<br/><span style='color:white'> <span style='color:black'>session =sessionFactory.openSession();<br/><span style='color:white'> <span style='color:#3f7f5f'>//Create new instance of Contact and set <span style='color:black'><br /> </span></span></span></span></span></span></p><p><span style='color:#3f7f5f; font-family:Georgia'><span style='font-size:12pt'>values in it by reading them from form object<span style='color:black'><br/><span style='color:white'> <span style='color:black'>System.out.println(<span style='color:#2a00ff'>"Inserting Record"<span style='color:black'>);<br/><span style='color:white'> <span style='color:black'>Contact contact = <span style='color:#7f0055'><strong>new </strong><span style='color:black'>Contact();<br/><span style='color:white'> <span style='color:black'>contact.setId(<span style='color:#990000'>3<span style='color:black'>);<br/><span style='color:white'> <span style='color:black'>contact.setFirstName(</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style='color:#2a00ff'><span style='font-size:10pt'>"Utkarsh</span><span style='font-size:12pt'>"<span style='color:black'>);<br/><span style='color:white'> <span style='color:black'>contact.setLastName(</span></span></span></span><span style='color:#2a00ff'><span style='font-size:10pt'>"Thakkar</span><span style='font-size:12pt'>"<span style='color:black'>);<br/><span style='color:white'> <span style='color:black'>contact.setEmail(</span></span></span></span><span style='color:#2a00ff'><span style='font-size:10pt'>"yjk@</span><span style='font-size:12pt'>yahoo.com"<span style='color:black'>);<br/><span style='color:white'> <span style='color:black'>session.save(contact);<br/><span style='color:white'> <span style='color:black'>System.out.println(<span style='color:#2a00ff'>"Done"<span style='color:black'>);<br/><span style='color:white'> <span style='color:black'>}<span style='color:#7f0055'><strong>catch</strong><span style='color:black'>(Exception e){<br/><span style='color:white'> <span style='color:black'>System.out.println(e.getMessage());<br/><span style='color:white'> <span style='color:black'>}<span style='color:#7f0055'><strong>finally</strong><span style='color:black'>{<br/><span style='color:white'> <span style='color:#3f7f5f'>// Actual contact insertion will happen at this step<span style='color:black'><br/><span style='color:white'> <span style='color:black'>session.flush();<br/><span style='color:white'> <span style='color:black'>session.close();<br/><br/><span style='color:white'> <span style='color:black'>}<br/><span style='color:white'> <span style='color:black'><br/><span style='color:white'> <span style='color:black'>}<br/>}<br /></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-60473804171035683542011-10-25T12:43:00.001+05:302011-11-23T16:56:44.842+05:30Dependency Injection (DI)<span xmlns=''><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>The first reference to what would eventually become <strong>Dependency Injection </strong>appeared in 1994 in a paper by Robert C. Martin called "The Dependency Inversion Principle". <br /></span></p><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>In "The Dependency Inversion Principle" (or <strong>DIP</strong>), the author states the three defining factors of "bad code":<br /></span></p><ul><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>It is hard to change because every change affects too many other parts of the system (Rigidity)<br /></span></div></li><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>When you make a change, unexpected parts of the system break (Fragility)<br /></span></div></li><li><div style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>It is hard to reuse in another application because it cannot be disentangled from the current application (Immobility)<br /></span></div></li></ul><p style='text-align: justify'><span style='font-family:Georgia; font-size:12pt'>According to Martin, interdependency causes these coding problems (we'll call them <strong>RFI </strong>for Rigidity, Fragility, and Immobility). To fix RFI issues in your OO code, DIP has two basic rules:<br /></span></p><p style='text-align: justify'><br /> </p><p><span style='font-family:Georgia; font-size:12pt'><em>1. High level modules should not depend upon low level modules, both should depend upon abstractions.<br /></em></span></p><p style='text-align: justify; margin-left: 36pt'><span style='font-family:Georgia; font-size:12pt'>In other words, high level modules – which contain your business logic and all of the important meat of your application – should not depend on lower level components. The reason for this is if these lower level components were to change, the changes might affect the higher level components as well. This is the defining concept behind dependency inversion, that the prevailing wisdom of having higher-level modules dependent on lower-level modules is in fact a bad idea.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><em>2. Abstractions should not depend upon details, details should depend upon abstractions.<br /></em></span></p><p style='text-align: justify; margin-left: 36pt'><span style='font-family:Georgia; font-size:12pt'>This is another way to say that before you begin coding to the abstraction – the interface or abstract class – you should find the common behaviors in the code and work backwards. Your interface abstraction should cater to the intersection between the needs of your business logic and the common behaviors of the lower level modules. You should also leave the details of how these behaviors are implemented to the implementation classes.</span></p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-38042388034432804322011-10-19T10:56:00.002+05:302011-10-21T10:55:08.678+05:30Spring Light weighted Framework<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGZ0_C3pCTwHHgVvaIs85BHDim20dIXMDfcPf4Wz8TQBnDCQ_VSp6BsnGS7Tz42OvSzZhH_3HpglH6kzVLjCeIltmWRHcQgjGxyyKGRmlTkXLk5nLbDAnIHP9XLAcTduBcPtx6EZS5PMQ/s1600/spring-overview.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"></a><span xmlns=""><p><span style=" ;font-family:Georgia;font-size:14pt;"><strong>Strategies<br /></strong></span></p><ul><li><span style=" ;font-family:Georgia;font-size:14pt;">Loose coupling with dependency injection<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Declarative programming with AOP<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Boilerplate reduction with templates<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Minimally invasive and POJO-oriented<br /></span></li></ul><p><br /></p><p><span style=" ;font-family:Georgia;font-size:14pt;"><strong>Spring Keywords<br /></strong></span></p><ul><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring Web Flow<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">BlazeDS Integration<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring-WS<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring Security<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring-DM<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">DmServer<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Bundlor<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">TcServer<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring Batch<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring Integration<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring LDAP<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring IDE / STS<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring Rich Client<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring .NET<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Spring BeanDoc<br /></span></li><li><span style=" ;font-family:Georgia;font-size:14pt;">Groovy/Grails</span></li></ul><div><span class="Apple-style-span" style="font-family:Georgia;font-size:7;"><br /></span></div><ul><li><span style=" ;font-family:Georgia;font-size:14pt;"><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px; "><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGZ0_C3pCTwHHgVvaIs85BHDim20dIXMDfcPf4Wz8TQBnDCQ_VSp6BsnGS7Tz42OvSzZhH_3HpglH6kzVLjCeIltmWRHcQgjGxyyKGRmlTkXLk5nLbDAnIHP9XLAcTduBcPtx6EZS5PMQ/s1600/spring-overview.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGZ0_C3pCTwHHgVvaIs85BHDim20dIXMDfcPf4Wz8TQBnDCQ_VSp6BsnGS7Tz42OvSzZhH_3HpglH6kzVLjCeIltmWRHcQgjGxyyKGRmlTkXLk5nLbDAnIHP9XLAcTduBcPtx6EZS5PMQ/s320/spring-overview.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5665812018044738546" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 320px; height: 253px; " /></a></span><span xmlns=""></span></span></li></ul></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-49776474033295327102011-10-17T17:08:00.001+05:302011-10-17T17:11:04.475+05:30MVC Design Patterns<span xmlns=''><p><span style='font-family:Georgia; font-size:14pt'><strong>Context</strong><br /> </span></p><p><span style='font-family:Georgia; font-size:12pt'>MVC is an architectural pattern that is used when developing interactive application such as a shopping cart on the Internet.<br /></span></p><p><span style='font-family:Georgia; font-size:14pt'><strong>Problem<br /></strong></span></p><p><span style='font-family:Georgia; font-size:12pt'>User interfaces change often, especially on the internet where look-and-feel is a competitive issue. Also, the same information is presented in different ways. The core business logic and data is stable</span>.<br /></p><p><span style='font-family:Georgia; font-size:14pt'><strong>Solution<br /></strong></span></p><p><span style='font-family:Georgia; font-size:12pt'>Use the concept of dividing the concerns to divide the application into three areas <br /></span></p><ul><li><span style='font-family:Georgia; font-size:12pt'><strong>Model </strong>:- Encapsulates the core data and the functionality<br /></span></li><li><span style='font-family:Georgia; font-size:12pt'><strong>View </strong>:- Encapsulates the presentation of the data there can be many views of the common data<br /></span></li><li><span style='font-family:Georgia; font-size:12pt'><strong>Controller:- </strong> accepts input from the user and makes request from the model for the data to produce a new view <br /></span></li></ul><p><span style='font-family:Georgia; font-size:12pt'>The model represents the structure of the data and the operations that can be performed on that data.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>The view represents the data that is in a presentable format.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>The controller translates the user action like mouse events, submit, keystrokes, words spoken with user input to call the call the specific operation with the model. <br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><strong>MVC example- Java Pet Store<br /></strong></span></p><p><span style='font-family:Georgia; font-size:12pt'>It demonstrates a real-world approach to application development, where the presentation of data is separated from the process of obtaining data from objects which interact with the enterprise or database tier. <br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>The design can be divided into the following tiers:<br /></span></p><ul><li><span style='font-family:Georgia; font-size:12pt'>Client Tier<br /></span></li><li><span style='font-family:Georgia; font-size:12pt'>Web tier<br /></span></li><li><span style='font-family:Georgia; font-size:12pt'>Enterprise Bean Tier<br /></span></li><li><span style='font-family:Georgia; font-size:12pt'>Enterprise Information System Tier<br /></span></li></ul><p><br /> </p><p><span style='font-family:Georgia; font-size:12pt'>The Client Tire is the only part which is visible to the end user of the application so it handles the view part it is also connected to the other tiers using some well defined interfaces.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'>The separate Client tier design provides flexibility and extensibility .<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><br /> </span> </p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-90617646529324585862011-10-10T17:08:00.001+05:302011-10-17T17:10:47.010+05:30Variable arguments<span xmlns=''><p><span style='color:black; font-family:Georgia; font-size:12pt'>You can use a construct called <em>varargs</em> to pass an arbitrary number of values to a method.<br /></span></p><p><span style='color:black; font-family:Georgia; font-size:12pt'>You will most commonly see varargs with the printing methods; for example, this printf method:<br /></span></p><p><span style='color:black; font-family:Georgia; font-size:12pt'>public PrintStream printf(String format, Object... args)<br /></span></p><p style='margin-left: 36pt'><br /> </p><p><span style='color:black; font-family:Georgia; font-size:12pt'>allows you to print an arbitrary number of objects. It can be called like this:<br /></span></p><p><span style='color:black; font-family:Georgia; font-size:12pt'>System.out.printf("%s: %d, %s%n", name, idnum, address);<br /></span></p><p style='margin-left: 36pt'><br /> </p><p><span style='color:black; font-family:Georgia; font-size:12pt'>or like this<br /></span></p><p><span style='color:black; font-family:Georgia; font-size:12pt'>System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, mail);<br /></span></p><p><span style='color:black; font-family:Georgia; font-size:12pt'>or with yet a different number of arguments.<br /></span></p><p><span style='font-family:Georgia; font-size:12pt'><br /> </span> </p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-5987217976356356412011-10-10T14:52:00.001+05:302011-10-10T14:57:03.263+05:30Overloading of Methods<span xmlns=''><ul><li><span style='color:black; font-family:Georgia; font-size:14pt'>The Java programming language supports <em>overloading</em> methods, and Java can distinguish between methods with different <em>method signatures</em>. This means that methods within a class can have the same name if they have different parameter lists<br /></span></li><li><span style='color:black; font-family:Georgia; font-size:14pt'>You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.<br /></span></li><li><div><span style='color:black; font-family:Georgia; font-size:14pt'>The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.<br /></span></div><p><br /> </p></li><li><div><span style='color:black; font-family:Georgia; font-size:12pt'><strong>Note:</strong> <br /></span></div><ul><li><span style='font-family:Georgia; font-size:12pt'><em><span style='color:black'>Overloaded methods should be used sparingly, as they can make code much less readable.</span><br /> </em></span></li></ul></li></ul><p><span style='color:black; font-size:13pt'><br /> </span> </p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-71933053565571005392011-10-10T14:29:00.001+05:302011-10-10T14:57:03.264+05:30Copying arrays using System class method<span xmlns=''><p><span style='color:black'><span style='font-family:Times New Roman; font-size:13pt'>The </span><span style='font-family:Courier New; font-size:10pt'>System</span><span style='font-family:Times New Roman; font-size:13pt'> class has an </span><span style='font-family:Courier New; font-size:10pt'>arraycopy</span><span style='font-family:Times New Roman; font-size:13pt'> method that you can use to efficiently copy data from one array into another:<br /></span></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'>public static void arraycopy(Object src,<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> int srcPos,<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> Object dest,<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> int destPos,<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> int length)<br /></span></p><p><span style='color:black'><span style='font-family:Times New Roman; font-size:13pt'>The two </span><span style='font-family:Courier New; font-size:10pt'>Object</span><span style='font-family:Times New Roman; font-size:13pt'> arguments specify the array to copy <em>from</em> and the array to copy <em>to</em>. The three </span><span style='font-family:Courier New; font-size:10pt'>int</span><span style='font-family:Times New Roman; font-size:13pt'> arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.<br /></span></span></p><p><span style='color:black; font-family:Times New Roman; font-size:13pt'>The following program, </span><span style='font-family:Courier New; font-size:10pt'>ArrayCopyDemo</span><span style='color:black'><span style='font-family:Times New Roman; font-size:13pt'>, declares an array of </span><span style='font-family:Courier New; font-size:10pt'>char</span><span style='font-family:Times New Roman; font-size:13pt'> elements, spelling the word "decaffeinated". It uses </span><span style='font-family:Courier New; font-size:10pt'>arraycopy</span><span style='font-family:Times New Roman; font-size:13pt'> to copy a subsequence of array components into a second array:<br /></span></span></p><p><br /> </p><p><span style='color:black; font-family:Courier New; font-size:10pt'>class ArrayCopyDemo {<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> public static void main(String[] args) {<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> 'i', 'n', 'a', 't', 'e', 'd' };<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> char[] copyTo = new char[7];<br /></span></p><p><br /> </p><p><span style='color:black; font-family:Courier New; font-size:10pt'> System.arraycopy(copyFrom, 2, copyTo, 0, 7);<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> System.out.println(new String(copyTo));<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'> }<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'>}<br /></span></p><p><span style='color:black; font-family:Times New Roman; font-size:13pt'>The output from this program is:<br /></span></p><p><span style='color:black; font-family:Courier New; font-size:10pt'>caffein<br /></span></p></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-13770919594371993182011-10-10T14:21:00.001+05:302011-10-10T14:57:03.264+05:30Java Variables<span xmlns=''><p><span style='font-family:Georgia; font-size:18pt'>Types of Variables<br /></span></p><ul><li><span style='font-family:Georgia; font-size:14pt'><em>Instance Variable: </em></span><span style='color:black; font-size:13pt'>Non-static fields are also known as <em>instance variables</em> because their values are unique to each <em>instance</em> of a class.</span><span style='font-family:Georgia; font-size:14pt'><em><br /> </em></span></li><li><span style='font-family:Georgia; font-size:14pt'><em>Class Variables:</em></span><span style='color:black'><span style='font-size:13pt'> A <em>class variable</em> is any field declared with the </span><span style='font-family:Courier New; font-size:10pt'>static</span><span style='font-size:13pt'> modifier; this tells the compiler that there is exactly one copy of this variable in existence; regardless of how many times the class has been instantiated.</span></span><span style='font-family:Georgia; font-size:14pt'><em><br /> </em></span></li><li><span style='font-family:Georgia'><span style='font-size:14pt'><em>Local Variables: </em></span><span style='font-size:12pt'>Variables</span></span><span style='color:black; font-size:13pt'> between the opening and closing braces of a method.</span><span style='font-family:Georgia; font-size:14pt'><em><br /> </em></span></li><li><span style='font-family:Georgia'><span style='font-size:14pt'><em>Parameters: </em></span><span style='font-size:12pt'>The Arguments of a method or a constructor.</span><span style='font-size:14pt'><em><br /> </em></span></span></li></ul><p><span style='font-family:Georgia; font-size:16pt'>Naming Conventions<br /></span></p><ul><li><span style='font-family:Georgia'><span style='font-size:12pt'>By convention it should start with the letter.</span><span style='font-size:16pt'><br /> </span></span></li><li><span style='font-family:Georgia'><span style='font-size:12pt'>Avoid $ and _ characters in the names.</span><span style='font-size:16pt'><br /> </span></span></li><li><span style='font-family:Georgia'><span style='font-size:12pt'>Whitespaces are not allowed.</span><span style='font-size:16pt'><br /> </span></span></li><li><span style='font-family:Georgia'><span style='font-size:12pt'>Keyword or any reserved word should not be used.</span><span style='font-size:16pt'><br /> </span></span></li><li><span style='font-family:Georgia; font-size:12pt'>For constants separate the words with the _.</span></li></ul></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-21016742338322527112011-10-10T14:11:00.001+05:302011-10-10T14:57:38.166+05:30Object initialization<span xmlns=''><ul><li><span style='font-size:14pt'><span style='color:black; font-family:Georgia'>As with class initialization, the simplest kind of object initialization is automatic initialization of object fields to default values.</span><br /> </span></li><li><div><span style='font-size:14pt'><span style='color:black; font-family:Georgia'>Example:</span><br /> </span></div><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'>class ObjectInitializationDemo1<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'>{<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> boolean b;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> byte by;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> char c;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> double d;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> float f;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> int i;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> long l;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> short s;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> String st;<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> public static void main (String [] args)<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> {<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> ObjectInitializationDemo1 oid1 = new ObjectInitializationDemo1 ();<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.b = " + oid1.b);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.by = " + oid1.by);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.c = " + oid1.c);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.d = " + oid1.d);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.f = " + oid1.f);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.i = " + oid1.i);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.l = " + oid1.l);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.s = " + oid1.s);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("oid1.st = " + oid1.st);<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'> }<br /></span></p><p style='margin-left: 9pt'><span style='color:black; font-family:Georgia; font-size:14pt'>}<br /></span></p></li><li><div><span style='font-size:14pt'>Output<br /></span></div><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>b = false<br /></span></code></pre></p><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>by = 0<br /></span></code></pre></p><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>c = <br /></span></code></pre></p><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>d = 0.0<br /></span></code></pre></p><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>f = 0.0<br /></span></code></pre></p><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>i = 0<br /></span></code></pre></p><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>l = 0<br /></span></code></pre></p><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>s = 0<br /></span></code></pre></p><p style='margin-left: 9pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>st = null<br /></span></code></pre></p><p><br /> </p></li><li><span style='font-size:14pt'>There is no class loading and byte code verifying in between.<br /></span></li><li><span style='font-size:14pt'>The same way the explicit assignment is done.<br /></span></li><li><span style='font-size:14pt'>The default constructer initializes the contents of the object. For that it uses <init> methodfrom JVM perspective<br /></span></li><li><span style='font-size:14pt'>The same way as in class initialization the object initialization also allows us to refer to the previously initialized field.<br /></span></li><li><div><span style='font-size:14pt'>Object Block Initailazation<br /></span></div><p style='margin-left: 36pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>{<br /></span></code></pre></p><p style='margin-left: 36pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("Initializing object " + hashCode ());<br /></span></code></pre></p><p style='margin-left: 36pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> int localVariable = 1;<br /></span></code></pre></p><p style='margin-left: 36pt'><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> }<br /></span></code></pre></p></li><li><span style='font-size:14pt'><span style='color:black; font-family:Georgia'>In many situations, you will not use object block initializers because you can use constructors to perform complex initialization tasks. </span><br /> </span></li><li><span style='color:black; font-family:Georgia'><span style='font-size:14pt'>For situations like: Anonymous inner classes often require object block initializers to perform complex initialization tasks.</span><h3><br /> </h3><span style='font-size:14pt'>Anonymous inner classes require object block initializers because anonymous inner classes have no names, constructors take on the names of their classes, and you cannot declare constructors in classes that have no names.</span></span><span style='font-size:14pt'><br /> </span></li><li><span style='font-size:14pt'><span style='color:black; font-family:Georgia'>In the case of hierarchies the class at the top most level is first initialized.</span><br /> </span></li><li><div><span style='font-size:14pt'>In object field/block initialization , one can access the field of the subclass but at that time the explicit value is not assigned so it will give us default value. <br /></span></div><p><span style='color:black; font-family:Courier New; font-size:10pt'><br /> </span> </p></li></ul></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-9314239743875578622011-10-10T11:48:00.001+05:302011-10-10T14:57:38.166+05:30Class initialization<span xmlns=''><ul><li><span style='font-family:Georgia; font-size:14pt'><span style='color:black'>Although we tend to think of initialization in terms of assigning values to variables, initialization is so much more. </span><br /> </span></li><li><span style='font-family:Georgia; font-size:14pt'><span style='color:black'>For example, initialization might involve opening a file and reading its contents into a memory buffer, registering a database driver, preparing a memory buffer to hold an image's contents, acquiring the resources necessary for playing a video, and so on.</span><br /> </span></li><li><span style='font-family:Georgia; font-size:14pt'><span style='color:black'>Java supports initialization via language features collectively known as <em>initializers.</em></span><br /> </span></li><li><span style='font-family:Georgia; font-size:14pt'><span style='color:black'>Class initialization is different from Object initialization and it happens before Object initialization.</span><br /> </span></li></ul><p><br /> </p><ul><li><div><strong><span style='color:black; font-family:Georgia; font-size:13pt'>Class Initialization</span><br /> </strong></div><ul><li><span style='font-family:Georgia; font-size:14pt'>A program contains no of classes, before the execution of java program the class loader loads the public class having the PSVM.<br /></span></li><li><span style='font-family:Georgia; font-size:14pt'>Then the byte code verifier verifies the class.<br /></span></li><li><span style='font-family:Georgia; font-size:14pt'>Then class initializes.<br /></span></li><li><span style='font-family:Georgia; font-size:14pt'>The class fields are set to default values.<br /></span></li><li><div><span style='font-family:Georgia; font-size:14pt'>For Example:<br /></span></div><p><br /> </p><ul><li><span style='color:black; font-family:Georgia; font-size:14pt'> static boolean b;//flase<br /></span></li><li><span style='color:black; font-family:Georgia; font-size:14pt'> static byte by;//0<br /></span></li><li><span style='color:black; font-family:Georgia; font-size:14pt'> static char c;//<br /></span></li><li><span style='color:black; font-family:Georgia; font-size:14pt'> static double d;//0.0<br /></span></li><li><span style='color:black; font-family:Georgia; font-size:14pt'> static float f;//0.0<br /></span></li><li><span style='color:black; font-family:Georgia; font-size:14pt'> static int i;//0<br /></span></li><li><span style='color:black; font-family:Georgia; font-size:14pt'> static long l;//0<br /></span></li><li><span style='color:black; font-family:Georgia; font-size:14pt'> static short s;//0<br /></span></li><li><div><span style='color:black; font-family:Georgia; font-size:14pt'> static String st;//null<br /></span></div><p><br /> </p></li></ul></li></ul></li><li><span style='font-family:Georgia; font-size:14pt'>We can also explicitly assign values to the class fields.<br /></span></li><li><span style='font-family:Georgia; font-size:14pt'>Those values are assigned to the fields just after the class is loaded and before any user defined method including main executes.<br /></span></li><li><span style='font-family:Georgia; font-size:14pt'>This initialization is done using <clinit>method of the JVM. It uses machine level instructions to assign those values. <br /></span></li><li><div><span style='color:black; font-family:Georgia; font-size:14pt'>Although a subsequently declared class field can refer to a previously declared class field, the reverse is not true: You cannot declare a class field initializer that refers to a class field declared later in source code. In other words, Java does not permit forward references with class field initializers, as the following code fragment demonstrates:<br /></span></div><ul><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>static int second = 1 + first;<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>static int first = 3;<br /></span></code></pre></li></ul></li><li><div><span style='font-family:Georgia; font-size:14pt'>In the cases when you need to read the contents of the file before the main method executes then we need to use the static block.<br /></span></div><ul><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>static<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> {<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("Acquiring filenames");<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> filenames = new File (".").list ();<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("Filenames acquired");<br /></span></code></pre></li><li><pre><code><span style='color:black; font-size:14pt'><span style='font-family:Georgia'> }</span><br /> </span></code></pre></li></ul></li><li><div><span style='color:black; font-family:Georgia; font-size:14pt'>Class block initializers are useful. For example, Sun's JDBC (Java Database Connectivity) API uses class block initializers to simplify database driver registration. Consider the following code fragment:<br /></span></div><ul><li><pre><code><span style='color:black'><span style='font-family:Georgia; font-size:14pt'>Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");</span><br /> </span></code></pre></li></ul></li><li><span style='font-family:Georgia; font-size:14pt'>Any variable declared in the block is having the scope only upto that block.<br /></span></li><li><div><span style='font-family:Georgia; font-size:14pt'>JVM allows us to declare any constant field without any explicit initializer, but u have to initialize it before its first use.<br /></span></div><ul><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>class ClassInitializationDemo5<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'>{<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> final static double PI;<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> static<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> {<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> PI = 3.14159;<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> int i;<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> for (i = 0; i < 5; i++)<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println (i);<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> }<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> static int j = i;//this vl cause problem as the I is local to that block<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> public static void main (String [] args)<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> {<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> System.out.println ("PI = " + PI);//this vl show 3.14 as the initialization is done in static block<br /></span></code></pre></li><li><pre><code><span style='color:black; font-family:Georgia; font-size:14pt'> }<br /></span></code></pre></li><li><pre><code><span style='color:black; font-size:14pt'><span style='font-family:Georgia'>}</span><br /> </span></code></pre></li></ul></li><li><span style='color:black; font-family:Georgia; font-size:14pt'>When a class hierarchy is involved, the compiler creates a separate<clinit> method for each class in that hierarchy. At runtime, the JVM loads all hierarchy classes and calls their <clinit> methods in a top-to-bottom order. </span></li></ul></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-86569923407825519742011-10-07T18:59:00.001+05:302011-10-10T14:57:38.167+05:30Class Loading while running JVM<span xmlns=''><ul><li><div><span style='font-family:Times New Roman; font-size:12pt'>The Order is <strong><br /> </strong></span></div><ul><li><span style='color:black; font-family:Times New Roman; font-size:12pt'><strong>Bootstrap classes</strong> - Classes that comprise the Java platform, including the classes in <span style='color:#444444'>rt.jar<span style='color:black'> and several other important jar files.</span><strong><br /> </strong></span></span></li><li><span style='color:black; font-family:Times New Roman; font-size:12pt'><strong>Extension classes</strong> - Classes that use the Java Extension mechanism. These are bundled as <span style='color:#444444'>.jar<span style='color:black'> files located in the extensions directory.</span><strong><br /> </strong></span></span></li><li><span style='color:black; font-family:Times New Roman; font-size:12pt'><strong>User classes</strong> - Classes defined by developers and third parties that do not take advantage of the extension mechanism. You identify the location of these classes using the <span style='color:#444444'>-classpath<span style='color:black'> option on the command line (the preferred method) or by using the CLASSPATH environment variable. </span></span></span></li></ul></li></ul></span>Unknownnoreply@blogger.comtag:blogger.com,1999:blog-316343243367261800.post-87680191666580170952011-10-07T15:32:00.004+05:302011-10-07T15:47:13.256+05:30Wild Cards while specifying the class path<span xmlns=""><ul><li><span style=" ;font-family:Times New Roman;font-size:12pt;color:black;">A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory <span style="color:#444444;">foo<span style="color:black;">, use either <span style="color:#444444;">foo; foo/*<span style="color:black;"> or <span style="color:#444444;">foo/*;foo<span style="color:black;">. The order chosen determines whether the classes and resources in <span style="color:#444444;">foo<span style="color:black;"> are loaded before JAR files in <span style="color:#444444;">foo<span style="color:black;">, or vice versa.</span><strong><br /> </strong></span></span></span></span></span></span></span></span></span></span></li><li><span style=" ;font-family:Times New Roman;font-size:12pt;color:black;">Subdirectories are not searched recursively. For example, <span style="color:#444444;">foo/*<span style="color:black;"> looks for JAR files only in <span style="color:#444444;">foo<span style="color:black;">, not in <span style="color:#444444;">foo/bar<span style="color:black;">, <span style="color:#444444;">foo/baz<span style="color:black;">, etc.</span><strong><br /> </strong></span></span></span></span></span></span></span></span></li><li><span style=" ;font-family:Times New Roman;font-size:12pt;"><span style="color:black;">If u want to load jars files or class file in a specific order that u have to specify the list itself in the class path but not using the wild cards.</span><strong><br /> </strong></span></li><li><span style=" ;font-family:Times New Roman;font-size:12pt;color:black;">Expansion of wildcards is done early, prior to the invocation of a program's <span style="color:#444444;">main<span style="color:black;"> method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory <span style="color:#444444;">foo<span style="color:black;"> contains <span style="color:#444444;">a.jar<span style="color:black;">, <span style="color:#444444;">b.jar<span style="color:black;">, and <span style="color:#444444;">c.jar<span style="color:black;">, then the class path <span style="color:#444444;">foo/*<span style="color:black;"> is expanded into <span style="color:#444444;">foo/a.jar; foo/b.jar; foo/c.jar<span style="color:black;">, and that string would be the value of the system property <span style="color:#444444;">java.class.path<span style="color:black;">.</span><strong><br /> </strong></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></li><li><span style=" ;font-family:Times New Roman;font-size:12pt;color:black;">The <span style="color:#444444;">CLASSPATH<span style="color:black;"> environment variable is not treated any differently from the <span style="color:#444444;">-class path<span style="color:black;"> (or <span style="color:#444444;">-cp<span style="color:black;">) command-line option. That is, wildcards are honoured in all these cases. However, class path wildcards are not honoured in the <span style="color:#444444;">Class-Path<span style="color:black;"> jar-manifest header.</span><strong><br /> </strong></span></span></span></span></span></span></span></span></li></ul><div><span class="Apple-style-span" style="font-family:'Times New Roman';font-size:7;"><br /></span></div><div><span class="Apple-style-span"><p style="font-size: -webkit-xxx-large; font-family: 'Times New Roman'; "></p></span></div></span>Unknownnoreply@blogger.com