Friday, October 29, 2010

Static Import

Hi ,

     While learning JUnit 4 , I came across  Static Imports of JAVA quite useful feature. Its syntax looks like

import static org.junit.Assert.*; 

    This allows the importing class to access static public members of Assert package without class name.

fail("Not yet implemented"); 

          The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.


       The static import can also import individual static member of class .

package com.blogspot.pravingole.demo;

import static java.lang.Math.PI;
import static java.lang.Math.*;

public class TestStaticImport {

 public static void main(String[] args) {
  float r = 5.1f;
  System.out.println("Circle area is : "+ (PI*r*r));
 }
}


 for more details click here

P.S. :  This static import don't  become member of importing class, so subclass can't access them without import.

Friday, October 22, 2010

What is JAR ,WAR and EAR . How to build them using ANT

HI,
In JEE, application modules are packaged as EAR, JAR and WAR based on their functionality.Each type of file (.jar, .war, .ear) is processed uniquely by application servers. I am just collecting the stuff for my reference.  :)

JAR:
       Jar file (file with a .jar extension) is the normal Java Application archive and intended to hold generic libraries of Java classes, resources, auxiliary files, etc.This can be added in classpath for compilation and to run java program. Generally in web applications we keep these files in lib directory of the application.
Ex: ojdbc14.jar – This contains all the classes to connect the oracle database
Servlet-api.jar – contains servlet related classes

Ant Task for JAR

 
for ant task details click here. 

WAR:
           Web modules which contains Servlet class files,JSP Files,supporting files, GIF and HTML files are packaged as JAR file with .war( web archive) extension. War files (files with a .war extension) are intended to contain complete Web applications. In this context, a Web application is defined as a single group of files, classes, resources, .jar files that can be packaged and accessed as one servlet context.

 Ant Task for WAR


   
  
  
  
  
  
 

for ant task details click here.


EAR:
        All above files(.jar and .war) are packaged as JAR file with .ear ( enterprise archive) extension and deployed into Application Server.  Ear files (files with a .ear extension) are intended to contain complete enterprise applications. In this context, an enterprise application is defined as a collection of .jar files, resources, classes, and multiple Web application.

Ant Task for EAR



      
    
 
for ant task details click here.

 Thanks.

Thursday, October 14, 2010

My favrite Vim commands

  • search - /
  • ignore case search - /\c
  • search for pattern
 /word1\(.*word2\)\@=
