Posts

Showing posts from 2009

New Features in Java Enterprise Edition 6 (JEE 6)

Latest version of Java Enterprise Edition is focused on eae of development and simplicity. Key goals of this version are Extensibility – supports for additional technologies and frameworks Profiles – support for creating custom profiles based on application scope or requirement. Pruning New Features introduced in this edition are Simplified EJB 3.1 No local business interface – one step further from EJB 3.0 specification, EJB 3.1 specifications removes mandatory business interface Singleton EJB – to share application wide data and support concurrent access. EJB 3.1 can be part of war file directly without creating a separate jar file EAR >>       WAR >>             JSP/Static Pages             WEB_INF >>                   web.xml               lib >>               classes >>                          Servlet and Other classes                     

Java Collection Best Practices

There are the few best practices for Java Collections 1. Decide approximate initial capacity of a collection -  As Collection implementations like ArrayList, HashMap, use Array internally for storing, you can speed up things  by setting initial capacity. 2. Use ArrayLIst, HahMap etc instead of VEctor , Hashtable to avoid synchronization overhead. Even better use array where ever possible. 3. Program to interface instead of implementation – It gives freedom of switching implementation without touching code. For Example : Use List list = new ArrayList(); Avoid : ArrayList list = new ArrayList(); 4. Always return empty collection object instead of null . For Example : Use public List getCustomer(){         //...         return new ArrayList(0); } Avoid : public List getCustomer(){         //...         return null;     } 5. Generally you use a java.lang.Integer or a java.lang.String cl

Implementing default parameter value in Java method

      Those who already familiar with C++ will be missing default parameter values to a function call in Java. Here is the way of implementing same feature in Java.        In Java language , method can accept one or multiple object instance as parameters. Here idea is to pass one object instance instead of multiple primitive type parameters. This object will have default values set to all or some of the object members fields. This explains how to implement the same by example. Here is an example of Simple Draw class as below. Parameter Class : package demo.graphics; public class Option {     public Option() {         //Setting Default Parameters values         color = "BLACK";         lineWidth = 1;         pattern =0;         this.startX = 0;         this.startY = 0;         this.endX = 0;         this.endY = 0;     }     private String color;     private int lineWidth ;     private int startX;

Strategy Design Pattern by example

This post explains Strategy design pattern using an example. When to Use ? – Its an alternative to Template Design Pattern. Use this pattern when template methods implementation as well as steps in which they need to execute are dynamic. Its like no of steps in algorithm vary or the class that implements the steps needs as independent inheritance hierarchy . How to use ? – Implement an interface defining the individual steps. Define a class that implements this interface ( individual steps) , this class is not forced to inherit from an abstract template super class. Main algorithm implementation class uses this interface implementation for each of algorithm steps execution An example – Same example as Template Method Design pattern is being used, Process order. Interface - package strategypattern; import java.util.List; import templatemethod.Order; public interface ProcessOrderHelper {     public  boolean  validateCustomer(String custName, int custId);    

Template Method Design Pattern by Example

This post explains Template Design Pattern using a simple example. When to Use ?   – When you know the steps of an algorithm and order in which they should be performed, but don’t know how to perform all of the steps or implementations of the steps vary from user to user. This pattern is the solution for algorithm implementation where invocation order of steps is fixed, but steps implementation may vary for example getting data from database or file system etc. How to Use ? – Encapsulate all individual steps steps we don’t know how to perform as abstract methods in an super abstract class and provide a final method that invokes steps in correct order. Concrete subclasses of this super abstract class implement the abstract methods that performs individual steps. They concept is that super class controls invocation order of the steps and leave implementation of these steps up to end user.       When implementing the Template Method pattern, the abstract class must factor out those

Deploying Services to OC4J with RESTful support

Image
       OC4J container provide support for RESTful services. OC4J installation comes with web service assembling tool (wsa) which take an extra optional parameter to give REST support. This post explains step by step approach to implement and deply RESTful services to OC4J. 1. Interface Code package demo.service; import java.rmi.Remote; import java.rmi.RemoteException; public interface StringService extends Remote{     public String toUppercase(String str) throws RemoteException;     public String toLowerCase(String str)throws RemoteException; } 2. Implementation for Interface package demo.service; import java.rmi.Remote; import java.rmi.RemoteException; public class StringServiceImpl {     public StringServiceImpl() {     }     public String toUppercase(String str){         return str.toUpperCase();     }     public String toLowerCase(String str){         retu

Building WebService with Custom Serializer using JDeveloper

Image
      JDeveloper 10g comes with UI based web service assembler. It converts simple java class to web service. You can customize web service assembling using Custom Serializer in case Service class accepts pojo class as parameters or return it. This post explains creating web service from simple java class. It is considered that you have gone through previous posts – JAXB by Jdeveloper and XML Document by Jdeveloper . So you have xml schema , xml document and set of java classes generated by JAXB compilation Here is step by step approach 1.    Code a CustomSerializer , it has to implement SOAPElementSerializer and override deserialize and serialize methods. Sample code is as below package jaxb.serializer; import javax.xml.bind.JAXBException; import oracle.webservices.databinding.SOAPElementSerializer; import javax.xml.soap.SOAPFactory; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPElement; import javax.xml.soap.Text; import javax.xm

JAXB with JDeveloper

Image
      Jdeveloper 10g comes with JAXB1.0 tool .  This tool makes XML programming just one click away.This tutorial explains using JABX compilation tool of JDeveloper. In previous post we have seen handling of XML schema and XML document . You can refer the same as prerequisite for this post . Here is the step by step approach 1. Compile xml schema 2. Mention package and Test class 3. This will generate set of java classes under mentioned java package.You will notice one interface and implementation for each complex type or node. ItemType.java and ItemTypeIMpl.java get generated for  complex types ItemType. As we had selected Main class for generation, it creates Main class for testing JAXB compilation.           4. Here is the marshalling and un-marshalling code . [ refer this post for JAXB Coding ] package jaxb.test; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Mar

Schema to XML document by JDeveloper

Image
This tutorial explain creating xml schema from scratch and creating xml document from it as well. Jdeveloper 10g has a very easy visual editor for creating xml schema as well as xml document. It is considered that you have basic knowledge of schema. Here are step by step approach 1.Create new project and them create new XML schema   2.By default, a simple schema header tag  and an example element   3. Here we are going to construct schema for an Order xml document like  Order>>Item_list>>Item . A order will have id,description,shipTo address and list of items. To construct such structure we will have three complex type order_type, item_type and order_list_type. So create order_type complex type by drag and drop Complex Type component and edit properties   4. Now drag and drop sequence component to complex component 5.  now drag and drop element components one by one and edit the properties   6. There will multiple items under an order

Separate Content, behavior and Presentation

Today the most dynamic thing in this world is the Web. These pages are made of no of things which are categories as presentation, behavior or functional and content. It would be very beneficial  to maintain all these artifacts loosely coupled. Here is a simple example of keeping separate presentation and functional logic. In a sign in page , user has to enter login name which server will check for availability. Here is sample code <input type="text" name="loginName" value="Enter Login Name" onblur="checkavailibility"/> This tag invoke “checkavailibility()” function on blur event. To maintain loose coupling we can rewrite same logic in a java script file as window.onload="initPage"; function initPage(){     document.getElementById("loginName").onblur="checkavailibility" } Here initPage function get called on onLoad page event. This function access page element and set onBlur prop

Publishing Simple Java class as Web Service using Axis

Image
In Previous post on Axis ( http://ydtech.blogspot.com/2009/06/deploying-apache-axis-to-oracle-oc4j.html ), I have explained how to deploy Axis as a Web Application. Now its time to utilize Axis web service framework. Apache Axis provides a very simple way of publishing any simple java class as Web Service. This post explains it in simple steps. 1. Code your service implementation as a Simple java class. For this example we take example of simple String Conversation Service( lower case to upper case). This is only for demo purpose. Now Our pojo java class would be public class StrConvService {     public StrConvService() {     }     public String invoke(String str){         return str.toUpperCase();     } } 2. Name this file as StrConvService.jws and copy it to <OC4J_Home>/jdev/home/application/<Axis_WebApp_Home>/<webapp> 3. Got to http://localhost:8888/axis-webapp/StrConvService.jws   4. Click on WSDL link on

Deploying Apache Axis to Oracle OC4J Application Server

Image
            Apache Axis is a framework processing SOAP message. It gives a very flexible ways of handling SOAP message. It provides very convenient  ways of creating and invoking web services. You can leave overhead of handling SOAP message to axis framework.  Axis is installed as Web Module in any application server, this web module will acts as wrapper Web Service for your simple pojo java classes. Any simple java class will get converted to web service using this Axis Web module. This tutorial explain deploying Axis to OC4j application server. 1. First download Axis archive from http://people.apache.org/dist/axis/nightly/ . 2. Extract the zip file. 3. to deploy Axis as Web App to OC4j , we need to create archive file (war) and then deploy through console. So to create a war file go to command prompt and go to <asxis_home>/webapps/axis/ 4. execute the command    jar –cvf axis-webapp.war .   This command will create axis-webapp.war file in current directory. 5.Now go

HTTP 1.0 to HTTP 1.1

A major improvement in the HTTP 1.1 standard is persistent connections. In HTTP 1.0, a connection between a Web client and server is closed after a single request/response cycle. In HTTP 1.1, a connection is kept alive and reused for multiple requests. Persistent connections reduce communication lag perceptibly, because the client doesn't need to renegotiate the TCP connection after each request.

REST vs SOAP based Web Services

Another Web service approach is getting popular i.e. REST based services. REST stands for Representational State Transfer. Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. In REST-full web services, the emphasis is on simple point-to-point communication over HTTP using plain old XML(POX). But architects and developers need to decide when this particular style is an appropriate choice for their applications. A RESTFul design may be appropriate when The web services are completely stateless. A caching infrastructure can be leveraged for performance. If the data that the web service returns is not dynamically generated and can be cached, then the caching infrastructure that web servers and other intermediaries inherently provide can be leveraged to improve performance. However, the developer must take care because such caches are limited to the HTTP GETmethod for most servers. The service produc

First Hand on JCS

Java Caching system (JCS) is open source java based caching system. JCS is useful for high read , low put application. The base of JCS is the Composite Cache, which is the pluggable controller for a cache region. There are fews terms relatated to JCS such as Element, Region, Auxiliaries etc  JCS is an object cache, exactly like putting object in hashtables. These objects are nothing but elements and each hashtable is Cache Region and customisable plugins to region are Auxiliaries.  You can read more about JCS at   http://jakarta.apache.org/jcs/index.html . This tutorial gives simple steps to get JCS working. 1. Download JCS from  http://jakarta.apache.org/jcs/DownloadPage.html 2. JCS needs two more jar files  1. common-logging.jar You will get this jar from apache tomcat installtion 2. concurrent.jar  You will get required java source code at  http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html. Once you download source c