Thursday, April 4, 2013

SuperMan bound by Java Monitors

Photo Taken from : http://goo.gl/2B5Sj


Its a dark time in the life of Super Man. Jor-El wants him to go on a voyage to prepare him for his ultimate destiny. Yet the Earth is faced with dooms-day and the Justice League needs their Man of Steel in action to save the world. But you cant do both at the same time since we have just one SuperMan. Also he cannot fight dooms day without first fulfilling his destiny and realizing his true powers. How do we call upon Superman without making the man go bonkers on what to do. This should be done in an orderly manner where one has to wait until the voyage is done.

We will make use of Java Monitors to help SuperMan listen to his Kryptonian father as well as come back in time to save the world from dooms day. First of all we define the Man of Steel;


/**
 * The awesome kryptonian man is represented by this class
 * 
 * @author Dinuka Arseculeratne
 *
 */
public class SuperMan {

 private boolean onVoyage = false;

 /**
  * Schedule a voyage for Superman. Note that this method first checks whether he is
  * already on a voyage, and if so calls the wait() method to hault the current thread
  * until notify is called and onVoyage is set to false.
  */
 public synchronized void goOnVoyage() {

  if (onVoyage) {
   try {
    System.out.println("SuperMan is already on a voyage. Please wait until he returns from his quest.");
    wait();
    System.out.println("His goyage is over, time for him to go on a new voyage....");
   } catch (InterruptedException e) {
    System.out.println(" I am SuperMan, i do not handle these petty exceptions");
   }

  }
  onVoyage = true;
  notify();

 }

 /**
  * This method calls Superman back from his current voyage. Again the method
  * checks whether Super man is not already on a voyage. If so the current thread is
  * Halted until he is schedule to go on a voyage because he needs to be on a voyage
  * to be called back in the first place.
  */
 public synchronized void returnFromVoyage() {

  if (!onVoyage) {
   try {
    System.out.println("SuperMan is not yet on a voyage. Please Wait.");
    wait();
    System.out.println("Great he has gone on a voyage, time to call him back!!");
   } catch (InterruptedException e) {
    System.out.println(" I am SuperMan, i do not handle these petty exceptions");
   }
  }
  onVoyage = false;
  notify();
 }
}


So we have defined SuperMan. Note that he has two methods defined. One which allows him to go on a voyage and another to call him back from his current voyage. As you can see SuperMan does not handle exceptions because, well.......... He is SuperMan and he is the exception :). You can see that before each call we check the boolean indicating whether he is on a voyage or not and depending on the method called, the wait() of the Object is called in order to halt the current thread that is calling the method until notify() is called by the thread that is currently operating on the object. Note that wait() and notify() should be called inside a synchronized method or block for it to work accurately. Because you first need to acquire a lock in order to halt or notify it.

Getting back to the previous issue, we know that both the Justice League and Jor-El need SuperMan but for different purposes. Lets see how this battle unravels with the following code snippet;


public class Test {

 public static void main(String[] args) {
  SuperMan superMan = new SuperMan();
  
  JusticeLeague justiceLeague = new JusticeLeague(superMan);
  justiceLeague.start();
  
  JorEl jorEl = new JorEl(superMan);
  jorEl.start();
  
 }

 

}

class JusticeLeague extends Thread{
 
 private SuperMan superMan = null;
 
 public JusticeLeague(SuperMan superMan)
 {
  this.superMan = superMan;
 }
 
 @Override
 public void run() {
  superMan.returnFromVoyage();
 }
}

class JorEl extends Thread{
 
 private SuperMan superMan = null;
 public JorEl(SuperMan superMan)
 {
  this.superMan = superMan;
 }
 
 @Override
 public void run() {
  superMan.goOnVoyage();
 }
 
}


Note that here we have JorEl and the JusticeLeagure operating on two different threads trying to access SuperMan concurrently. As you can see from our main method, the JusticeLeague wants to call back SuperMan in order to save the world. But fortunately he is not yet on a voyage so its illegal to ask him to return. Then comes JorEl asking his son to go on a voyage to fulfill his true destiny. It is only after this voyage that he can return to save planet Earth. If you run this now you can see that the JusticeLeague thread is halted until SuperMan goes on the voyage and notify is called. Just for fun try to comment out the notify() method and you will see the application will hang because now one thread will wait indefinitely until it is notified of the completion of the process.

