Skip to content

Metabolomics Fiehn Lab

Sections
Personal tools
You are here: Home » Members » Gert Wohlgemuth » java » xdoclet - ejb based webservice

xdoclet - ejb based webservice

Document Actions
this is how to generate an ejb based webservice with xdoclet
What's about

After I decided to work with ejb's and webservices I googled a little bit how to generate all files with xdoclet.

I didn't found much about this. I found a couple of examples, but no one was working... So I combined them and tried until I get my first xdoclet based webservice.

Requirements:

  • xdoclet 1.23
  • jboss 4.02
  • ant 1.6x
  • lot's of coffee
The Bean

we only need a simple small session Bean, so this bean just has one method and returns "test"

Just have a look at the used xdoclet tags, they are the only thing you have to know sofar....

package test;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

/**
 * @ejb.bean name="Test" display-name="Name for Test" description="Description for Test" jndi-name="ejb/Test" type="Stateless"  view-type="service-endpoint"
 *
 * @jboss.port-component name = "TestEndpoint"
 * @jboss.
 * @ejb.interface service-endpoint-class="test.TestEndpoint"
 * @wsee.port-component name="TestEndpoint"
 *
 */
public class TestBean implements SessionBean {
    public TestBean() {
        super();
    }

    public void setSessionContext(SessionContext ctx) throws EJBException,
            RemoteException {
    }
    public void ejbRemove() throws EJBException, RemoteException {
    }

    public void ejbActivate() throws EJBException, RemoteException {
    }

    public void ejbPassivate() throws EJBException, RemoteException {
    }

    public void ejbCreate() throws CreateException  {

    }
    /**
     * @ejb.interface-method view-type = "all"
     * @return
     */
    public String getTest() {
        return "test";
    }
}

The Buildfile

this is the most difficult part as far as I can say, it takes me a while until I get a running service based on my ejb. Because all the tutorials had one or another small mistake in it, or are not compatibel to the actual jboss version and so on and so on. It was also a long time ago that I actually wrote an ejb... So that's one working buildfile.

Please copy the xdoclet and jboss libs to: lib
Please have your sources in: src
and you compiled classes in: bin

In this buildfile I just ignored the standards with setting all flexible, because of the reduced complecity.

<?xml version="1.0" encoding="UTF-8"?>
<project default="xdoclet" name="test">
    <path id="path">
        <pathelement path="bin" />
        <fileset dir="lib">
            <include name="*.jar" />
        </fileset>
    </path>

    <target name="xdoclet" description="EJB-Configuration">
        <delete dir="target" includeemptydirs="true">
        </delete>
        <!-- ejb stuff -->
        <taskdef classpathref="path" classname="xdoclet.modules.ejb.EjbDocletTask" name="ejbdoclet" />
        <ejbdoclet ejbSpec="2.1" destDir="target/src" force="true">
            <fileset dir="src" includes="**/*Bean.java" />
            <service-endpoint />
            <remoteinterface />
            <localinterface />
            <localhomeinterface />
            <homeinterface />
            <entitypk />
            <entitycmp />
            <entitybmp />
            <utilobject />
            <deploymentdescriptor destDir="target/META-INF" />
            <jboss Version="4.0" destDir="target/META-INF" />
        </ejbdoclet>

        <!-- webservice documents -->
        <taskdef classpathref="path" classname="xdoclet.modules.wsee.WseeDocletTask" name="wseedoclet" />
        <wseedoclet jaxrpcMappingFile="mappings.xml" wsdlFile="wsdl/service.wsdl" destDir="target/META-INF" force="true" verbose="true">
            <fileset dir="src" includes="**/*Bean.java" />
            <deploymentdescriptor>
                <configParam name="Name" value="TestService" />
            </deploymentdescriptor>
            <jaxrpc-mapping/>
        </wseedoclet>

        <!-- compile sources, needed to generate the wsdl file -->
        <javac destdir="bin">
            <classpath refid="path" />
            <src path="src" />
            <src path="target/src" />
        </javac>

        <!-- generate wsdl file -->
        <mkdir dir="target/META-INF/wsdl" />
        <java classname="org.apache.axis.wsdl.Java2WSDL" classpathref="path" fork="yes">
            <arg value="-otarget/META-INF/wsdl/service.wsdl" />
            <arg value="-lhttp://this.value.is.replaced.by.jboss" />
            <arg value="-sTestEndpointPort" />
            <arg value="test.TestEndpoint" />
        </java>

        <!-- replace mistakes in webservice file -->
        <replace file="target/META-INF/webservices.xml" token="WEB-INF/wsdl/service.wsdl" value="META-INF/wsdl/service.wsdl" />
               
        <!-- cp mappings.xml to rigt location -->
        <copy todir="target/WEB-INF" file="target/META-INF/mappings.xml"></copy>       
               
        <!-- copy classes -->

        <copy todir="target">
            <fileset dir="bin">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <jar destfile="target.jar" basedir="target">
            <include name="**/*.*" />
            <exclude name="src/**/*.java" />
        </jar>

       <!-- deploy the file
        <copy file="target.jar" todir="C:\jboss-4.0.2\server\default\deploy" />
    </target>

</project>

thats all
  • we have to change one value in the webservice file, because of the jboss specifications that the wsdl files for ejb based webservices should be in META-INF
  • if you want to you complex types you have to take care of that the namespace in your wsdl file is synchronized with your mapping file. do something like         <replace file="target/META-INF/mappings.xml" token="urn-test" value="http://test" />
  • we need the mapping file in WEB-INF
I don't know why, It was just the only way to get rid of

So it's ugly and there must be defnitly a better way todo this. But at least it's working and mainly generated.


The Client

this client is based on the example from the jboss site. And I think it's really cool that it's generated the needed stubs at runtime. All what I need is the Interface of the service.

As argument you need the path to your webservice, so something like: http://<server>:<port>/target/TestEndpoint?wsdl

package test;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;

/**
 * @author wohlgemuth
 *
 */
public class TestWebServiceClient {

    public static void main(String[] args) throws MalformedURLException,
            ServiceException, RemoteException {
        String urlstr = args[0];

        System.out.println("Contacting webservice at " + urlstr);

        URL url = new URL(urlstr);

        QName qname = new QName("http://test", "TestEndpointService");

        ServiceFactory factory = ServiceFactory.newInstance();
        Service service = factory.createService(url, qname);

        TestEndpoint test = (TestEndpoint) service.getPort(TestEndpoint.class);

        System.out.println("output:" + test.getTest());
    }

}

Created by zwluxx
Last modified 2005-10-13 06:55 PM
 

Powered by Plone

This site conforms to the following standards: