Thursday, 13 March 2014

Best approach for formatting date in java


The most common approach to format the date as required by using SimpleDateFormat.


This approach causes problems when multiple threads access the same instance of the class variable, due to lack of synchronization.


The below are the most frequent exceptions will occurs:
  • java.lang.NumberFormatException
  • java.lang.ArrayIndexOutOfBoundsException

There are two approaches to handle these exceptions in Multi-threading environment.
  1. By using ThreadLocal<SimpleDateFormat>: ThreadLocal is a best appraoch to implement Per Thread Singleton classes or per thread context information.
  2. FastDateFormat.getInstance(): FastDateFormat is available in commons-lang-version.jar. it has several overloaded methods of getInstance() to provide the date format for Thread-safe environment. Please find the API documents here.

Sample code snippet by using SimpleDateFormat:
package com.samples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatExample {
 
 private static DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.format(currentDate));
 }

}



Sample code snippet by using ThreadLocal:
package com.samples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadLocalDateFormatExample {

 private static ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
  protected DateFormat initialValue() {
   return new SimpleDateFormat("MM/dd/yy");
  }
 };
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.get().format(currentDate));
 }

}


Sample code snippet by using FastDateFormat:
package com.samples;

import java.util.Date;

import org.apache.commons.lang.time.FastDateFormat;

public class FastDateFormatExample {

 private static final FastDateFormat dateFormat = FastDateFormat.getInstance("dd-MM-yyyy");
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.format(currentDate));
 }
}

Hence, the best approach is to format the date isby using FastDateFormat if your project has commons-lang.jar dependency, otherwise can use Threadlocal approach.

Wednesday, 5 March 2014

Unix basic commands

Below are the few basic Unix commands which will be more useful for Java developers who are working in Unix environment.

List all users:
cat /etc/passwd |grep "/home" |cut -d: -f1
awk -F":" '{ print "username: " $1 "\t\tuid:" $3 }' /etc/passwd

The above commands will list all the users in the system.

Some Basic commands:
cd path - change directory.
mv path1/filename1 path2/filename2 - Move files from one directory to another directory.
ls [options]- list the file names.
ll [options]- list the file names with permissions and size.
cp path/filename1 path2/filename2 - Copy files from one directory to another directory.
cat filename - To print the content of the file.
diff filenmae1 filename2 - To view the differences between two files.
./filename.sh - To run sh files. This is mostly used for executing shell scripts and server stop start.
view filename - To view the content of the file.
vi filename - To view and update the file.
top - To check the CPU performance and utilization.
df - To check the disk space utilized.

vi & view editor commands:
----------------------------
When a opened a file with vi/view command, the file will open in command mode as default. To make changes in the files needs bring to "Insert Mode".
i/<Ins> - insert text before cursor, until <Esc> hit. Also called "Insert Mode".
I - insert text at beginning of current line, until <Esc> hit
<Esc> - Change to "Command Mode".

:w - saves the current file without quitting
:# - move to line #
:$ - move to last line of file
?string - Search backward for string
/string - Search forward for string
n - move to next occurrence of search string
N - move to next occurrence of search string in opposite direction
<Esc>:w! - Saves the file
<Esc>:q! - Quit editing, no save

Create a .jar file:
$ jar -cvf <file_namer.jar> <class_file_path>

To work with above command, first we need to change the directory to JDK installed bin directory and execute the command.

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -cvf calculator.jar com/calc/Calculator.class
$ /opt/WebSphere7/AppServer/java/bin/jar -cvf ibank.jar com/calc/*.class

The second command is useful when there are multiple class to be include in a jar. the path /opt/WebSphere7/AppServer/java/bin is a JDK installed path from whepsher server.


Update a .jar file:
jar -uvf <file_namer.jar> <class_file_path>

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -uvf calculator.jar com/calc/Calculator.class
$ /opt/WebSphere7/AppServer/java/bin/jar -uvf calculator.jar com/calc/*.class

Unzar a .jar file:
$ jar -xvf <file_namer.jar>

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -xvf calculator.jar
Once the above command executed, we will get the class files in unix files system. So first we need to change the directory where we need the files to be located and provide bin full path while executing the command.
If we not change the directory and executed from JDK installed path, all extracted files will be placed in JDK installed path.

Find a Class file in .jar file:
grep classname *.jar

Ex:
grep com/calc/Calculator.class *.jar
grep Ibank.class *.jar

To Know the package exactly:
$ /opt/WebSphere7/AppServer/java/bin/jar -tvf calculator.jar|grep Calculator*

Check HTTP Server status:
$ ps -efw|grep httpd

File system size:
When we are facing with file system space issue and not able to find which directory occupies more memory, use the below command.

du -sm *| sort -nr | head -15
du -sh * | sort -nr | more

To transfer tar files from one IP to another IP:
Login to the IP address, where the file is available and execute the below command.
$ /usr/local/openssh/bin/scp <path>/<file_name>  <userid>@<ip_address>:/tmp/


Thursday, 30 January 2014

Spring Annotations

Introduction:

In Spring, XML based configurations is the most popular configuration style. Spring uses annotations as an alternative to XML for declarative configuration.

To make annotations and work in spring, use the <context:component-scan> tag in spring bean configuration file. So that spring will scan and find out the beans and register in the Spring Container.

Syntax:

Ex:

Note: If it requires adding multiple packages to scan, use comma separator.

Auto Components Scan Annotation Types:
In Spring, there are 4 types of auto components scan annotation types
  • @Component – Indicates an auto scan component.
  • @Repository – Indicates DAO component in the persistence layer.
  • @Service – Indicates a Service component in the business layer.
  • @Controller – Indicates a controller component in the presentation layer.

<context:annotation-config> is used to activate annotations in beans already registered in the application context.

Component-scan vs annotation-config:
<context:annotation-config>: Only registers 4 BeanPostProcessors that are part of the Spring Framework and have specific configuration objectives.
  • CommonAnnotationBeanPostProcessor: Recognizes and processes the JSR 250 common annotations (@PostConstruct, @PreDestroy, @Resource).
  • AutowiredAnnotationBeanPostProcessor: Recognizes the Autowired related annotations (@Autowired, @Value, @Inject, @Qualifier, etc)
  • RequiredAnnotationBeanPostProcessor: Recognizes the @Required annotation
  • PersistenceAnnotationBeanPostProcessor: Recognizes the @PersistenceUnit and @PersistenceContext annotations (related to JPA).
All of these annotations are typically placed either on a class field, constructor or method. <context:component-scan>: is a super set of the <context:annotation-config> tag. It registers the same bean post processors and also will perform component scanning. It looks for classes in the given base-package that are annotated with a category of annotations (@Component, @Repository, @Controller, etc). The purpose of using is to provide an alternative way to 'discover' Spring components other than using XML.

Spring related Annotations:

Below are the few important spring related annotations and their package.
Spring Annotation
Package
@Controller
org.springframework.stereotype.Controller
@RequestMapping
org.springframework.web.bind.annotation.RequestMapping
@RequestParam
org.springframework.web.bind.annotation.RequestParam
@PathVariable
org.springframework.web.bind.annotation.PathVariable
@ModelAttribute
org.springframework.web.bind.annotation.ModelAttribute
@SessionAttributes
org.springframework.web.bind.annotation.SessionAttributes
@PreAuthorize
org.springframework.security.access.prepost.PreAuthorize

@Controller Annotation:

The @Controller annotation indicates that a particular class serves the role of a controller. The controller class is no longer need to extends any of base controllers. When we @Controller annotation, its mandatory to use @RequestMapping annotation. This annotation is used to map a particular http/https request.

@RequestMapping Annotation:

This annotation can be used at both class level and method level. In case of multi-action controller, it’s recommended to use this annotation at method level and also at class level if required.

Syntax at class level: @RequestMapping("/") 
Syntax at method level: @RequestMapping(value = “/”, method = RequestMethod.[GET/POST]) 

We can use wildcard characters like * for path pattern matching. 

Simple scenario:
@Controller
@RequestMapping("/blogspot")
public class blogspotController {

 @RequestMapping(value="/properties")
 public String findAllProperties(){

 }
}
Testing URL: /blogspot/properties

Request parameter binding:
@RequestMapping(value="/properties")
public String findByProperty(@RequestParam("propertyId") String propertyId){
  
}
Testing URL: /blogspot/properties?propertyId =<some_id>

Extracting path variables:
@RequestMapping(value="/blogspot/{propertyId}")
public String findByProperty(@PathVariable String paropertyId){

}
(OR)
@RequestMapping(value="/blogspot/{propertyId}")
public String findByProperty(@PathVariable("propertyId") String somePropertyId){

}
Testing URL: /blogspot/properties/propertyId/23

Extracting multiple path variables:
@RequestMapping(value="/properties/{propertyId}/owner/{ownerId}")
public String findByProperty( @PathVariable String propertyId,  @PathVariable String ownerId){

}
Testing URL: /blogspot/ properties/23/owner/39

Extracting regular expressions:
@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")
public String regularExpression( @PathVariable String textualPart, @PathVariable String numericPart){

}
Testing URL: /blogspot/text_chars.123

@RequestParam Annotation:

It’s a method argument annotation. This @RequestParam annotation can be used for getting the parameters from the request(query parameters). The scope of the variable passed is method scope. Syntax:
@RequestParam(value = “requestParam”, required = true/false, defaultValue = "defaultVal") dataTye variableName)
Ex:
public void printPageInfo(@RequestParam(value = "pageNumber", required = false, defaultValue = "1")  Integer pageNumber)

@PathVariable Annotation:

It’s a method argument annotation. The @PathVariable annotation can be used for getting the value of URI template variable. Syntax:
@PathVariable("pathVarName") dataTyep variableName)
Ex: If we want to create a controller that would allow users to see view pages by using a URL as below http://parameshk.blogspot.in/search/label/Java - display details for java http://parameshk.blogspot.in/search/label/Spring - display details for spring
@Controller
@RequestMapping("/search")
public class ShowPagesController {
    @RequestMapping("/label/{labelName}") // Handle any request of the form "/label/XXXXX"
    public String showProduct(Model model, @PathVariable("labelName") String labelName) {
    }
}

@ModelAttribute Annotation:

It supports RequestMapping annotated handler classes. It binds a method parameter or method return value to a named model attribute that exposed to a web view. Uses of @ModelAttribute: Useful to read data from an existing model by assigning it to handler method parameters. If a view/jsp want to populate some values while onload, we can inject the data to jsp by using this annotation. Ex: Below is the code snippet, which binds Employee object to a view page. Code snippet in controller:
@RequestMapping(value="/property-details")  
public ModelAndView processPerson(@ModelAttribute Property prop) {  
    ModelAndView modelAndView = new ModelAndView();  
    modelAndView.setViewName("property-details");  
    modelAndView.addObject("property", prop);  
    return modelAndView;  
}
Code snippet in jsp/view page:
Property Details:
Property Name: ${ property.name}
Property Location: ${ property.location}
Property Owner: ${ property.owner}

@ SessionAttributes Annotation:

The @SessionAttributes annotation is used to store the model object in the session. It is used on the Controller class level to declare used session attributes. All Model attributes having names or types defined in this annotation will be automatically persisted and restored between the subsequent requests. It’s a very small declaration instead of using HttpSession's getAttribute() & setAttribute() calls. Model attributes are persisted at the end of request handling by AnnotationMethodHandlerAdapter, after calling the handler method responsible for request handling and after determining the ModelAndView for the response. Their names in Model are used as their names in the persistent storage. Ex: @SessionAttributes("form") will trigger persisting Model attribute named "form" as HttpSession attribute named "form". Persisted Model attributes will be removed only when SessionStatus.setComplete() method called in handler methods. Ex:
@Controller
@RequestMapping(value="/dashboard", method=RequestMethod.POST)
@SessionAttributes("property")
public class LoginController {
    @RequestMapping(value="/authenticate",method=RequestMethod.POST)
    public String authenticate(@RequestParam String propertyId, Model model){
        Property property = new Property();
        property.setPropertyId(propertyId);
        property.setName("PPB_1");
        property.setLocation("USA");
        model.addAttribute("property", property);
        return "xxxxx";
    }
}
Note: To store multiple objects in @SessionAttributes use comma separator.

RESTful Web Service - JAX-RS Annotations:

  • Jersey, the reference implementation of JAX-RS, implements support for the annotations defined in JSR 311, making it easy for developers to build RESTful web services by using the Java programming language.
  • REST is an architectural style which is based on web-standards and the HTTP protocol.
  • In REST based architecture everything is a resource. A resource is accessed via a common interface based on the HTTP standard methods.
  • In a REST based architecture you typically have a REST server which provides access to the resources and a REST client which accesses and modify the REST resources.
  • Every resource should support the HTTP common operations. Resources are identified by global IDs (which are typically URIs).
  • REST allows that resources have different representations, e.g. text, xml, json etc. The rest client can ask for specific representation via the HTTP protocol (content negotiation).
JAX-RS Annotation
Package
@Path
javax.ws.rs.Path
@Produces
javax.ws.rs.Produces
@Consumes
javax.ws.rs.Consumes
@GET
javax.ws.rs.GET
@POST
javax.ws.rs.POST
@PUT
javax.ws.rs.PUT
@DELETE
javax.ws.rs.DELETE
@FormParam
javax.ws.rs.FormParam
@PathParam
javax.ws.rs.PathParam
@QueryParam
javax.ws.rs.QueryParam

@Path Annotation:

It’s a both class & method level annotation. This identifies the URI path template to which the resource responds. The @Path annotation's value is a partial URI path template relative to the base URI of the server on which the resource is deployed, the context root of the WAR, and the URL pattern to which the Jersey helper servlet responds.

Syntax:
    @Path("/uri")

The @Path annotation is not limited to simple path expressions. We can also have the ability to insert regular expressions into @Path's value.

Syntax: @Path (“uri/{variable[:regular-expression]}”)
Ex:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/properties")
public class UserRestService {

 @GET
 public Response getProperties() {
  //TODO provide method body
 }
 //Target Url: /properties

 @GET
 @Path("/ppb")
 public Response getPropertyPPB() {
  //TODO provide method body
 }
 //Target Url: /properties/ppb
 
 @GET
 @Path("{name}")
 public Response getPropertyByName(@PathParam("name") String name) {
  //TODO provide method body
 }
 //Target Url: /properties/abcdefg

 @GET
 @Path("{id : \\d+}") //id is regular expression which accepts only numbers
 public Response getPropertyById(@PathParam("id") String id) {
  //TODO provide method body
 }
 //Target Url: /properties/1234

 @GET
 @Path("/propertyname/{propertyname : [a-zA-Z][a-zA-Z_0-9]+}")
 public Response getPropertyByPropertyName(@PathParam("propertyname") String propertyname) {
  //TODO provide method body
 }
 //Target Url: /properties/propertyname/myproperty123

}

@Consumes Annotation:

The @Consumes annotation is used to specify which MIME media types of representations, a resource can accept/ consume from the client. @Consumes annotation can be applied at both the class and method levels. If it’s applied at class level, by default all the methods will accept the same MIME type. If it’s applied at the method level, overrides the class level annotation if applied. The possible MIME types which can be used for this annotation can be available in the class javax.ws.rs.core.MediaType.

Syntax: 
   @Consumes(MediaType.XXX) @Consumes({MediaType.XXX, MediaType.YYY, …})

@Produces Annotation:

The @Produces annotation is used to specify the MIME media types of representations a resource can produce and send back to the client. @Produces annotation can be applied at both the class and method levels. If it’s applied at class level, by default all the methods will accept the same MIME type. If it’s applied at the method level, overrides the class level annotation if applied. The possible MIME types which can be used for this annotation can be available in the class javax.ws.rs.core.MediaType.

Syntax: 
    @Produces (MediaType.XXX) @Produces ({MediaType.XXX, MediaType.YYY, …})

@GET, @PUT, @POST, @DELETE & @HEAD Annotations:

@GET, @PUT, @POST, @DELETE and @HEAD are resource method designator annotations defined by JAX-RS and which correspond to the similarly named HTTP methods. In the example above, the annotated Java method will process HTTP GET requests. The behavior of a resource is determined by which of the HTTP methods the resource is responding to.
  • GET defines a reading access of the resource without side-effects. The resource is never changed via a GET request, e.g. the request has no side effects (idempotent).
  • PUT creates a new resource, must also be idempotent.
  • DELETE removes the resources. The operations are idempotent, they can get repeated without leading to different results.
  • POST updates an existing resource or creates a new resource.

@QueryParam Annotation:

Binds the value of a HTTP query parameter to a resource method parameter or resource class field or resource class bean property. A default value can be specified using the @DefaultValue annotation. Syntax:
@QuaryParam(“param1”) @DefaultValue(“defaultVal”) datatype varName
Ex:
@QuaryParam(“param1”) @DefaultValue(“defaultVal”) datatype varName

Ex:
@GET
@Path("eligibleBooks")
public ProductInfos getEligibleProducts(
 @QueryParam("category") @DefaultValue("JAVA") String category,
 @QueryParam(bookId) String bookId, 
 @QueryParam("bookName") String bookName,
 @QueryParam("accountType") String accountType,
 @QueryParam("countryCode") @DefaultValue("US") String countryCode) {
    
 //TODO method body
}

Target URL: /eligibleBooks?category=JAVA&bookId=1234&bookName=abcd123&accountType=technology&countryCode=IN

Tuesday, 3 December 2013

Java Program that provides optimized solution for a builder


Write a java program to provide the optimised solution for the below requirements.
  1. Program should have three input parameters:
    1. Total number of rooms
    2. Minimum rooms per building
    3. Maximum room per building.
  2. Should calculate the number of rooms per building between min-max number provided
  3. Choose a factor that gives minimum reminder
  4. If there are more than one factor giving minimum reminder, choose the highest factor.
Program:
package com.builder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BuildingOptimizer {

 /**
  * @param args
  */
 public static void main(String[] args) {
  printOptimizedSolution(16, 4, 8);
 }
 
 public static void printOptimizedSolution(int totalNum, int minNum, int maxNum) {
  
  Map<Integer, Integer> reminderMap = new HashMap<Integer, Integer>();
  StringBuffer output = new StringBuffer("Optimised Solution is : " );
  
  for(int statNum = maxNum; statNum <= minNum; statNum--) {
   if(totalNum % statNum == 0) {
    reminderMap.put(0, statNum);
    break;
   }
  }
  
  if(reminderMap.size() == 0) {
   
   for(int statNum = minNum; statNum <= maxNum; statNum++) {
    reminderMap.put((totalNum % statNum), statNum);
   }
  }
  
  List<Integer> remindersList = new ArrayList<Integer>(reminderMap.keySet());
  Collections.sort(remindersList);
  
  Integer devider = reminderMap.get(remindersList.get(0));
  Integer divisions = totalNum / devider;
  
  for(int i = 1; i <= divisions; i++ ) {
   output.append(devider);
   if(i != divisions) {
    output.append(", "); // comma should not append at the end
   }
  }
  
  if(remindersList.get(0) != 0) {
   output.append(", ").append(remindersList.get(0));
  }
  
  System.out.println(output);
 }

}

Wednesday, 20 November 2013

Why wait() ,notify() and notifyAll() methods are in Object class instead of Thread class?


These methods works on the locks and locks are associated with Object and not Threads. Hence, it is in Object class.

The methods wait(), notify() and notifyAll() are not only just methods, these are synchronization utility and used in communication mechanism among threads in Java.

Synchronization: The concept of synchronization will make sure that only one Thread is accessing an object/resource at the same time.
Java concurrency model uses locks to implement mutually exclusive access to objects in a multi-threaded environment. Locks are associated with objects not with the Threads. Hence, Locks are made available on per Object basis.
 In Java, to achieve mutually exclusive access on objects, a threads needs to acquire lock and other threads needs to wait to acquire lock. And they don’t know which threads holds lock instead they just know the lock is hold by some other thread and they should wait for lock instead of knowing which thread is inside the synchronized block and asking them to release lock. This analogy fits with wait and notify being on object class rather than thread in Java.

Example:


                                               
I would like to discuss with a well known example to get a clear understanding of this topic. Let’s take a real time banking scenario. Suppose two or more customers having a joint account, which can be permitted to perform the transactions through multiple channels like Teller, ATM, Mobile Banking, and Internet Banking. Currently the account having the balance 2000/- and the first user trying to purchase worth of 1500/- through Internet Banking other user trying to withdraw 1500/- through ATM. Here whichever the channel first access the account to perform the transaction will acquires a lock and other channel will wait for the lock. The channel which is waiting for lock status is not aware of which channel acquires the lock and at the same time the channel which is already acquired the lock is not aware of that who are waiting to acquire lock on the particular account. Here lock is applied on the account and not on channel.