If not for Java Monitors, SuperMan would have failed since he would have gone to face doomsday without first going on his voyage and fulfilling his destiny. And Java saves the world again.

Note : The story is fictional yet Java Monitors are real


Thank you for reading everyone. And have a great day ahead. If you feel like it, please do leave by a comment. Cheers!!

Wednesday, March 27, 2013

jqGrid Learnings

I have been using jqGrid for a few projects that was done using PHP. For anyone who does not know what jqGrid is, its a fully featured library that allows you to display and manipulate your data in tabular form. Saves you tons of time writing pure tables to handle and manipulate your data. It has two variants where one is the commercialized version (jqSuite) and the free version is maintained here. The API is very much verbose with a plethora of methods for almost anything you would ever want to do in a grid. This post will be aimed at use cases to which i used jqGrid and what changes i needed to do in order to get it working.


  • Custom search on a button click
By default jqGrid sends a data search GET request on page load. It also has a refresh button in the bottom part. What i wanted was to send a GET request with the search parameters on a button click. To achieve this i first built the URL with the parameters as follows;


var length = jsPatientSearchCtrl.length;
var searchURL= "server.php?";
for(var i=0;i<length;i++){

 if($('#'+jsPatientSearchCtrl[i].id).val()!=''){

   searchURL+="&";
  
   searchURL+=jsPatientSearchCtrl[i].desc+"="+$('#'+jsPatientSearchCtrl[i].id).val();
   
 }
}
Here i am iterating through the JSON array i have defined to hold the data objects within my given page. After creating the URL with the parameters you have to tell jqGrid to reload the data as follows;

jQuery("#list2").jqGrid('setGridParam',{url:searchURL}).trigger("reloadGrid")

This will send another GET request to the server to reload the Grid with new data according to our changed search criteria when the user clicks on the search button.


  • Editing grid data using a form submission
By default jQGrid provides the ability to edit the fields within our Grid by opening a dialog where by you can change the details for a given record and submit it. To do this, first you have to enable the edit option in your grid. You do this as follows;


jQuery("#list2").jqGrid('navGrid',
'#pager2',
{edit:true,add:false,del:true,search:false}
); 
Here i have enabled the edit option by setting the value to true and have disabled the add and search functionality as this was not required in my scenario. After that you need to define which columns you want to edit. You do this within your colModel parameter which you give when constructing your jqGrid. I present what i have used;


colModel:[ 
  {name:'Ref',index:'id', width:55,sortable:false,editable:false,editoptions:{readonly:true,size:10}},
  {name:'Identity',index:'patient_identity', width:90,sortable:false,editable:true,editoptions:{size:10}}, 
  {name:'Reference',index:'patient_identity', width:90,sortable:false,editable:true,editoptions:{size:10}}, 
  {name:'Date',index:'date', width:90,sortable:false,editable:true, editoptions:{size:10}}, 
  {name:'Time',index:'time', width:50,sortable:false,editable:true, editoptions:{rows:"1",cols:"10"}}, 
  {name:'Name',index:'patient_name', width:130,sortable:false,editable:true,edittype:"textarea", editoptions:{rows:"1",cols:"20"}}, 
  {name:'Referred',index:'referred_by', width:100,sortable:false,editable:true,edittype:"textarea", editoptions:{rows:"1",cols:"20"}}, 
  {name:'Blood',index:'blood_collected_at', width:120, align:"right",sortable:false,editable:true,edittype:"textarea", editoptions:{rows:"1",cols:"20"}}]
Important things to note here is that certain fields we have set editable:false which signifies that these values will not be able to be changed by the user when editing. To enable editing on a particular column we define it as editable:true. Optionally you can define a size. You do this using the editoptions attribute. For example as per the above code snippet, we have used editoptions:{size:10}  to give a size of 10 to that particular text field.

If you want to define a text area, this can also be achieved. Again revisiting the above code, we have done this using editiontype:"textarea",editoptions:{rows:"1",cols"20"}. There are many other types you can define such as check boxes etc. More info on the supported types in the edit option can be found here.


  • Close the Add/Edit dialogs upon submit
By default the Add/Edit dialogs do not close after submitting the values. You have to press the close button yourself. It was required for me to close the dialog on submit. This was achieved as follows;


