a cup of java

Here’s your cup of hot java & web 2.0 cookies. Enjoy it.

Posts Tagged ‘j2ee

How-to: Write web services using Axis2

without comments

This is a very basic post on writing a web service provider and consumer using Axis2. You can find such posts anywhere in the web. Here I’ve tried to make it as simple as I can.

Set up: What all we need to do?

1. Axis2 runtime.
I tried with WAR distribution from http://ws.apache.org/axis2/download/1_5/download.cgi
2. A web/app server. I am using Tomcat.

Using the Axis2 runtime & setting it up

Extract the Axis2 WAR distribution and keep it in the deploy directory of the server. In case of Tomcat keep it in webapps. This is how my directory structure look like.

image

The WEB-INF is the most important directory. Tell you why. Unlike our normal WEB-INF in archives, this directory hold some special sub-directories. Here goes which contains what.

classes – compiled Java classes. We can find some Axis specific classes here.
conf – axis.xml (Axis configuration file)
lib – All necessary Axis2 libraries (JARs)
modules – Don’t ask me. Even I’m not sure. Did I mention that I’m also a beginner? :)
services – All web service archives & services.list

Coding: What we need to write?

1. The service provider. A Java class.
2. service.xml. The web services descriptor.
3. build.xml. To build and deploy the web services archive.
4. The service consumer. Another Java class to consume the services offered

And we are good to go now. Let’s make our hands dirty with some Java code. This is very simple and kudos to Axis2.

The service provider: SampleService.java

package subin.rnd.ws;
public class SampleService
{
  public WsOutput doSomething(WsInput anInput)
  {
    System.out.println("doSomething()");
    WsOutput anOutput = new WsOutput();
    anOutput.setResponseString("I did some thing to " + anInput.getName());
    return (anOutput);
 }
}

WsInput is a sample input class to demonstrate that we can have more complex IO is possible. Similarly WsOutput is the output class. Instead of using WsInput / WsOutput for IO, we can use normal data types like integer, float, string etc too.

WsInput.java

package subin.rnd.ws;
import java.io.Serializable;
public class WsInput implements Serializable
{
 private String name;
 public void setName(String name)
 {
   this.name = name;
 }

 public String getName()
 {
   return (this.name);
 }
}

WsOutput.java

package subin.rnd.ws;
import java.io.Serializable;
public class WsOutput implements Serializable
{
 private String responseString;

 public void setResponseString(String response)
 {
   this.responseString = response;
 }
 public String getResponseString()
 {
   return (this.responseString);
 }
}

We have the Java part of the web-service ready. But that’s not enough. We need to deploy the web-service as an AAR – Axis Archive – file. An AAR  is just another zip file (like a JAR) with a funky extension :D . The AAR should contain the class files along with the services.xml – web service descriptor.

Web service descriptor: services.xml

<service name="SampleWs" scope="application">
 <description>Subin's sample webs service</description>
 <messagereceivers>
 <messagereceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
 class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
 <messagereceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
 class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
 </messagereceivers>
 <operation name="doSomething" />
<parameter name="ServiceClass">subin.rnd.ws.SampleService</parameter>
</service>

The services.xml is the place where we define the details of the services being offered.

Build file: build.xml

<project name="my.webservice.test" default="build.aar">
<property name="deploy.dir" value=".../webapps/axis2.war/WEB-INF/services" />
<property name="file.name" value="subinws.aar" />

 <target name="build.aar">
   <javac srcdir="src" destdir="bin" />
   <echo>Copying services.xml to bin</echo>
   <copy file="META-INF/services.xml"
       tofile="bin/META-INF/services.xml" overwrite="true"/>
   <jar basedir="bin" destfile="${file.name}" />
   <echo>Deleting services.xml from bin</echo>
   <delete dir="bin/META-INF" />
   <copy file="${file.name}" tofile="${deploy.dir}/${file.name}"  />
 </target>

</project>

So I hope we have an AAR ready, which is copied to the services directory. But we’ve not done yet. Now we have to make an entry in services/services.list file. Just insert the name of the ARR file at the end of it – in this case “subinws.aar”.

Please wait for part 2.

An interesting find ! Google App Engine with Java support

without comments

image

I was just going through code.google.com, and just find a link titled “App Engine”. I was not at all surprised as they do this every time. With Google something is new every time. I went on reading. Oh God! Free J2EE app engine where we can host our archives ! This is one of the coolest things I was waiting to happen. The server where I hosted (and I’m still with them) my website does not have Java support and I spend many hours in writing Php codes after many hours of searching and testing, which I could have done in minutes with Java. And most importantly it’s almost FREE ! We have to pay them only if we are crossing the specified bandwidth & page view limits.