Friday, 15 November 2013

Prototype Design Pattern in Java

In general, a prototype is a template of any object before the actual object is constructed. This design pattern is useful when your application needs to create a number of similar instances of a class with small variations.
Also creation of a new object is a time taking process and an expensive too. So this pattern works by cloning of an object rather than creation.

When to use this pattern?
  • If the cost of creating the object is expensive or complicated.
  • When trying to keep the number of classes in an application to a minimum.
  • When adding or removing objects at runtime
  • When the client application needs to be unaware of the object creation, composition and representation.
  • Objects are required which are similar to the existing objects.
What does Prototype pattern do?
It allows making new instances by copying the existing instances. The copied cloned object is different from the original object. The state of the original is the same as the cloned one, at the time of cloning. Thereafter each object may undergo state change based on the operations performed on it. We can modify the objects to perform different things as well.
Please ensure that you want to deep clone or shallow clone your prototype because both will have different behaviour on runtime. If deep copy is needed, you can use a good technique given here using in memory serialization.
Shallow Clone:
When copied object contains references of other objects, the contained objects will not be cloned. Shallow clone copies only top level structure of an object. It can be done by using clone() method.
Deep Clone:
The object is copied along with the objects it refers to. Deep clone copies all the levels of the object from top to the bottom recursively. It can be done by using serialization or by overriding writing clone method in such way that to take all copied of contained objects.
Contained Object: Suppose Subject is a class which used by other classes.
package com.edu;

public class Subject {
 
 private String subjectName;
 
 public Subject(String subj) { //Constructor
  this.subjectName = subj;
 }

 public String getSubjectName() {
  return subjectName;
 }

 public void setSubjectName(String subjectName) {
  this.subjectName = subjectName;
 }

}
Implementing Shallow Clonning:
package com.edu.shallow;

import com.edu.Subject;

public class Student implements Cloneable {

 private String studentName;
 private Subject subject; //Contained Object
 
 public Student(String studName, String subjName) {
  this.studentName = studName;
  this.subject = new Subject(subjName);
   }

 
 @Override
 public Object clone() {
  //Shallow Copy
  try {
   return super.clone();
  } catch(CloneNotSupportedException cnsExp) {
   return null;
  }
 }
 
 public String getStudentName() {
  return studentName;
 }
 public void setStudentName(String studentName) {
  this.studentName = studentName;
 }
 public Subject getSubject() {
  return subject;
 }
 public void setSubject(Subject subjectName) {
  this.subject = subjectName;
 }


 @Override
 public String toString() {
  return "studentName = " + studentName + ", subject = " + subject.getSubjectName();
 }
 

}
Test Shallow Clonning:
package com.edu.shallow;

public class ShallowCloneTest {

 
 public static void main(String[] args) {
  
  Student stud = new Student("Mohan", "Discrete Structures");

  System.out.println("Original Object: " + stud.toString());

  //Clone Object
  Student clonedStud = (Student) stud.clone();

  System.out.println("Cloned Object: " + clonedStud.toString());

  //Modify the Original Object 
  stud.setStudentName("Rajesh");
  stud.getSubject().setSubjectName("Computer Organisation");

  System.out.println("Original Object after it is updated: " + stud.toString());

  System.out.println("Cloned Object after updating original object: " + clonedStud.toString());
 }

}
Shallow Clonning Testing Output:
Original Object: studentName = Mohan, subject = Discrete Structures
Cloned Object: studentName = Mohan, subject = Discrete Structures
Original Object after it is updated: studentName = Rajesh, subject = Computer Organisation
Cloned Object after updating original object: studentName = Mohan, subject = Computer Organisation
Implementing Deep Clonning by overriding clone() method:
package com.edu.deep;

import com.edu.Subject;

public class Student implements Cloneable {

 private String studentName;
 private Subject subject; //Contained Object
 
 public Student(String studName, String subjName) {
  this.studentName = studName;
  this.subject = new Subject(subjName);
   }

 @Override
 public Object clone() {
  
  Student student = new Student(studentName, subject.getSubjectName());
  return student;
 }
 
 public String getStudentName() {
  return studentName;
 }
 public void setStudentName(String studentName) {
  this.studentName = studentName;
 }
 public Subject getSubject() {
  return subject;
 }
 public void setSubject(Subject subjectName) {
  this.subject = subjectName;
 }


 @Override
 public String toString() {
  return "studentName = " + studentName + ", subject = " + subject.getSubjectName();
 }
 

}
Test Deep Clonning:
package com.edu.deep;

public class DeepCloneTest {

 
 public static void main(String[] args) {
  
  Student stud = new Student("Mohan", "Discrete Structures");

  System.out.println("Original Object: " + stud.toString());

  //Clone Object
  Student clonedStud = (Student) stud.clone();

  System.out.println("Cloned Object: " + clonedStud.toString());

  //Modify the Original Object 
  stud.setStudentName("Rajesh");
  stud.getSubject().setSubjectName("Computer Organisation");

  System.out.println("Original Object after it is updated: " + stud.toString());

  System.out.println("Cloned Object after updating original object: " + clonedStud.toString());
 }

}
Deep Clonning Test Output:
Original Object: studentName = Mohan, subject = Discrete Structures
Cloned Object: studentName = Mohan, subject = Discrete Structures
Original Object after it is updated: studentName = Rajesh, subject = Computer Organisation
Cloned Object after updating original object: studentName = Mohan, subject = Discrete Structures

Thursday, 14 November 2013

How to call Stored Procedures in Hibernate?


DB Objects:
      1.     Tables
      2.     Views
      3.     Sequences
      4.     Stored Procedures, etc.

All Object/Relational mapping frameworks will have entity relationship between DB Tables/Views with Java POJO Data Transfer Objects. To communicate with these DB objects is very easy and can be managed with the method given by framework like save(), update(), delete(), saveOrUpadate(), etc.
In Hibernate, if user wants to communicate with Stored Procedures from underlying DB, needs to follow the below approach.

Procedures have two kinds of parameters in any Database.
      1.     IN Parameters
      2.     OUT Parameters

Step #1: Open a connection from StatelessSession.
Step #2: Call stored procedure by using prepareCall() method of Statement object.
Step #3: Pass/Provide all IN parameters of stored procedures by using setXXX() of Statement object.
Step #4: Register the type of OUT parameters by using registerOutParameter() of Statement object.
Step #5: Call executeUpdate() method on Statement Object.
Step #6: Get the OUT parameters provided by stored procedures by using getXXX() method of Statement object.

Code Snippet:

String queryString = "{call schema.storedProcExample(?,?,?,?)}";
int sequenceNo = 0;

Connection con = getSessionFactory().openStatelessSession().connection();
CallableStatement stmt = null;
try {
            stmt = con.prepareCall(queryString);
            stmt.registerOutParameter(1, java.sql.Types.INTEGER);
            stmt.setInt(2, 1);
            stmt.setString(3, “2”);
            stmt.setDate(4, new java.sql.Date(System.currentTimeMillis()));
            stmt.executeUpdate();
            sequenceNo = stmt.getInt(1);
} catch (SQLException e) {
            LOGGER.info("getTransactionSequenceNo : {}", "Exception");
            LOGGER.info("getTransactionSequenceNo Exception: {}", e);
} finally {
            try {
                        if (stmt != null)
                                    stmt.close();
            } catch (Exception e) {
                        LOGGER.error("exception while closing statement", e);
            }
            try {
                        if (con != null)
                                    con.close();
            } catch (Exception e) {
                        LOGGER.error("exception while closing connection", e);
            }
}

Note: The types of OUT parameters can be available from java.sql.Types class file.

Characteristics of StatelessSession in Hibernate:
  Ø  Neither does not implement a first-level cache nor interact with any second-level cache.
  Ø  Collections are ignored.
  Ø  Operations performed via a stateless session bypass Hibernate's event model and interceptors.



Tuesday, 12 November 2013

Singleton Design Pattern in Java

Singleton Class:
The implementation of a Singleton class enforces, there must be a maximum of only one object of its type at any given time. Also Singleton class will make sure that its object won’t be garbage collected during the application running.

First of all to construct a Singleton object, programmer should aware of in how many ways an object can be created. So that can take precautions to make sure that the class following Singleton Design Pattern.

1. Possible ways to create an Object:
1.1. By using new keyword. This is the most commonly used approach in java.
SingletonObject object = new SingletonObject();

1.2. By using Class.forName(). It can be possible if the class has public default constructor.
SingletonObject object = (SingletonObject) Class.forName("com.edu.SingletonObject").newInstance();

1.3. By using the clone() method we can create a copy of an existing object.
SingletonObject object = new SingletonObject();
SingletonObject anotherObj = object.clone();

1.4. By using the Object deserialization, can able to create an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(inputStream );
SingletonObject object = (SingletonObject) inStream.readObject();

2. Construction of Singleton Object by preventing above possibilities:
2.1. Use a private constructor, hence we cannot create an object of a class outside of it’s by using new keyword. This also prevents creation of an object from Class.forName(). Will get the flowing exception at run time.
private SingletonObject() {}

2.2. To prevents from cloning, simple write a clone() method in Singleton java file and throw an exception saying that cloning is not supported.
public Object clone() throws CloneNotSupportedException {
 throw new CloneNotSupportedException();
} 

2.3. To prevent from de-serialization process, implement Serialization interface and provide the body for both methods readObject() and readResolve() in such a way that it should provide the same current object. The total implementation of a Singleton Java class as follows:
package com.edu;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.lang.reflect.Constructor;
import java.io.Serializable;

public class SingletonObject implements Serializable {
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private static SingletonObject singletonObject;
 public static int x;
 
 private SingletonObject() { }
 
 public static synchronized SingletonObject getInstance() {
  if (singletonObject == null) {
   singletonObject = new SingletonObject();
   singletonObject.x = x + 10;;
  }
  return singletonObject;
 }
 
 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
  ois.defaultReadObject();
  synchronized (SingletonObject.class) {
   if (singletonObject == null) {
    singletonObject = this; // only if everything succeeds
   }
  }
 }
 
 private Object readResolve() throws ObjectStreamException {
        if(singletonObject != null);
        return singletonObject;
    }
 
 
  
 public Object clone() throws CloneNotSupportedException {
  throw new CloneNotSupportedException();
 }
}

3. Why synchronized keyword used for getInstance() method:
Since the singleton instance is a static class variable in the stored in the PermGen space of the heap. This applies getInstance() instance method as well since it is static too. In the multithreading environment to prevent each thread to create another instance of singleton object and thus creating concurrency issue we will need to use locking mechanism. This can be achieved by synchronized keyword. By using this synchronized keyword we prevent Thread2 or Thread3 to access the singleton instance while Thread1 inside the method getInstance().

Client Code:
package com.edu;

import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;

public class SingletonTest {
 