/.*Word1.*word2  
  • replace :   %s/word1/replaceword/gc 
  • to append at end of line  :   %s/$/word/gc
  • to append at begin of line  :  %s/^/word/gc
  • to append newline character at end of line  :  %s/$/\r/gc
  • to remove new line character  :  %s/\r//gc
  • to enable Hex editing :%!xxd
  • to go back in binary mode :%!xxd -r
  • To remove all lines not having getDisplayName

  1. replace othr lines with blank
    :%s#^\(.*getDisplayName.*\)\@!.*$##gc

    2.  remove blank lines 
        :v/\S/d
        3.  to remove all lines having getDisplayName

                  just remove @! FROM SEARCH QUERY

      • :vsp      " Vertically split the window

      • " Changing Case use with
      •  lowercase line guu
      •  uppercase line gUU
      •  lowercase line Vu
      •  uppercase line VU
      •  flip case line g~~
      •  Upper Case Word vEU
      •  Flip Case Word vE~
      •  lowercase entire file ggguG
      • buffer commands
      :bn (next buffer),
      :bp (previous buffer)
      :buffers (list open buffers)
      :b (open buffer n)
      :bd (delete buffer).
      :e will just open into a new buffer
      • Grep Command
        • :vim[grep][!] /{pattern}/[g][j] {file} ... 
        • :lvim /\<\(house\|home\)\>/gj *.txt 
        • :lw list selected buffers in grep

      Apache ANT : Learning path

      Hi folks,

             This is not another blog post about using famous build tool Apache Ant. In this post , I am going to list the resources that i found useful while learning ANT.


      Thanks

      Tuesday, October 12, 2010

      How to setup JAVA_HOME environment variable on Windows 7

      Please follow the instructions to set up JAVA_HOME environment variable in your Windows 7 PC. First find out the installation folder of Java development kit (JDK) in your machine.Let's assume it is installed in the folder "C:/jdk6"

      1. Go to Control Panel 
      2. If Control Panel is viewed by Category then select "System & Security " and here select "System" link,otherwise System link is directly inside Control Panel.
      3. On System screen select "Advanced system setting" link in left panel. This will open "System Properties" dialog box with "Advanced" tab preselcted.
      4. Click the Environment Variables button
      5. Under System Variable, click New
      6. Enter the variable name as JAVA_HOME
      7. Enter the variable value as the install path for the Development Kit.  (Eg. C:/jdk6)
      8. Click OK
      9. Click Apply Changes

      Tuesday, October 5, 2010

      Getting executing directory for Servlet web application

      Hi ,

      Some time we need to know the directory in which our web-app is running.In servlet this can be done using following code.

      String webAppPath = req.getSession().getServletContext().getRealPath("//");
      
      

      This is useful when you need to generate some file for other application.

      Tuesday, August 24, 2010

      Sql query to list rowcount for all table in schema

      Some times we face problems that require to find no. of rows in all tables in schema .

      Following are sql queries that list table name and their row count,

      For Microsoft SQL Server:
      SELECT distinct(t.name) AS table_name,i.rows
      FROM sys.tables AS t
      INNER JOIN sys.sysindexes AS i ON t.object_id = i.id
       

      For Oracle
      select table_name,num_rows counter
      from dba_tables 
      where owner = 'XXX'
      order by table_name;
      

      Tuesday, August 17, 2010

      Hotdeployment of properties file

      Hi,
           In last month we faced another Internationalization problem with properties file hot deployment. Hot deployment is commonly used to deploy updates for live web-application.
          The problem was when we hot deployed web-app the messages of  new screen are not pulled from corresponding properties file. ResourceBundle unable to find new keys ,leading to all labels are showing keys. But when we restarted server the messages are shown perfectly.
           After analyzing we realized that the properties file are not reloaded after hot deployment while all other classes are reloaded properly.This is because of ResourceBundle class, it caches properties file on read and returns retrieve strings from cache. When the class redeployed this cache remain untouched.
           The solution to this to clear this cache. we added static block in our class to clear ResourceBundle cache.
      following is the code we currently using :

      Monday, May 31, 2010

      Remove lines from log not matching search criteria

      Hi folks,

          In last week I am walking through our application log file for performance enhancement. Where I was continuously needed to find some method logs (like time consumed etc ). The log file i retrieved from test server is almost 80 MB . As I was only interested in to the retrieval of particular method logs ,to analyse the trend. So what I needed was to remove all other lines from log file which don't contain my method. I have done this using my favorite editor VIM . For that i Just needed  to fire 2 commands. I found this is very easy and useful.let's see,

      Suppose I have to find all logs for method getUiControls the commands will be
      • Search The string 'getUiControls' in vim
              /\c^.*getUiControls

                here you can confirm whether the highlighted lines shows your selection criteria (For Advanced VI          users : you can build any search queries to get intended selection)

      • Delete all lines not selected in previous search ,This command must follow 1st .
                  :v//d



      Thanks

      Saturday, May 1, 2010

      InternationalizationJava JEE Application

      In last few months I have been working on I18N. During this project activity I realized need to document the I18N experience. Most of the time programs are not written considering internationalized distribution. So we get in to the task of defining what can be internationalized (Scope). There is good link which provides checklist for this task

      http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html

      There are lots of tutorials available on net for implementing I18N for java app. listing few of them that I referred,

      For I18N of java Application

      http://java.sun.com/docs/books/tutorial/i18n/index.html

      For I18N of JSF application

      http://www.javabeat.net/tips/102-implement-internationalization-and-localizati.html

      Eclipse provide plug-in to externalize messages from java application have a look at this url

      http://www.eclipse.org/articles/Article-Internationalization/how2I18n.html

      Sorting String

      http://java.sun.com/docs/books/tutorial/i18n/text/collationintro.html

      http://w3hjava.com/?p=3

      For handling number formatting

      http://www.ibm.com/developerworks/java/library/j-numberformat/

      In rest of the post I will be writing about the problems we faced while doing Internationalization.

      • Alphabets are shown incorrectly in Japanese, German language messages.

      Though the properties files contain proper translation the messages shown incorrectly with BOM characters. This problem occurred because Java compiler and other Java tools can only process files which contain ASCII character-set. So we need to convert our properties files in non English languages so that the Non-ASCII characters should be converted in to ASCII “\u####“ notation. For that java provides native2ascii Converter, please refer the link

      http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/native2ascii.html

      In our project we used native2ascii ant task for this purpose

      http://ant.apache.org/manual/OptionalTasks/native2ascii.html

      • {0} placeholders in parametrized messages are not replaced in French language .

      This problem caused because of MessageFormat Class that we used to replace placeholders . This class treat single quote (‘) as delimiter. In French some word contain single quote this lead to the problem. For us we replaced single quote (‘) with double single quote (‘’) for parametrized messages before passing them to message format.

      • Resource bundle returns English messages though other language locale is configured.

      We have all our properties as per locale and converted using native2ascii tool still our other language messages are messed up. This problem was caused because of properties file. While adding new messages we used different editors like notepad, editplus, etc. These editors add some header at the beginning of file so that they can identify file encoding while opening it in future. This cause problem as some of our properties file contains messages from first line. So the resource bundle was unable to load those properties. So we added some commented messages at the beginning of properties file and our problem was solved.

      • Storing and sorting strings in database

      For this we need to change the column type from varchar to nvarchar. Also sorting of strings requires the sorting algorithm to be aware of the language of the strings being sorted. Since sorting is done differently in different languages. For this reason, even the DB needs to be informed of the collation setting so that ORDER BY queries return data in the correct order.

      http://msdn.microsoft.com/en-us/library/aa174903%28SQL.80%29.aspx