You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by da...@apache.org on 2006/10/30 08:27:40 UTC

svn commit: r469082 - in /incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf: interceptor/StaxInInterceptor.java interceptor/StaxOutInterceptor.java test/AbstractCXFTest.java

Author: dandiep
Date: Sun Oct 29 23:27:40 2006
New Revision: 469082

URL: http://svn.apache.org/viewvc?view=rev&rev=469082
Log:
CXF-174: allow overriding of stax implementations. Yes, this means I have
CXF over JSON working :-). The bad news is I can't commit the tests for
this for a couple days while I clean up the code, but I wanted this in 
M1 so I thought I'd get it committed now.  

Modified:
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/test/AbstractCXFTest.java

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java?view=diff&rev=469082&r1=469081&r2=469082
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java Sun Oct 29 23:27:40 2006
@@ -20,24 +20,28 @@
 package org.apache.cxf.interceptor;
 
 import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.ResourceBundle;
 
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
+import org.apache.cxf.staxutils.StaxUtils;
 
 /**
  * Creates an XMLStreamReader from the InputStream on the Message.
  */
 public class StaxInInterceptor extends AbstractPhaseInterceptor<Message> {
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(StaxInInterceptor.class);
-    private XMLInputFactory xif;
-    
+    private static Map<Object, XMLInputFactory> factories = new HashMap<Object, XMLInputFactory>();
+
     public StaxInInterceptor() {
         super();
         setPhase(Phase.POST_STREAM);
@@ -51,7 +55,7 @@
         String encoding = (String)message.get("Encoding");
         XMLStreamReader reader;
         try {
-            reader = getXMLInputFactory().createXMLStreamReader(is, encoding);
+            reader = getXMLInputFactory(message).createXMLStreamReader(is, encoding);
         } catch (XMLStreamException e) {
             throw new Fault(new org.apache.cxf.common.i18n.Message("STREAM_CREATE_EXC", BUNDLE), e);
         }
@@ -59,11 +63,40 @@
         message.setContent(XMLStreamReader.class, reader);
     }
 
-    protected XMLInputFactory getXMLInputFactory() {
-        if (xif == null) {
-            xif = XMLInputFactory.newInstance();
+    public static XMLInputFactory getXMLInputFactory(Message m) throws Fault {
+        Object o = m.getContextualProperty(XMLInputFactory.class.getName());
+        if (o instanceof XMLInputFactory) {
+            return (XMLInputFactory)o;
+        } else if (o != null) {
+            XMLInputFactory xif = (XMLInputFactory)factories.get(o);
+            if (xif == null) {
+                Class cls;
+                if (o instanceof Class) {
+                    cls = (Class)o;
+                } else if (o instanceof String) {
+                    try {
+                        cls = ClassLoaderUtils.loadClass((String)o, StaxInInterceptor.class);
+                    } catch (ClassNotFoundException e) {
+                        throw new Fault(e);
+                    }
+                } else {
+                    throw new Fault(
+                                    new org.apache.cxf.common.i18n.Message("INVALID_INPUT_FACTORY", 
+                                                                           BUNDLE, o));
+                }
+
+                try {
+                    xif = (XMLInputFactory)(cls.newInstance());
+                    factories.put(o, xif);
+                } catch (InstantiationException e) {
+                    throw new Fault(e);
+                } catch (IllegalAccessException e) {
+                    throw new Fault(e);
+                }
+            }
+            return xif;
+        } else {
+            return StaxUtils.getXMLInputFactory();
         }
-
-        return xif;
     }
 }

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java?view=diff&rev=469082&r1=469081&r2=469082
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java Sun Oct 29 23:27:40 2006
@@ -20,24 +20,28 @@
 package org.apache.cxf.interceptor;
 
 import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.ResourceBundle;
 
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
+import org.apache.cxf.staxutils.StaxUtils;
 
 /**
  * Creates an XMLStreamReader from the InputStream on the Message.
  */
 public class StaxOutInterceptor extends AbstractPhaseInterceptor<Message> {
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(StaxOutInterceptor.class);
-    private XMLOutputFactory xof;
-    
+    private static Map<Object, XMLOutputFactory> factories = new HashMap<Object, XMLOutputFactory>();
+
     public StaxOutInterceptor() {
         super();
         setPhase(Phase.PRE_PROTOCOL);
@@ -55,15 +59,15 @@
         String encoding = (String)message.get("Encoding");
         XMLStreamWriter writer;
         try {
-            writer = getXMLOutputFactory().createXMLStreamWriter(os, encoding);
+            writer = getXMLOutputFactory(message).createXMLStreamWriter(os, encoding);
         } catch (XMLStreamException e) {
             throw new Fault(new org.apache.cxf.common.i18n.Message("STREAM_CREATE_EXC", BUNDLE), e);
         }
 
         message.setContent(XMLStreamWriter.class, writer);
-        
+
         message.getInterceptorChain().doIntercept(message);
-        
+
         try {
             writer.close();
         } catch (XMLStreamException e) {
@@ -71,11 +75,39 @@
         }
     }
 
-    protected XMLOutputFactory getXMLOutputFactory() {
-        if (xof == null) {
-            xof = XMLOutputFactory.newInstance();
+    public static XMLOutputFactory getXMLOutputFactory(Message m) throws Fault {
+        Object o = m.getContextualProperty(XMLOutputFactory.class.getName());
+        if (o instanceof XMLOutputFactory) {
+            return (XMLOutputFactory)o;
+        } else if (o != null) {
+            XMLOutputFactory xif = (XMLOutputFactory)factories.get(o);
+            if (xif == null) {
+                Class cls;
+                if (o instanceof Class) {
+                    cls = (Class)o;
+                } else if (o instanceof String) {
+                    try {
+                        cls = ClassLoaderUtils.loadClass((String)o, StaxInInterceptor.class);
+                    } catch (ClassNotFoundException e) {
+                        throw new Fault(e);
+                    }
+                } else {
+                    throw new Fault(new org.apache.cxf.common.i18n.Message("INVALID_INPUT_FACTORY", 
+                                                                           BUNDLE, o));
+                }
+
+                try {
+                    xif = (XMLOutputFactory)(cls.newInstance());
+                    factories.put(o, xif);
+                } catch (InstantiationException e) {
+                    throw new Fault(e);
+                } catch (IllegalAccessException e) {
+                    throw new Fault(e);
+                }
+            }
+            return xif;
+        } else {
+            return StaxUtils.getXMLOutputFactory();
         }
-
-        return xof;
     }
 }

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/test/AbstractCXFTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/test/AbstractCXFTest.java?view=diff&rev=469082&r1=469081&r2=469082
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/test/AbstractCXFTest.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/test/AbstractCXFTest.java Sun Oct 29 23:27:40 2006
@@ -83,9 +83,9 @@
         return new CXFBusFactory().createBus();
     }
 
-    protected Node invoke(String address, 
-                          String transport,
-                          String message) throws Exception {
+    protected byte[] invokeBytes(String address, 
+                                 String transport,
+                                 String message) throws Exception {
         EndpointInfo ei = new EndpointInfo(null, "http://schemas.xmlsoap.org/soap/http");
         ei.setAddress(address);
 
@@ -109,12 +109,20 @@
 
         byte[] bs = obs.getResponseStream().toByteArray();
         
+        return bs;
+    }
+    
+    protected Node invoke(String address, 
+                          String transport,
+                          String message) throws Exception {
+        byte[] bs = invokeBytes(address, transport, message);
+        
         ByteArrayInputStream input = new ByteArrayInputStream(bs);
         try {
             return DOMUtils.readXml(input);
         } catch (SAXParseException e) {
             throw new IllegalStateException("Could not parse message:\n" 
-                                            + obs.getResponseStream().toString());
+                                            + new String(bs));
         }
     }