Wednesday, April 15, 2009

How to avoid annoying NullPointerException

Hi All,

Today, I want to discuss how can we avoid this "NullPointerException". This NullPointerException is very annoying and we have to use too much try/catch statements to control the behavior of this Exception.

Normally, when defining any variable in the methods we have to initialize them and we initialize them to Null, if it is an object type variable.

I am writing one phesdocode to represent this behavior.

public void hello(){

Aobject a = null;

// do some operations

if(){

// do something

a = some value assigned to it
} else {

// on else condition

a = new Aobject();
}

return a;
}

In this way when we use this method in any combinition, we'll never have NullPointerException rather if there is no value u will get empty object.

It will improve the performance what I believe so. If someone disagree with this, please do comment and give us the best way to avoid this NullPointerException.

Regards

Sunday, April 12, 2009

Usage of Signleton and Static

Hi All,

In my professional life I have seen people who use singleton and static classes with such a lousiness and without having any clear concepts about it.

So I thought I should comeup with the ideas which could become the guidelines to the developer(s) about using them properly and efficiently.

When you want to do some action or you need to develop some utitlity classes, always try to use "static" because it will not create any instance and no memory allocation will happen.

When you want to develop application which need single instance, or you want to initialize some data which can be used over the whole life cycle of the application, then use "Singleton".

Normally, the "Entry Point" of the application are made "Singleton" as its instance remain the same over the lifecycle of the application.

One more example is when we need to cache some data, we can use both static and singleton. However, the best way is to use "Singleton" as we can control the instance.

And about memory consumption, yes ofcourse static is faster than singleton but the difference is so minor which could be easily ignored.

Cheers and regards



Thursday, April 9, 2009

Manipulating Microsoft Office 2007 with java - Creating "docx"

Hi All,

As we know that Microsoft Office suit 2007 is based on openXML formate. It is not so common how we can use it with Apache POI.


Apache POI is still very much exciting api used to manipulate Microsoft Office suit (<2007).>

I will post series of examples in my different posts, so that users could find easy to use openXML4J.

You can download the openxml4j from http://openxml4j.org/

Download all jar files and put into the classpath of your project.

Apart from that u also need to download and use "dom4j.jar"

Now I will give you one small example which will guide u how to create the Office 2007 "docx" file by using jaya


import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.openxml4j.opc.Package;
import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackagePartName;
import org.openxml4j.opc.PackageRelationshipTypes;
import org.openxml4j.opc.PackagingURIHelper;
import org.openxml4j.opc.StreamHelper;
import org.openxml4j.opc.TargetMode;

/**
* @author Anees
*
*/
public class CreatingSimpleDocument {

/**
* @param args
*/
public static void main(String[] args) {
try{
File outputDocument = new File("c:/icba10/simple.docx");
// Create package
Package pkg = Package.create(outputDocument);
// -------------------------------------- Creating parts -----------------------------
/*
* main part
*/
PackagePartName corePartName = PackagingURIHelper.createPartName("/word/document.xml");
// create main part relationship
pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
// Create main document part
PackagePart corePart =
pkg.createPart(corePartName,
"application/vnd.openxmlformats- officedocument.wordprocessingml.document.main+xml");
// Create main document part contents
Document doc = DocumentHelper.createDocument();
Namespace nsWordprocessinML = new Namespace("w",
"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
Element elDocument = doc.addElement(new QName("document",
nsWordprocessinML));
Element elBody = elDocument.addElement(new QName("body",
nsWordprocessinML));
Element elParagraph = elBody.addElement(new QName("p",
nsWordprocessinML));
Element elRun = elParagraph
.addElement(new QName("r", nsWordprocessinML));
Element elText = elRun.addElement(new QName("t", nsWordprocessinML));
elText.setText("Hello! This is Anees-ur-Rehman who wanna fly high.....!");

// Save the XML structure into the part
StreamHelper.saveXmlInStream(doc, corePart.getOutputStream());

// Save package
pkg.close();
}catch(Exception e){
e.printStackTrace();
}
}

}

This is the simple example which will give you the idea how to manipulate openxml.

Cheers

Anees

Wednesday, April 8, 2009

Encrypting the Connection information in persistence.xml in JPA

Hi All,

I want to share my experience and the way I used to encrypt the connection related information in JavaEE applications.

I was working on JPA as a standalone ORM solution for my application. Please follow the steps I have explained below to remove the connection information from the persistence.xml

Step 1) Remove the Connection related properties from the persistence.xml file

persistence unit of the persistence.xml is "periodicManager"

Step 2) Create entityManager by injecting the properties as Map


ResourceBundle resBundle = ResourceBundle.getBundle("path/to/persistence");

Map persistenceMap = new HashMap();
Enumeration bundleKeys = resBundle.getKeys();
while(bundleKeys.hasMoreElements()){
String key = bundleKeys.nextElement();
String value = resBundle.getString(key);

persistenceMap.put(key, value);
}

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("periodicManager", persistenceMap);

entityManager = entityManagerFactory.createEntityManager();

By this way we can remove the connection related information from the persistence.xml and injected into Persistence's createEntityManagerFactory.

Now, you can put the connection related information to the properties file. You can encrypt the properties file by using any encryptor program.

Cheers




Sunday, April 5, 2009

JBPM+ JPA sharing same session

Hi All,

I was working on the aspect that if we have to use jBPM in the environment where we already have JPA based session management.

In this scenario, we have no intention to use jBPM connection rather we want to share the connection with JPA.

To achieve this, I cameup with one simple solution i.e. injecting the JPA session to the jBPM session.

In order to inject, please follow the following steps to avoid the jBPM hibernate connection.

Step 1) Create the EntityManger and get the Session

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("unitName");

EntityManager entityManger = entityManagerFactory.createEntityManger();

org.hibernate.Session jpaSession =(Session) entityManager.getDelegate();

Step 2) Now get the JbpmContext and inject above session into it

JbpmConfiguration configuration = JbpmConfiguration.getInstance();

JbpmContext jbomContext = configuration.createJbpmContext();

jbpmContext.setSession(jpaSession);



In this way we can avoid the multiple JDBC connections of JPA and jBPM.

Cheers and enjoy the jBPM