Monday, April 25, 2011

Writing an Ant Task

Many are having a need to write their own ant task, just like I had. I wanted to search some strings in a set of files and write the result into some destination files. Just listing the steps to achieve the same.

1. I work with Eclipse. Created a Java project and created the below build.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="Search" basedir="." default="jar">

    <target name="clean" description="Delete all generated files">
        <delete dir="classes"/>
        <delete file="search.jar"/>
    </target>

    <target name="compile" description="Compiles the Task">
        <javac srcdir="src" destdir="classes"/>
    </target>

    <target name="jar" description="JARs the Task" depends="compile" >
        <jar destfile="search.jar" basedir="classes"/>
    </target>
  
</project>

2. Wrote a Java code which extends Task class. I wanted to have sourceFile, destFile and searchString as parameters and hence having three  instance variables as sourceFileString, destFileString and searchString.  On execute() method functionality has been implemented.

import org.apache.tools.ant.Task;
import java.io.*;

public class SearchTask extends Task {
   
    private String sourceFileString = null;
    private String destFileString = null;
    private String searchString = null;
   
    public void setSourceFile(String sourceFile)
    {   
        this.sourceFileString = sourceFile;
        System.out.println(" Source file set to "+sourceFile);
    }
   
    public void setDestFile(String destFile)
    {
        this.destFileString = destFile;
        System.out.println(" dest file set to "+destFile);
    }
   
    public void setSearchString(String searchString)
    {
        this.searchString = searchString;
        System.out.println(" search string set to "+searchString);
    }
   
    public void execute()
    {
        System.out.println(" Execute called ");
        File sourceFile = new File(sourceFileString);
        if(sourceFile.isDirectory())
        {
            File[] array = sourceFile.listFiles();
            for(int i=0;i<array.length;i++)
            {
                File file = array[i];
                search(file);
            }
        }
        else
        {
            search(sourceFile);
        }
    }
   
    private void search(File sourceFile)
    {
        System.out.println(" searching starts ");
       
        BufferedReader reader = null;
        BufferedWriter writer = null;
       
        try
        {
            reader = new BufferedReader(new FileReader(sourceFile));
            writer = new BufferedWriter(new FileWriter(destFileString));
            String line  = null;
           
            while( (line = reader.readLine()) != null)
            {
                System.out.println(" line =="+line+" searchString "+searchString);
                if(line.indexOf(searchString) > -1)
                {
                    writer.write(line);
                    writer.newLine();
                }
            }
        }
        catch(Exception ee)
        {
            ee.printStackTrace();
        }
        finally
        {
            try
            {
                writer.close();
                reader.close();
            }
            catch(Exception ee)
            {
                ee.printStackTrace();
            }
        }
    }
}


3. Executed jar task and created search.jar. Then added the below task.

<target name="search" depends="jar" description="searching the file">
        <taskdef name="search" classname="SearchTask" classpath="search.jar"/>
        <search sourceFile="e:\\searchFile.txt" destFile="e:\\searchresult.txt" searchString="cool" >
        </search>

</target>

Friday, April 22, 2011

My Passport renewal Experience

Had my passport expired and wanted to renew it. Thought of writing it here so that it will help some people who are yet to renew their passport.

http://passport.gov.in/ is the website where we shall get all related details.

1. We can download the application form from the above site and get appointment to visit the passport office.
2. To renew we need two solid address proof.  Old passport can be considered as one address proof when there is no address change. Otherwise bank passbook can be used. Ration card is not accepted as address proof.  List of documents can be found in the below link.
http://passport.gov.in/cpv/checklist.htm
3. When I entered Madurai passport office, there is a long queue for certification verification. They rejected my application just because I wrote one reference address in pen. I went to ex-servicemen and filled-in a new application which had some other date of appointment. Very frustrated and left that office.
4. Second time I went there and since I had ration card as address proof they rejected my application.
5. My native is in Aruppukottai which falls in Virudhunagar district. So I went to Virudhunagar collectorate and gave 200 to get D.D and filled-in application form. This time my application entered into the system.
6. I got police verification in two weeks and they asked me to come to the police station. I went to Aruppukottai police station and waited for 3 hours to see superindent of police. He asked me two or three questions and its over.
7. I got a letter from madurai passport office saying that I need to submit the damage sworn affidavit. In fact my old passport is not at all damaged and I went there and submitted. Passport office asked me some basic questions.
8. Finally in three weeks I got the passport.

Here comes the interesting part, following are the differences in 10 years.

1. Process is very fast. In one week you can get a tatkal passport and in three weeks you can get normal passport if you have all the documents.
2. Some passport officers are very kind in nature and they are really helping the needy people.
3. Having ex-serviceman booth in passport office is really a good idea.
4. Still some passport officers are not people friendly. This is not changed even in the last 10 years.

Thursday, April 21, 2011

My UDP based JMS Solution

Just wondered and searched udp based JMS solutions for high performance need. Could not find any and hence decided to write one through java. Till now able to create Queues and made them communicate each other. My design has,

1.  A communication provider maintains a list of Queues and Topics. Mainly list of Queue Initiators and on subscription it will have Queue Clients too. It will have a list of Topic subscribers too.
2. A Queue initiator that communicates with ConnectionProvider to register the queue name and its ip/port details.
3. A Queue client that subscribes with ConnectionProvider and get the details of Queue initiator.
4. Queue initiator and Queue client can communicate each other with high performance. On closure, commuincation provider can be updated.
5. Communication Provider , Queue initiator , Queue clients are distributed. They can run in three different jvm.
6. Currently all objects are extending Thread object. But it can be enhanced by having one thread per jvm that receives all packets and then notifies clients.

Have made a working version that supports create, delete, subscribe and unsubscribe. On a i3 CPU 64 bit, 3 GB, 2.27 GHz, 2.26 GHz machine, it is able to send/receive 6700 udp packets per second. Recently have made it to support topic also.