You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Benson Margulies <bi...@basistech.com> on 2007/11/19 00:17:06 UTC

Converting XFire-based local transport trick to CXF

I'd be grateful for any pointers as to how to map the 'invoke' method in
here to CXF.

package com.basistech.xfire;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
 
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamReader;
 
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.aegis.type.Configuration;
import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.exchange.InMessage;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.invoker.BeanInvoker;
import org.codehaus.xfire.transport.Channel;
import org.codehaus.xfire.transport.MapSession;
import org.codehaus.xfire.transport.Transport;
import org.codehaus.xfire.transport.local.LocalTransport;
import org.codehaus.xfire.util.STAXUtils;
import org.codehaus.xfire.wsdl.WSDLWriter;
import org.xml.sax.SAXException;
 
/**
 * This is a local-transport XFire container. It's used for things like
generating WSDL files or running unit tests 
 * of services.
 */
public class EmbeddedServiceProcessor {
 
    private Service service;
    private XFire xfire;
    private Class serviceClass;
 
    static class ByteBufferInputStream extends InputStream {
 
        private ByteBuffer buffer;
 
        public ByteBufferInputStream(ByteBuffer buffer) {
            this.buffer = buffer;
        }
 
        @Override
        public int read() throws IOException {
            try {
                return buffer.get();
            } catch (BufferUnderflowException e) {
                return -1;
            }
        }
 
    }
 
    public EmbeddedServiceProcessor(Class serviceClass) {
        this.serviceClass = serviceClass;
    }
 
    public void initialize(Object implementation, List<String>
extraClassNames)
            throws ParserConfigurationException, SAXException,
IOException {
        AegisBindingProvider binder = new AegisBindingProvider();
 
        xfire = XFireFactory.newInstance().getXFire();
        AnnotationServiceFactory serviceFactory = new
AnnotationServiceFactory(
                new Jsr181WebAnnotations(), xfire.getTransportManager(),
binder);
 
        DefaultTypeMappingRegistry tmr = (DefaultTypeMappingRegistry)
binder
                .getTypeMappingRegistry();
 
        // here we disuade XFire from its rather annoying tendency to
assume
        // that, just because
        // anything in Java can be null, that we want to advertise all
that
        // nullity all over.
        Configuration configuration = tmr.getConfiguration();
        configuration.setDefaultMinOccurs(1);
        configuration.setDefaultNillable(false);
 
        // Create a properties hashmap
        HashMap<String, Object> props = new HashMap<String, Object>();
 
        // Enable the writing of xsi:type attributes
        props.put(AegisBindingProvider.WRITE_XSI_TYPE_KEY,
Boolean.TRUE);
 
        // add class names here for extra types if we need them.
        props.put(AegisBindingProvider.OVERRIDE_TYPES_KEY,
extraClassNames);
        service = serviceFactory.create(serviceClass, props);
        // when all we want to do is extract the WSDL, don't try to set
up the implementation at all.
        // the service object is all we need.
        if(implementation != null) {
            service.setInvoker(new BeanInvoker(implementation));
            xfire.getServiceRegistry().register(service);
        }
    }
 
    public void terminate() {
        xfire.getServiceRegistry().unregister(service);
        service = null;
        xfire = null;
    }
 
    /**
     * Invoke a service with an XML message, return an XML message. 
     * Used for interop testing.
     * 
     * @param inputXML
     * @return
     * @throws Exception
     */
    public byte[] invoke(String serviceName, ByteBuffer inputXML)
            throws Exception {
        // This seems like a nasty piece of poor modularity.
        Thread.currentThread().setContextClassLoader(
                getClass().getClassLoader());
 
        MapSession session = new MapSession();
 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MessageContext context = new MessageContext();
        context.setSession(session);
        context.setXFire(xfire);
        context.setProperty(Channel.BACKCHANNEL_URI, out);
 
 
context.setService(xfire.getServiceRegistry().getService(serviceName));
 
        ByteBufferInputStream stream = new
ByteBufferInputStream(inputXML);
        XMLStreamReader xmlStreamReader =
STAXUtils.createXMLStreamReader(
                stream, "UTF-8", null);
 
        InMessage msg = new InMessage(xmlStreamReader);
 
        Transport t = xfire.getTransportManager().getTransport(
                LocalTransport.BINDING_ID);
        Channel c = t.createChannel();
 
        c.receive(context, msg);
 
        return out.toByteArray();
    }
 
    public byte[] getWSDL(String serviceName) throws Exception {
        WSDLWriter ww = service.getWSDLWriter();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ww.write(baos);
        return baos.toByteArray();
    }
 
}


RE: Converting XFire-based local transport trick to CXF

Posted by Benson Margulies <bi...@basistech.com>.
Oh, never mind. As usual, as soon as I sent this I found the example I
was missing. 