jQuery("#list2").jqGrid('navGrid',
'#pager2',
{edit:true,add:true,del:true,search:false}
,{width:500,closeAfterEdit: true},{width:500,closeAfterAdd:true}); 

As you can see within the code snippet, i have set the attribute closeAfterAdd  and closeAfterEdit  as true. This will close the Add/Edit dialog when the user clicks on the submit button.

Another thing to note here is that when you enable editing and adding, you need to specify to which URL the POST parameters on submit should be sent to. You do this as follows;


jQuery("#list2").jqGrid({
editurl: "editperson.php"
  });

Note that the data for add, edit and delete are sent to the same URL, so you need to identify the operation uniquely. jqGrid by default sets a parameter for us to distinguish this when sending the POST request to the URL we prodive in the editurl attribute. With PHP this is how we handle it;


 if(isset($_POST["oper"])){
 $operation = $_POST["oper"];
 
 switch($operation){
 case "del":
 break;
 
 case "add":
 break;
 
 case "edit":
 break;
 }
}

As you can see jqGrid by default sets a parameter named oper which defines which operation the data that is sent belongs to. So we can handle our logic according to whether its an add/edit or delete.

That is about it on use cases i have come across when using jqGrid. Some of the problems i faced were solved after some Google searching and putting together information gathered from various sources. Hence the purpose of this post was to put it altogether and provide a one-stop point for most of the common scenarios you might use jqGrid for.

Thank you for reading. Have a great day ahead.

Monday, March 25, 2013

Drupal to the rescue

My sister wanted to jump start her business in migration services related to Sri Lanka and wanted a website done. Contemplating on how to approach this within the time frame requested i was thinking along a few dimensions.


  1. The site should be built in 1-2 days time
  2. Must be cost effective to host and manage
  3. Ability for my sister to manage the content as time goes by
  4. Deliver with minimal effort
First i thought i would mash up a site coding it myself. As the cost effective (the elegant way of say cheap) way of hosing as i know is with PHP i wanted to initially do the coding myself. As i am primarily from a Java background and given my PHP skills were a bit rusty since the only time i really code in PHP is when i do freelancing related projects, i was thinking whether i could finish it on the given timeline.

This is when i thought in the line of CMSs (Content Management Systems). There are many options to chose from such as Drupal, Wordpress, Joomla. Wordpress byitself is not a fully fledged CMS per se, but it gives you the ability to administer and manage your content.I went ahead with Drupal mainly because i found an ebook that explained it in detail and i was able to figure it out within a couple of hours time. Im not saying the other two options are less competent, but in the end i believe you have to approach the solution with something you are comfortable with given the time frame and since this is not like a mission critical system i am aiming to develop there were no explicity trade-offs.

Drupal with its modular architecture made me feel at home since its concept is easily comprehensible. Also its intuitive administrative console allowed me to teach my sister how to manage the site in the future without my help since she would want to change the content as time goes on.

The main important elements with regards to Drupal was;

  • Regions
          Region is all about page layout in its simplest terms. If you are to hand code with html and css, you will probably use div elements to divide your page for instance to hold the header part, footer section, and main content section. With Drupal you have a multitude of themes to chose from. I would say first design your website deciding on how you want your elements to be aligned. I use Adobe Fireworks for my initial design purposes. Then depending on your page layout you need to select the Drupal theme that best fits your layout since all themes have a specific layout to place content.

  • Blocks
         A block is a particular element in which you group a set of content that you want to display. It can be a navigation menu, or you can even build your own custom block which would consist of a certain section you want to present. Also blocks give you the ability to be able to be displayed only in certain pages which i believe is such a great feature to give the flexibility to the user.

  • Content Types
         There are mainly two content types when you first start out. Stories and Pages. A Story typical is published on the home page and allows the users to comment. It is recommended to use this for news feed and content that is updated regularly. Pages are static content which is mostly what is required when building web pages. You can even stick it on the front page if required so it can act like a story in someway. The beauty of Drupal is that you can even define your own content types to match your requirements. With Views and CCK (Content Construction Kit), you can customize Drupal according to your needs.

This is the site i built for my sister in the end. Its not flashy or anything, but hey this is the best i could achieve with around 6 hours of work to get it up and running. I used the basic bartik theme that comes by default since i did not see the need to adapt any other. You can also change the look and field by injecting your own customer CSS file. That is another topic by itself.