 public static void main(String[] args) throws SecurityException, NoSuchMethodException, 
  IllegalArgumentException, InvocationTargetException, CloneNotSupportedException {
  try {
   
   //1. TO test for new keyword
   /*
   SingletonObject obj1 = new SingletonObject();
   */
   
   //2. To test Class.forName()
   /*
   SingletonObject obj = (SingletonObject)Class.forName("com.edu.SingletonObject").newInstance();
   System.out.println(obj.getInstance().x);
   */
   
   //3. To test Reflection
   /*
   Class obj = (Class) Class.forName("com.edu.SingletonObject");
   Constructor constructor = obj.getDeclaredConstructor();
   SingletonObject obj1 = constructor.newInstance("SingletonObject");
   SingletonObject object = obj1.getInstance();
   */
   
   
   //4. To test cloning
   /*
   SingletonObject obj = SingletonObject.getInstance();
   obj.clone();
   System.out.println(obj.x);
   */
   
   
   //5. To test object de-serialization
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(SingletonObject.getInstance());
            oos.close();

            java.io.InputStream is = new java.io.ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(is);
            SingletonObject singletonObject = (SingletonObject)ois.readObject();
            
            System.out.println(singletonObject.x);
   
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
}

Test Results:
Enable either one option in the test program and run one by one.
1. Will get compilation error.
Error: The constructor SingletonObject() is not visible.
2. Will get Runtime error.
Error:
java.lang.IllegalAccessException: Class com.edu.SingletonTest can not access a member of class com.edu.SingletonObject with modifiers "private"
 at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
 at java.lang.Class.newInstance0(Class.java:351)
 at java.lang.Class.newInstance(Class.java:310)
 at com.edu.SingletonTest.main(SingletonTest.java:21)

3. Will get runtime error:
Error:
java.lang.IllegalAccessException: Class com.edu.SingletonTest can not access a member of class com.edu.SingletonObject with modifiers "private"
 at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:505)
 at com.edu.SingletonTest.main(SingletonTest.java:30)

4. Since we have provided the method implementation for this scenario in such a way that to provide the same copy of an object. It will provide the same.

When to use Singleton Design Pattern:
Any class which you want to be available to whole application and whole only one instance is viable is candidate of becoming Singleton. One example of this is Runtime class , since on whole java application only one runtime environment can be possible making Runtime Singleton is right decision. Another example is a utility classes like Popup in GUI application, if you want to show popup with message you can have one PopUp class on whole GUI application and anytime just get its instance, and call show() with message.

Uses of Singleton Design Pattern:
DB Connection: We can create a Database connection once and can provide a global point of reference.
Configuration File: It creates a single instance of the configuration file which can be accessed by multiple calls concurrently as it will provide static config data loaded into in-memory objects. The application only reads from the configuration file at the first time and thereafter from second call onwards the client applications read the data from in-memory objects.
Cache: We can use the cache as a singleton object as it can have a global point of reference and for all future calls to the cache object the client application will use the in-memory object.

Saturday, 9 November 2013

Spring Batch Flat File Reader reads multiple files


Problem :
Implement a standalone flight search engine that lists matching flights for a traveller who is looking to fly between 2 locations on a given date. The input locations should be in 3-letter location code format as present in the airlines flight data.
Specifications: You are given 3 CSV files, each containing the available flights data for British Airways, Lufthansa Airlines and Air France respectively.
Each file contains the following fields:
  1. FLIGHT_NUM - Unique flight number, starting with 2-letter airline code.
  2. DEP_LOC - Departure location code of the flight.
  3. ARR_LOC - Arrival location code of the flight.
  4. VALID_TILL - Date (DD-MM-YYYY) till which each flight is available. It means that this flight would fly once every day till this date.
  5. FLIGHT_TIME - Local time (HHmm) at which the flight departs from the departure location.
  6. FLIGHT_DURN - Flight duration (HH.mm) from departure location to the arrival location.
  7. FARE - This is the fare of the flight per person in $ (US Dollars).
Following are the constraints should apply:
  • The engine should accept 4 input parameters: Departure Location, Arrival Location, Flight Date, Output Preference. “Output Preference” is a String suggesting whether the flight results should be sorted only by fare, or by both fare and flight duration.
  • The engine should then search for the flights in all 3 CSV files, and list the aggregate results on standard output, sorted by Fare and Flight Duration, depending on “Output Preference”.
  • The engine should be written considering that there could be more CSV files in future, and each CSV file might contain more data than present.
  • If the Departure location or/and Arrival location is not present in any of the CSV files, then engine should return a user friendly error.
  • If there are no flights available for the user entered input parameters, then engine should return a different user friendly error.
Input data: The following PIPE delimited sample data is given to you. You are allowed to extend on this data and create heavier input files. Flight_Details_1.csv
FLIGHT_NUM|DEP_LOC|ARR_LOC|VALID_TILL|FLIGHT_TIME|FLIGHT_DURN|FARE
AF299|FRA|LHR|20-11-2010|0600|4.10|480
AF118|DUB|MUC|21-12-2010|1410|5.40|580
AF371|AMS|MAD|30-11-2010|1210|3.45|320
AF453|BOS|CDG|20-11-2010|1350|7.30|1000
AF544|BOM|LHR|10-12-2010|1150|8.10|950
AF271|AMS|MAD|27-10-2010|1100|3.30|500
AF249|JFK|LHR|01-12-2010|1550|8.40|1030
Flight_Details_2.csv
FLIGHT_NUM|DEP_LOC|ARR_LOC|VALID_TILL|FLIGHT_TIME|FLIGHT_DURN|FARE
AF299|FRA|LHR|20-11-2010|0600|4.10|480
AF118|DUB|MUC|21-12-2010|1410|5.40|580
AF371|AMS|MAD|30-11-2010|1210|3.45|320
AF453|BOS|CDG|20-11-2010|1350|7.30|1000
AF544|BOM|LHR|10-12-2010|1150|8.10|950
AF271|AMS|MAD|27-10-2010|1100|3.30|500
AF249|JFK|LHR|01-12-2010|1550|8.40|1030
Flight_Details_3.csv
FLIGHT_NUM|DEP_LOC|ARR_LOC|VALID_TILL|FLIGHT_TIME|FLIGHT_DURN|FARE
BA123|DEL|AMS|12-10-2010|0050|8.00|950
BA412|BOS|CDG|31-12-2010|0210|7.50|800
BA413|BOS|AMS|30-11-2010|1530|7.00|750
BA111|LHR|PEK|30-10-2010|2340|12.50|1200
BA765|LHR|BOM|31-12-2010|1420|8.50|825
BA322|CDG|NRT|15-11-2010|0010|13.00|1150
BA438|DEL|AMS|30-11-2010|1325|10.50|920
Problem Solution:
  1. Create a Data TransferObject/ValueObject which can hold the properties of the flight.
  2. Create another Data Transfer Objects which suits for search criteria.
  3. Create a mapper class which splits the each line at specified delimiter and map the tokens to ValueObject.
  4. Create a Writer class and implement the business logic.
  5. Make necessary configurations to run the program.
  6. Create a client program and to provide search criteria.
FlightDetailsVO.java: A Data Transfer/Value Object which has the flight properties.
package com.flight.dto;

import java.io.Serializable;
import java.util.Date;

public class FlightDetailsVO implements Serializable {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private String flightNum;
 private String depLoc;
 private String arrLoc;
 private Date validTill;
 private String flightTime;
 private double flightDuration;
 private double fare;
 
 public String getFlightNum() {
  return flightNum;
 }
 public void setFlightNum(String flightNum) {
  this.flightNum = flightNum;
 }
 public String getDepLoc() {
  return depLoc;
 }
 public void setDepLoc(String depLoc) {
  this.depLoc = depLoc;
 }
 public String getArrLoc() {
  return arrLoc;
 }
 public void setArrLoc(String arrLoc) {
  this.arrLoc = arrLoc;
 }
 public Date getValidTill() {
  return validTill;
 }
 public void setValidTill(Date validTill) {
  this.validTill = validTill;
 }
 public String getFlightTime() {
  return flightTime;
 }
 public void setFlightTime(String flightTime) {
  this.flightTime = flightTime;
 }
 public double getFlightDuration() {
  return flightDuration;
 }
 public void setFlightDuration(double flightDuration) {
  this.flightDuration = flightDuration;
 }
 public double getFare() {
  return fare;
 }
 public void setFare(double fare) {
  this.fare = fare;
 }
 
 @Override
 public String toString() {
  return flightNum + "|" 
    + depLoc + "|" 
    + arrLoc + "|" 
    + validTill + "|" 
    + flightTime + "|" 
    + flightDuration + "|" 
    + fare;
 }
 
}

SearchVO.java: It is used to pass the user given search criteria to search engine.
package com.flight.dto;

import java.io.Serializable;
import java.util.Date;

public class SearchVO implements Serializable {
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private String depLoc;
 private String arrLoc;
 private Date flightDate;
 private String sortedBy;
 
