Thursday, June 4, 2009

HowTo integrate Apache Axis and WebBeans

IMO WebBeans becomes a pretty cool dependency injection framework for Java. It works in a type-safe way (greetz to Spring ;-)) by using annotations. It provides Interceptors, Decorators and Event-Processing as well.
On the other hand Axis is a powerful framework to implement WebServices.

If you want to use the WebBeans benefits in your Axis Services you have to integrate both technologies. Axis provides two simple ways to do this:
  1. with a custom MessageReceiver
  2. with a ServiceObjectSupplier
So lets integrate WebBeans into Axis.
I'm running on Tomcat 6.0.18 with Apache Axis2 1.4.1 and WebBeans 1.0.0. Preview1.

Create a WebBeans integration class

package org.j4fry.webbeans;
import javax.inject.manager.Manager;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.jboss.webbeans.CurrentManager;

public abstract class WebBeansIntegration {
public static Object createInstance(AxisService axisService) throws AxisFault {
Parameter parameterServiceClass = axisService.getParameter(Constants.SERVICE_CLASS);
Class serviceClass = null;
try {
serviceClass = Class.forName(parameterServiceClass.getValue().toString());
} catch (ClassNotFoundException e) {
throw new AxisFault("There's not valid serviceClass specified", "serviceClass");
}
Manager manager = CurrentManager.rootManager();
return manager.getInstanceByType(serviceClass);
}
}

Possibility 1: Adjust Axis to use this integration class via a custom Message Receiver

package org.j4fry.webbeans;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;

public class CustomMessageReceiver extends org.apache.axis2.rpc.receivers.RPCMessageReceiver {
@Override
protected Object makeNewServiceObject(MessageContext msgCtx) throws AxisFault {
return WebBeansIntegration.createInstance(msgCtx.getAxisService());
}
}

Possibility 2: Adjust Axis to use this integration class via ServiceObjectSupplier

package org.j4fry.webbeans;
import org.apache.axis2.AxisFault;
import org.apache.axis2.ServiceObjectSupplier;
import org.apache.axis2.description.AxisService;

public class WebBeansObjectSupplier implements ServiceObjectSupplier {
public Object getServiceObject(AxisService axisService) throws AxisFault {
return WebBeansIntegration.createInstance(axisService);
}
}

Tomcat Configuration - web.xml
Tomcat's web.xml have to reference the WebBeans Manager (of course you have to include the AxisServlet and the mapping information as well)

<resource-env-ref>
<description>WebBeans Manager</description>
<resource-env-ref-name>app/Manager</resource-env-ref-name>
<resource-env-ref-type>javax.inject.manager.Manager</resource-env-ref-type>
</resource-env-ref>

Axis Service Configuration - services.xml
The services.xml from your Axis-Service archive (.aar file)
If you choose possibility 1 you have to include your custom Message Receiver with the following tag inside the service-element:

<messageReceivers>
<messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" class="org.j4fry.webbeans.CustomMessageReceiver"/>
</messageReceivers>

If you choose possiblity 2 you have to add the "ServiceObjectSupplier" parameter to your service element like this

<parameter name="ServiceObjectSupplier">org.j4fry.webbeans.WebBeansObjectSupplier</parameter>

Use WebBeans Annotation inside your Service class
If you've finished the previous steps you can use the WebBeans Annotations to inject some resources in your service class.

Resources
WebBeans RI 1.0.0 Preview1 (thanks to JBoss): http://downloads.sourceforge.net/jboss/webbeans-1.0.0.PREVIEW1.zip
Apache Axis2 1.4.1: http://ws.apache.org/axis2/

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete