diff options
Diffstat (limited to 'branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom')
2 files changed, 35 insertions, 18 deletions
diff --git a/branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java b/branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java index dd9fbfbfd0..b6fa362ed9 100644 --- a/branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java +++ b/branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java @@ -16,7 +16,6 @@ * specific language governing permissions and limitations * under the License. */ - package org.apache.tuscany.sca.databinding.jaxb.axiom; import java.io.OutputStream; @@ -37,6 +36,7 @@ import javax.xml.stream.XMLStreamWriter; import org.apache.axiom.om.OMDataSource; import org.apache.axiom.om.OMOutputFormat; import org.apache.axiom.om.util.StAXUtils; +import org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper; /** * @@ -55,11 +55,14 @@ 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 = context.createMarshaller(); + marshaller = JAXBContextHelper.getMarshaller(context); } return marshaller; } + + private void releaseMarshaller(Marshaller marshaller) { + JAXBContextHelper.releaseJAXBMarshaller(context, marshaller); + } public XMLStreamReader getReader() throws XMLStreamException { // FIXME: [rfeng] This is a quick and dirty implementation @@ -75,11 +78,12 @@ public class JAXBDataSource implements OMDataSource { // marshaller.setProperty(Marshaller.JAXB_ENCODING, format.getCharSetEncoding()); AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { - Marshaller marshaller = getMarshaller(); - // Set the FRAGEMENT property to avoid duplicate XML declaration - marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); - marshaller.marshal(element, xmlWriter); - marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE); + try { + Marshaller marshaller = getMarshaller(); + marshaller.marshal(element, xmlWriter); + } finally { + releaseMarshaller(marshaller); + } return null; } }); @@ -93,8 +97,12 @@ public class JAXBDataSource implements OMDataSource { // marshaller.setProperty(Marshaller.JAXB_ENCODING, format.getCharSetEncoding()); AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { - Marshaller marshaller = getMarshaller(); - marshaller.marshal(element, output); + try { + Marshaller marshaller = getMarshaller(); + marshaller.marshal(element, output); + } finally { + releaseMarshaller(marshaller); + } return null; } }); @@ -107,8 +115,12 @@ public class JAXBDataSource implements OMDataSource { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { - Marshaller marshaller = getMarshaller(); - marshaller.marshal(element, writer); + try { + Marshaller marshaller = getMarshaller(); + marshaller.marshal(element, writer); + } finally { + releaseMarshaller(marshaller); + } return null; } }); diff --git a/branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/OMElement2JAXB.java b/branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/OMElement2JAXB.java index 00e99aaacd..827a1684bc 100644 --- a/branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/OMElement2JAXB.java +++ b/branches/sca-equinox/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/OMElement2JAXB.java @@ -16,7 +16,6 @@ * specific language governing permissions and limitations * under the License. */ - package org.apache.tuscany.sca.databinding.jaxb.axiom; import java.security.AccessController; @@ -51,15 +50,21 @@ public class OMElement2JAXB extends BaseTransformer<OMElement, Object> implement try { return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws JAXBException, XMLStreamException { + Unmarshaller unmarshaller = null; + XMLStreamReader reader = null; + Object result = null; // Marshalling directly to the output stream is faster than marshalling through the // XMLStreamWriter. // Take advantage of this optimization if there is an output stream. JAXBContext jaxbContext = JAXBContextHelper.createJAXBContext(context, false); - Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - XMLStreamReader reader = source.getXMLStreamReaderWithoutCaching(); - Object result = - unmarshaller.unmarshal(reader, JAXBContextHelper.getJavaType(context.getTargetDataType())); - reader.close(); + try { + unmarshaller = JAXBContextHelper.getUnmarshaller(jaxbContext); + reader = source.getXMLStreamReaderWithoutCaching(); + result = unmarshaller.unmarshal(reader, JAXBContextHelper.getJavaType(context.getTargetDataType())); + } finally { + reader.close(); + JAXBContextHelper.releaseJAXBUnmarshaller(jaxbContext, unmarshaller); + } return JAXBContextHelper.createReturnValue(jaxbContext, context.getTargetDataType(), result); } }); |