 public String getDepLoc() {
  return depLoc;
 }
 public void setDepLoc(String depLoc) {
  this.depLoc = depLoc;
 }
 public String getArrLoc() {
  return arrLoc;
 }
 public void setArrLoc(String arrLoc) {
  this.arrLoc = arrLoc;
 }
 public Date getFlightDate() {
  return flightDate;
 }
 public void setFlightDate(Date flightDate) {
  this.flightDate = flightDate;
 }
 public String getSortedBy() {
  return sortedBy;
 }
 public void setSortedBy(String sortedBy) {
  this.sortedBy = sortedBy;
 }

}
FlightSearchFieldSetMapper.java: It’s a field set mapper and it will fetch all the lines from multiple files and will set to a list and will provide the same to writer.
package com.flight.mapper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import com.flight.dto.FlightDetailsVO;

public class FlightSearchFieldSetMapper implements FieldSetMapper<FlightDetailsVO> {

 private static final String PARSE_ERROR = "PARSE_ERROR";
 @Override
 public FlightDetailsVO mapFieldSet(FieldSet fieldSet) throws BindException {
  
  FlightDetailsVO flightDetailsVO = new FlightDetailsVO();
  
  flightDetailsVO.setFlightNum(fieldSet.readString(0));
  flightDetailsVO.setDepLoc(fieldSet.readRawString(1));
  flightDetailsVO.setArrLoc(fieldSet.readRawString(2));
  
  String validTill = fieldSet.readRawString(3);

  try {
   flightDetailsVO.setValidTill(convertStringToDate(validTill));
  } catch(ParseException pe) {
   flightDetailsVO.setFlightNum(PARSE_ERROR);
  }
  
  flightDetailsVO.setFlightTime(fieldSet.readRawString(4));
           flightDetailsVO.setFlightDuration(Double.parseDouble(fieldSet.readRawString(5)));
  flightDetailsVO.setFare(Double.parseDouble(fieldSet.readRawString(6)));
  
  return flightDetailsVO;
 }
 
 public Date convertStringToDate(String strDate) throws ParseException {
  
  SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
  return formatter.parse(strDate);
 }
 
 public Date converStringToTime(String StrTime) throws ParseException {
  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
  return formatter.parse(StrTime);
 }
 
}
FlightSearchItemWriter.java: It’s Item Writer, in our program it will act as search engine.
package com.flight.writer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemWriter;

import com.flight.dto.FlightDetailsVO;
import com.flight.dto.SearchVO;

public class FlightSearchItemWriter implements ItemWriter<FlightDetailsVO>, ItemStream {
 
 private SearchVO searchVO;

 @Override
 public void write(List<? extends FlightDetailsVO> flightDetailsVOList) throws Exception {
  ArrayList<FlightDetailsVO> flightDetailsList = new ArrayList>FlightDetailsVO<(flightDetailsVOList);
  
  ArrayList<FlightDetailsVO> availFlightsList = new ArrayList>FlightDetailsVO<();
  
  if(getSearchVO() != null) {
   for(FlightDetailsVO currVO : flightDetailsList) {
    if(currVO.getValidTill().compareTo(searchVO.getFlightDate()) >= 0) {
     availFlightsList.add(currVO);
    }
   }
   
   if(availFlightsList != null && availFlightsList.size() > 0) {
    System.out.println("FLIGHT_NUM|DEP_LOC|ARR_LOC|VALID_TILL|FLIGHT_TIME|FLIGHT_DURN|FARE");
    System.out.println("------------------------------------------------------------------");
    
    Collections.sort(availFlightsList, new Comparator<FlightDetailsVO>() {  
        @Override  
        public int compare(FlightDetailsVO o1, FlightDetailsVO o2) {  
            int fareComp = Double.compare(o1.getFare(), o2.getFare());  
            if(searchVO.getSortedBy().equalsIgnoreCase("both")) {
             if (fareComp != 0) {  
                 return fareComp;  
             }  
             int durComp = Double.compare(o1.getFlightDuration(), o2.getFlightDuration());
             return durComp;
            } 
            return fareComp; 
        }  
    });    
    for(FlightDetailsVO availFlightVO : availFlightsList) {
     System.out.println(availFlightVO.toString());
    }
   }
  } else {
   System.out.println("Search criteria is not provided.....");
  }
 }

 @Override
 public void close() throws ItemStreamException {
  
 }

 @Override
 public void open(ExecutionContext arg0) throws ItemStreamException {
  
 }

 @Override
 public void update(ExecutionContext arg0) throws ItemStreamException {
  
 }

 public SearchVO getSearchVO() {
  return searchVO;
 }

 public void setSearchVO(SearchVO searchVO) {
  this.searchVO = searchVO;
 }


}
flight-seaarch-context.xml: It’s a configuration file, which contain all spring beans definitions.

    
 
  
   
    
   
  
 

 
  
  
 
  
 
  
  
  
   
    
     
      
     
    
    
     
    
   
  
 
 
 
  
 
 
 

 
  
 
 
 
  
 



FlightSearchRun.java: Its client program for Flight Search engine.
package com.flight.main;

import java.text.SimpleDateFormat;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.flight.dto.SearchVO;
import com.flight.writer.FlightSearchItemWriter;

public class FlightSearchRun {
 
 public static void main(String[] args) {
  String[] springConfig  = {"resources/xml/flight-seaarch-context.xml"};
  
  ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
  
  JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
  Job job = (Job) context.getBean("flightSearchJob");
  try {
   SearchVO searchVO = new SearchVO();
   searchVO.setDepLoc("DUB");
   searchVO.setArrLoc("MUC");
   searchVO.setSortedBy("both");
   
   SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
   searchVO.setFlightDate(formatter.parse("21-12-2013"));
   
   FlightSearchItemWriter writer = (FlightSearchItemWriter) context.getBean("flightSearchItemWriter");
   writer.setSearchVO(searchVO);
   
   JobExecution execution = jobLauncher.run(job, new JobParameters());
   System.out.println("Exit Status : " + execution.getStatus());

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}