Author: rfeng
Date: Tue May 12 19:51:40 2009
New Revision: 774053

URL: http://svn.apache.org/viewvc?rev=774053&view=rev
Log:
Leverage optimization from the Axiom 1.2.7

Modified:
    
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java
    
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java
    
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java
    
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java

Modified: 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java
URL: 
http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java?rev=774053&r1=774052&r2=774053&view=diff
==============================================================================
--- 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java
 (original)
+++ 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java
 Tue May 12 19:51:40 2009
@@ -6,22 +6,24 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 package org.apache.tuscany.sca.databinding.jaxb.axiom;
 
 import java.io.OutputStream;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.io.Writer;
+import java.lang.reflect.Method;
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
@@ -33,8 +35,10 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
-import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.om.OMDataSourceExt;
 import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.ds.OMDataSourceExtBase;
+import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 import org.apache.axiom.om.util.StAXUtils;
 import org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper;
 
@@ -42,7 +46,7 @@
  *
  * @version $Rev$ $Date$
  */
-public class JAXBDataSource implements OMDataSource {
+public class JAXBDataSource extends OMDataSourceExtBase {
     private JAXBContext context;
     private Object element;
     private Marshaller marshaller;
@@ -55,11 +59,11 @@
     private Marshaller getMarshaller() throws JAXBException {
         if (marshaller == null) {
             // For thread safety, not sure we can cache the marshaller
-            marshaller = JAXBContextHelper.getMarshaller(context); 
+            marshaller = JAXBContextHelper.getMarshaller(context);
         }
         return marshaller;
     }
-    
+
     private void releaseMarshaller(Marshaller marshaller) {
         JAXBContextHelper.releaseJAXBMarshaller(context, marshaller);
     }
@@ -73,6 +77,27 @@
         return StAXUtils.createXMLStreamReader(reader);
     }
 
+    /**
+     * If the writer is backed by an OutputStream, then return the OutputStream
+     * @param writer
+     * @return OutputStream or null
+     */
+    private static OutputStream getOutputStream(XMLStreamWriter writer) throws 
XMLStreamException {
+        if (writer.getClass() == MTOMXMLStreamWriter.class) {
+            return ((MTOMXMLStreamWriter)writer).getOutputStream();
+        }
+
+        try {
+            Method method = writer.getClass().getMethod("getOutputStream");
+            return (OutputStream)method.invoke(writer);
+
+        } catch (NoSuchMethodException e) {
+            return null;
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
     public void serialize(final XMLStreamWriter xmlWriter) throws 
XMLStreamException {
         try {
             // marshaller.setProperty(Marshaller.JAXB_ENCODING, 
format.getCharSetEncoding());
@@ -80,10 +105,15 @@
                 public Object run() throws Exception {
                     try {
                         Marshaller marshaller = getMarshaller();
-                        marshaller.marshal(element, xmlWriter);                
  
+                        OutputStream os = getOutputStream(xmlWriter);
+                        if (os != null) {
+                            marshaller.marshal(element, os);
+                        } else {
+                            marshaller.marshal(element, xmlWriter);
+                        }
                     } finally {
                         releaseMarshaller(marshaller);
-                    } 
+                    }
                     return null;
                 }
             });
@@ -128,9 +158,48 @@
             throw new XMLStreamException(e.getException());
         }
     }
-    
+
     public Object getObject() {
         return element;
     }
 
+    public void close() {
+    }
+
+    public OMDataSourceExt copy() {
+        return new JAXBDataSource(element, context);
+    }
+
+    public byte[] getXMLBytes(final String encoding) throws 
UnsupportedEncodingException {
+        try {
+            return AccessController.doPrivileged(new 
PrivilegedExceptionAction<byte[]>() {
+                public byte[] run() throws JAXBException, XMLStreamException, 
UnsupportedEncodingException {
+                    try {
+                        StringWriter sw = new StringWriter();
+                        Marshaller marshaller = getMarshaller();
+                        marshaller.marshal(element, sw);
+                        return sw.toString().getBytes(encoding);
+                    } finally {
+                        releaseMarshaller(marshaller);
+                    }
+                }
+            });
+        } catch (PrivilegedActionException e) {
+            Throwable t = e.getCause();
+            if (t instanceof UnsupportedEncodingException) {
+                throw (UnsupportedEncodingException)t;
+            } else {
+                throw new RuntimeException(t);
+            }
+        }
+    }
+
+    public boolean isDestructiveRead() {
+        return false;
+    }
+
+    public boolean isDestructiveWrite() {
+        return false;
+    }
+
 }

Modified: 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java
URL: 
http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java?rev=774053&r1=774052&r2=774053&view=diff
==============================================================================
--- 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java
 (original)
+++ 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java
 Tue May 12 19:51:40 2009
@@ -19,15 +19,22 @@
 
 package org.apache.tuscany.sca.databinding.jaxb.axiom.ext;
 
+import javax.xml.bind.JAXBException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.impl.builder.CustomBuilder;
 
 /**
  * JAXBCustomBuilder creates an OMSourcedElement backed by a JAXBDataSource
  * for the specified namespace and localPart.
  */
