Friday, December 12, 2008

JAX-WS RI, JBoss and Guice

So, I needed to use the guicemanaged annotation on some webservices at work, this was the way I got it working, if anyone has a better way, please let me know:

  1. Download the jaxws-ri lib, unpack it
  2. Download guice
  3. Download guicemanaged-0.2.jar
  4. Copy the following libs to /server//lib:
    • aopalliance.jar
    • guicemanaged-0.2.jar
    • guice-snapshot20081016.jar
    • jaxws-rt.jar
    • jaxb-xjc.jar
    • streambuffer.jar
    • stax-ex.jar
This advice is probably good even if you don't need guice. When i copied all the jars from the jaxws RI package, jboss freaked out.

Thursday, November 6, 2008

Google Guice and JAX-WS RI revisited

I've been coding a bit more with the guice-extension for jax-ws, and the result is actually quite nice, see below for how your web service endpoint would look when it is handled by guice:
@GuiceManaged(module = WebServiceModule.class)
public class AddNumbersImpl implements AddNumbers {

    private Calculator calculator;

    public void setCalculator(Calculator calc)
    public int addNumbers(int num1, int num2)
        return this.calculator.calc(num1,num2);
And WebServiceModule looks like this:
public class WebServiceModule extends AbstractModule {
    protected void configure() {

It can handle endpoints in any scope you want, if you want just one instance of the endpoint, you either annotate the class with @Singleton or bind it in Singleton scope in your module.

Head over to the jax-ws commons project on to get the code, binaries and some information on how to use it.

Tuesday, November 4, 2008

Google Guice and jax-ws

To integrate google guice with jax-ws, you need to use the InstanceResolver extension point. this way your endpoints will look like this;

@WebService(targetNamespace = "http://marcus", name="AddNumbers")
public class AddNumbersImpl {
  public void setCalculator(Calculator calc)

To achieve this you need to create the annotation GuiceManaged;
@WebServiceFeatureAnnotation(id=GuiceManagedFeature.ID, bean=GuiceManagedFeature.class)
public @interface GuiceManaged {

Then you need to implement the GuiceManagedFeature:
public class GuiceManagedFeature extends WebServiceFeature {
    public static final String ID="some_nice_id";

    public GuiceManagedFeature()
    public String getID() {
        return ID;
And finally the actual InstanceResolver:
public class GuiceManagedInstanceResolver extends AbstractMultiInstanceResolver {
    private T instance=null;
    public GuiceManagedInstanceResolver(@NotNull Class clazz)
    public T resolve(@NotNull Packet packet) {
            Injector injector= Guice.createInjector(new WebServiceModule());
        return instance;

That should do it, I'll update this post soon with a URL to the actual code (with documentation on how to use it. My plan is to make it more extensible (i.e. no hard dependency on the WebServiceModule) and possible to use in a non-singleton scope soon.