Archive for November 2010

what happens after death?

Last night me, dharam paji and sanjay (not the one from mahabharat) had discussion on the basic questions which science has not been able to answer yet. questions like - what happens after death e.g.? we discussed about kriya yog and lot of meditation exercises using which we can control our breath, soul consciousness etc. it went over 3 hours with no results, so here i am putting a basic question -
1. what happens after death? 
2. do we chose our next life (parents, family etc)?
3. do we take our consciousness with us when we die?
4. being in kalyug and influenced by nears and dears, can we follow what Geeta says?
5. can we practice kriya yoga to convert ourself to pure energy (as everything in this universe is made of energy and Shiva being the supreme creator of that energy)?

there are lots of questions, lets start with the above set only ... any thoughts?
Tuesday, November 30, 2010
Posted by Munish Gogna

Exposing existing business object as a web service and using JMS to publish events ...

I am thinking of my next article where in I will be taking one existing EJB 3.0 bean or a plain pojo doing some particular piece of business and publishing it as a web service and using JMS to publish the events (like auditing the events to the database).

I would love to have ideas from you guys about what should I focus and plan for my next article around exposing already existing piece of code as a web service. So if you have a piece of code that you want to expose as a web service, you are at the right place :)

Looking forward.
Sunday, November 28, 2010
Posted by Munish Gogna

Creating and Deploying JAX-WS web service on Tomcat 6

Hi recently I had to provide a wrapper around an EJB 3.0 remote service to come up with a simple web service project that would be deployed over Tomcat and accessed in a simple http way due to some accessibility issues. Now as I cannot reveal the actual requirement I implemented last week so here i am presenting a simple demo kind of service with following signature.

public AccountDetails getAccountDetails(String accountNo, SecurityToken token);

The service will return the account details of a particular account number, provided the token is valid (generated using some Security module of the application). In nutshell, the client will ask for a token from Security module and then invoke this method. The service will validate the token to see if the caller can invoke the method or not?
Sounds good.. lets move ahead.

Libraries we are going to use include JAXB and JAX-WS, as both of them have sensible defaults, the number of annotations can be kept to the minimum. Also in my opinion, it is always best to develop WSDL and schemas by hand to ensure that the service contract is appropriately defined and also that the schema can be re-used (by other services) and extended if necessary. I do not prefer using annotations for automatically producing WSDL and schema at runtime so let's start with the definition, i am using inline schema as we have a very simple requirement here, though we should have schema definitions in separate xsd files.


<?xml version="1.0" encoding="UTF-8"?>
<definitions name="AccountDetails"

<xsd:schema xmlns=""
elementFormDefault="qualified" targetNamespace="">
<element name="geAccountDetails_INTO" type="tns:GeAccountDetails_INTO" />
<element name="accountDetails_TO" type="tns:AccountDetails_TO" />
<element name="accountDetailsFault" type="tns:AccountDetailsFault" />
<element name="securityToken" type="tns:SecurityToken" />
<complexType name="GeAccountDetails_INTO">
<element name="accountNo" type="xsd:string" />
<complexType name="AccountDetails_TO">
<element name="accNo" type="xsd:string" />
<element name="accType" type="xsd:string" />
<element name="balance" type="xsd:decimal" />
<complexType name="SecurityToken">
<element name="token" type="xsd:string" />
<element name="validTill" type="xsd:date" />
<complexType name="AccountDetailsFault">
<element name="faultInfo" type="xsd:string" />
<element name="message" type="xsd:string" />

<!-- messages format -->
<message name="accountDetailsRequest">
<part name="parameters" element="tns:geAccountDetails_INTO" />
<part name="request_header" element="tns:securityToken"/>
<message name="accountDetailsResponse">
<part name="parameters" element="tns:accountDetails_TO" />
<message name="accountDetailsFault">
<part name="faultInfo" element="tns:accountDetailsFault" />

<!-- define getAccountDetails operation here -->
<portType name="AccountDetailsPortType">
<operation name="getAccountDetails">
<input message="tns:accountDetailsRequest" />
<output message="tns:accountDetailsResponse" />
<fault message="tns:accountDetailsFault" name="accountDetailsFault"/>

