Monday, November 24, 2008

Understanding Java Classloading and Hierarchy

This article provides the basics of Java Classloading and it makes every successful Java programmer a foundation to understand Classloading behaviour.

Classloaders are fundamental part of the Java Virtual Machine (JVM) that loads classes into memory and is responsible for finding and loading class files at run-time.

Classloader Hierarchy:

Following listing describes on the hierarchy of class loading from top to bottom as parent to child. First three Classloaders together typically shows the Java Classloading hierarchy and with the addition of the fourth Classloader (which has its own hierarchy) shows a typical Enterprise Application Classloader hierarchy.



Things to remember about Classloading:

1. Delegation – Always the current associated Classloader delegates the request to load a class to the immediate parent Classloader before attempting to load on their own. Delegation happens only when a particular class is not loaded in cache already.

2. Visibility - Classes loaded by parent Classloader are visible to all child Classloaders but not vice versa.



3. Uniqueness - Once a class is loaded by any of its parent Classloader, child Classloader in the hierarchy will never reload the class again.

4. Configurable - Most Application Specific Classloaders are configurable based on which the server automatically creates a hierarchy of Classloaders when an application (EAR) is deployed where root shared Classloader is created for loading bean libraries of EJB JAR, independent child Classloader are created for loading classes under the WEB-INF and WEB-INF/lib directories of WAR and for JSP classes for each web application. The parent of the hierarchy created is always System Classpath Classloader. The process of redeploying the application modules without server restart is termed as hot-deployment and is closely related to Classloading.

Tuesday, November 4, 2008

New features of Java 6 (Java Mustang/J2SE 6.0/JDK 1.6)

Core libraries features:

Features include changes to core libraries of java.lang, java.io, and java.util packages. It includes the following changes:

java.lang package features: New methods have been introduced in the java.lang.String and java.lang.Math class.

· Empty String check: Includes isEmpty method for checking empty String.

Usage: Use myString.isEmpty() instead of using myString.equals("") or myString.length==0 where myString is of type java.lang.String.

· Floating point enhancements: Includes methods like scalb, getExponent, copySign, nextUp, nextAfter for making floating point comply with IEEE 754 standard.

java.io package features: New methods have been introduced in the java.io.File class.

· File manipulations: Includes methods like getFreeSpace, getUsableSpace, getTotalSpace, serReadable, setWritable, setExecutable, and canExecute for manipulating free space (with and without account restriction imposed by OS), total space, read/write/execute permission details on the drive operated.

java.util package features: New methods have been introduced in the java.util.Arrays class.

· Array reallocation: Includes methods like copyOf and copyOfRange (both comes with 10 overloaded methods to support primitive types and objects) for copying the elements of one array to another array providing scope for resizing/reallocating.

Usage:

..

// Create array of length 5

int intArr1[]={1,2,3,4,5};

// Copy all elements of array created earlier and reallocate the size with additional one element

int intArr2[]=Arrays.copyOf(intArr1, 6); // intArr2 will hold 1,2,3,4,5,0

// Copy elements from 2nd position of array created earlier and reallocate the size with two additional elements

int intArr3[]=Arrays.copyOfRange(intArr1, 2, 7); // intArr3 will hold 3,4,5,0,0

..

· New Collection framework types: The Java Collection framework adds few new interfaces and implementations

Wednesday, October 29, 2008

Spring Framework: Spring Basics (Inversion of Control & Dependency Injection)

Before starting on with the fundamentals of Spring framework, we need to understand the term “Inversion of Control” and “Dependency Injection”.

As per Martin Fowler, “In Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name - Inversion of Control”.

Inversion of Control/Dependency Injection pattern:

• Inversion of Control (IoC) container is a light weight container that helps assembling components into a cohesive application. Examples of such containers are Spring IoC Container, Pico Container etc.

• Dependency Injection (DI) pattern is a design pattern for the IoC containers for wiring components.

• In simple words, generally in Java programming we have a scenario where one object creates object instances of other dependant classes using the “new” keyword thus tightly coupling the dependant object with the actual object. To arrive at an loosely coupled model, rather than creating the dependant objects from the main object, we have assemblers/containers which assembles these dependant objects with the main object. This inverse process of creating the dependant object and injecting it to the main object is termed as DI.

• There are three different styles of DI namely Constructor Injection, Setter Injection and Interface Injection which we will discuss further in this article.

Spring: The Core Technologies:

Spring framework is an implementation of the Inversion of Control (IoC) design pattern and uses the org.springframework.beans and org.springframework.context as the core base packages. Now we will see using the two underlying interfaces as part of the above packages namely org.springframework.beans.factory.BeanFactory and org.springframework.context.ApplicationContext. They apply the Dependency Injection (DI) pattern to separate application’s configuration and dependency specification from actual code just behaving like the assembler briefed earlier.

The BeanFactory interface:

• BeanFactory interface is an implementation of factory design pattern allowing an object to be created and retrieved by name through advanced configuration mechanism.

• In short, it just instantiates and configure beans.

• It lazily loads all singleton beans. That is, it does not create bean instances when Bean Factory is created.

The ApplicationContext interface:

• ApplicationContext interface is a superset of BeanFactory that will include all functionalities that BeanFactory provides apart from other additional enterprise functionalities.

• In short, it instantiates, configures and provides supporting infrastructure to enable enterprise features like transactions, AOP, i18N, etc and is preferable over BeanFactory.

• It pre-loads all singleton beans. That is, it creates bean instances when Application Context is created.

Spring IoC Container:

• A light weight container for assembling components.

• BeanFactory and ApplicationFactory are the actual representation of the Spring IoC Container.

• In Spring context, objects (POJO) are referred as beans and are managed by the bean container also termed as IoC container. This means that IoC container provides a run-time environment for all your beans just like the Web Container providing run-time environment for all server-side scripting like Java Servlets, JSP managing its life cycle.

• Spring IoC Container manages the beans based on the bean definitions specified inside a XML based configuration metadata. This configuration file should be created by the developer and serves as a input to the container.

Instantiating the IoC Container:

IoC Container can be instantiated by creating instance of either object of type BeanFactory or ApplicationContext.

Using BeanFactory: This is the basic container for simple applications. There are several implementations of the BeanFactory, but most widely used implementation is the org.springframework.beans.factory.xml.XmlBeanFactory class which takes up object of type org.springframework.core.io.Resource. Some of the commonly used resource implementations are ByteArrayResource, ClassPathResource, FileSystemResource, InputStreamResource, PortletContextResource, ServletContextResource, UrlResource.

Using ApplicationContext: This is an advanced container and is preferred over BeanFactory. Most widely used implementation of the ApplicationContext are ClassPathXmlApplicationContext, FileSystemXmlApplicationContext.

Example#1: Instantiating a bean using Spring

Typical step-by-step example in creating a simple bean instance through Spring is as follows

• Create a simple java (bean) program.

package workspace.samples.springdemo.beancreation;

public class SimpleBean
{
public SimpleBean()
{
System.out.println("SimpleBean Instance created using Spring");
}
}

• Create a Spring configuration file.



• Create a simple client program where we instantiating the IoC Container and access the bean configured in the XML meta data file through the container

package workspace.samples.springdemo.beancreation;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class SimpleBeanClient
{
public SimpleBeanClient()
{
// To create the IoC Container using BeanFactory and retrieve bean instance from factory
BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("beanCreationContext.xml"));
SimpleBean simpleBeanObj1=(SimpleBean) beanFactory.getBean("simpleBean");

// To create the IoC Container using ApplicationContext and retrieve bean instance from context
ApplicationContext context=new ClassPathXmlApplicationContext("beanCreationContext.xml");
SimpleBean simpleBeanObj2=(SimpleBean) beanFactory.getBean("simpleBean");
}

