Wednesday, May 5, 2010

How to use PMD in Eclipse ?

Before launching Eclipse make sure you have enough memory for PMD.(If not know how to install PMD Eclipse plugin then follow this link How to install PMD in Eclipse ? ) This is particularly important when analyzing large projects. In these situations PMD tends to be memory-hungry. Hence, make sure to start with as much memory as you can afford, for example 512M (eclipse.exe -vmargs -Xmx512M)
    1. Launch Eclipse
    2. Let you have previously created a Java Project PMDDemo, Otherwise you need to create a new project in your workspace File | New | Project...
    3. In the Package Explorer right-click on PMDDemo project and select New | Class
    4. In the following dialog enter the class name as DemoPMD and click Finish
    5. A new class DemoPMD is created in project's default package. Paste the following code into the new class:

      public class DemoOMD {
       
        private static final String bAD = "BAD";
        
          public void BadMethodName() {
                  System.out.println("Hello PMD World!");
           }
        
           public String thisIsCutAndPaste(String first, int second) {
                  System.out.println("New world");
                  return "New world";
           }
        }
    6. Similarly, create a second class DemoPMDThread and paste the following code:

    public class DemoPMDThread extends Thread {

       public DemoPMDThread(String str) {
             super(str);
      }

     public void run() {
             for (int i = 0; i < 100; i++) {
                     System.out.println(i + " " + getName());
                     try {
                             sleep((long) (Math.random() * 1000));
                     } catch (InterruptedException e) {

                     }
             }
             System.out.println("DONE! " + getName());
      }

      public void WRITE_SOMETHING(String INPUT_PARAMETER) {
             System.out.println(INPUT_PARAMETER);
      }

      public static void main(String[] args) {
             new Yang("Good").start();
             new Yang("Bad").start();
      }

      public String thisIsCutAndPaste(String pFirst, int pSecond) {
             System.out.println("New world");
             return "New world";
      }
    }


    7. In the Package Explorer right-click on QA Project and select PMD | Check Code With PMD
       
      Wait for PMD to scan DemoPMD and DemoPMDThread
      If PMD Violations view is not open navigate to Window | Show View | Other... and select PMD | PMD Violations, you'll get following view...


      In the PMD Violations view notice a list of 27 violations. In large projects this list could easily grow up to several thousand. This is one of the reasons PMD allows violations be filtered by priority. Priority is a configurable attribute of a PMD rule. PMD assigns priorities from 1 to 5 and each priority is represented by a colored square at the top-right corner of the view. These little squares are actually clickable on-off switches used to control the visibility of the violations they represent.
      The results table itself is well laid out and most columns are sortable. It is also possible to get more detail on a violation by simply right-clicking it and selecting Show Details. PMD pops-up a dialog with information such as rule name, implementation class, message, description and an example. This feature can be helpful when trying to makes sense of a new rule or letting inhouse developers know about a particular company rule or coding convention.



        Finally, in the PMD Violations table it is possible to add review annotations to the source where the violation occurred. This can be an effective way to mark target files for further review. Just right-click on any violation in the list and select Mark review. PMD will insert a review annotation to the Java source right above the violation line itself. The review annotation should look like this:

        // @PMD:REVIEWED:MethodNamingConventions: by Sanjay Singh on 5/08/10 5:04 PM

        Review annotations can be removed anytime by right-clicking QA Project and selecting PMD | Clear violations reviews. Similarly, PMD Violations can be cleaned-up by right-clicking QA Project and selecting PMD | Clear PMD Violations..

        How to install PMD in Eclipse ?

        The easiest way to install PMD is by using the remote update site.
        Users behind firewalls should check proxy settings before going any further.
        If these settings are misconfigured the updater will not work. PMD also
        supplies a zip file for manual installation. Download the file and follow
        the readme. Demonstrated below is installing PMD via the Eclipse Software Updater.

        1. Launch Eclipse.
        2. Navigate to Help | Software Updates | Find and Install...
        3. Select "Search for new features to install" and click Next
        4. Click New Remote Site...
        5. Enter a name (PMD) and the URL http://pmd.sourceforge.net/eclipse

        http://www.eclipsezone.com/articles/pmd/images/remote_site.png

        1. In Sites to include in search check PMD and click Finish
        2. In the Search Results dialog check PMD for Eclipse 3 3.1.0 and click Next

        http://www.eclipsezone.com/articles/pmd/images/install_pmd_remote.png

        1. Accept the terms of the license agreements and click Next
        2. Click Finish.
        3. Wait for Eclipse to download the required jar files, then click Install
        4. Restart the workbench. This will load the PMD plugin.
        5. Navigate to Window | Show View | Other...
        6. Select PMD | PMD Violations
        7. PMD Violations view should appear at the bottom pane

         

        Sunday, May 2, 2010

        How do I make my web application be the Tomcat default application ?

        You have created and tested a first web application (traditionally called "besthowtodo"), users can access it via the URL "http://besthowtodo.company.com/besthowtodo". You are very proud and satisfied. But now, how do you change the setup, so that "besthowtodobesthowtodo" gets called when the user enters the URL "http://besthowtodo.company.com" ?
        The pages and code of your "besthowtodo" application currently reside in (CATALINA_BASE)/webapps/besthowtodo/. In a standard Tomcat installation, you will notice that under the same directory (CATALINA_BASE)/webapps/, there is a directory called ROOT (the capitals are important, even under Windows). That is the residence of the current Tomcat default application, the one that is called right now when a user calls up "http://besthowtodo.company.com[:portNumber]". The trick is to put your application in it's place.
        First stop Tomcat.
        Then before you replace the current default application, it may be a good idea to make a copy of it somewhere else.
        Then delete everything under the ROOT directory, and move everything that was previously under the (CATALINA_BASE)/webapps/besthowtodo/ directory, toward this (CATALINA_BASE)/webapps/ROOT directory. In other words, what was previously .../besthowtodo/WEB-INF should now be .../ROOT/WEB-INF (and not .../ROOT/besthowtodo/WEB-INF).
        Just by doing this, you have already made you webapp into the Tomcat default webapp.
        One step is left : you also need to have, within your application, a default servlet. This, you do by means of an appropriate url-mapping in the WEB-INF/web.xml configuration file of your application. Make sure you have something like this in that file :
        <servlet>
        <servlet-name>Best HowToDO Servlet</servlet-name>
        <servlet-class>com.besthowtodo.tutorials.Servlet.Number1</servlet-class>
        </servlet>
        
        <servlet-mapping>
        <servlet-name>Best HowToDO Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
        </servlet-mapping>

        Restart Tomcat and you're done.
        Call up "http://besthowtodo.company.com/" and enjoy.
        Addendum 1 : If you are deploying your application as a war file..
        The above instructions relate to the situation where you are "manually" deploying your application as a directory-and-files structure under the /webapps directory. If instead you are using the "war" method to deploy your application, the principle is about the same :
        - delete the ROOT directory
        - name your war file "ROOT.war" (capitals mandatory)
        - drop the ROOT.war file directly in the /webapps directory.
        Tomcat will automatically deploy it.