<!-- bind the operations -->
<binding name="AccountDetailsBinding" type="tns:AccountDetailsPortType">
<soap:binding transport="" style="document" />
<operation name="getAccountDetails">
<soap:operation soapAction="getAccountDetails" />
<soap:body use="literal" parts="parameters"/>
<soap:header use="literal" part="request_header" message="tns:accountDetailsRequest"></soap:header>
<soap:body use="literal" />
<fault name="accountDetailsFault">
<soap:fault name="accountDetailsFault" use="literal" />

<!-- name and location of the service -->
<service name="AccountDetailsService">
<port name="AccountDetailsPort" binding="tns:AccountDetailsBinding">
<soap:address location="http://localhost:8080/account/details" />

I am not going to explain the In and outs of this wsdl , in a nutshell it defines one operation 'getAccountDetails' which takes accountNo and returns back the account details like account type, balance etc. Please note that I have also added a security header token that will validate the caller (left to the implementation of the service).

Now as we are done with our wsdl and schema, let's generate the portable artifacts from our service definition. JAX-WS includes a tool that can do this for us, we will use this tool to generate portable artifacts like Service Endpoint Interface (SEI), Service and Exception classes.These artifacts can be packaged in a WAR file with the WSDL and schema documents along with the endpoint implementation to be deployed.

To generate the artifacts , we run following command:
wsimport C:\devel\workspace\webservice\WebContent\WEB-INF\wsdl\accounts.wsdl   -p -keep -Xnocompile

This will create the following artifacts in package.
Now as we have got all the artifacts we are ready to implement our service, the interface we need to implement is AccountDetailsPortType , so let's do it.

Here is our dummy implementation class:

import java.math.BigDecimal;
import javax.jws.WebService;