CMSs save alot of time you would spend aligning your content and adapting and incorporating possible changes in the future. It allows you to manage your content, security and users with ease. Also it prevents your site from known attacks such as cross side scripting, sql injections etc which otherwise you would be handling by yourself or through a framework you would be using. Though it is not as exciting as hand coding yourself, for me, it was the right tool for the job in this given situation. After all you should work smart and not hard right ;)

Cheers all and thank you for reading. As always comments and criticisms are always welcome.



Saturday, February 23, 2013

DZone, your too sweet!!

And the wait is finally over, because today i just received my DZone Goody pack. Got a mail from the local postal office saying it was held at the respective post office and wanted me to come and collect it. I actually had no idea that it was the goody pack that arrived. My eyes simply lit up once i saw the box. WOW... could not wait until i got home to open it up.

And this is what i found;





Loved each item in the pack specially the t-shirt and the USB drive. Ofcourse the missles were pretty darn awesome too ;)..

Thank you so much DZone and this definitely is a push the right direction for me to continue contribution even more to your esteemed community.

Cheers


Saturday, January 12, 2013

Dev vs QA, should there really be a distinction?

We had our scrum of scrum meetings last Wednesday where all scrum masters meet up with our line manager to discuss issues, bottlenecks and success stories of our previous sprint. One issue highlighted was the fact that sometimes the QA(Quality assurance) personnel are clogged up with testing issues that are in the "Ready for test" stage. So one idea that came about was that given the fact that some developer resources are freed up during this period, that it would be better off putting up some of their time to test the issues.

My first reaction was "HELL NO!!". My primary concern was that a developer will never have the mindset of a QA personnel when it comes to testing since he/she would always have a stringent approach on how to view the application. Also there is a notion among a few that doing QA is so "feminine" (again this is what i have observed and would differ from the context where i am in). So i took a step back, took a deep breath, turned off my dev-mode and looked at the suggestion in a more holistic manner.

I'm a person who loves to acquire new knowledge, be it from reading books, journals or simple through other's experience (young or old). So for me this turned out to be a way i could grab some new knowledge. Specially about software testing and quality. Now i know writing unit tests count for something, yet that is at its name dictates a unit level testing. When it comes to quality assurance, that is a whole new beast in its own right. Now you have to think in the perspective of how users interact with the system and not in the way you wrote the code for the application to behave.

By learning the art of QA testing, i believe that it would enable me to write more quality code since i would have the user's mindset and approach when testing my own code before i push it in to version control. Also in scrum the notion is that we as a team commit to deliver a set of issues we plan for a particular sprint. Hence if i do development or testing doesn't really matter since either way i am contributing towards helping my team achieve our team's commitment. It should not be thought of as someone else's responsibility. It's the team's responsibility to deliver the planned work on time with quality.

But such a change of mindset does not happen overnight. Its not merely about you going and telling each developer that he/she has got to start testing issues from the next sprint. I believe of leading by example. It's a win-win situation since i gain an extra skill of quality assurance testing whilst the team will emancipate themselves from the notion of thinking testing is not a part of responsibility of a developer (Hopefully). Change needs time to sink-in, and you cant coerce anyone into embracing change, but you can influence them through your actions.

What if some people never gets on board with this idea? Well i would say its their loss and my gain since in this dynamic business context we live in, i strongly believe if you try to stay with the pack, you will always be where you are right now.

Break away from restrictive thinking, like Lion-O of thunder-cats tells his awesome sword to give him sight beyond sight, we should strive to look through the limitations and think in ways we usually would not and get our lazy bottoms out of the conservative thinking mode.

That my friends brings me to the end of my note for the day. I hope you enjoyed reading it. Please do leave by your comments, views, criticisms and thank you for taking the time to read through.



 

Monday, December 17, 2012

Checking out what is new with Servlet 3.0

With the JEE6 specification hitting the market, some major changes have taken place with respect to how you would approach developing applications in the enterprise application world. In this article i would be touching upon a few changes that were done with respect to web application development.

First things first, say good bye to the web.xml deployment descriptor (at least for parts of it). Well its not like it is deprecated, but with the rise of the usage of annotations and their usage, the new specification allows us to define our configuration using annotations, though some thing such as welcome file lists, context params etc will still need to go inside your web.xml . Annotations available for use are;


  • @WebServlet
  • @WebFilter
  • @WebInitParam
  • @WebListener
  • @MultipartConfig
In this article i would be checking out the @WebServilet and @WebFilter annotations. Let us see how we would usually map a servlet in the web.xml era;





    <servlet>
        <servlet-name>myservlet</servlet-name>
        <servlet-class>com.example.MyServlet</servlet-class>
    </servlet>
 
 <servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>/hello</url-pattern>
 </servlet-mapping>

With the Servlet 3.0 spec, now configuring a Servlet is as easy as annotating a class that extends HttpServlet. Lets see how that looks like;


@WebServlet("/student")
public class StudentServlet extends HttpServlet{

 /**
  * 
  */
 private static final long serialVersionUID = 2276157893425171437L;

 @Override
 protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
   throws ServletException, IOException {
  StringBuilder response = new StringBuilder(500);
  response.append("<html><body>").append("Registered Student : ").append(arg0.getParameter("txtName")).append("</body></html>");
  arg1.getOutputStream().write(response.toString().getBytes());
  arg1.getOutputStream().flush();
  arg1.getOutputStream().close();
 }
}

All you need is the @WebServlet annotation. In order for this to work, the class should reside either in the WEB-INF/classes folder or within a jar residing in the WEB-INF/lib folder. Next up lets see how we would configure a filter with annotations.


package com.blog.example.servlettest;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter("/student")
public class StudentFilter implements Filter{

 @Override
 public void destroy() {
 }

 @Override
 public void doFilter(ServletRequest arg0, ServletResponse arg1,
   FilterChain arg2) throws IOException, ServletException {
  
  if(arg0.getParameter("txtName")==null || arg0.getParameter("txtName").isEmpty())
  {
   arg1.getWriter().append("Invalid name supplied");
   arg1.getWriter().flush();
   arg1.getWriter().close(); 
  }
  else
  {
   arg2.doFilter(arg0, arg1);
  }
 }

 @Override
 public void init(FilterConfig arg0) throws ServletException {
  // TODO Auto-generated method stub
  
 }

}


Again very easy. Just a mere annotation to notify it as a filter. Note that here we implement the Filter interface. The value or the urlPatterns should be available. Using both is illegal as per the specification.

In the coming weeks i will cover the other new annotations available with JEE6 and wrap up with a comprehensive example using them together. If JEE6 will replace Spring framework or not is not a question by itself, but i believe we would be seeing some fierce competition between the two. The annotations vs xml debate is more or less resolved with people with preference for each holding their own grounds. I believe a little bit from both worlds would be beneficial for an application.

You can download and run a sample example which i have uploaded here. If you are using JBoss-AS7 all you need to do is run the application server on standalone mode and do a mvn package jboss-as:deploy and point the browser to http://localhost:{port}/servlet3.0.

That is it for today. Thank you for reading and if you have any comments or suggestions for improvement, please do leave by a comment.

Have a good day all!!

Thursday, December 13, 2012

Configuring syntax highlighting for your blog

Thought i would put up a quick reference guide on how to configure syntax highlighting for people trying to figure it out for the first time. It is not that hard anyways. Let me guide you step by step. First of all just before the close head (</head>) tag insert the following;


<style type='text/css'>
.dp-highlighter
{
 font-family: &quot;Consolas&quot;, &quot;Courier New&quot;, Courier, mono, serif;
 font-size: 12px;
 background-color: #E7E5DC;
 width: 99%;
 overflow: auto;
 margin: 18px 0 18px 0 !important;
 padding-top: 1px; /* adds a little border on top when controls are hidden */
}

/* clear styles */
.dp-highlighter ol,
.dp-highlighter ol li,
.dp-highlighter ol li span 
{
 margin: 0;
 padding: 0;
 border: none;
}

.dp-highlighter a,
.dp-highlighter a:hover
{
 background: none;
 border: none;
 padding: 0;
 margin: 0;
}

.dp-highlighter .bar
{
 padding-left: 45px;
}

.dp-highlighter.collapsed .bar,
.dp-highlighter.nogutter .bar
{
 padding-left: 0px;
}

.dp-highlighter ol
{
 list-style: decimal; /* for ie */
 background-color: #fff;
 margin: 0px 0px 1px 45px !important; /* 1px bottom margin seems to fix occasional Firefox scrolling */
 padding: 0px;
 color: #5C5C5C;
}