-public class JAXBCustomBuilder 
-//FIXME: [rfeng] Re-enable it after we move to AXIOM 1.2.7
-// implements CustomBuilder 
-{
+public class JAXBCustomBuilder implements CustomBuilder {
 
     private JAXBDSContext jdsContext;
 
@@ -40,8 +47,6 @@
         this.jdsContext = context;
     }
 
- // FIXME: [rfeng] Re-enable it after we move to AXIOM 1.2.7
-    /*
     public OMElement create(String namespace,
                             String localPart,
                             OMContainer parent,
@@ -71,7 +76,6 @@
             throw new OMException(e);
         }
     }
-    */
 
     /**
      * The namespace identifier for the SOAP 1.1 envelope.
@@ -89,7 +93,7 @@
      */
     private boolean shouldUnmarshal(String namespace, String localPart) {
 
-        // Don't unmarshall SOAPFaults or anything else in the SOAP 
+        // Don't unmarshall SOAPFaults or anything else in the SOAP
         // namespace.
         // Don't unmarshall elements that are unqualified
         if (localPart == null || namespace == null

Modified: 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java
URL: 
http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java?rev=774053&r1=774052&r2=774053&view=diff
==============================================================================
--- 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java
 (original)
+++ 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java
 Tue May 12 19:51:40 2009
@@ -22,8 +22,6 @@
 import java.io.OutputStream;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
@@ -42,14 +40,11 @@
  * To marshal or unmarshal a JAXB object, the JAXBContext is necessary.
  * In addition, access to the MessageContext and other context objects may be 
necessary
  * to get classloader information, store attachments etc.
- * 
+ *
  * The JAXBDSContext bundles all of this information together.
  */
 public class JAXBDSContext {
 
-    private static final Logger log = 
Logger.getLogger(JAXBDSContext.class.getName());
-    private static final boolean DEBUG_ENABLED = log.isLoggable(Level.FINER);
-
     private JAXBContext jaxbContext = null; // JAXBContext
 
     /**
@@ -74,16 +69,15 @@
      */
     public Object unmarshal(XMLStreamReader reader) throws JAXBException {
 
-        Unmarshaller u = JAXBContextHelper.getUnmarshaller(getJAXBContext());
+        Unmarshaller u = JAXBContextHelper.getUnmarshaller(jaxbContext);
 
         Object jaxb = null;
 
         // Unmarshal into the business object.
-        jaxb = unmarshalElement(u, reader); // preferred and always used for
-        // style=document
+        jaxb = unmarshalElement(u, reader); // preferred and always used for 
style=document
 
         // Successfully unmarshalled the object
-        // JAXBUtils.releaseJAXBUnmarshaller(getJAXBContext(cl), u);
+        JAXBContextHelper.releaseJAXBUnmarshaller(jaxbContext, u);
 
         // Don't close the reader.  The reader is owned by the caller, and it
         // may contain other xml instance data (other than this JAXB object)
@@ -110,7 +104,7 @@
 
     /**
      * Preferred way to marshal objects.
-     * 
+     *
      * @param b Object that can be rendered as an element and the element name 
is known by the
      * Marshaller
      * @param m Marshaller
@@ -123,7 +117,7 @@
         AccessController.doPrivileged(new PrivilegedAction<Object>() {
             public Object run() {
                 // Marshalling directly to the output stream is faster than 
marshalling through the
-                // XMLStreamWriter. 
+                // XMLStreamWriter.
                 // Take advantage of this optimization if there is an output 
stream.
                 try {
                     OutputStream os = (optimize) ? getOutputStream(writer) : 
null;
@@ -160,7 +154,7 @@
 
     /**
      * Preferred way to unmarshal objects
-     * 
+     *
      * @param u Unmarshaller
      * @param reader XMLStreamReader
      * @return Object that represents an element

Modified: 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java
URL: 
http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java?rev=774053&r1=774052&r2=774053&view=diff
==============================================================================
--- 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java
 (original)
+++ 
tuscany/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java
 Tue May 12 19:51:40 2009
@@ -33,20 +33,17 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.axiom.om.OMDataSourceExt;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.ds.OMDataSourceExtBase;
 import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 import org.apache.axiom.om.util.StAXUtils;
 
 /**
  * OMDataSource backed by a jaxb object
  */
-public class JAXBDataSourceExt
-
-//FIXME: [rfeng] Re-enable it after we move to AXIOM 1.2.7
-// extends OMDataSourceExtBase  
-{
-
+public class JAXBDataSourceExt extends OMDataSourceExtBase {
     private static final Logger log = 
Logger.getLogger(JAXBDataSourceExt.class.getName());
 
     private Object jaxb;
@@ -61,10 +58,9 @@
     public void close() {
     }
 
-    // FIXME: [rfeng] Re-enable it after we move to AXIOM 1.2.7
-    //    public OMDataSourceExt copy() {
-    //        return new JAXBDataSourceExt(jaxb, context);
-    //    }
+    public OMDataSourceExt copy() {
+        return new JAXBDataSourceExt(jaxb, context);
+    }
 
     public Object getObject() {
         return jaxb;
@@ -92,7 +88,7 @@
         try {
             writer.close();
         } catch (XMLStreamException e) {
-            // An exception can occur if nothing is written to the 
+            // An exception can occur if nothing is written to the
             // writer.  This is possible if the underlying data source
             // writers to the output stream directly.
             if (log.isLoggable(Level.FINER)) {


Reply via email to