@WebService(name = "AccountDetailsService", 
   portName = "AccountDetailsPort", 
   endpointInterface = "", 
   wsdlLocation = "WEB-INF/wsdl/accounts.wsdl",
public class AccountDetailsServiceImpl implements AccountDetailsPortType {
public AccountDetailsTO getAccountDetails(GeAccountDetailsINTO parameters,
SecurityToken requestHeader) throws AccountDetailsFault_Exception {
AccountDetailsTO detailsTO = new AccountDetailsTO();
// validate token 
// populate response
detailsTO = getDetailsFromSomewhere (parameters.getAccountNo());
return detailsTO;

private AccountDetailsTO getDetailsFromSomewhere(String accountNo) throws AccountDetailsFault_Exception {
if(accountNo == null || accountNo.trim().length()==0){
AccountDetailsFault faultInfo = new AccountDetailsFault();
faultInfo.setFaultInfo("missing account number");
faultInfo.setMessage("acclount number is required field");
throw new AccountDetailsFault_Exception("account no missing", faultInfo);
AccountDetailsTO detailsTO = new AccountDetailsTO();
detailsTO.setBalance(new BigDecimal(10000));
return detailsTO;

private void validateToken(SecurityToken requestHeader) throws AccountDetailsFault_Exception {

if ("83711070".equals(requestHeader.getToken()) && requestHeader.getValidTill() != null){
System.out.println("token processed successfully...");
} else {
AccountDetailsFault faultInfo = new AccountDetailsFault();
faultInfo.setFaultInfo("Header token Invalid");
faultInfo.setMessage("can't help");
throw new AccountDetailsFault_Exception("invalid token", faultInfo);

This is just a dummy implementation for illustration purpose only. Now as our service implementation is done, we proceed to package this in a war and deploy it on tomcat.

Now we create a standard web.xml, which defines WSServletContextListenerWSServlet and structure of a web project.

Next we create a sun-jaxws.xml, defines the web service implementation class.

This file is required regardless of whether we publish our web service on tomcat, glassfish or any other server. OK so far so good , let's build our application and deploy it on tomcat, here is the ant script.


<project name="webservice" basedir="../" default="deploy">

<!-- Project settings -->
<property name="project.distname" value="account" />
<!-- Local system paths -->
<property file="${basedir}/ant/" />
<property name="webroot.dir" value="${basedir}/WebContent" />
<property name="webinf.dir" value="${webroot.dir}/WEB-INF" />
<property name="build.dir" value="build" />

<path id="compile.classpath">
<!-- classpath for Jax WS related stuff -->
<pathelement path="${webinf.dir}/lib/activation.jar" />
<pathelement path="${webinf.dir}/lib/jaxb-api.jar" />
<pathelement path="${webinf.dir}/lib/jaxb-impl.jar" />
<pathelement path="${webinf.dir}/lib/jaxp-api.jar" />
<pathelement path="${webinf.dir}/lib/jaxws-api.jar" />
<pathelement path="${webinf.dir}/lib/jaxws-rt.jar" />
<pathelement path="${webinf.dir}/lib/jsr173_api.jar" />
<pathelement path="${webinf.dir}/lib/jsr181_api.jar" />
<pathelement path="${webinf.dir}/lib/resolver.jar" />
<pathelement path="${webinf.dir}/lib/saaj-api.jar" />
<pathelement path="${webinf.dir}/lib/saaj-impl.jar" />
<pathelement path="${webinf.dir}/lib/sjsxp.jar" />
<pathelement path="${webinf.dir}/lib/stax-ex.jar" />
<pathelement path="${webinf.dir}/lib/streambuffer.jar" />
<pathelement path="${webinf.dir}/classes" />
<pathelement path="${classpath.external}" />
<pathelement path="${classpath}" />

<!-- define your folder for deployment -->
<property name="deploy.dir" value="deploy" />
<!-- Local system paths -->
<property file="${basedir}/ant/" />
<property name="webroot.dir" value="${basedir}/WebContent" />
<property name="webinf.dir" value="${webroot.dir}/WEB-INF" />
<property name="build.dir" value="build" />
<!-- Check timestamp on files -->
<target name="prepare">
<tstamp />
<!-- Copy any resource or configuration files -->
<target name="resources">
<copy todir="${webinf.dir}/classes" includeEmptyDirs="no">
<fileset dir="JavaSource">
<include name="**/*.conf" />
<include name="**/*.properties" />
<include name="**/*.xml" />
<!-- Normal build of application -->
<target name="compile" depends="prepare,resources">
<javac srcdir="JavaSource" destdir="${webinf.dir}/classes">
<classpath refid="compile.classpath" />
<!-- Remove classes directory for clean build -->
<target name="clean" description="Prepare for clean build">
<delete dir="${webinf.dir}/classes" />
<mkdir dir="${webinf.dir}/classes" />
<!-- Build entire project -->
<target name="build" depends="prepare,compile" />
<target name="rebuild" depends="clean,prepare,compile" />
<!-- Create binary distribution -->
<target name="war" depends="build">
<mkdir dir="${build.dir}" />
<war basedir="${webroot.dir}" warfile="${build.dir}/${project.distname}.war" webxml="${webinf.dir}/web.xml">
<exclude name="WEB-INF/${build.dir}/**" />
<exclude name="WEB-INF/src/**" />
<exclude name="WEB-INF/web.xml" />
<!-- Create Client -->
<target name="jar">
<jar destfile="${build.dir}/${project.distname}_client.jar"
<!-- deploy on tomcat -->
<target name="deploy" depends="war,jar">
<delete file="${deploy.dir}/${project.distname}.war" />
<delete dir="${deploy.dir}/${project.distname}" />
<copy file="${build.dir}/${project.distname}.war" todir="${TOMCAT_HOME}\webapps" />

NOTE: jars in lib should be carefully chosen, otherwise it will make your life hell.

Now let's test the application, point your browser to http://localhost:8080/account/details , if you see something like this, it means you have successfully deployed the service.

Now let's test the service, we can use the client generated by the wsimport tool as :

public static void main(String[] args) throws Exception {
AccountDetails accountDetails = new AccountDetails();
AccountDetailsPortType port = accountDetails.getAccountDetailsPort();
AccountDetailsTO details = port.getAccountDetails(new GeAccountDetailsINTO(), new SecurityToken());

For those who want to invoke the service using soap stuff, they can use tool like soapUI (can be downloaded from, lets make some soap calls now:

case: invalid security token

case : valid security token
Add caption

Directory structure of this example is as follow:
By default, Tomcat does not comes with any JAX-WS dependencies, So, you have to include it manually.
2. Download JAX-WS RI distribution.
3. Unzip it and copy the jars mentioned in build.xml to lib folder.

Thats all for now ... Please provide your valuable comments or any suggestion.

It's Munish Gogna signing off now, have to prepare Dinner :)

Posted by Munish Gogna
Tag :

source code of the JAX WS example..

You can now download the source code of the 'Creating and Deploying JAX-WS web service on Tomcat 6' article at dzone (check the resource section at bottom).

Here are the details: 
Posted by Munish Gogna

Popular Post


enums (1) java (2) JAX-RS (1) JPA (1) mysql (1) request 2 (1) RESTful (1) sphinx (1) tomcat (1) web service (2) ws (2)