cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ryan Zoerner <>
Subject demo.jaxrs.server.CustomInvoker
Date Thu, 12 May 2011 07:20:11 GMT
In the course of the assignment that Sergey provided me with, I augmented
the http-demo, which I talked a bit here:

Now, the purpose of this customInvoker was for the Server to print a message
the the terminal each time a method was invoked.

Here is the code:
package demo.jaxrs.server;

import java.lang.reflect.Method;

import org.apache.cxf.jaxrs.JAXRSInvoker;
import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.utils.InjectionUtils;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;
import org.apache.cxf.message.Exchange;

public class CustomInvoker extends JAXRSInvoker

    public CustomInvoker()

    public Object invoke(Exchange exchange, Object request)
        ResourceProvider provider = getResourceProvider(exchange);
        Object rootInstance = getServiceObject(exchange);
        Object serviceObject = getActualServiceObject(exchange,
        OperationResourceInfo ori =
        ClassResourceInfo cri = ori.getClassResourceInfo();

         * This is where the method to be invoked upon is gotten from.
        Method methodToInvoke =


         * This prints the message to the terminal about which method will
         * be invoked upon through the server code.
            "\n" +
            "***" +
            "\n" +
            "The method about to be invoked" +
            "\n" +
            "by the CustomInvoker" +
            "\n" +
            "is named" +
            "\n" +
            "--->" +
            "  " +
            methodToInvoke.getName() +
            "\n" +
            "***" +

         * This calls the invoke method of the
         * which invokes upon the method which is obtainable from the
         * exchange Object, as demonstrated by this method of this
        return super.invoke(exchange, request);

     * Because this method was private, I had to copy-and-paste it from the
     * superclass. Another thing that I just thought of is that if you need
     * to throw exceptions from, say, JAXRSInvoker, you won't get all of the

     * resources that you need if you just copy-and-paste and change the
     * package name. Therefore, you could, instead, make a subclass and just

     * @Override what you have copied and pasted, then insert your
    private ResourceProvider getResourceProvider(Exchange exchange)
        Object provider = exchange.remove(JAXRSUtils.ROOT_PROVIDER);
        if (provider == null)
            OperationResourceInfo ori =
            ClassResourceInfo cri = ori.getClassResourceInfo();
            return cri.getResourceProvider();
            return (ResourceProvider) provider;

This helped me see, better, what the invoker is actually doing with its
code, and how simple it
is to do surgical alterations of coded applications, without having to know
much about what else
is actually happening.

This has application to the integration because EJB needs to have its
resources injected at the
time of Construction, according to JAX-RS 1.1, and EJB annotations must be
processed, during
any method invokation, which is what the invoker is doing here is calling
the method of a
resource whose lifecycle is being governed by a
In this case,  org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider. I
also had to create a
CustomResourceProvider, which I may also publish to the list in a subsequent


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message