public static void main(String args[])
{
SimpleBeanClient beanClient=new SimpleBeanClient();
}
}


Example#2: Spring DI: Setter Injection

Typical step-by-step example in injecting dependent objects through Spring DI is as follows

• Create two simple java program of which one is dependent on another.

EngineBean.java:

package workspace.samples.springdemo.beaninjection;

public class EngineBean
{
public EngineBean()
{
System.out.println("[EngineBean] [instance created]");
}
public void checkEngine()
{
System.out.println("[EngineBean] [checkEngine] ...");
}
}

CarBean.java:

package workspace.samples.springdemo.beaninjection;

public class CarBean
{
public CarBean()
{
System.out.println("[CarBean] [instance created]");
}
// This is required for the Spring Container to inject the bean EngineBean into this CarBean object
public EngineBean engineBean;
public void setEngineBean(EngineBean engineBean)
{
System.out.println("[CarBean] [EngineBean instance injected into CarBean]");
this.engineBean=engineBean;
}

public void startEngine()
{
engineBean.checkEngine();
System.out.println("[CarBean] [startEngine]...");
}
}


• Create a Spring configuration file



• Create a java client program which creates instances of the two java beans and then injects the dependent bean into the other bean through Spring DI.


package workspace.samples.springdemo.beaninjection;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class CarDriverClient
{
public CarDriverClient()
{
System.out.println("[CarDriverClient]");
// To create the IoC Container using BeanFactory and retrieve bean instance from factory
BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("carBeanInjectionContext.xml"));
CarBean carBeanObj1=(CarBean) beanFactory.getBean("carBean");
carBeanObj1.startEngine();

// To create the IoC Container using ApplicationContext and retrieve bean instance from context
ApplicationContext context=new ClassPathXmlApplicationContext("carBeanInjectionContext.xml");
CarBean carBeanObj2=(CarBean) beanFactory.getBean("carBean");
carBeanObj2.startEngine();
}
public static void main(String args[])
{
CarDriverClient carDriverClient=new CarDriverClient();
}
}

Sunday, October 12, 2008

Spring Framework: Getting Started

This is a quick introductory tutorial on getting started with Spring framework. It is intended for people setting-up the development environment with Spring framework installation and understanding the Spring distribution.

Introduction:

Please go through the quick start tutorial posted earlier to understand Spring framework and its architecture before getting started.

http://w-o-r-k-s-p-a-c-e.blogspot.com/2008/10/article-spring-framework-introduction.html

Installation of Spring framework comes up as a zip file distribution and the release information can be found under readme.txt file of the distribution. This tutorial covers the installation of Spring Framework 2.5.5 distribution that requires JDK 1.4.2 or later and J2EE 1.3. So we will use Sun JDK 1.5 with eclipse

Installation:

• Download the binary distribution from http://www.springframework.org/download
• The binary distribution of Spring framework consist of the following directory layout



• To install Spring framework, copy/extract the distribution files to a particular directory

Set Environmental Variables:

• Set JAVA_HOME environment variable to the directory where JDK is installed.
• Add Spring_Installation/dist/spring.jar to classpath.
• Add Spring_Installation/lib/jakarta-commons/commons-logging.jar to classpath.

Thursday, October 9, 2008

Spring Framework: Introduction

This is a quick introductory tutorial to the Spring framework and their Architecture. It is intended for people starting out with understanding the Spring modules.

Introduction:

• Spring is a modular Java/J2EE application framework.

• Spring is an open source light-weight framework to address the complexity of any enterprise application development.

• Spring is a well organized and layered architecture with pre-defined modules that can work independently or in combination, providing a framework for J2EE application development allowing us to use only components that are needed.

• Spring can be used with any J2EE server. Primary focus of Spring is to allow reuse of business and data access objects across J2EE environments, Standalone environments, test environments etc without being tied up with just J2EE services.

Spring Framework Architecture:

Following are the various modules of Spring Framework Architecture



Spring CORE:

• Spring Core is the core container for the entire Spring framework providing the base essential functionality features like dependency injection.

• Primary component of the core container is the “Bean Factory”, an implementation of factory pattern to instantiate bean creation. It applies the Inversion of Control (IoC) pattern to separate application’s configuration and dependency specification from actual code.

• It includes supporting utilities and bean container.

Spring CONTEXT:

• Spring Context is a configuration file providing context information to the Spring framework.

• It provides API for obtaining message resources.

• It includes enterprise services like JNDI access, i18n support, EJB integration, remodeling email, UI validation and scheduling functionality.

Spring DAO:

• Spring Data Access Object (DAO) is an abstraction layer providing meaningful exception hierarchy for managing database connection, exception handling and errors thrown by different database vendors.

• It reduces the amount of code one need to write, such as opening and closing connections.

• It includes transaction infrastructure, JDBC support and DAO support.

Spring ORM:

• Spring Object Relational Mapping (ORM) is an interfacing layer providing support for most ORM framework.

• It includes integration support for Hibernate, iBATIS SQL Maps, JDO, JPA, TopLink etc.

Spring AOP:

• Spring Aspect Oriented Programming (AOP) which is fully integrated with Spring configuration management allows to enable AOP features to any objects managed by the Spring framework and incorporates declarative transaction management into your application especially without EJB.

• It includes run time support for the cross-cutting behavior called “aspects” through its meta-data programming that adds annotations to the source code which instructs Spring in applying aspects across multiple components in a system thus complementing OOP with AOP model.

Spring WEB:

• Spring Web is built on top of the Spring Context module providing context for web based applications.

• It includes integration support with various (Model-View-Controller) MVC frameworks like Struts, JSF and web works etc.

Spring WEB MVC:

• Spring Web MVC is a full-featured MVC implementation for building web applications which is highly configurable.

• It includes support for multiple view technologies like JSP, velocity, Tiles, POI, iText etc.

Reference Links:

Spring, one of the leading platform to build and run Enterprise Java
http://www.springframework.org/

Tuesday, October 7, 2008

Quick Start Tutorial: Apache ANT

This is a quick introductory tutorial to the Ant build tool. It is intended for people starting out with Ant and Java development, and aims to provide enough detail to get started.

Introduction:

ANT (originally an acronym for Another Neat Tool) is a platform independent java based build tool written purely in java. Ant is particularly good at automating complicated repetitive tasks and thus is well suited for automating standardized build processes. Ant accepts instructions in the form of XML based documents thus are extensible and easy to maintain.

Installation:

* Download the binary distribution of Ant from http://ant.apache.org/.
* The binary distribution of Ant consists of the following directory layout.


