Friday, May 15, 2009

Autoboxing and Unboxing in java example

Java is an Object Oriented programming language. We have to deal with primitive types and objects together.

Here autoboxing comes to play the role. It wraps the primitive data type into a object. There are wrapper classes for it.

For Example : -

 public class FormatPrint {

     public static void main(String[] args) {

       System.out.printf("There is only %d thing.", 1);

     }

   }


The signature of the printf() is

printf(String format, Object... args)

But, inthe above example i have printed a integer value. This is called as autoboxing. This can be used in Generics,too.

Here is java normal java generic example : -

 import java.util.ArrayList;

   public class ManualBoxing {

     public static void main(String[] args) {

        ArrayList list = new ArrayList();

        for(int i = 0; i < 10; i++){

            list.add(new Integer(i));

        }

     }

   }

But we don't need to "new Integer(i)". We can use autoboxing and simply the code as follow.

import java.util.ArrayList;

    public class Autoboxing {

      public static void main(String[] args) {

          ArrayList<Integer> list = new ArrayList<Integer>();

          for(int i = 0; i < 10; i++){

             list.add(i);

          }

      }

    }

For further details : -

http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

Wednesday, May 13, 2009

Delete non empty directory in java

import java.io.File;

class DeleteDir {

public static void main(String args[]) {

deleteDirectory(new File(args[0]));

}

static public boolean deleteDirectory(File path) {

if( path.exists() ) {

File[] files = path.listFiles();

for(int i=0; i<files.length; i++) {

if(files[i].isDirectory()) {

deleteDirectory(files[i]);

}

else {

files[i].delete();

}

}

}

return( path.delete() );

}

}

Monday, April 27, 2009

How to log records to log file? Using log4j

This piece of code will explain the usage of the log4j. In java development, you may have faced for error logging situation. Here is the solution.

Make a log configuration file with follwoing details in your project. Here log configuration file name is "logconfig.txt"

log4j.logger.debugl=INFO,debugl

log4j.appender.debugl.layout=org.apache.log4j.PatternLayout

log4j.appender.debugl=org.apache.log4j.DailyRollingFileAppender

log4j.appender.debugl.File=Log//data_usage_log.txt

log4j.appender.debugl.DatePattern='.'yyyy-MM

log4j.appender.debugl.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}|%m %n


log4j.appender.debugl.File=c:/data_usage_log.txt

There must be a file called "data_usage_log.txt" in C directory. Then it will upgrade with logs.

Add the following methods to your class constructors.

Logger preLogger

constructor(){    


PropertyConfigurator.configure("logconfig.txt");

preLogger = Logger.getLogger("debugl");

}


you can add your error report as follows
preLogger.info(“Logging ”);

Sunday, April 19, 2009

Combine two pdf files using itext

Here is a simple way to combine two pdf files into one.
I have used iText-2.1.5.jar

This is the sample code

import java.io.FileOutputStream;

import com.lowagie.text.pdf.PdfCopyFields;

import com.lowagie.text.pdf.PdfReader;

public class PDFTest {

public static void main(String[] args) throws Exception {

System.out.println("Concatenate Two PDF");

PdfReader reader1 = new PdfReader("D:/upload/first.pdf");

PdfReader reader2 = new PdfReader("D:/upload/second.pdf");

PdfCopyFields copy = new PdfCopyFields(new FileOutputStream("D:/upload/concatenatedPDF.pdf"));

copy.addDocument(reader1);

copy.addDocument(reader2);

copy.close();

}

}

Thursday, April 16, 2009

Formatting text on Textarea/ Remove unwanted chars in Textarea

function escapeVal(textarea,replaceWith){

textarea.value = escape(textarea.value);

for(i=0; i<textarea.value.length; i++){

if(textarea.value.indexOf("%0D%0A") > -1){

textarea.value=textarea.value.replace("%0D%0A",replaceWith)

}

else if(textarea.value.indexOf("%0A") > -1){

textarea.value=textarea.value.replace("%0A",replaceWith)

}

else if(textarea.value.indexOf("%0D") > -1){

textarea.value=textarea.value.replace("%0D",replaceWith)

}

}

return unescape(textarea.value);

}


If you need to remove un wanted chars in a textarea you can use the above java script.

parameters
----------

set the textarea object as the 1st parameter and 2nd paramter is the replaceable char with unwanted ones.

Monday, April 6, 2009

Direct file access in weblogic server

In weblogic, you can keep a directory for all images,pdfs,....

how to do ...

------------

you have to create a "weblogic.xml" file in WEB-INF in your application. This weblogic.xml should be like this.

<?xml version="1.0" encoding="UTF-8"?>

<weblogic-web-app>

<virtual-directory-mapping>

<local-path>/apps/test</local-path>

<url-pattern>/images/*</url-pattern>

</virtual-directory-mapping>

</weblogic-web-app>



suppose you have a directory called as images in path /apps/test/images. You can give users to direct access those files as follows.

http://localhost/AppName/images/test.jpeg;


This is called as Virtual directory mapping in weblogic.

Connection pool/jndi

In normal java development, we use jdbc database connections. It takes several mili seconds to make connection between database server and your application.

In connection pool concept, there are several connections are built with the application server startup. This make web applications more fast.

jndi
------
As i mentioned, it makes several connection at the app sever startup. Developers can access those connections using jndi. There is a jndi for a conncetion pool. All the jndi are in the java jvm.

Developers has to look up for the jndi names and use the database connection.

Suppose your connection pool is "dbtest" . And you have mapped this pool with jndi name "jdbc/dbtest".

your lookup mathod will be like this in netbeans.

private DataSource getDbtest() throws NamingException {
Context c = new InitialContext();
return (DataSource) c.lookup("java:comp/env/dbtest");
}


The above method returns a sql data source. You can get a connection using it.