.dp-highlighter.nogutter ol,
.dp-highlighter.nogutter ol li
{
 list-style: none !important;
 margin-left: 0px !important;
}

.dp-highlighter ol li,
.dp-highlighter .columns div
{
 list-style: decimal-leading-zero; /* better look for others, override cascade from OL */
 list-style-position: outside !important;
 border-left: 3px solid #6CE26C;
 background-color: #F8F8F8;
 color: #5C5C5C;
 padding: 0 3px 0 10px !important;
 margin: 0 !important;
 line-height: 14px;
}

.dp-highlighter.nogutter ol li,
.dp-highlighter.nogutter .columns div
{
 border: 0;
}

.dp-highlighter .columns
{
 background-color: #F8F8F8;
 color: gray;
 overflow: hidden;
 width: 100%;
}

.dp-highlighter .columns div
{
 padding-bottom: 5px;
}

.dp-highlighter ol li.alt
{
 background-color: #FFF;
 color: inherit;
}

.dp-highlighter ol li span
{
 color: black;
 background-color: inherit;
}

/* Adjust some properties when collapsed */

.dp-highlighter.collapsed ol
{
 margin: 0px;
}

.dp-highlighter.collapsed ol li
{
 display: none;
}

/* Additional modifications when in print-view */

.dp-highlighter.printing
{
 border: none;
}

.dp-highlighter.printing .tools
{
 display: none !important;
}

.dp-highlighter.printing li
{
 display: list-item !important;
}

/* Styles for the tools */

.dp-highlighter .tools
{
 padding: 3px 8px 3px 10px;
 font: 9px Verdana, Geneva, Arial, Helvetica, sans-serif;
 color: silver;
 background-color: #f8f8f8;
 padding-bottom: 10px;
 border-left: 3px solid #6CE26C;
}

.dp-highlighter.nogutter .tools
{
 border-left: 0;
}

.dp-highlighter.collapsed .tools
{
 border-bottom: 0;
}

.dp-highlighter .tools a
{
 font-size: 9px;
 color: #a0a0a0;
 background-color: inherit;
 text-decoration: none;
 margin-right: 10px;
}

.dp-highlighter .tools a:hover
{
 color: red;
 background-color: inherit;
 text-decoration: underline;
}

/* About dialog styles */

.dp-about { background-color: #fff; color: #333; margin: 0px; padding: 0px; }
.dp-about table { width: 100%; height: 100%; font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; }
.dp-about td { padding: 10px; vertical-align: top; }
.dp-about .copy { border-bottom: 1px solid #ACA899; height: 95%; }
.dp-about .title { color: red; background-color: inherit; font-weight: bold; }
.dp-about .para { margin: 0 0 4px 0; }
.dp-about .footer { background-color: #ECEADB; color: #333; border-top: 1px solid #fff; text-align: right; }
.dp-about .close { font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; background-color: #ECEADB; color: #333; width: 60px; height: 22px; }

/* Language specific styles */

.dp-highlighter .comment, .dp-highlighter .comments { color: #008200; background-color: inherit; }
.dp-highlighter .string { color: blue; background-color: inherit; }
.dp-highlighter .keyword { color: #069; font-weight: bold; background-color: inherit; }
.dp-highlighter .preprocessor { color: gray; background-color: inherit; }

</style>
<!-- Add-in CSS for syntax highlighting -->  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shCore.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCpp.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCSharp.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCss.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushDelphi.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJava.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJScript.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPhp.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPython.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushRuby.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushSql.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushVb.js' type='text/javascript'/>  
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushXml.js' type='text/javascript'/>  

Next up you need to initialize a few javascript functions to make the syntax highlighting work. Therefore insert the following snippet just before the close body (</body>) tag;


<!-- Add-in Script for syntax highlighting -->  
<script language='javascript'>  
dp.SyntaxHighlighter.BloggerMode();  
dp.SyntaxHighlighter.HighlightAll(&#39;code&#39;);  
</script> 
That is it. Afterwards whenever you want to post a code in one of your posts just have your code within pre tag blocks as follows;

<pre class="java" name="code">
//your code goes here
</pre>


Note that before pasting code within the block you need use an html encoder. I usually prefer this html encoder which is an online tool. Also there are many options you can use within the class attribute. More information on the brushes available can be found here.

Happy coding people!!