We can write code with Google’s Eclipse plug-in (with GWT support !), test them with in Eclipse and deploy them from Eclipse! Isn’t that cool? All you need is to have a Google account and just get it done. We can have up to nine applications each with a unique URL to access and we can even link them with our Google Apps too.

Check this video: Get an overview of App Engine’s new Java runtime and see a demo of a sample app from creation to deployment.

URL: http://www.youtube.com/watch?v=P3GT4-m_6RQ

-
For me this is nicest offer from Google in the recent times. I don’t know whether all of you are going to agree with this. But at least I’m happy. Hope all of you will find it nice.

Google App Engine: http://code.google.com/appengine/
FAQ: http://code.google.com/appengine/kb/
App Engine Blog: http://googleappengine.blogspot.com/
Getting started: http://code.google.com/appengine/docs/java/gettingstarted/

Written by Subinkrishna G

August 2, 2009 at 8:17 pm

Code: ByteStreamResponseWrapper

without comments

Maxi was asking for the source code of ByteStreamResponseWrapper. This is related to this post (How to: write a servlet filter).

package subin.rnd.enterprise.servlet.wrapper;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import subin.rnd.enterprise.servlet.io.ServletOutputStreamImpl;

public class ByteStreamResponseWrapper
extends HttpServletResponseWrapper
{
    private ByteArrayOutputStream byteStream;

    public ByteStreamResponseWrapper(HttpServletResponse response)
    {
        super(response);
    }

    @Override
    public ServletOutputStream getOutputStream()
    {
        ServletOutputStreamImpl outputStream = null;

        this.byteStream =  (null == this.byteStream)
            ? new ByteArrayOutputStream() : this.byteStream;
        outputStream = new ServletOutputStreamImpl(this.byteStream);

        return (outputStream);
    }

    @Override
    public PrintWriter getWriter()
    {
        PrintWriter printWriter = null;

        this.byteStream =  (null == this.byteStream)
            ? new ByteArrayOutputStream() : this.byteStream;
        printWriter = new PrintWriter(this.byteStream);

        return (printWriter);
    }

    @Override
    public String toString()
    {
        return ((null == this.byteStream)
                ? null : new String(this.byteStream.toByteArray()));
    }

    public byte[] toBytes()
    {
        return ((null == this.byteStream)
                ? null : this.byteStream.toByteArray());
    }
}

Then there is ServletUtility & ServletOutputStreamImpl.

Code: ServletUtility

package subin.rnd.enterprise.servlet.util;

import java.io.OutputStream;
import javax.servlet.ServletResponse;

public class ServletUtility
{
    /**
     * Writes the bytes to the {@link OutputStream}
     *
     * @param response
     * @param bytes
     */
    public static void write(ServletResponse response, byte[] bytes)
    {
        int contentLength = -1;
        OutputStream outputStream = null;

        if ((null != response) &&
            (null != bytes) &&
            (bytes.length > 0))
        {
            contentLength = bytes.length;

            try
            {
                response.setContentLength(contentLength);
                outputStream = response.getOutputStream();
                outputStream.write(bytes);
            }
            catch (Exception exception)
            {
                outputStream = null;
            }
            finally
            {
                try
                {
                    if (null != outputStream) outputStream.close();
                }
                catch (Exception e){}
            }
        }
    }
}

ServletOutputStreamImpl is an implementation of ServletOutputStream. I think I don’t need to put the source code of it.

Written by Subinkrishna G

April 21, 2009 at 11:12 am

A Jboss 5 issue

with 2 comments

jbosscorp_logo

Me and my colleague Venu were working on some Jboss 5 stuff  (infact migrating from Jboss 4 to 5) when we got the following exception.

Exception:

ERROR [my.package.name] doSomething() Ex: javax.naming.CommunicationException [Root exception is java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646]

javax.naming.CommunicationException [Root exception is java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646]

at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:725)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:590)
at javax.naming.InitialContext.lookup(InitialContext.java:351)

.. .. ..

Caused by: java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646

at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:546)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1552)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1466)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1699)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1305)

We are trying to connect to another server which is running on Jboss 4 and trying to access a remote EJB (session bean). Till now I’ve no clear idea about this whole issue. If any of you can put some light to this issue, please.

I will update this place once we tackle the issue.

Update 1:
I found some useful information here: https://jira.jboss.org/jira/browse/EJBTHREE-749

The problem seems to be mismatching SerialVersionUID in org.jboss.ejb3.remoting.IsLocalInterceptor of  jboss-ejb3-client.jar.

Update 2: 28-Jan-2009
This issue has been fixed from Jboss-4.2.2 GA onwards. So all those who are using versions lesser than 4.2.2 are more likely to get this issue.

Find the JIRA post by Venu here: https://jira.jboss.org/jira/browse/EJBTHREE-1696

Written by Subinkrishna G

January 22, 2009 at 5:02 pm