RokL
RokL

Reputation: 2812

How do I provide my own web service implementation instance?

I have a web service (JAX-RPC) that runs on application server (Websphere Application Server 7.0). Normally the development process looks like this:

When the web service is deployed, MyService_SEI is the declared service interface and the application server instantiates a MyService instance by means of the public no-arg constructor.

But what if I want to do constructor injection (i.e. have MyService class without a no-arg constructor) or if I want to provide a dynamic proxy object which implements MyService_SEI and use that?

Is there a way I can take control of the instantiation procedure (like a filter or interceptor) to achieve this?

Upvotes: 2

Views: 626

Answers (2)

tgkprog
tgkprog

Reputation: 4598

an easy method would be to make two classes. one your class with all the bells and whistles (constructor injection etc lets call it worker). and the actual service. the service would delegate what it needs to the worker class, who it can get by calling some factory method.

The factory can even look at some common db or other config to decide which run time instance (which class, what config, shared or common) so you have good separation and power

Just cause you are using one framework/ method of injection does not mean you cannot mix to make it more powerful

Upvotes: 1

groo
groo

Reputation: 4448

You can't do constructor injection as Injection always occur after the default constructor is called. If you try to use an injected reference inside the default constructor it will ALWAYS fail, there's no workaround for this as this is mandate by the specification. So the first option you mentioned is discarded.

For the second option, using a filter or interceptor, you actually have an option. WebSphere WebServices are build using Axis2 implementation and Axis provide a way of implementing Handlers.

You can add handlers into the JAX-WS runtime environment to perform additional processing of request and response messages.

Here's a handler example, from Axis documentation:

package org.apache.samples.handlersample;

import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class SampleProtocolHandler implements
       javax.xml.ws.handler.soap.SOAPHandler<SOAPMessageContext> {

   public void close(MessageContext messagecontext) {
   }

   public Set<QName> getHeaders() {
       return null;
   }

   public boolean handleFault(SOAPMessageContext messagecontext) {
       return true;
   }

   public boolean handleMessage(SOAPMessageContext messagecontext) {
       Boolean outbound = (Boolean) messagecontext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
       if (outbound) {
           // Include your steps for the outbound flow.
       }
       return true;
   }

}

And than you add a handler.xml file like this:

 <?xml version="1.0" encoding="UTF-8"?>

    <jws:handler-chain name="MyHandlerChain">
            <jws:protocol-bindings>##SOAP11_HTTP ##ANOTHER_BINDING</jws:protocol-bindings>
            <jws:port-name-pattern 
             xmlns:ns1="http://handlersample.samples.apache.org/">ns1:MySampl*</jws:port-name-pattern>
       <jws:service-name-pattern 
             xmlns:ns1="http://handlersample.samples.apache.org/">ns1:*</jws:service-name-pattern>
            <jws:handler>
                    <jws:handler-class>org.apache.samples.handlersample.SampleLogicalHandler</jws:handler-class>
            </jws:handler>
            <jws:handler>
                    <jws:handler-class>org.apache.samples.handlersample.SampleProtocolHandler2</jws:handler-class>
            </jws:handler>
            <jws:handler>
                    <jws:handler-class>org.apache.samples.handlersample.SampleLogicalHandler</jws:handler-class>
            </jws:handler>
            <jws:handler>
                    <jws:handler-class>org.apache.samples.handlersample.SampleProtocolHandler2</jws:handler-class>
            </jws:handler>
    </jws:handler-chain>

Upvotes: 2

Related Questions