Wednesday, April 6, 2011

Spring Web Service - Part III ( Creating Web Services by java bean both for SOAP 1.1 & SOAP 1.2)

In the Last part of the servies, we will learn how to Develop web services that will support both SOAP 1.1 and SOAP 1.2 protocol, (Spring WS does not provide out of box support for this and we need to tweak the things little bit).
There are many ways through which you can generate the Web services, like
  • Manually creating the WSDL file and place it in WEB-INF directory of your web application.
  • Creating the Request XML XSD and let the framework to create the WSDL
The Second one is preferred approach as compared to the first one, as you does not have to code the WSDL directly either with tool or yourself. 
In order to create the following tutorial, i had taken help from a number of blogs, following are the their links, however I could not find a complete one, so summarizing all this in a single post.
Listed here are the steps:
  1. First we will create a xsd which represents the schema of the input xml, below is a snipped of it,  It only contains three fields that need to be passed to the Server, ( ID, Name, Email) and in response two things are being returned to the client ( CODE, DESCRIPTION)
    
    
        
            
                
                    
                    
                    
                
            
        
    
     
            
                
                    
                    
                    
                
            
        
    
        
            
                
                 
                 
                
            
        
    
        
            
                
                     
                     
                
            
        
    
        
            
                
                
            
        
    
        
      
       
        
        
        
       
      
        
    
        
         
       
        
        
       
      
        
    
    
    
    it Clearly Shows that the contents of Subscription Request and Subscription Response, as described above.
  2. Now we need to create the Java Objects corresponds to both Request and Response.
    SubscriptionRequest.java

    package com.test;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    /**
     *
     * @author PankajB
     */
    @XmlRootElement(namespace="http://krams915.blogspot.com/ws/schema/oss",name="subscriptionRequest")
    public class SubscriptionRequest {
    
     private String id;
     private String name;
     private String email;
    
       @XmlElement(name="email",namespace="http://krams915.blogspot.com/ws/schema/oss")
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
       @XmlElement(name="id",namespace="http://krams915.blogspot.com/ws/schema/oss")
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        @XmlElement(name="name",namespace="http://krams915.blogspot.com/ws/schema/oss")
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
    }
    
     SubscriptionResponse.java
    package com.test;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    /**
     *
     * @author PankajB
     */
    @XmlRootElement(namespace = "http://krams915.blogspot.com/ws/schema/oss", name = "subscriptionResponse")
    public class SubscriptionResponse {
    
        private String code;
        private String description;
    
        @XmlElement(name = "code", namespace = "http://krams915.blogspot.com/ws/schema/oss")
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        @XmlElement(name = "description", namespace = "http://krams915.blogspot.com/ws/schema/oss")
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    }
    
    

  3. Now we need to write our main POJO which which handle the request and generate a response. It pretty simply class, annotated with some special annotations.
    package com.test;
    
    import javax.xml.ws.soap.SOAPBinding;
    import org.springframework.ws.server.endpoint.annotation.Endpoint;
    import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
    import org.springframework.ws.server.endpoint.annotation.RequestPayload;
    import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
    
    /**
     *
     * @author PankajB
     */
    @Endpoint(value = SOAPBinding.SOAP12HTTP_BINDING)
    public class SubscriptionService {
    
        @PayloadRoot(localPart = "subscriptionRequest", namespace = "http://krams915.blogspot.com/ws/schema/oss")
        @ResponsePayload
        public SubscriptionResponse processSubscription(@RequestPayload SubscriptionRequest subscriptionRequest) {
    
    
            SubscriptionResponse response = new SubscriptionResponse();
            System.out.println("Coming Here.........  " + subscriptionRequest.getName());
            response.setCode("234");
            response.setCode("Successfully Executed the application.");
    
            return response;
    
        }
    }
    
    
    here it simply, receive the Payload ( what is being sent by the client in the XML and converted to our SubscriptionRequest Object and generates an SubscriptionResponse type of object, which will again be automatically going to be converted by the Spring Jaxb marshaller in the XML format.
    This Ends the first part of the story, now let's move to the configuration.
  4. First of all in the configuration files, let's visit web.xml
    
        
            spring-ws
            org.springframework.ws.transport.http.MessageDispatcherServlet
            
                transformWsdlLocationstrue
            1
        
        
            spring-ws
            /service/*
        
        
            
                30
            
        
        
            index.jsp
        
    
    
    
    This Simply tells the container that this servlet is going to receive any of the call that matches with the URI /service/*

  5. Now we have the spring-ws-servlet.xml (spring-ws comes from servlet name above in web.xml) which will define all the components related to Spring Web Service.
    
        
        
        
       
            
         
        
        
        
        
        
            
      
        
            
                
                    com.test.SubscriptionRequest
                    com.test.SubscriptionResponse
    
                
            
            
                
                    
                
            
        
    
        
            
        
    
     
        
    
     
        
            
            
        
    
     
        
                
        
       
    
    

    Most of the Things has been commenting in the XML itself, for better understanding.

  6. Since we need to support both SOAP 1.1 & SOAP 1.2 we need to write out own factory, that is going to be an implementation of SoapMessageFactory.
    /*
     *
     * This All has been done as per the things present at this link.
     * http://forum.springsource.org/showthread.php?t=56560
     *
     */
    
    package com.test.soap;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.web.context.request.RequestAttributes;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.ws.WebServiceMessage;
    import org.springframework.ws.soap.SoapMessage;
    import org.springframework.ws.soap.SoapMessageFactory;
    import org.springframework.ws.soap.SoapVersion;
    import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
    import org.springframework.ws.transport.TransportInputStream;
    
    public class MyFactory implements SoapMessageFactory, InitializingBean {
            // This is the Request Context Attribute.
     private static final String REQUEST_CONTEXT_ATTRIBUTE = "MyFactory";
    
     private static final Log logger = LogFactory.getLog(MyFactory.class);
    
            // Two message factories for processing two differnet types of protocols.
     private SaajSoapMessageFactory soap11MessageFactory = new SaajSoapMessageFactory();
     private SaajSoapMessageFactory soap12MessageFactory = new SaajSoapMessageFactory();
    
            // This Object, will be responsible for choosing the Protocol on Runtime, it can be application/xml or text/xml (SOAP 1.2 & SOAP 1.1)
     private SoapProtocolChooser soapProtocolChooser = new MySoapProtocolChooser();
    
     private void setMessageFactoryForRequestContext(SaajSoapMessageFactory mf) {
      RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
      attrs.setAttribute(REQUEST_CONTEXT_ATTRIBUTE, mf, RequestAttributes.SCOPE_REQUEST);
     }
    
     private SaajSoapMessageFactory getMessageFactoryForRequestContext() {
      RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
      SaajSoapMessageFactory mf = (SaajSoapMessageFactory) attrs.getAttribute(REQUEST_CONTEXT_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
      return mf;
     }
    
            // Function called, when we are settign the SOPA Version
     public void setSoapVersion(SoapVersion version) {
                System.out.println("setSoapVersion called with: " + version + " -- ignoring");
     }
    
            // This Function, will set teh SOAP Proptocl chooser
     public void setSoapProtocolChooser(SoapProtocolChooser soapProtocolChooser) {
                System.out.println("Setting out the SOAP Protocol Chooser");
      this.soapProtocolChooser = soapProtocolChooser;
     }
    
            // Function will be invoked, when Spring will create the Bean.
     public void afterPropertiesSet() throws Exception {
      soap11MessageFactory.setSoapVersion(SoapVersion.SOAP_11);
      soap11MessageFactory.afterPropertiesSet();
      soap12MessageFactory.setSoapVersion(SoapVersion.SOAP_12);
      soap12MessageFactory.afterPropertiesSet();
                    System.out.println("Setting both the SOAP Version to 1.1 and 1.2");
     }
    
    
        // Function for creating the Web Service Message.
        public SoapMessage createWebServiceMessage() {
            return getMessageFactoryForRequestContext().createWebServiceMessage();
        }
    
        // Function for creating the Web Service Message from inputStream. 
        public SoapMessage createWebServiceMessage(InputStream inputStream) throws IOException {
            setMessageFactoryForRequestContext(soap12MessageFactory);
      if (inputStream instanceof TransportInputStream) {
                TransportInputStream transportInputStream = (TransportInputStream) inputStream;
             if (soapProtocolChooser.useSoap11(transportInputStream)) {
              setMessageFactoryForRequestContext(soap11MessageFactory);
             }
            }
      SaajSoapMessageFactory mf = getMessageFactoryForRequestContext();
      if (mf == soap11MessageFactory) {
       System.out.println("Final soapMessageFactory? " + soap11MessageFactory);
      } else {
       System.out.println("Final soapMessageFactory? " + soap12MessageFactory);
      }
      return mf.createWebServiceMessage(inputStream);
        }
    
        
    }
    
    This class is not alone itself, and works with the help of some associating classes & interfaces described below.
    SoapProtocolChooser.java Interface: This is the Interface that is being defined for Choosing a SOAP Protocol chooser class at run time.
    package com.test.soap;
    
    import java.io.IOException;
    
    import org.springframework.ws.transport.TransportInputStream;
    
    public interface SoapProtocolChooser {
     public boolean useSoap11(TransportInputStream transportInputStream) throws IOException;
    }
    
    MySoapProtocolChooser.java Implementation of Above Defined Interface:
    /**
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.test.soap;
    
    /**
     *
     * @author PankajB
     */
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.ws.transport.TransportInputStream;
    
    public class MySoapProtocolChooser implements SoapProtocolChooser {
    
        private static final Log logger = LogFactory.getLog(MySoapProtocolChooser.class);
        private static final Pattern userAgentPattern = Pattern.compile("html", Pattern.CASE_INSENSITIVE);
    
        public boolean useSoap11(TransportInputStream transportInputStream) throws IOException {
            for (Iterator headerNames = transportInputStream.getHeaderNames(); headerNames.hasNext();) {
                String headerName = (String) headerNames.next();
                logger.debug("found headerName: " + headerName);
                for (Iterator headerValues = transportInputStream.getHeaders(headerName); headerValues.hasNext();) {
                    String headerValue = (String) headerValues.next();
                    logger.debug("     headerValue? " + headerValue);
                    // Something weird with case names
                  /*  if (headerName.toLowerCase().contains("user-agent")) {
                    System.out.println("UserAgent  - " + headerValue);
                    Matcher m = userAgentPattern.matcher(headerValue);
                    if (m.find()) {
                    logger.debug("Found AXIS in header.  Using SOAP 1.1");
                    return true;
                    }
                    }*/
                    // This is the code written in order to support multiple Endpints by selection of SOAP
                    if (headerName.toLowerCase().contains("content-type")) {
                        logger.debug("Content Type  - " + headerValue);
    
                        if (headerValue.trim().toLowerCase().contains("text/xml")) {
                            logger.debug("Found text/xml in header.  Using SOAP 1.1");
                            return true;
                        }
    
                    }
                }
            }
            return false;
        }
    }
    
    
    The Above code is pretty self explanatory.
    This Class is being invoked by MyFactory class in method createWebServiceMessage(InputStream) to determine which SOAP Message Factory is going to be used, either 1.1 or 1.2
That's all we required, now we can simply deploy our application to the web container after creating a WAR file.
Now simply call the HTTP URI: http://localhost:8080/SpringWSGen/service/service/SubService/subscription.wsdl
( Note: service has been writted two times because:
      1. we have service/* servlet mapping, so first one belongs to it.
      2. we have service/SubService in the dynamic WSDL generation tag in sprng-ws-servlet.xml, so second one refers to it. Obviously one can go ahead with any mappings he want )
you will see the following WSDL Generated. now it can be invoked in any tool example SOAP UI to get to know which one is being called. As the WSDL File has the bindings for both the SOAP 1.1 & SOAP 1.2

  So that's all for the time beings. here can you create Spring Web Services through this. I will have some interesting URL's thta just add to the above functionaltiy, that i will add later on.
Thanks Spring, for creating such a light weight framework.
Please let me know, in case you want the complete source code. I will host it somewhere.

Saturday, March 5, 2011

Spring Web Services 2 - Part II ( Marshalling , UnMarshalling using JAXB)

In second part of the series, we will talk about how to use Marshaller's in order to automatically convert your request and response object to the SOAP Body XML (of request n response ) with the corresponding Namespace defined in the Java Source files.
Just like the first part, we will use Spring WS2- Part I we will use the same web service hosted at W3Schools.com but this time, with a very short code, as compared to Part-I .
Spring Web Services 2 makes use of a number of marshallers and unmarshallers in order to perform the tasks that include Spring OXM, Castom, Jaxb2. As JAXB 2 is the specification, so we will go by that only.

Note: Please add Jaxb libraries in your classpath while performing the action during this tutorial.

Configuration: So we will directly delve in the spring configuration (acjaxb.xml) as required for this tutorial.



    
    
    
    
        
       
            

                springwsjxb.CelsiusToFahrenheit
                springwsjxb.CelsiusToFahrenheitResponse
                
            
        
  
        
            
                
            
        
    

    
        
    

     
     
        
        
        
        
        
    



As our current configuration describes that we are using two classes CelsiusToFahrenheit and CelsiusToFahrenheitResponse present in the springws packagejxb. Here is the corresponding source code for both of them. The Source code includes the Namespace details also, it becomes very important once we need to generate the XML, in any case if we missed out the namespace the coressponding web service will not be able to parse it and will throw an exception.
CelsiusToFahrenheit.java


package springwsjxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 *
 * @author PankajB
 */
@XmlType(name="tem")
@XmlRootElement(name="CelsiusToFahrenheit",namespace="http://tempuri.org/")
public class CelsiusToFahrenheit {

    private int celsius;
   

    @XmlElement(name="Celsius",namespace="http://tempuri.org/")
    public int getCelsius() {
        return celsius;
    }

    public void setCelsius(int celsius) {
        this.celsius = celsius;
    }
}

CelsiusToFahrenheitResponse.java



package springwsjxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="CelsiusToFahrenheitResponse",namespace="http://tempuri.org/")
public class CelsiusToFahrenheitResponse {

    private int result;

    //CelsiusToFahrenheitResult is the name of the XML Element which is being returned from the Web service and belongs to namespace "http://tempuri.org/"

    @XmlElement(name="CelsiusToFahrenheitResult",namespace="http://tempuri.org/")
    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

}

As per the requirements of the web service, we had kept only those variables here in our objects.
Now our Main.java file which will be responsible for invoking the web service and give us the response object.
Main.java


package springwsjxb;

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.SoapMessage;

/**
 *
 * @author PankajB
 */
public class Main {

   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try
        {
   // Load the spring web service configuration file
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springwsjxb/acjaxbb.xml");
        WebServiceTemplate webServiceTemplate = (WebServiceTemplate)applicationContext.getBean("webServiceTemplate");       
        // Creating the Request Object and setting it properties
        CelsiusToFahrenheit celsiusToFahrenheit=new CelsiusToFahrenheit();
        celsiusToFahrenheit.setCelsius(100);
        // Invoking the web service and getting the response back.
                CelsiusToFahrenheitResponse c=(CelsiusToFahrenheitResponse)webServiceTemplate.marshalSendAndReceive(celsiusToFahrenheit,new WebServiceMessageCallback() {

     // Setting the SOAP Action
        public void doWithMessage(WebServiceMessage message) {
            ((SoapMessage)message).setSoapAction("http://tempuri.org/CelsiusToFahrenheit");
        }
    });


               System.out.println("THE RESPONSE From Web Service IS "+ c.getResult());

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

}
The code is pretty self explanatory, except at one point where we need to cast the object to our corresponding response, as the web service template does not provide any way to specify in the parameter list about the kind of response object we are expecting to receive.

Once it gets executed we will receive the result on to our console.
Someetimes our web servie is accessible only through HTTPS protocol, however the tried to just replace the URI Of my web services template, and it seems to work without any added configurtion. It might be possible that we need to install the certificate on our system either manually or through a software called PORTECLE.
So that's all for the time.
 In the next part we will see how to create a web service through spring ws 2 and implement the same endpoint for both SOAP 1.1 & SOAP 1.2 i.e. we will not bother about whether client is invoking through text/xml or application/soap+xml (SOAP 1.2). Really Spring WS makes it so easy.
 Thanks Arjen Postuma.


Tuesday, March 1, 2011

Spring Web Services 2 - Part I

This is an introductory article, in a three part series which illustrates the use of Spring Web Services in a Java/J2EE Application.
All the Examples has been created by using Spring 3, Spring Web Services 2, JAX-WS, JAXB 2.2, and wsdl4j.jar

This Part will simplify the process of invoking a simple web service located at w3schools.com ( to convert a temperature from Celsius to Fahrenheit ).
Web Service URL is : http://www.w3schools.com/webservices/tempconvert.asmx 
WSDL is : http://www.w3schools.com/webservices/tempconvert.asmx?wsdl

Note: Please use SOAP UI, an excellent tool for testing the web services.

So as per Spring WS Configuration, we will create a WebServiceTemplate which follows the same pattern as of JDBCTemplate, RestTemplate etc
So our application context (ac.xml) file in this case is:



    
    
    
     
     
        
        
       
    



Now we have to construct two Classes:
1. This Class will represent the java representation of the SOAP REQUEST BODY with all the tags defined through JAXB 2.2 framework. CelsiusToFahrenheit.java 

package springws;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 *
 * @author PankajB
 * namespace represent the namespace of the elements, plz see wsdl for greater information
 * CelsiusToFahrenheit is the Root Element name
 */
@XmlType(name="tem")
@XmlRootElement(name="CelsiusToFahrenheit",namespace="http://tempuri.org/")
public class CelsiusToFahrenheit {

    private int celsius;
   

    @XmlElement(name="Celsius",namespace="http://tempuri.org/")
    public int getCelsius() {
        return celsius;
    }

    public void setCelsius(int celsius) {
        this.celsius = celsius;
    }
}




2. This class will represent the response that is coming from the Web service. CelsiusToFahrenheitResponse.java


package springws;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="CelsiusToFahrenheitResponse",namespace="http://tempuri.org/")
public class CelsiusToFahrenheitResponse {

    private int result;


    @XmlElement(name="CelsiusToFahrenheitResult",namespace="http://tempuri.org/")
    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }
}



Now the Final Part will be is to load the xml file and perform the invocation. The Source Code is easy to read, hence does not contain much comments (Sorry for this guys!!!)

Main.java
package springws;

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.SoapMessage;

/**
 *
 * @author PankajB
 */
public class Main {

   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try
        {
   // getting the file present in springws package
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springws/ac.xml");
  // get the Bean
        WebServiceTemplate webServiceTemplate = (WebServiceTemplate)applicationContext.getBean("webServiceTemplate"); 
  // Create the Marshaller, so that we can generate the request SOAP Body XMl
        JAXBContext jc= JAXBContext.newInstance(CelsiusToFahrenheit.class);
        Marshaller m=jc.createMarshaller();
  // Creating the Request Object
        CelsiusToFahrenheit celsiusToFahrenheit=new CelsiusToFahrenheit();
        celsiusToFahrenheit.setCelsius(10);
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        StringWriter wr=new StringWriter();
  // Marshalling the object to the writer
        m.marshal(celsiusToFahrenheit, wr);
        System.out.println(wr.toString());
        StringWriter finalResponseWriter = new StringWriter();
  // Creating the Source and Result object, that will contain the corressponding REUQEST & RESPONSE.
        StreamSource webServiceInput = new StreamSource(new StringReader(wr.toString()));
        StreamResult webServiceOutput = new StreamResult(finalResponseWriter);
  // Invoking the Web Service
        webServiceTemplate.sendSourceAndReceiveToResult(webServiceInput,new WebServiceMessageCallback() {

         // This is very much required, since we need to set the ACTION as defined in the WSDL. ( Since a web service can contain multiple options
        public void doWithMessage(WebServiceMessage message) {
   // Please see the WSDL for more details.
            ((SoapMessage)message).setSoapAction("http://tempuri.org/CelsiusToFahrenheit");
        }
    }, webServiceOutput);
 // This line, will print the Response to the Console
            System.out.println(finalResponseWriter.toString());
          
   // This will simply unmarshal the xml response in the Java Object (for easy handling of response)
            JAXBContext jaxc=JAXBContext.newInstance(CelsiusToFahrenheitResponse.class);
            CelsiusToFahrenheitResponse tr=(CelsiusToFahrenheitResponse)jaxc.createUnmarshaller().unmarshal(new StringReader(finalResponseWriter.toString()));
            System.out.println(tr.getResult()  + "  is the result");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

}
Once the following will be executed, we will see the response on our console.

In the next two parts we will see the following:
Part 2: Making use of JAXB Marshalling & UnMarshalling to automate the above manual marshalling through coding.
Part 3: How to create a web service through Spring WS and provide a simple end point to support both SOAP 1.1 & SOAP 1.2
So, stay tuned.

Tuesday, January 18, 2011

Java 4,5,6.. What next???? Python.. Scala.. Erlang...

For a Java Fan it becomes a lot easier day by day  to lost in the overgrowing languages in the JVM world, that includes Groovy, Jython and a host of other languages with Groovy being the Flag bearer. However with the stagnancy of Java ( i m saying this because the next java version is slated to be released in Summer of 2012) and  with the introduction of other languages, it seems a good choice/time for a java developer to taste other languages. Some of the major languages that a java developer i think one should follow on is.


  • PYTHON: It seems to be a great language by syntax and looks like a relative of 'C' language. The Current version of Python 2.7 is seems to be much popular than 3x version. Because of its ability to run on multiple platforms that includes (Linux, JVM, .Net) i think this should be the first choice of a developer. The emergence of Various Development Frameworks like WxPython, Django ( best for web app development) provide a strong reason to go for this language. In addition we also have to consider Google's Bet on python, ( Google really invest heavily on python with Guido Van Rassum on this board, ya the same person who created python). I think before trying out the same java feature ever and ever again, one should see the new angle python brought into development e.g. Virtual enviroment, Easy_install, Pip etc. The central repository pypi definitely helps the developer to get an understanding of all the libraries with their documentation a lot.
    Some important links are:
    http://zetcode.com/wxpython/
    http://www.searchforpython.com
    http://www.clemesha.org/blog/modern-python-hacker-tools-virtualenv-fabric-pip/
    http://www.catonmat.net/blog/learning-python-programming-language-through-video-lectures/
    http://code.google.com/edu/languages/google-python-class/
    http://agiliq.com/blog/2010/11/i-am-so-starving-same-web-app-in-various-python-we/
    http://sujitpal.blogspot.com/2010/08/python-web-application-with-spring.html
  • ERLANG: Wanna go for a concurrent languages, that ERLANG seems to be extremely fit to the concurrent world. Developed by Ericssion in 1985 and open sourced in 2000 this language serves as a basis for one of biggest platforms on the world, that includes FACEBOOK CHAT and facebook page rendering things. The Syntax is seems to be an complex one, but with the time it turns out to do the thing in a much simpler way. It also plays a great part in the NoSql movement with CouchDB being created in it. So give ERLANG a try, if concurrency seems to exist anywhere near you.
    In addition this also support Web development, Nitrogen is the framework here, that one needs to look into.
  • GROOVY: The ability to call itself Java++ and with the recent web development in GRAILS framework ( somewhat similiar to Rails framwork) will make this language an excellent choice for Java developers, since the language fundaments and syntax seems a lot similar to java. ( Relationship can be considered Java -> Groovy as C -> C++). since you can use all the java libraries can be used in GROOVY, so a java developer can find him/her on the best side of river & safe at home.
  • SCALA: The mixture of OOP & FP ( functional programming) and its concurrent features makes this languages an excellent bread earning language of future. Since scala provide the concurrency in terms of "ACTORS" ( ya inherited from ERLANG)  and with web development frameworks like LIFT, SINATRA it can be termed as the replacement of java on to the JVM in the next coming years. Like Groovy you can use all existing java libraries, however the learning curve is little steep because of the complexity of the language. But as per my observation and as per the blogosphere world this language can fit extremely well in the Java world.
    Even as per a blog the Groovy developer says "if he knows that Scala exists and is about to come then he would never had developed Groovy". This statement itself says a lot about Scala.
    Some important links are:
    http://www.slideshare.net/mariogleichmann/scala-a-scalable-language
    http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html  
Personally out of above 4 i will mark the languages in this order Python -> Scala -> Erlang unless you have a special inclination or have a special requirement.
So go ahead, give it a try and find out to do the thing differently rather then repeating yourself each and every time. SO remember, remain DRY ( dont repeat yourself).
          If you want to know about the Good Books in any of the above, please write me in the comments section or mail me. Surely, will love to tell anyone.
        Sorry, for the Typos..

Monday, January 10, 2011

No Nonsense - Apache + Python + Django installation gude

Python + Apache Integration:
  In order to work python with apache, we need to take care of the python version with the apache version. if we are using python version <= 2.5 then we have to use mod_python instead of using mod_wsgi. MOD_WSGI used to work with python 2.6 and python 2.7 version.

This post will simply presents a guide to install Django on the Apache 2.2 version with python 2.7
For that we need to follow the steps in the order:


  •  Download the Mod_wsgi.so from the following link http://code.google.com/p/modwsgi/ as per your apache and python version.
  •  Place the ".so" apache module file in the Modules folder of apache installation.
  • Modify the Httpd.conf file to include the following line in it. (make sure you rename the so file to mod_wsgi, otherwise use the complete name in the below listing)
    LoadModule wsgi_module modules/mod_wsgi.so   
  • Now create a sample Django project called "Sample" in ("C:\djangoprojects") directory and create multiple apps in it. The directory structure of Sample is given below.  (dont care about apache_django.py we will discuss this filelater on )

  • Now create a simple app "Users" in the this django project "c:\djangoprojects\sample", the directory contents are as below
  • Make sure you are able to run the Project using the inbuilt Django server. by using following URL (http://localhost:8000/Sample/Users)
  • Now Create a file called "apache_django.wsgi" in the Sample project folder. This file basically provides an bridge between the APACHE and the Django. This provides the apache with the wsgi handler for the Python Web based application.
    Paste the following contents in this file.
    # apache django settings.
    import os
    import sys
    sys.path.append("C:\\djnagoProjects")
    os.environ['DJANGO_SETTINGS_MODULE'] = 'Sample.settings'
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    where, Sample.settings refers to the Settings.py file of Django present in the Sample project folder as shown above.
  • Now in the apache Httpd.conf file paste the following lines at the bottom
    WSGIScriptAlias /Django "C:/djnagoProjects/Sample/apache_django.wsgi"
    <Directory "C:/djnagoProjects/Sample">
    Order allow,deny
    Allow from all
    </Directory>
    Where, the first line will specify that the whole application will be accessbile at (http://localhost/Django) and the individual module will be available at (http://localhost/Django/Users).
    And the rest of the lines are simply allow  you to allow the access rights over the directory.
  • That's all, just make sure you restart apache and that's the complete magic is being done through Mod_wsgi bridge. Now access the path (http://localhost/Django) and the result is



    Please write back, in case i made an error , or u face any problems....
    Happy Pythoning.......

Sunday, January 9, 2011

Monitoring Your server - java

During development and testing times we required to continously monitor our server for a number of resons:

  1. Find out amount of memory taken
  2. Find out the number of classes loaded in memory
  3. Checking out the different types of memory being used by our program ( Heap, Permgen etc. )
  4. To get a know how of all the SQL commands issued by the application
  5. and a number of things.... that a developer had to take care.
This can be achieved using a number of open source tools available freely on our blogosphere. Two of the most popular are
  1. Java Melody ( Awesome one , Java Melody )
  2. Lambda Probe ( http://www.lambdaprobe.org/d/index.htm )
 both are excellent tools and how to use them is being covered in the online documentation of both of the projects. Both of them comes as a war file, so we can directly deploy it to the server and monitor the details of our requests, sessions, memory and lots of things.
 however There is an advantage that i found of Melody over Probe is that melody can be used to cover up a single application on to the server i.e. if you have an WAR deployed on to the server, you can find the details only in context of the same application, and not to the complete server. In addition Melody is an active project while Probe seems to be an dead one.
Both of them has the unique advantage of running over multiple servers because both of them supports the Server API specification, so we just need to use some filters which are being present in the assoicated Jar Files and make the available to our application.
As I am started using Java melody i will post more of the details regarding it soon. 
Here are some of the attached details being thrown by both of them. I had used Melody within a particular application while Probe to cover up the whole server.
Happy Monitoring........



Tuesday, December 21, 2010

Command Line Debugging in Java ::: JDB

Ever wondered, how the IDE helps us in debugging the program , or our application by using breakpoints, watches, expressions etc.
In the world of java you can do it easliy by using jdb command line action provided by Java.. lets write down a simple program to perform command line debugging in java.
Lets write down a simple program to add two numbers in java.  
  1. import java.io.*;

  2. public class Sum

  3.  {
  4.    public static void main(String arfs[])
  5.    {

  6.      int i,j;
  7.      i=10;
  8.      j=25;
  9.      System.out.println(" I = " + i + " J = " + j);
  10.      int sum=i+j;
  11.      System.out.println("Sum = " + sum);

  12.    }
  13.  }
Now compile the above code by using
javac -g Sum.java
Here -g option is used to inject the debug information into the generated class files, This can easily be viewed by checking the file size using with and without "g" option. 

Start the Execution of this program by using following command

>java -Xdebug -agentlib:jdwp=transport=dt_s
hmem,address=9999,server=y,suspend=y Sum 
  the message "Listening for transport dt_shmem at address: 9999" is generated showing that the debugger is started with this program and ready to listen the request on the port no 9999. 

Open a separate window and type the following at the command prompt.
> jdb -attach 9999
  Following output will be generated
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...

> VM Started: No frames on the current call stack
main[1] 

Now the debugger is ready to start debugging the program. We just need to pass the jdb specific commands to the debugger.
main[1]: This represents the current call stack on the heap where the execution is.
Now assume main[1] will be your command prompt for the time being.
1. Type cont 
 main[1] cont (PRESS ENTER)
output will be: 
> Set deferred breakpoint Sum.main

Breakpoint hit: "thread=main", Sum.main(), line=10 bci=0
10         i=10;

main[1]
2. Now Type "Stop at Sum:12" to instruct the debugger to stop the execution of the program at the 12 line.
main[1] Stop at Sum:12
Output will be:  Set breakpoint Sum:12
3. Now Type "cont" to continue with the execution of the code.
4. Now as per the program i and j has been initialized, so use the following command to view the values present in i and j.
main[1] print i 
output >>  i = 10
Similarly u can inspect the value of other variables that are being initialized till the execution of the line, otherwise you will get null
5. Now type "cont" to continue the execution.
  It will simply exit the debugger and will fall on to the main prompt.

so "cont" is being used to continue with the execution of the program
 "Stop at Sum:12" to stop the execution of program at a particular line.

 I hope it would be helpful for beginners, to understand how to debug your program through command line. 
Following are the screenshots attached.




For this tutorial i had used the tutorial from the following awesome links: