diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-12 19:51:40 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-12 19:51:40 +0000 |
commit | 75e2e59cbce9101409649a60b1f674a16d3f96d1 (patch) | |
tree | 37c18e62bc87e8d6cc4448aa706c47314545d906 /branches/sca-java-1.x/modules/databinding-jaxb-axiom | |
parent | cf52969d8a2d48928f1caaa8b0e6e2aa0074e7ed (diff) |
Leverage optimization from the Axiom 1.2.7
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@774053 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-java-1.x/modules/databinding-jaxb-axiom')
4 files changed, 105 insertions, 42 deletions
diff --git a/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java b/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java index b6fa362ed9..68098fd2c2 100644 --- a/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java +++ b/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java @@ -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.XMLStreamException; 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 @@ import org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper; * * @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 @@ public class JAXBDataSource implements OMDataSource { 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 @@ public class JAXBDataSource implements OMDataSource { 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 class JAXBDataSource implements OMDataSource { 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 @@ public class JAXBDataSource implements OMDataSource { 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; + } + } diff --git a/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java b/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java index b7a395a623..bf6a96023d 100644 --- a/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java +++ b/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBCustomBuilder.java @@ -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 @@ public class JAXBCustomBuilder 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 @@ public class JAXBCustomBuilder throw new OMException(e); } } - */ /** * The namespace identifier for the SOAP 1.1 envelope. @@ -89,7 +93,7 @@ public class JAXBCustomBuilder */ 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 diff --git a/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java b/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java index 6fd65d4752..a1520a6b62 100644 --- a/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java +++ b/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDSContext.java @@ -22,8 +22,6 @@ package org.apache.tuscany.sca.databinding.jaxb.axiom.ext; 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 @@ import org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper; * 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 class JAXBDSContext { */ 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 @@ public class JAXBDSContext { /** * 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 @@ public class JAXBDSContext { 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 @@ public class JAXBDSContext { /** * Preferred way to unmarshal objects - * + * * @param u Unmarshaller * @param reader XMLStreamReader * @return Object that represents an element diff --git a/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java b/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java index 7702d099b4..d2a3dc5acd 100644 --- a/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java +++ b/branches/sca-java-1.x/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/ext/JAXBDataSourceExt.java @@ -33,20 +33,17 @@ import javax.xml.stream.XMLStreamException; 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 class JAXBDataSourceExt 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 @@ public class JAXBDataSourceExt 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)) { |