Tuesday, May 5, 2009

ICeFaces open sources its pdf renderer

Hi All,

ICEFaces big name in the Ajax based JSF components has open sourced its famous pdf viewer component. Its commerical part only has additional Font engine.

In this post, I am posting an example about using ICEPdf viewer library for swing applications as I am a big fan of swing rich clients.

public class ViewerComponentExample {
public static void main(String[] args) {

// Get a file from the command line to open
String filePath = args[0];

// build a component controller
SwingController controller = new SwingController();

SwingViewBuilder factory = new SwingViewBuilder( controller );

JPanel viewerComponentPanel = factory.buildViewerPanel();

JFrame applicationFrame = new JFrame();
applicationFrame.getContentPane().add( viewerComponentPanel );

// Now that the GUI is all in place, we can try openning a PDF
controller.openDocument( filePath );

// show the component
applicationFrame.pack();
applicationFrame.setVisible(true);
}
}

Actually, this example is taken from the ICEPdf distribution. It is a well-explained example. I found this example as very easy to use and it has all required features we usually need in PDF viewer.

Before this api, I was using Adobe abandoned "bean" project which has very old version of pdf render. I also tried pdf-renderer project hosted by java.dev.net. I found this library as very optimised and well-coded.

Cheers and enjoy ICEPDf

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





Tuesday, February 3, 2009

Using 3rd Party non-GWT jars in GWT project

Hello guys. 

Today I want to share my experience how I able to use 3rd-party non-GWT jar insie my GWT application. These 3rd-party jar can be apache-commons-* or any other project  developed by other team memebers.

Now I will explain step by step way how we can easily use external jar file wish GWT. The only thing which should be taken into consideration is that the project jar we are using has all source should be translateble to the GWT and vice versa.

Now we proceed step by step.

Step 1) lets we had created one project "com.eaglecoder.ejb" which contain the domain object i.e            POJO which will be used to set /get data in our GWT project. 
    - We will create XXX.gwt.xml file at the root of that project i.e. TestingUserDomain.gwt.xml"

the TestingUserDomain.gwt.xml file should contain the "source" entry which will define the path of the pojo.


TestingUserDomain.gwt.xm

       <module>
   
<inherits name='com.google.gwt.user.User'/>
  <source path="com.eaglecoders.erp.ejb.domain"/>
 
  </module>

Step 2) we will export this project to "jar" file i.e. com.eaglecoders.ejb.jar.

Step 3) we create GWT project and add this jar file into its BuildPath and also in its classpath to compile the GWT source. i.e. "MainApp-shell.cmd" and "MainApp-compile.cmd". 

Step 4) Add the following line in your MainApp.gwt.xml file to use the ejb project created in
 step 1 - step 3

    <inherits name = 'TestingUserDomain'/>


this name is always the first section of the gwt.xml file name. i.e. TestingUserDomain.gwt.xml.

hopefully this will help you all guys how easily you can use your external jar file with GWT project.

Cheers