* To install Ant, copy the distribution files to a particular directory.
* Set the ANT_HOME environment variable to the directory where ant is installed.
* Add ANT_HOME/lib/*.jar to classpath.
* Set JAVA_HOME environment variable to the directory where JDK is installed.
* Add the Ant’s bin directory to your path.

Basics: A Simple build process for Java project

Ant uses what it calls a build file to set out steps it will take. An Ant build file comes in the form of an XML document; all that is required is a simple text editor to edit the build files. The Ant installation comes with a JAXP-Compliant XML parser; this means that the installation of an external XML parser is not necessary.

Syntax: ant [options] [target [target2 [target3] …]]

Example #: Creating a COMPILE task for java

A simple example is shown below followed by a set of instructions indicating how to use Ant. This example will demonstrate on how-to write a simple build file for creating an output folder, compiling java source, and outputting the generated class files to the output folder.

Steps to build the compile ant task:

* Create similar to the below code into a file named build.xml. Create a directory and place the xml file into it.


(1) As Ant build files are XML files, the document begins with an XML declaration which specifies the version of XML in use.

(2) The root element of an Ant build file is the project element, it has three attributes. Out of these attributes, default is the only required attribute.

# name – denotes the name of the project.
# default – denotes the default target to be used when no target is specified.
# basedir – denotes the base directory of the project. By default, it denotes the parent directory of the build file.

(3) The property element allows the declaration of properties which are like user-definable variables available for use within the build file. The name attribute specifies the name of the property and value attribute specifies the value of the property. In this example, src property has a value “.” and output property has a value “build”.

In order to refer this property, specify the name of the property between ${ and }. In this example, the property src and output are later referred under target elements.

(4) The target element is used as a wrapper for a sequence of actions. A target has a name attribute so that it can be referenced from elsewhere, either externally from the command line or internally via the depends keyword, or through a direct call. It has a number of possible attributes out of which the name attribute is the only required attribute.

# name – denotes the target name which can be referenced from elsewhere.
# depends – denotes a comma separated list of all the targets on which this target depends. In other words, depends contains all the targets that must be executed prior to executing this target.
# if – allows adding conditional attribute to a target based on the value of the property.
# unless – converse of “if” attribute. That is, the targets content would be executed unless the property is set.
# description – provides a short description of the target.

In this example, the target “init” – initialize is used to create the output folder if it doesn’t exist through the mkdir element with the name specified by the output property defined earlier. The echo element is used to echo the message text to the current loggers and listeners which means System.out unless overridden. The task can also echo to a file.

(5) As explained in four, depends allows one to specify other targets that must be executed prior to the execution of this target. In this example, depends=”init” is used to indicate that the compile target requires that the target named init be executed prior to executing the body of compile.

The javac element, as used above, is a task, tasks are performed in the body of a target, in this case, the source directory is specified by referencing the src property and the destination directory is specified by referencing the output property. The example above causes javac to be executed, compiling all the .java files in the directory specified by the src property and placing the resultant .class files in the directory specified by the build property.

* Create a sample java source file and place it in the same directory as build.xml file.


* Type the command ant at the command line in the directory. This would create a directory called build under the test directory, compile the entire java source (BuildTest.java) under test directory and place the compiled class files (BuildTest.class) under the build directory that’s been created earlier.


* A nice feature of Ant is that by default, only those .java input files that have a more recent timestamp than their corresponding .class output files will be compiled.

A Typical Project: Build process of a J2EE web project

A typical J2EE web application is defined as a hierarchy of directories and files in a standard layout. Such a hierarchy can be accessed in its "unpacked" form, where each directory and file exists in the file system separately, or in a "packed" form known as a Web ARchive, or WAR file. The former format is more useful during development, while the latter is used when you distribute your application to be installed.

Example #: Using Ant to manage the J2EE web development and build process

This example will demonstrate on how-to write a build file for creating a J2EE web application.

Steps to build the web application task:

* Save the below code into a file named build.xml. Create a directory and place the xml file into it.


(1) These properties generally define the file and directory names (or paths) like application name, build directory, distribution directory, documentation directory, java source code location path, document files like html, jsp, js etc., external library path that affect where the build process stores its output.

(2) Instead of replying on the CLASSPATH environmental variable, Ant includes features that make it easier to dynamically construct the classpath for each compilation. Includes all jar files to classpath under the lib directory.

(3) The "all" target is a shortcut for running the "clean" target followed by the "compile" target, to force a complete recompile.

(4) The "clean" target deletes any previous "build" and "dist" directory, so that you can be ensured the application can be built from scratch.

(5) The "prepare" target is used to create the "build" destination directory, and copy the static contents of your web application to it.

(6) The "dist" target creates a binary distribution of your application in a directory structure ready to be archived in a war file. Note that this target depends on two others:
# "compile" so that the entire web will have been assembled
# "javadoc" so that the application Javadoc will have been created

(7) The "javadoc" target creates Javadoc API documentation for the Java classes included in your application. Normally, this is only required when preparing a distribution release, but is available as a separate target in case the developer wants to create Javadoc independently.

(8) The "prepare" target is used to create the "build" destination directory, and copy the static contents of the web application to it.

Core ANT Tasks - glimpse:

There are large numbers of tasks available with Ant; the following table will show some of the core tasks. Refer the links section to know more about other tasks.



Tips:

* Use properties – Ensures reusability of build files by using property element.
* Use relative file paths – Ensures projects portability.
* Comment Ant build files – Adding descriptions attribute to targets ensures better readability.
* Ant documentation – Ant’s documentation is a comprehensive and contains many useful examples.

Reference Links:

* Apache Ant Resource Homepage
  http://ant.apache.org/resources.html
* Apache Ant Manual
  http://ant.apache.org/manual/index.html
* Binary distribution of Apache Ant
  http://ant.apache.org/bindownload.cgi
* Complete list of Apache Ant Tasks
  http://ant.apache.org/manual/anttaskslist.html

Thursday, October 2, 2008

Tools: Build Process Management & Integration

Build tools are mainly used and very much helpful in automating the build process for developers. They automate any tasks that developer might encounter during construction, testing, or deploying software application thus saving time drastically.

Following are few good practices on build process:

* Builds should be portable across operation systems.
* Builds should be independent of any IDE.
* Builds should support for multiple tasks/activities automatically.
* Continuous Integration (CI) of build for frequent integration activities.

Few of the well-known and widely used open source tools for build process are listed below:

Apache ANT - Another Neat Tool (ANT) is a Java based build scripting tool that uses XML based build file that describes the steps to build a project. It uses task-driven approach with many built-in tasks readily available with the tool.

Reference links: http://ant.apache.org/

Apache Maven - Maven is a Java based build scripting tool that also uses XML based build file that describes the project itself in a declarative way to build a project rather on steps. It uses declarative approach where project structure and contents are described promoting the standard directory structure.

Reference links: http://maven.apache.org/

CruiseControl - CruiseControl is a open source continuous integration tool for continuous build process management that uses XML based configuration file. It uses a process on CI server that periodically checks for any updates in source code repository, runs the build process and notifies listeners (including team members) through email, RSS etc.

Reference links: http://cruisecontrol.sourceforge.net/

Wednesday, September 24, 2008

Interview Q & A: Java Threads

#1 What is daemon thread and How will you create a daemon thread?

Daemon threads are low priority threads running in background. By calling setDaemon(true), we can make a thread daemon. GC in java is a good exampe for daemon thread.

#2 What are different ways in creating a thread and which is recommended?

There are two ways to create a thread:

* By Implementing Runnable interface (recommended) and implementing run() method
* By extending Thread class and implementing run() method

#3 Explain thread life-cycle?

* Ready state: Whenever a Thread is created, it reaches the ready state. start() method puts the thread in ready state.
* Running state: run() method called automatically by the start() method moves the thread from ready state to running state.
* Blocked/Wait state: wait() and sleep() methods moves the thread from running state to blocked state. By calling notify() or notifyAll() and yield() method will make them again move to ready state.
* Dead state: Thread moves to dead state after completion of run() method.

#4 What do you mean by Thread Synchronization?

Thread synchronization allows only one thread to execute at a time in a multithreaded environment. We can either have a method synchronized or a synchronization block.

#5 What is the Default priority of Thread?

NORM_PRIORITY is the default priority of a Thread.

#6 What is deadlock? How do you prevent it?

Deadlock is a condition at which two processes are waiting for each other to complete before proceeding. It is a cyclic dependency where one should take care to prevent such scenarios and no API methods are available to prevent it.

Interview Q & A: Java Collections Framework

#1 What is the difference between arrays and Collections in java?

* Arrays cannot be resized whereas Collections can be resized and are form of dynamic arrays.
* Arrays forces to specify the initial size whereas Collection does not.
* Arrays can store primitives whereas Collection stores only objects.

#2 What is Collection framework?

Collection framework provides a set of classes and interfaces for sorting and manipulating group of objects as a single unit.

#3 What are similarities and differences between Iterator and Enumeration interface?

Similarities:
* Iterator and Enumeration are used to step through elements of a Collection.

Differences:
* Enumeration has methods only to traverse and fetch objects serving as read-only whereas Iterator has methods to manipulate objects like adding/removing objects. So use Enumeration whenever we need to have Collection objects as read-only.

#4 What is the difference between java.util.List and java.util.Set interfaces?

* List interface allows duplicate elements whereas Set does not allow duplicate elements.
* List is an ordered collection and also allows positional indexing (add or retrieving elements at a particular index) whereas Set is an unordered collection.

#5 What are SortedSet/SortedMap used for?

Interface for maintaining elements in sorted order and implementation classes are TreeSet/TreeMap.

#6 What is the difference between java.util.Queue and java.util.Stack interfaces?

Queue supports ordering of element in FIFO basis. That is element first added is the first element to get Whereas Stack supports ordering element in LIFO basis.

#7 What is the difference between ArrayList and LinkedList?

* ArrayList can be used wherever we need to support random access of elements without adding or removing elements of the list.
* LinkedList can be used wherever we need to add, retrieve and remove elements at the beginning and end of the list.

#8 What is the difference between HashMap and TreeMap?

* TreeMap maintains the order and HashMap does not.
* HashMap is faster for adding, retrieving, removing of elements from the collection and TreeMap is faster during traversing in a sorted manner.

#9 What is the difference between Vector and ArrayList?

* Vector is synchronized whereas ArrayList is not. Arraylist as is unsynchronized gives better performance than Vector in a non-multithreaded environment and Vector gives better performance in multithreaded environment.
* Vector has a default size of 10 whereas ArrayList has no default size.

#10 What is the difference between HashTable and HashMap?

* HashTable is synchronized whereas HashMap is unsynchronized. HashMap gives better performance in non-threaded environment as is unsynchronized when compared with HashTable.
* HashTable does not allow null whereas HashMap allows null as key/value.

#11 How to synchronize a HashMap?

Using Collections.synchronizeMap(hashMap)

Tuesday, September 23, 2008

New features of Java 5 (Java Tiger/J2SE 5.0/Jdk 1.5)

Following section gives a quick introductory of the new features of Java 5

Generics:
Provides compile time type-safety.
 o  Example: List<String> myList= new ArrayList<String>(); where myList is an array list of String type.

Enhanced for loop:
For statement can be used to iterate over an array or a collection.
 o  Example: for(Object object: myList) where myList if an ArrayList collection and its elements are assigned to object.

Autoboxing/unboxing:
Automatic conversion of wrapper types to primitive types and vice versa.
 o  Example: boxing: int i=new Integer(42);
 o  Example: unboxing: Integer iObj=36;

Enum type:
A new enumeration type in java
 o  Example: private enum CustomerType {Individual, Corporate}; later can be referenced as CustomerType.Individual or CustomerType.Corporate.

Static import:
Provides a way to access final static fields without referencing it with class name.
 o  Example: import static java.util.Calendar.SATURDAY; later can be directly referenced as SATURDAY instead of Calendar.SATURDAY.

Varargs:
Allows methods to have variable length of arguments. It can be used instead of array.
 o  Example: public static void main(String... args) where … says that there is 0 or more arguments.

Annotations:
Provides notes in java programs to ask java compiler to do something.
 o  Example: @Override, @Deprecated

Reference links:
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

Wednesday, September 17, 2008

Interview Q & A: Java Basics

#1 What is the difference between JVM, JRE and JDK?

JVM is required to run bytecode (which is the compiled format of java program). They are different for different OS.

JRE is a java distribution with JVM and java core libraries. This is good enough to run bytecode.

JDK is a java distribution with JRE plus compiler and other tools. This is good enough to write, compile and run code.

#2 What is method overloading and method overriding?

Method overloading: Same method name with different signature. This is also called static polymorphism (response is decided during compile time). It uses early binding.

Method overriding: Same method name with same signature. This is also called dynamic polymorphism (response is decided during run time). It uses late binding.

#3 What is upcast and downcast?

If we convert a base class object to one of its derived class object, it is termed as downcast and vice versa is termed as upcast.
 o  Upcast example: Object obj=new String();
 o  Downcast example: String str=(String) obj;

#4 What is the similarities and difference between abstract class and interface?

Similarities:
 o  Both abstract class and interface cannot be instantiated.

Differences:
 o  Abstract classes have partial implementation whereas interfaces are fully unimplemented classes. That is, abstract classes can have either or both of abstract methods (unimplemented methods/methods without any definition) or concrete methods (methods with definition) whereas interfaces can have only methods with no implementations.
 o  Class can implement multiple interfaces, but can implement only one abstract class.
 o  All methods in an interface are public and all variables are public final static by default which makes them as constants. Variables defined in an interface cannot be overridden by its implementing class.

#5 How to choose between abstract classes and interfaces?

Abstract classes over interfaces:
Abstract classes are faster than interfaces as interface requires an extra indirection to identify the corresponding method in class where implementation resides.
If we have a scenario where we need to provide a default implementation to one of the methods and want to force developers to provide implementation for another method during sub classing, then abstract class can be chosen. This way it allows developers to decide based on needs whether to override the default concrete methods as well apart from providing implementation for abstract methods.

Interfaces over abstract classes:
Provide a form of multiple inheritances. That is class can implement multiple interfaces and also interface can extend multiple interfaces as all interfaces has no implementations.
As all variables inside interfaces are public final static, they can be accessed directly by interfacename.variablename which in turn is very much useful in defining constants that can be used across application.

#6 What are Exceptions in Java?

An event that occurs during the execution of a program that disrupts the normal flow.

#7 What are checked and unchecked exceptions?

Checked exceptions: Exceptions that can be dealt with (continue to run with some alternative code) are called checked exceptions. All compile time exceptions are checked exceptions. They can be handled by using try/catch and finally blocks or declaring “throws” in a method.

Unchecked exceptions: Exceptions that cannot be dealt with are called unchecked exceptions. All runtime exceptions or errors are unchecked exceptions. They are exceptions that should never happen (a bug in the code that needs a permanent fix).

#8 How are exceptions types classified?

System level exceptions – an error that occurs during system level programming. Usually they are not propagated to Client UI. Example, null pointer exceptions or runtime exceptions which your environment should take care and choose to decide on what needs to be done.

Application level exceptions – exceptions that occurs during application level programming, usually propagated to Client UI. Example, withdraw should not be done due to insufficient funds.

#9 How will you create custom exceptions?

By creating a class extending the java.lang.Exception.

#10 What is the use of this and super keyword?

“this” keyword is used to denote current object. If a class has multiple constructors, calling one constructor from another can be achieved through this keyword.
“super” keyword is used to call the base class constructor

#11 What are marker interfaces?

Marker interfaces are empty interfaces which does not have any method declarations just to convey the JVM that they should be treated differently. They are also called null interfaces. For example, java.io.Serializable is a marker interface.

#12 What is the difference between AWT and Swing components?

* AWT components are heavy-weight components and Swing components are light-weight components.
* Heavy-weight means they are platform dependant components that uses native OS peer classes whereas light-weight components does not depend on the OS. For example, look and feel of AWT button component uses native look and feel of OS whereas Swing button components can have thier own look and feel.

#13 What is the difference between static and non-static variables?

Static variables are associated with a class whereas non-static variables are associated with an instance.

#14 How will you do inter-applet communication?

By using AppletContext interface, inter-applet communication can be performed.

#15 Explain automatic garbage collection in Java? Why GC cannot be forced in Java

Garbage collection is automatically taken care by java as a low priority daemon thread that runs in the background. Thats the reason why GC cannot be forced in Java. Whenever objects are unreachable, that objects finalize() method is called before the object is garbage collected thus thier resources are reclaimed or reused. Referencing an object to null will just make the object eligible for GC.

#16 How will you prevent an object from being unreachable?

An unreachable object can be made reachable by overriding its finalize() method to access any other reachable objects

#17 What is the difference between String and StringBuffer?

String objects are immutable whereas StringBuffer are mutable which makes it faster in concatenation.

#18 What is the difference between == and equals() method?

== is used to compare object references whereas equals() method is used to compare the object contents.

Tuesday, September 9, 2008

Interview Q & A: General Technical Concepts

#1. What is the difference between Process and Thread?

Process is an instance of a program to execute its instructions. Whenever a process is created, process scheduler of the OS will set its status to “wait” and loads the process into main memory from secondary storage like Hard Disk, CD-ROM. After which the process is loaded into the processor (context switching) to execute its instruction moving it to “running” state. During the execution, if the process has to wait for user input or waits for some other resources; it will be moved to “blocked” state. Once process is done with its execution, it is moved to “terminate” state where it will be removed from main memory. For instance, MS Word is a program and its instance can be created to perform documentation.

Threads are basically sub-processes of a process. Each sub-process is termed as a thread executing its own instructions. A process can thus have multiple threads executing in parallel. For instance, MS Word can have multiple threads where one thread can do spell check while other thread might still allow the user to type or arrange document layout.

#2. What is the difference between Multiprocessing, Multitasking, and Multithreading?

Multiprocessing is the ability of a system to support more than one processor and/or ability to allocate tasks between them. For instance a tightly-coupled multiprocessor system contains multiple processors within a single computer system interconnected at the bus level and a loosely-coupled multiprocessor system (also called as clustering) contains multiple standalone computers (with its own processors) interconnected via high speed communication system or local area networks like an Ethernet.

Multitasking is the ability to run more than one program or task at the same time. That is, one process need not wait for other process to finish unlike single-tasking. For instance, you can work on with MS Word simultaneously while browsing or programming.

Multithreading is the ability to execute more than one thread or sub-process in parallel. For instance, MS Word can have multiple threads where one thread can do spell check while other thread might still allow the user to type or arrange document layout.

#3. What is Clustering?

Group of servers that act like a single system or group of computers linked together to achieve high availability/ fail over capabilities and to load balance the application.

#4. What are the different types of Clustering?

Basic types of clustering includes Software clustering and Hardware clustering.

Usually Application clustering is termed as Software clustering where clustering software is installed in each server in the group and uses the software to control the clusters. For instance, creating multiple nodes or multiple instances of server and configuring them in cluster is a good example for software clustering. Also configuring web server installed in one machine to balance the load on different application servers installed in different machines forming a cluster is also another good example for software clustering.

Hardware clustering is one which requires a specialized hardware to control the cluster. For instance, load balance hardware’s like resonators or Cisco load balancing hardware can be used to control the load on each server in the cluster.

#5. What is web syndication and what are the different syndication formats available currently?

Web syndication is a way in which web feeds or web resources made available from one site to other web sites whenever their contents are added or updated. There are two mostly used formats namely Atom and Really Simple Syndication (RSS).

#6. What is in-memory session state replication?

Usually in a clustered environment when a client request is first handled and a session is created by one server in a cluster, the same session may not be available for other servers in cluster. To replicate the session created in one server to all other servers in cluster, we go for in-memory session state replication configuration.

#7. What is the difference between developing Web applications based on CGI and Java Servlet?

Common Gateway Interface (CGI) is a process based model and Java Servlet is a thread based model. That is, for each and every request, in CGI it needs a separate process area whereas in Java Servlet model, only one process area is created and handled with multiple threads.

#8. What is HTTP Tunneling? Give an example implementation of Java based HTTP Tunneling?

HTTP Tunneling is a technique in which communications performed are encapsulated within the HTTP protocol. In other words, creating a sub-protocol or a tunnel inside your HTTP protocol for performing communication between the client-server. In Java HTTP Tunneling is used for Applet-to-Servlet communication where we have a scenario of Java Applet embedded inside a web page residing in client-side of the browser to communicate or pass objects to a Java Servlet residing on server-side.

#9. What is meant by Connection Pool?

Connection pool is a cache of connection objects of the underlying resource maintained in order to reuse them instead of creating it for each and every request for performance improvements. For instance, opening and maintaining a database connection for each user is costly and thus by creating a pool of connection objects the user may not wait to establish a connection to the database.

#10. What are commonly used protocols and their default ports?

HTTP: Hyper Text Transfer Protocol used for processing hyper texts like HTML, normal texts, etc. Default port: 80.
HTTP over SSL: Hyper Text Transfer Protocol used for processing hyper texts over Secure Socket Layer. Default port: 443.
FTP: File Transfer Protocol used for transferring files. Default port: 21.
SMTP: Simple Mail Transfer Protocol used for sending emails. Default port: 25.
IMAP: Internet Message Access Protocol used for retrieving emails. Default port:143
POP: Post Office Protocol used for retrieving emails. Default port: 110.
IIOP: Internet Inter-ORB Protocol used for distributed programs written in different languages to communicate over the internet.

#11. Is HTTP a stateless or stateful protocol? If stateless, how will be the state maintained?

HTTP is a stateless protocol. It does not maintain the state of the client. Only way to maintain the state is to use Http Sessions or Cookies or URL Rewriting or through hidden form fields.

#12. What is a Sandbox in Java? Explain on browser sandbox and server sandbox on Java application?

Sandbox is a security restriction imposed on an application preventing it from actually performing certain functions for specific reason.

Browser sandbox: In case of applets loaded inside a web page, the restriction imposed by the browser on the applet base application to access local file system or any local resources is termed as browser sandbox.

Server sandbox: The restriction imposed by the Web or Application server on a Web application deployed on it in performing its intended function is termed as Server Sandbox. For instance, if we try to use the call System.exit(0) in any of the server-side codes like JSP or Java Servlet then upon accessing this file will not actually terminate and exit the JVM instead results in security exception. Thus this exception was the result of server sandbox of restricting the server-side code in performing its System.exit(0) functionality.

#13 What is the difference between Web Server and Application Server?

Web server serves only web based clients like web browser through HTTP protocol whereas an Application server serves different clients like web browser, standalone java application clients, GUI based java application etc through different protocols. Web server does not manage its own resources in supporting transactions and database connection pooling whereas an Application server manages its own resources including security, transaction processing, resource pooling, and messaging thus supports middleware services.

Its often misunderstood that web servers cannot and only application servers can do load balancing, caching and clustering whereas it can be actually achieved by adopting certain strategies.

#14 Explain about Sticky IP configuration in Clustering?

For applications which does not have the session replication configured, by configuring Sticky IP will assure that the server which handles the first request will handle the other subsequent requests from the same client.

Tuesday, September 2, 2008

Adding Icons (Favicons & RSS feed icons) to the address bar for your Web Application

There are two types of icons that can be added to your web site namely Favorite Icons and RSS Icons.



Adding Favicons (favorite icons) to Web Sites or Web Pages:

Favicons are important as they provide visual indicators to visitors and help them to easily associate the content with a bookmark in their browser. Favicons are graphically associated with a particular Web Site or Web Page. Any graphical browsers display them as a visual reminder of the Web site identity in the address bar or in tabs.

To add favicon to Web site, you need the following

* An image which needs to be displayed as favorite icon
* An approach specifying that the above image is to be used as favicon.

Image format:

The format of the image must be one of PNG, GIF, or ICO and the image must be of 16x16 pixels or 32x32 pixels.

Approach to add Favicons:

There are two ways of adding Favicons to Web site or Web Page.

* By using link tag where it uses HTML to indicate the location of an icon for a given page. Add the link element to the head section of the document.

Example: <link rel="icon" type="image/png" href="/path/image.png"/>

* By placing a file called favicon.ico in the root directory of the web server.

Example: In Apache Tomcat Web server, you can see the file named favicon.ico under the tomcat_installation/webapps/ROOT folder. Just similar to this, you can have your image for your web applications root directory.

Adding Feedicons (RSS Icons) to Blogs based Web Sites:

RSS Icons provide a means to appear in the address bar of your browser for Blogs based Web Sites providing access to users for feeds. This allows any users to directly use these icons to subscribe to that feed link through the browser.

Approach to add RSS Icons to your Blog Site:

* By using link tag where it uses HTML to indicate the location of an icon for a given page. Add the link element to the head section of the document.

Example: <link rel="alternate" type="application/rss+xml" title="Feed Title" href="Feed Location"/>

For both the cases, Link element has the different attributes like rel element containing what this link is providing. The type element which should be self explanatory. Next is the title, which performs the same function as title elsewhere on the web; displaying a friendly name instead of the resource's location. Finally, href is the location of the link's target on the internet, its URL. We then close the tag.

Monday, September 1, 2008

Best Practice: Project Conventions - J2EE Web Application Development

This document gives a quick introductory article on recommended coding conventions for web application development. It is intended for people starting out with J2EE web development, and aims to provide enough detail to get started. These conventions do not cover all possible directories, files or artifacts that might be part of a project. However, it would be proven to be quite generally applicable guideline assisting every developer on how to best separate project files from files that are distributed as part of an application download.

A web application is defined as a hierarchy of directories and files in a standard layout. Such a hierarchy can be accessed in its "unpacked" form (exploded directory format), where each directory and file exists in the file system separately, or in a "packed" form known as a Web ARchive, or WAR file. The former format is more useful during development, while the latter is used when you distribute your application to be installed.

The top-level directory of your web application hierarchy is also the document root of your application. Here, you will place the HTML files and JSP pages that comprise your application's user interface. When the system administrator deploys your application into a particular server, he or she assigns a context path to your application. Thus, if the system administrator assigns your application to the context path /mywebapp, then a request URI referring to /mywebapp/index.html will retrieve the index.html file from your document root.

Standard Directory Layout

To facilitate creation of a Web Application Archive file in the required format, it is convenient to arrange the "executable" files of your web application in the same organization as required by the WAR format itself. To do this, you will end up with the following contents in your applications

* "Document root" directory - *.html, *.jsp, etc. - The HTML and JSP pages, along with other files that must be visible to the client browser (such as JavaScript, style sheet files, and images) for your application. In larger applications you may choose to divide these files into a subdirectory hierarchy, but for smaller apps, it is generally much simpler to maintain only a single directory for these files.

* /WEB-INF/web.xml - The Web Application Deployment Descriptor for your application. This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints that you want the server to enforce for you.

* /WEB-INF/classes/ - This directory contains any Java class files (and associated resources) required for a web application, including both servlet and non-servlet classes, that are not combined into JAR files. If your classes are organized into Java packages, you must reflect this in the directory hierarchy under /WEB-INF/classes/. For example, a Java class named com.mycomp.mypackage.MyServlet would need to be stored in a file named /WEB-INF/classes/com/mycomp/mypackage/MyServlet.class.

* /WEB-INF/lib/ - This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party (external) class libraries or JDBC drivers.

Source Organization – Directory Structure

The first recommendation for building a web application would be to separate the directory hierarchy containing the source code from the directory hierarchy containing the deployable application. Maintaining these separations has the following advantages:

* Contents of the source directories can be more easily administered, moved, and backed up if the “executable” version of the application is not intermixed.

* Files that make up an installable distribution of the application are mush easier to select when the deployment hierarchy is separate.

Recommended directory structure for a web application:

/build -
Created by the build tasks and used to hold project-wide build items such as compiled source, assembled modules, or files generated by the Javadoc tool.
When deploying a Web module, consider building an unzipped view of the component to enable deployment as a directory structure directly to an application server.

/dist -
Created by the top-level ant dist task, structures under this directory represent the unzipped versions of the binary created by the project.

/docs -
Contains all the documentation for a project, including HTML files, installation and setup files, etc.

/conf -
Configuration or other set-up files, such as Web service configuration files needed by a component or module during the build process. Includes files that are placed in a module's META-INF directory. Also includes configuration files that may require processing prior to being placed in the final module.

/setup -
Contains files that are relevant to the environment for a project or application. This directory may contain database SQL files, ant files containing shared tasks, or any other files that are used to configure a container for a project or application.

/src -
Contains the java source files.

/test -
The top-level test/ directory contain project-wide tests. Each individual component or module should also include a unit test, which should be placed in the src/test directory for each component or module.

/web -
Contains the static content of the resulting WAR file.

/web/WEB-INF -
Contains the web.xml deployment descriptor and static configuration files, such as faces-config.xml. May also include vendor-specific runtime deployment descriptors, such as sun-web.xml. Generally, this directory contains files that are copied rather than changed during a build. Dynamic configuration files should be placed in the
conf/ directory.

/web/lib -
Holds specific versions of components of external libraries used by an application.
If you have multiple binaries in a project, it may be difficult for users with low bandwidth to download the application. Consider including ant targets that download the correct versions of dependent binaries at build time.

Pictorial representation for the recommended directory structure for a web application




References links

* http://java.sun.com/blueprints/code/projectconventions.html
Guidelines, Patterns, and Code for End-to-End Java Applications

Tools: Firefox Add-Ons: Web developer tools

Just thought of sharing the following web development tools which are very useful for our day-to-day web development activities. If you are working on any UI related issues and taking more time on fixing them then here is a way to fix them quickly on real time and also to monitor on web page performance. These tools are extensions available for Firefox browser.

Tools for tweaking HTML, CSS and debugging Javascripts:

Firebug: Add-on for UI debugging made easier. Firebug integrates with Firefox to put a wealth of development tools at your fingertips while you browse. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page. You can use it directly to edit the page which gets reflected on the browser as soon as it gets changed.

Using firebug, you can perform the following

* Inspect HTML and CSS - To start inspecting HTML form elements, click the Firebug icon in the lower right hand corner of the firefox browser once the plugin is installed. You can also go to Tools > Firebug > Open Firebug. Once the Firebug pane comes up, click the Inspect button. Then you can move your mouse over the page and see an outline around elements of the page. Just click on the element you need to inspect and see the section highlighted in the HTML code in the Firebug pane. Also, on the right side of Firebug is a list of CSS styles that are used by this element. You can either edit the CSS or click the stop sign to the left of a line to temporarily disable that line and view changes immediately in the browser. Also you can include new HTML elements in appropriate position and view them during runtime.

* Debug JavaScript - To start debugging JavaScripts within your web page, first open firebug and then click on script tab. Once you view the JavaScript in the script tab pane, set breakpoints by clicking on its line number. Refresh the page and once the particular breakpoint is reached, firebug opens in debug mode and you can step through the code using the debug controls on the firebug pane. By using this, we can watch variables and thier values on the right side of the pane rather making anoying alerts in identifying values passed during runtime.

Reference links: https://addons.mozilla.org/en-US/firefox/addon/1843

Tools for web page performance monitoring:

YSlow: Add-on for UI performance analyzer. YSlow analyzes web pages and tells you why web pages are slow based on Yahoo's rules (http://developer.yahoo.com/yslow/help/index.html#guidelines) for high performance web sites.

* Monitoring the web page - To start monitoring the web page, click the YSlow icon in the lower right hand corner of the firefox browser once the plugin is installed. Once the YSlow pane comes up, click on the Performance button which will generate a performance report with recommendations on enhancing the web page performance. It also provides a graphical representations on the size of each sections like HTML/Text, Javascript files, Stylesheet files, Images etc using the stats button and also provides the response time and other details for each components using the components button next to the performance button.

Reference links: https://addons.mozilla.org/en-US/firefox/addon/5369

Wednesday, August 27, 2008

Using PreparedStatement for variable number of parameters / handle SQL statements or queries with "IN" Clause

Following article discusses on using PreparedStatement to work on SQL queries involving clauses of type "IN"

Basic flow behind executing a SQL Statement:

* First, DBMS or RDBMS will compile the SQL statement
* Secondly, Execute the SQL statements

PreparedStatement in Java is an object representation of pre-compiled SQL statement unlike normal Statement in Java. Usually PreparedStatement are considered faster upon comparison with Statement but only when there are few iterations completed before they can actually catch-up with Statement otherwise only Statement gives better performance for fewer execution cycles.

Effective usage of PreparedStatement:

* Use them for SQL statements which will be executed multiple times.
* Use them for SQL statements that takes parameters though it can be used for statements without any parameters. This way you can use the same statement with different values to execute it.
* Use them for SQL statements that has parameters that are likely to have special characters so that we dont have to worry about handling parameters having quotes or any special characters.

Problem of SQL statement with variable number of parameters:

When we have a SQL statement which takes parameters with special characters and has variable number of parameters, there arises a problem on how to supply values for the PreparedStatement parameters (question mark placeholders) which are dynamic or variant in nature. Statement object might be a better choice to handle such cases as SQL statement can be dynamically constructed but problem would be to handle special characters.

Solution:

Solution to the above problem is to dynamically construct the PreparedStatement parameters using plain java programing and then supply values the same way in which variable parameters are constructed. Just remember though doing this way might not be having significant performance improvement as compared to its actual features, this helps in handling special characters and for scenarios where SQL statement has same set of parameters. That is, the pre-compiled SQL statement feature is used to some extent only when it has the same set of parameters passed as for variable parameters are unchanged. In case of parameters changing, anyway it will try to pre-compile for each change preventing it to effectively use its own feature.

Example: Implementation for the above solution:

SQL statement: SELECT * FROM EMPLOYEE WHERE COMP_NAME IN ("Jame's Consulting", "D'Souze Inc.", "Build-factory (P) Ltd")

Above SQL statement uses "IN" clause which might have variable parameters of company name (COMP_NAME). Based on the user input, the parameters might be one or more. Though we can use Statement here, we need to handle special characters like single quotes or any other.

Approach 1: Construct PreparedStatement for the above scenario assuming single parameter

....
String compNames[]={"Jame's Consulting", "D'Souze Inc.", "Build-factory (P) Ltd"};
String sqlQuery="SELECT * FROM EMPLOYEE WHERE COMP_NAME IN (?)";
PreparedStatement pstmt = con.prepareStatement(sqlQuery);
for(int i=0;i<compNames.length;i++)
{
pstmt.setString(i, compNames[i]);
ResultSet rs=pstmt.executeQuery();
}
....

This approach will have to execute the PreparedStatement for each and every company name which does not actually fit our requirement of using SQL statement with "IN" clause effectively. Also there are multiple network calls to execute multiple queries.

Approach 2 (Recommended): Construct PreparedStatement for the above scenario with dynamically constructing the number of parameters

....
String compParams="(";
String compNames[]={"Jame's Consulting", "D'Souze Inc.", "Build-factory (P) Ltd"};
String sqlQuery="";
PreparedStatement pstmt = con.prepareStatement(sqlQuery);
for(int i=0;i<compNames.length;i++)
{
compParams=compParams+"?,";
}
compParams=compParams.substring(0, compParams.lastIndexOf(","))+")";
sqlQuery="SELECT * FROM EMPLOYEE WHERE COMP_NAME IN "+compParams;
PreparedStatement pstmt = con.prepareStatement(sqlQuery);
int j=1;
for(int i=0;i<compNames.length;i++)
{
pstmt.setString(j, compNames[i]);
j=j+1;
}
ResultSet rs=pstmt.executeQuery();
....

This will effectively use the SQL statement with "IN" clause by executing the query only once. However as discussed earlier, this approach will help only in using PreparedStatement to handle special characters if the parameters are variant in nature for different calls other than other typical advantages of PreparedStatement.

Monday, August 25, 2008

Web Site Optimization Techniques using HTTP Compression

Following article covers the basic web site optimization techniques for better, faster web page loading that would save significant download time.

Using HTTP Compression:

This HTTP Compression technique uses the GZIP compression which is simple and one of the effective way to improve performance of web page loading.

Problem:   Whenever a particular web site is accessed from browser, it loads the particular page by making a request to the web server. Response content takes its time to get transferred to the client browser through the network.

Following is a typical request/response sequence without compression.

•   Client Browser: Makes a request for particular web page
•   Server: Processes the request and sends the HTML response to the browser over the network
•   Client Browser: Renders the HTML response whose content is of some specific size.

Size does matter of page loading time as the browser has to render the response sent by the web server over the network. If the response content is of big size, then it will take more time to load as the entire content has to be transferred over the network (intranet/internet).

Solution:   Efficient way to overcome the above problem is to compress the response content (zipping the content) instead of sending the actual content. This way we can save bandwidth and download time.

Following is a typical request/response sequence with compression.

•    Client Browser: Makes a request for particular web page with information that it accepts compressed content as part of the header information’s. [Accept-Encoding: gzip or deflate (two compression schemes)]
•    Server: Processes the request and sends the HTML response with information to the browser that it has compressed content as part of the header information’s. [Content-Encoding: gzip]
•    Client Browser: Would download the zipped/compressed content, decompress/extract and then render which takes relatively less time.



Usage: Enabling compression is one of the fastest ways to improve your site’s performance. But probably use it for compressing HTML, CSS and JavaScript as other contents like images; media files are already most likely compressed.

Note:
•    Few older browsers like MS IE 4.0, Netscape 1.0 on MS Windows 95 have problems with this compression technique. However most of the traffic now comes from new browsers in order to utilize the latest technology advancement, this technique is recommended and is widely been used nowadays.
•    When you use the "Content-Encoding: gzip" HTTP header, the "Cache-Control: no-cache" header may not work on Internet Explorer version 5.5 Service Pack 2 (SP2) and 6. Meaning Content is always cached.

Reference Links:

http://developer.yahoo.net/blog/archives/2007/07/high_performanc_3.html
http://www.http-compression.com/

Friday, August 8, 2008

Best Practice: Basic performance tuning of SQL statements in Oracle

Though this topic seem to be already known to many developers (mainly Oracle developers), I decided to blog on this so as to provide a quick start on basic tuning of SQL statements for Java developers. Reason being most Java developers always tend to concentrate on thier core areas and do not provide much focus on back-end related tasks. Either they write SQL statements sometimes at high performance cost without thier knowledge or highly depend upon Oracle experts to tune the SQL statements later.

Understanding of Oracle execution plan:

For every SQL statements executed to read or write data, Oracle does the following

• Parses the statement
• Builds a execution plan for that particular statement
• Processes the statement

Using Explain Plan for logging execution plan:

EXPLAIN PLAN command is used to determine the execution plan Oracle follows to execute a specified SQL statement without actually executing it. This means EXPLAIN PLAN runs very fast even if the statement being explained is a query that takes more time to run. So the actual statement is never executed by EXPLAIN PLAN.

This important diagnostic tool will help to identify in-efficient SQL statements by which appropriate tunning on them can be performed. Running this command inserts data describing each step of the execution plan into a work or specified plan table behaving as a sample output table containing all needed sequence of information when SQL statement gets executed.

To log the execution plan, execute the below SQL statement on any SQL select statement.

Syntax (default table): explain plan for [sql_statement]

Example:
SQL> explain plan for select * from emp where ename='smith';

Syntax (user-defined table): explain plan into [user_defined_table] for [sql_statement]

Example:
SQL> explain plan into user_execute_plan for select * from emp where ename='smith';

One other way to get these execution information is to set AUTOTRACE ON which will actually run the SQL statement unlike EXPLAIN PLAN and produces the execution plan of that SQL statement. This will take the same time as executing the SQL statement as it actually runs the statement to output the result along with the execution information and statistics.

Example:

SQL> set autotrace on;
SQL> select * from emp where ename='smith';

To view the execution plan, execute a simple SQL select statement on the plan table or use the script utlxpls.sql provided by Oracle.

Example:
SQL> select operation, option, object_name from plan_table;
OR
SQL> @$oracle_home/rdbms/admin/utlxpls.sql;

Output operations of execution plan from the work table:



In this case, TABLE ACCESS is made first with a full table scan and then results are fed to SELECT STATEMENT. Note that Full table scan means the entire table is accessed first before select is made.

Basic tunning of SQL statement using index:

Based on the output of the execution plan, SQL statements can be tunned for better performance. The execution plan generated helps in identifying whether the operation TABLE ACCESS uses full table scan which may not be as appropriate for good performance. To prevent this full table scan, create an index on the column. If we only query fields of a table that are already in an index, Oracle doesn't have to read the data blocks because it can get the relevant data from the index:

Other ways to use execution plan is to determine the cost (CPU/IO) of executing that statement.

Now create an index and log the execution plan:

SQL> create index emp_index on emp(ename);
SQL> delete plan_table;
SQL> explain plan for select * from emp where ename='smith';
SQL> select operation, option, object_name from plan_table;

Output operations of execution plan from the work table after creating index:



In this case, index (EMP_INDEX) is used first, then used for TABLE ACCESS with range scan. It is not done by a full table scan but rather by using data row id. Range scan means index was used and it can return more than one row.

Now use the indexed column and log the execution plan:

SQL> delete plan_table;
SQL> explain plan for select ename from emp where ename='smith';
SQL> select operation, option, object_name from plan_table;

Output operations of execution plan for select on indexed column from the work table after creating index:



If we only query fields of a table that are already in an index, Oracle doesn't have to read the data blocks because it can get the relevant data from the index without TABLE ACCESS thus improving performance of the SQL statement.

NOTE: If the table has a constraint set, then instead of range scan, it will use unique scan which means index was used and it returns exactly one row.

EXPLAIN PLAN statement is a data manipulation language (DML) statement, rather than a data definition language (DDL) statement. If you want to keep the rows generated by an EXPLAIN PLAN statement in the output table, then you must commit the transaction containing the statement to maintain those data for future analysis purposes as well. If you decide not to keep the rows generated by EXPLAIN PLAN statement in the output table, then you must rollback the transaction or delete those rows from the plan table.

There is no special system privileges required to use the EXPLAIN PLAN statement if you have just the insert privileges on the plan table. However you must have sufficient privileges to execute the statement which you are trying to explain.

Wednesday, August 6, 2008

Understanding the Non-Functional Requirements (NFR)

Requirements gathering is broadly classified into two categories namely functional requirements and non-functional requirements. Usually functional requirements concentrates mainly on business functionality of the proposed application and non-functional requirements mainly focuses on service level requirements. Business Analyst usually does the gathering of functional requirements and it is the role of the technical guy "Architect" who works along on gathering the non-functional requirements which would help during designing the system.

For developing an enterprise application, apart from gathering functional requirements we should also take care of various non-functional requirements (NFR) as well. These requirements are service-level requirements which should be discussed and worked with stack-holders during the Inception and Elaboration phases of a project to define quality service level measurements for each of the service-level requirements. While designing architecture, the following service-level requirements should be addressed:

• Performance
• Scalability
• Reliability
• Availability
• Extensibility
• Maintainability
• Manageability
• Security

Performance:

Performance requirements are usually measured as below.
• In terms of response time for a given screen transaction per user.
• In terms of transaction throughput, which is the number of transaction in a given time period, usually one second.
• For example, a particular screen transaction should have a response time of 2-5 seconds or a throughput of 50 transactions per second.

Scalability:

• It is the ability of a system to support the required quality of service when there is an increase in load without changing the system.
• To better understand scalability, we should know about what the system capacity is about. Capacity is usually the maximum number of processes or users that the system can handle still maintaining the quality of service.
• For example, during increase in load of the system, if the system meets the required performance of response time as five seconds, then the system is scalable. But if the system makes more than 5 seconds, then it denotes that system is not scalable.
• In other words, when the system is on its capacity and could not respond within the acceptable limits, it denotes that system has reached its maximum scalability. Thus to make the system scale, additional hardware should be done. Adding hardware can be done horizontally or vertically.
• Horizontal scaling means adding one more machine.
• Vertical scaling means adding additional processors, memory, disk space etc within the same machine. This type of scaling is much easier than horizontal scaling as adding processors, memory, and disk space does not impact the architecture of the system.


Reliability:

• It ensures the integrity and consistency of an application and all its transactions.
• When load increases, the system should still continue to process requests and handle requests as accurately as before the increase in load.
• If the system is not reliable during load increase, then it is not scalable.

Availability:

• It ensures the system/services are always accessible.
• Even if the components fail (reliability fails), the system will still be available due to the failover capabilities by having the components redundant.


Extensibility:

• It is the ability of a system to add new functionalities or modify existing functionality without impacting the existing functionality.
• You cannot measure extensibility when the system is deployed, but it shows up the first time you must extend the functionality of the system.
• You should consider the following when you create the architecture and design to help ensure extensibility: low coupling, interfaces, and encapsulation.

Maintainability:

• It is the ability to correct issues in the existing functionality without impacting other components of the system.
• You cannot measure extensibility when the system is deployed.
• When creating an architecture and design, you should consider the following to enhance the maintainability of a system: low coupling, modularity, and documentation.

Manageability:

• It is the ability to manage the system to ensure the continued health of a system with respect to scalability, reliability, availability, performance, and security.
• Your architecture must have the ability to monitor the system and allow for dynamic system configuration.

Security:

• It is the ability to ensure that the system cannot be compromised.
• Security includes not only issues of confidentiality and integrity, but also relates to Denial-of-Service (DoS) attacks that impact availability.
• Creating an architecture that is separated into functional components makes it easier to secure the system because you can build security zones around the components. If a component is compromised, then it is easier to contain the security violation to that component.