> -----Original Message-----
> From: Benson Margulies [mailto:bim2007@basistech.com] 
> Sent: Sunday, November 18, 2007 6:17 PM
> To: cxf-user@incubator.apache.org
> Subject: Converting XFire-based local transport trick to CXF
> 
> I'd be grateful for any pointers as to how to map the 
> 'invoke' method in here to CXF.
> 
> package com.basistech.xfire;
>  
> import java.io.ByteArrayOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.nio.BufferUnderflowException;
> import java.nio.ByteBuffer;
> import java.util.HashMap;
> import java.util.List;
>  
> import javax.xml.parsers.ParserConfigurationException;
> import javax.xml.stream.XMLStreamReader;
>  
> import org.codehaus.xfire.MessageContext;
> import org.codehaus.xfire.XFire;
> import org.codehaus.xfire.XFireFactory;
> import org.codehaus.xfire.aegis.AegisBindingProvider;
> import org.codehaus.xfire.aegis.type.Configuration;
> import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry;
> import org.codehaus.xfire.annotations.AnnotationServiceFactory;
> import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
> import org.codehaus.xfire.exchange.InMessage;
> import org.codehaus.xfire.service.Service;
> import org.codehaus.xfire.service.invoker.BeanInvoker;
> import org.codehaus.xfire.transport.Channel;
> import org.codehaus.xfire.transport.MapSession;
> import org.codehaus.xfire.transport.Transport;
> import org.codehaus.xfire.transport.local.LocalTransport;
> import org.codehaus.xfire.util.STAXUtils;
> import org.codehaus.xfire.wsdl.WSDLWriter;
> import org.xml.sax.SAXException;
>  
> /**
>  * This is a local-transport XFire container. It's used for 
> things like generating WSDL files or running unit tests
>  * of services.
>  */
> public class EmbeddedServiceProcessor {
>  
>     private Service service;
>     private XFire xfire;
>     private Class serviceClass;
>  
>     static class ByteBufferInputStream extends InputStream {
>  
>         private ByteBuffer buffer;
>  
>         public ByteBufferInputStream(ByteBuffer buffer) {
>             this.buffer = buffer;
>         }
>  
>         @Override
>         public int read() throws IOException {
>             try {
>                 return buffer.get();
>             } catch (BufferUnderflowException e) {
>                 return -1;
>             }
>         }
>  
>     }
>  
>     public EmbeddedServiceProcessor(Class serviceClass) {
>         this.serviceClass = serviceClass;
>     }
>  
>     public void initialize(Object implementation, List<String>
> extraClassNames)
>             throws ParserConfigurationException, 
> SAXException, IOException {
>         AegisBindingProvider binder = new AegisBindingProvider();
>  
>         xfire = XFireFactory.newInstance().getXFire();
>         AnnotationServiceFactory serviceFactory = new 
> AnnotationServiceFactory(
>                 new Jsr181WebAnnotations(), 
> xfire.getTransportManager(), binder);
>  
>         DefaultTypeMappingRegistry tmr = 
> (DefaultTypeMappingRegistry) binder
>                 .getTypeMappingRegistry();
>  
>         // here we disuade XFire from its rather annoying 
> tendency to assume
>         // that, just because
>         // anything in Java can be null, that we want to 
> advertise all that
>         // nullity all over.
>         Configuration configuration = tmr.getConfiguration();
>         configuration.setDefaultMinOccurs(1);
>         configuration.setDefaultNillable(false);
>  
>         // Create a properties hashmap
>         HashMap<String, Object> props = new HashMap<String, Object>();
>  
>         // Enable the writing of xsi:type attributes
>         props.put(AegisBindingProvider.WRITE_XSI_TYPE_KEY,
> Boolean.TRUE);
>  
>         // add class names here for extra types if we need them.
>         props.put(AegisBindingProvider.OVERRIDE_TYPES_KEY,
> extraClassNames);
>         service = serviceFactory.create(serviceClass, props);
>         // when all we want to do is extract the WSDL, don't 
> try to set up the implementation at all.
>         // the service object is all we need.
>         if(implementation != null) {
>             service.setInvoker(new BeanInvoker(implementation));
>             xfire.getServiceRegistry().register(service);
>         }
>     }
>  
>     public void terminate() {
>         xfire.getServiceRegistry().unregister(service);
>         service = null;
>         xfire = null;
>     }
>  
>     /**
>      * Invoke a service with an XML message, return an XML message. 
>      * Used for interop testing.
>      * 
>      * @param inputXML
>      * @return
>      * @throws Exception
>      */
>     public byte[] invoke(String serviceName, ByteBuffer inputXML)
>             throws Exception {
>         // This seems like a nasty piece of poor modularity.
>         Thread.currentThread().setContextClassLoader(
>                 getClass().getClassLoader());
>  
>         MapSession session = new MapSession();
>  
>         ByteArrayOutputStream out = new ByteArrayOutputStream();
>         MessageContext context = new MessageContext();
>         context.setSession(session);
>         context.setXFire(xfire);
>         context.setProperty(Channel.BACKCHANNEL_URI, out);
>  
>  
> context.setService(xfire.getServiceRegistry().getService(servi
> ceName));
>  
>         ByteBufferInputStream stream = new 
> ByteBufferInputStream(inputXML);
>         XMLStreamReader xmlStreamReader = 
> STAXUtils.createXMLStreamReader(
>                 stream, "UTF-8", null);
>  
>         InMessage msg = new InMessage(xmlStreamReader);
>  
>         Transport t = xfire.getTransportManager().getTransport(
>                 LocalTransport.BINDING_ID);
>         Channel c = t.createChannel();
>  
>         c.receive(context, msg);
>  
>         return out.toByteArray();
>     }
>  
>     public byte[] getWSDL(String serviceName) throws Exception {
>         WSDLWriter ww = service.getWSDLWriter();
>         ByteArrayOutputStream baos = new ByteArrayOutputStream();
>         ww.write(baos);
>         return baos.toByteArray();
>     }
>  
> }
> 
>