cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject cxf git commit: [CXF-6458] Start work on trying to "close" the Unmarshallers that implement closeable.
Date Fri, 19 Jun 2015 19:22:12 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 6d8303541 -> 174bd11dc


[CXF-6458] Start work on trying to "close" the Unmarshallers that implement closeable.


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/174bd11d
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/174bd11d
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/174bd11d

Branch: refs/heads/master
Commit: 174bd11dcfeae47998723757542abe56c792cc76
Parents: 6d83035
Author: Daniel Kulp <dkulp@apache.org>
Authored: Fri Jun 19 15:21:20 2015 -0400
Committer: Daniel Kulp <dkulp@apache.org>
Committed: Fri Jun 19 15:21:20 2015 -0400

----------------------------------------------------------------------
 .../org/apache/cxf/common/jaxb/JAXBUtils.java   | 50 ++++++++++++++++++++
 .../AbstractBPBeanDefinitionParser.java         |  7 ++-
 .../jsse/TLSClientParametersConfig.java         |  4 +-
 .../spring/AbstractBeanDefinitionParser.java    | 10 +++-
 .../configuration/spring/JAXBBeanFactory.java   |  4 +-
 .../cxf/ws/addressing/VersionTransformer.java   | 18 +++++--
 .../org/apache/cxf/jaxb/io/DataReaderImpl.java  | 31 ++++++++----
 .../org/apache/cxf/jaxrs/ext/xml/XMLSource.java | 13 +++--
 .../cxf/jaxrs/provider/JAXBElementProvider.java |  4 +-
 .../org/apache/cxf/jaxrs/utils/JAXBUtils.java   | 16 ++++++-
 .../handler/AnnotationHandlerChainBuilder.java  |  4 +-
 .../handler/logical/LogicalMessageImpl.java     |  5 +-
 .../handler/soap/SOAPMessageContextImpl.java    |  5 +-
 .../org/apache/cxf/jaxws/spi/ProviderImpl.java  | 18 +++++--
 .../cxf/jaxrs/provider/json/JSONProvider.java   |  4 +-
 .../apache/cxf/ws/addressing/soap/MAPCodec.java |  5 +-
 ...ndpointReferenceDomainExpressionBuilder.java | 38 ++++++---------
 .../builder/jaxb/JaxbAssertionBuilder.java      |  5 +-
 .../apache/cxf/wsdl/JAXBExtensionHelper.java    |  4 +-
 .../internal/WSDiscoveryServiceImpl.java        |  3 +-
 20 files changed, 182 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/core/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java b/core/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
index d4b900e..fe3d2ed 100644
--- a/core/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
@@ -21,6 +21,7 @@ package org.apache.cxf.common.jaxb;
 
 
 import java.io.BufferedReader;
+import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -48,10 +49,12 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.PropertyException;
 import javax.xml.bind.SchemaOutputResolver;
+import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.attachment.AttachmentMarshaller;
 import javax.xml.bind.attachment.AttachmentUnmarshaller;
@@ -59,6 +62,7 @@ import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.Result;
+import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.stream.StreamResult;
 
@@ -164,6 +168,52 @@ public final class JAXBUtils {
     private JAXBUtils() {
     }
     
+    public static void closeUnmarshaller(Unmarshaller u) {
+        if (u instanceof Closeable) {
+            //need to do this to clear the ThreadLocal cache
+            //see https://java.net/jira/browse/JAXB-1000
+
+            try {
+                ((Closeable)u).close();
+            } catch (IOException e) {
+                //ignore
+            }
+        }
+    }
+    public static Object unmarshall(JAXBContext c, Element e) throws JAXBException {
+        Unmarshaller u = c.createUnmarshaller();
+        try {
+            return u.unmarshal(e);
+        } finally {
+            closeUnmarshaller(u);
+        }
+    }
+    public static <T> JAXBElement<T> unmarshall(JAXBContext c, Element e, Class<T>
cls) throws JAXBException {
+        Unmarshaller u = c.createUnmarshaller();
+        try {
+            return u.unmarshal(e, cls);
+        } finally {
+            closeUnmarshaller(u);
+        }
+    }
+    public static Object unmarshall(JAXBContext c, Source s) throws JAXBException {
+        Unmarshaller u = c.createUnmarshaller();
+        try {
+            return u.unmarshal(s);
+        } finally {
+            closeUnmarshaller(u);
+        }
+    }
+    public static <T> JAXBElement<T> unmarshall(JAXBContext c, XMLStreamReader
reader,
+                                                Class<T> cls) throws JAXBException
{
+        Unmarshaller u = c.createUnmarshaller();
+        try {
+            return u.unmarshal(reader, cls);
+        } finally {
+            closeUnmarshaller(u);
+        }
+    }
+    
     public static String builtInTypeToJavaType(String type) {
         return BUILTIN_DATATYPES_MAP.get(type);
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/core/src/main/java/org/apache/cxf/configuration/blueprint/AbstractBPBeanDefinitionParser.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/configuration/blueprint/AbstractBPBeanDefinitionParser.java
b/core/src/main/java/org/apache/cxf/configuration/blueprint/AbstractBPBeanDefinitionParser.java
index c71601c..c211c8c 100644
--- a/core/src/main/java/org/apache/cxf/configuration/blueprint/AbstractBPBeanDefinitionParser.java
+++ b/core/src/main/java/org/apache/cxf/configuration/blueprint/AbstractBPBeanDefinitionParser.java
@@ -48,6 +48,7 @@ import org.apache.aries.blueprint.mutable.MutableValueMetadata;
 import org.apache.cxf.bus.blueprint.BlueprintBus;
 import org.apache.cxf.common.jaxb.JAXBContextCache;
 import org.apache.cxf.common.jaxb.JAXBContextCache.CachedContextAndSchemas;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.DOMUtils;
@@ -335,7 +336,7 @@ public abstract class AbstractBPBeanDefinitionParser {
         public Object createJAXBBean(String v) {
             XMLStreamReader reader = StaxUtils.createXMLStreamReader(new StringReader(v));
             try {
-                Object o = ctx.createUnmarshaller().unmarshal(reader, cls);
+                Object o = JAXBUtils.unmarshall(ctx, reader, cls);
                 if (o instanceof JAXBElement<?>) {
                     JAXBElement<?> el = (JAXBElement<?>)o;
                     o = el.getValue();
@@ -360,6 +361,7 @@ public abstract class AbstractBPBeanDefinitionParser {
                                             Class<?> c) {   
         try {
             XMLStreamWriter xmlWriter = null;
+            Unmarshaller u = null;
             try {
                 StringWriter writer = new StringWriter();
                 xmlWriter = StaxUtils.createXMLStreamWriter(writer);
@@ -375,7 +377,7 @@ public abstract class AbstractBPBeanDefinitionParser {
                 bean.addProperty(propertyName, factory);
 
             } catch (Exception ex) {                
-                Unmarshaller u = getContext(c).createUnmarshaller();
+                u = getContext(c).createUnmarshaller();
                 Object obj;
                 if (c != null) {
                     obj = u.unmarshal(data, c);
@@ -393,6 +395,7 @@ public abstract class AbstractBPBeanDefinitionParser {
                 }
             } finally {
                 StaxUtils.close(xmlWriter);
+                JAXBUtils.closeUnmarshaller(u);
             }
         } catch (JAXBException e) {
             throw new RuntimeException("Could not parse configuration.", e);

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/core/src/main/java/org/apache/cxf/configuration/jsse/TLSClientParametersConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/configuration/jsse/TLSClientParametersConfig.java
b/core/src/main/java/org/apache/cxf/configuration/jsse/TLSClientParametersConfig.java
index 5677b9b..7df3e05 100644
--- a/core/src/main/java/org/apache/cxf/configuration/jsse/TLSClientParametersConfig.java
+++ b/core/src/main/java/org/apache/cxf/configuration/jsse/TLSClientParametersConfig.java
@@ -36,6 +36,7 @@ import javax.xml.stream.XMLStreamReader;
 import org.apache.cxf.common.injection.NoJSR250Annotations;
 import org.apache.cxf.common.jaxb.JAXBContextCache;
 import org.apache.cxf.common.jaxb.JAXBContextCache.CachedContextAndSchemas;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.configuration.security.TLSClientParametersType;
 import org.apache.cxf.staxutils.StaxUtils;
@@ -140,7 +141,7 @@ public final class TLSClientParametersConfig {
         
         StringReader reader = new StringReader(s);
         XMLStreamReader data = StaxUtils.createXMLStreamReader(reader);
-        Unmarshaller u;
+        Unmarshaller u = null;
         try {
             u = getContext().createUnmarshaller();
             JAXBElement<TLSClientParametersType> type = u.unmarshal(data, TLSClientParametersType.class);
@@ -156,6 +157,7 @@ public final class TLSClientParametersConfig {
             } catch (XMLStreamException ex) {
                 throw new RuntimeException(ex);
             }
+            JAXBUtils.closeUnmarshaller(u);
         }
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/core/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java
b/core/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java
index 3e02b51..d43eae0 100644
--- a/core/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java
+++ b/core/src/main/java/org/apache/cxf/configuration/spring/AbstractBeanDefinitionParser.java
@@ -38,8 +38,10 @@ import org.w3c.dom.Attr;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
+
 import org.apache.cxf.common.jaxb.JAXBContextCache;
 import org.apache.cxf.common.jaxb.JAXBContextCache.CachedContextAndSchemas;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.DOMUtils;
@@ -340,6 +342,7 @@ public abstract class AbstractBeanDefinitionParser
                                             Class<?> c) {
         try {
             XMLStreamWriter xmlWriter = null;
+            Unmarshaller u = null;
             try {
                 StringWriter writer = new StringWriter();
                 xmlWriter = StaxUtils.createXMLStreamWriter(writer);
@@ -354,7 +357,7 @@ public abstract class AbstractBeanDefinitionParser
                 jaxbbean.addConstructorArgValue(c);
                 bean.addPropertyValue(propertyName, jaxbbean.getBeanDefinition());
             } catch (Exception ex) {
-                Unmarshaller u = getContext(c).createUnmarshaller();
+                u = getContext(c).createUnmarshaller();
                 Object obj;
                 if (c != null) {
                     obj = u.unmarshal(data, c);
@@ -370,6 +373,7 @@ public abstract class AbstractBeanDefinitionParser
                 }
             } finally {
                 StaxUtils.close(xmlWriter);
+                JAXBUtils.closeUnmarshaller(u);
             }
         } catch (JAXBException e) {
             throw new RuntimeException("Could not parse configuration.", e);
@@ -432,8 +436,9 @@ public abstract class AbstractBeanDefinitionParser
     protected static <T> T unmarshalFactoryString(String s, JAXBContext ctx, Class<T>
cls) {
         StringReader reader = new StringReader(s);
         XMLStreamReader data = StaxUtils.createXMLStreamReader(reader);
+        Unmarshaller u = null;
         try {
-            Unmarshaller u = ctx.createUnmarshaller();
+            u = ctx.createUnmarshaller();
             JAXBElement<?> obj = u.unmarshal(data, cls);
             return cls.cast(obj.getValue());
         } catch (RuntimeException e) {
@@ -446,6 +451,7 @@ public abstract class AbstractBeanDefinitionParser
             } catch (XMLStreamException ex) {
                 throw new RuntimeException(ex);
             }
+            JAXBUtils.closeUnmarshaller(u);
         }
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/core/src/main/java/org/apache/cxf/configuration/spring/JAXBBeanFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/configuration/spring/JAXBBeanFactory.java b/core/src/main/java/org/apache/cxf/configuration/spring/JAXBBeanFactory.java
index c542482..9e4efc9 100644
--- a/core/src/main/java/org/apache/cxf/configuration/spring/JAXBBeanFactory.java
+++ b/core/src/main/java/org/apache/cxf/configuration/spring/JAXBBeanFactory.java
@@ -28,6 +28,7 @@ import javax.xml.bind.Unmarshaller;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.staxutils.StaxUtils;
 
 /**
@@ -44,7 +45,7 @@ public final class JAXBBeanFactory {
         
         StringReader reader = new StringReader(s);
         XMLStreamReader data = StaxUtils.createXMLStreamReader(reader);
-        Unmarshaller u;
+        Unmarshaller u = null;
         try {
             Object obj;
             u = context.createUnmarshaller();
@@ -67,6 +68,7 @@ public final class JAXBBeanFactory {
             } catch (XMLStreamException ex) {
                 throw new RuntimeException(ex);
             }
+            JAXBUtils.closeUnmarshaller(u);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/core/src/main/java/org/apache/cxf/ws/addressing/VersionTransformer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/ws/addressing/VersionTransformer.java b/core/src/main/java/org/apache/cxf/ws/addressing/VersionTransformer.java
index 588c83f..dec36de 100644
--- a/core/src/main/java/org/apache/cxf/ws/addressing/VersionTransformer.java
+++ b/core/src/main/java/org/apache/cxf/ws/addressing/VersionTransformer.java
@@ -26,12 +26,16 @@ import java.util.Map;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
 import javax.xml.namespace.QName;
 //import javax.xml.ws.EndpointReference;
 //import javax.xml.ws.wsaddressing.W3CEndpointReference;
 
+
+
 import org.w3c.dom.Element;
 
+import org.apache.cxf.common.jaxb.JAXBUtils;
 // importation convention: if the same class name is used for 
 // 2005/08 and 2004/08, then the former version is imported
 // and the latter is fully qualified when used
@@ -436,12 +440,16 @@ public class VersionTransformer {
             return null;
         }
         JAXBContext ctx = getExposedJAXBContext(tns);
-        JAXBElement<?> o = ctx.createUnmarshaller().unmarshal(ref, getExposedReferenceType(tns));
-        if (o != null) {
-            return convertToNative(o.getValue());
+        Unmarshaller um = ctx.createUnmarshaller();
+        try {
+            JAXBElement<?> o = um.unmarshal(ref, getExposedReferenceType(tns));
+            if (o != null) {
+                return convertToNative(o.getValue());
+            }
+            return convertToNative(null);
+        } finally {
+            JAXBUtils.closeUnmarshaller(um);
         }
-        return convertToNative(null);
-        
     }
     /**
      * Converts a version specific EndpointReferenceType to the native version

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataReaderImpl.java
----------------------------------------------------------------------
diff --git a/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataReaderImpl.java
b/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataReaderImpl.java
index de13029..374736f 100644
--- a/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataReaderImpl.java
+++ b/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataReaderImpl.java
@@ -34,6 +34,7 @@ import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.namespace.QName;
 
 import org.apache.cxf.common.i18n.Message;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.databinding.DataReader;
 import org.apache.cxf.interceptor.Fault;
@@ -173,20 +174,32 @@ public class DataReaderImpl<T> extends JAXBDataBase implements
DataReader<T> {
             }
         }
         
-        Object obj = JAXBEncoderDecoder.unmarshall(createUnmarshaller(), reader, part, 
-                                             unwrapJAXBElement);
-        onCompleteUnmarshalling();
-        
-        return obj;
+        Unmarshaller um = createUnmarshaller();
+        try {
+            Object obj = JAXBEncoderDecoder.unmarshall(um, reader, part, 
+                                                 unwrapJAXBElement);
+            onCompleteUnmarshalling();
+            
+            return obj;
+        } finally {
+            JAXBUtils.closeUnmarshaller(um);
+        }
     }
 
     public Object read(QName name, T input, Class<?> type) {
-        Object obj = JAXBEncoderDecoder.unmarshall(createUnmarshaller(), input,
+        Unmarshaller um = createUnmarshaller();
+        
+        try {
+            Object obj = JAXBEncoderDecoder.unmarshall(um, input,
                                              name, type, 
                                              unwrapJAXBElement);
-        onCompleteUnmarshalling();
-        
-        return obj;
+            onCompleteUnmarshalling();
+            
+            return obj;
+        } finally {
+            JAXBUtils.closeUnmarshaller(um);
+        }
+
     }
 
     private void onCompleteUnmarshalling() {

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
index c1272a6..062338d 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
@@ -47,6 +47,7 @@ import org.w3c.dom.NodeList;
 
 import org.xml.sax.InputSource;
 
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
@@ -358,10 +359,14 @@ public class XMLSource {
                 c = provider.getClassContext(cls);
             }
             Unmarshaller u = c.createUnmarshaller();
-            if (cls.getAnnotation(XmlRootElement.class) != null) {
-                return cls.cast(u.unmarshal(s));
-            } else {
-                return u.unmarshal(s, cls).getValue();
+            try {
+                if (cls.getAnnotation(XmlRootElement.class) != null) {
+                    return cls.cast(u.unmarshal(s));
+                } else {
+                    return u.unmarshal(s, cls).getValue();
+                }
+            } finally {
+                JAXBUtils.closeUnmarshaller(u);
             }
         } catch (Exception ex) {
             throw new RuntimeException(ex);

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
index f17c93b..4c61df9 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
@@ -168,13 +168,14 @@ public class JAXBElementProvider<T> extends AbstractJAXBProvider<T>
 {
         }
         
         XMLStreamReader reader = null;
+        Unmarshaller unmarshaller = null;
         try {
             
             boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(type);
             Class<?> theGenericType = isCollection ? InjectionUtils.getActualType(genericType)
: type;
             Class<?> theType = getActualType(theGenericType, genericType, anns);
 
-            Unmarshaller unmarshaller = createUnmarshaller(theType, genericType, isCollection);
+            unmarshaller = createUnmarshaller(theType, genericType, isCollection);
             addAttachmentUnmarshaller(unmarshaller);
             Object response = null;
             if (JAXBElement.class.isAssignableFrom(type) 
@@ -217,6 +218,7 @@ public class JAXBElementProvider<T> extends AbstractJAXBProvider<T>
 {
             } catch (XMLStreamException e) {
                 // Ignore
             }
+            JAXBUtils.closeUnmarshaller(unmarshaller);
         }
         // unreachable
         return null;

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXBUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXBUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXBUtils.java
index 27ed25a..aba95d2 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXBUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXBUtils.java
@@ -18,9 +18,12 @@
  */
 package org.apache.cxf.jaxrs.utils;
 
+import java.io.Closeable;
+import java.io.IOException;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
+import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
@@ -28,7 +31,18 @@ public final class JAXBUtils {
     private JAXBUtils() {
         
     }
-    
+    public static void closeUnmarshaller(Unmarshaller u) {
+        if (u instanceof Closeable) {
+            //need to do this to clear the ThreadLocal cache
+            //see https://java.net/jira/browse/JAXB-1000
+
+            try {
+                ((Closeable)u).close();
+            } catch (IOException e) {
+                //ignore
+            }
+        }        
+    }
     public static Object convertWithAdapter(Object obj,
                                             Class<?> adapterClass,
                                             Annotation[] anns) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/AnnotationHandlerChainBuilder.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/AnnotationHandlerChainBuilder.java
b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/AnnotationHandlerChainBuilder.java
index 914db9e..d8d62f4 100644
--- a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/AnnotationHandlerChainBuilder.java
+++ b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/AnnotationHandlerChainBuilder.java
@@ -44,6 +44,7 @@ import org.apache.cxf.Bus;
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.i18n.Message;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.jaxws.handler.types.PortComponentHandlerType;
@@ -265,8 +266,7 @@ public class AnnotationHandlerChainBuilder extends HandlerChainBuilder
{
     private void processHandlerElement(Element el, List<Handler> chain) {
         try {
             JAXBContext ctx = getContextForPortComponentHandlerType();
-            PortComponentHandlerType pt = ctx.createUnmarshaller()
-                .unmarshal(el, PortComponentHandlerType.class).getValue();
+            PortComponentHandlerType pt = JAXBUtils.unmarshall(ctx, el, PortComponentHandlerType.class).getValue();
             chain.addAll(buildHandlerChain(pt, classLoader));
         } catch (JAXBException e) {
             // TODO Auto-generated catch block

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
index dfe6ad0..835289a 100644
--- a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
+++ b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageImpl.java
@@ -47,6 +47,7 @@ import org.w3c.dom.Node;
 import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.binding.soap.saaj.SAAJFactoryResolver;
 import org.apache.cxf.binding.soap.saaj.SAAJUtils;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.interceptor.Fault;
@@ -241,14 +242,14 @@ public class LogicalMessageImpl implements LogicalMessage {
                     parent.removeChild(ds.getNode());
                 }
                 try {
-                    return arg0.createUnmarshaller().unmarshal(ds);
+                    return JAXBUtils.unmarshall(arg0, ds);
                 } finally {
                     if (parent instanceof DocumentFragment) {
                         parent.insertBefore(ds.getNode(), next);
                     }
                 }
             } 
-            return arg0.createUnmarshaller().unmarshal(getPayload());
+            return JAXBUtils.unmarshall(arg0, getPayload());
         } catch (JAXBException e) {
             throw new WebServiceException(e);
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPMessageContextImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPMessageContextImpl.java
b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPMessageContextImpl.java
index ffcf1ce..6dc8af0 100644
--- a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPMessageContextImpl.java
+++ b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/soap/SOAPMessageContextImpl.java
@@ -40,6 +40,7 @@ import javax.xml.ws.handler.soap.SOAPMessageContext;
 
 import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxws.context.WrappedMessageContext;
 import org.apache.cxf.message.Message;
@@ -97,9 +98,7 @@ public class SOAPMessageContextImpl extends WrappedMessageContext implements
SOA
                 if ((allRoles
                     || roles.contains(she.getActor())) 
                     && name.equals(she.getElementQName())) {
-                    
-                    ret.add(context.createUnmarshaller().unmarshal(she));
-                    
+                    ret.add(JAXBUtils.unmarshall(context, she));
                 }
             }
             return ret.toArray(new Object[ret.size()]);

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/spi/ProviderImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/spi/ProviderImpl.java b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/spi/ProviderImpl.java
index 87b3830..4e6925b 100644
--- a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/spi/ProviderImpl.java
+++ b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/spi/ProviderImpl.java
@@ -51,6 +51,7 @@ import org.w3c.dom.Element;
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.common.i18n.Message;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.feature.Feature;
 import org.apache.cxf.helpers.DOMUtils;
@@ -190,7 +191,7 @@ public class ProviderImpl extends javax.xml.ws.spi.Provider {
     public static EndpointReferenceType convertToInternal(EndpointReference external) {
         if (external instanceof W3CEndpointReference) {
             
-            
+            Unmarshaller um = null;
             try {
                 Document doc = DOMUtils.newDocument();
                 DOMResult result = new DOMResult(doc);
@@ -202,13 +203,16 @@ public class ProviderImpl extends javax.xml.ws.spi.Provider {
                 //jaxContext = ContextUtils.getJAXBContext();
                 JAXBContext context = JAXBContext
                     .newInstance(new Class[] {org.apache.cxf.ws.addressing.ObjectFactory.class});
-                EndpointReferenceType internal = context.createUnmarshaller()
+                um = context.createUnmarshaller();
+                EndpointReferenceType internal = um
                     .unmarshal(reader, EndpointReferenceType.class)
                     .getValue();
                 return internal;
             } catch (JAXBException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
+            } finally {
+                JAXBUtils.closeUnmarshaller(um);
             }
             return null;
         } else {
@@ -370,7 +374,11 @@ public class ProviderImpl extends javax.xml.ws.spi.Provider {
                 return AccessController.doPrivileged(new PrivilegedExceptionAction<W3CEndpointReference>()
{
                     public W3CEndpointReference run() throws Exception {
                         Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
-                        return (W3CEndpointReference)unmarshaller.unmarshal(writer.getDocument());
+                        try {
+                            return (W3CEndpointReference)unmarshaller.unmarshal(writer.getDocument());
+                        } finally {
+                            JAXBUtils.closeUnmarshaller(unmarshaller);
+                        }
                     }
                 });
             } catch (PrivilegedActionException pae) {
@@ -399,8 +407,9 @@ public class ProviderImpl extends javax.xml.ws.spi.Provider {
             final XMLStreamReader reader = StaxUtils.createXMLStreamReader(eprInfoset);
             return AccessController.doPrivileged(new PrivilegedExceptionAction<EndpointReference>()
{
                 public EndpointReference run() throws Exception {
+                    Unmarshaller unmarshaller = null;
                     try {
-                        Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
+                        unmarshaller = getJAXBContext().createUnmarshaller();
                         return (EndpointReference)unmarshaller.unmarshal(reader);
                     } finally {
                         try {
@@ -408,6 +417,7 @@ public class ProviderImpl extends javax.xml.ws.spi.Provider {
                         } catch (XMLStreamException e) {
                             // Ignore
                         }
+                        JAXBUtils.closeUnmarshaller(unmarshaller);
                     }
                 }
             });

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
index ab90546..2a53be9 100644
--- a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
+++ b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
@@ -217,6 +217,7 @@ public class JSONProvider<T> extends AbstractJAXBProvider<T>
 {
         
         XMLStreamReader reader = null;
         String enc = HttpUtils.getEncoding(mt, "UTF-8");
+        Unmarshaller unmarshaller = null;
         try {
             InputStream realStream = getInputStream(type, genericType, is);
             if (Document.class.isAssignableFrom(type)) {
@@ -229,7 +230,7 @@ public class JSONProvider<T> extends AbstractJAXBProvider<T>
 {
             Class<?> theGenericType = isCollection ? InjectionUtils.getActualType(genericType)
: type;
             Class<?> theType = getActualType(theGenericType, genericType, anns);
             
-            Unmarshaller unmarshaller = createUnmarshaller(theType, genericType, isCollection);
+            unmarshaller = createUnmarshaller(theType, genericType, isCollection);
             XMLStreamReader xsr = createReader(type, realStream, isCollection, enc);
             
             Object response = null;
@@ -270,6 +271,7 @@ public class JSONProvider<T> extends AbstractJAXBProvider<T>
 {
             } catch (XMLStreamException e) {
                 throw ExceptionUtils.toBadRequestException(e, null);
             }
+            JAXBUtils.closeUnmarshaller(unmarshaller);
         }
         // unreachable
         return null;

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/soap/MAPCodec.java
----------------------------------------------------------------------
diff --git a/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/soap/MAPCodec.java b/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/soap/MAPCodec.java
index e0a5bf6..75e20d4 100644
--- a/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/soap/MAPCodec.java
+++ b/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/soap/MAPCodec.java
@@ -46,6 +46,7 @@ import org.apache.cxf.binding.soap.SoapMessage;
 import org.apache.cxf.binding.soap.SoapVersion;
 import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
 import org.apache.cxf.binding.soap.interceptor.SoapActionInInterceptor;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.headers.Header;
@@ -470,11 +471,11 @@ public class MAPCodec extends AbstractSoapInterceptor {
         // REVISIT generate MessageAddressingHeaderRequired fault if an
         // expected header is missing 
         AddressingProperties maps = null;
+        Unmarshaller unmarshaller = null;
         try {
             List<Header> header = message.getHeaders();
             if (header != null) {
                 LOG.log(Level.FINE, "Inbound WS-Addressing headers");
-                Unmarshaller unmarshaller = null;
                 Set<Element> referenceParameterHeaders = null;
                 QName invalidCardinalityQName = null;
                 Iterator<Header> iter = header.iterator();
@@ -588,6 +589,8 @@ public class MAPCodec extends AbstractSoapInterceptor {
             }
         } catch (JAXBException je) {
             LOG.log(Level.WARNING, "SOAP_HEADER_DECODE_FAILURE_MSG", je); 
+        } finally {
+            JAXBUtils.closeUnmarshaller(unmarshaller);
         }
         return maps;
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/attachment/external/EndpointReferenceDomainExpressionBuilder.java
----------------------------------------------------------------------
diff --git a/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/attachment/external/EndpointReferenceDomainExpressionBuilder.java
b/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/attachment/external/EndpointReferenceDomainExpressionBuilder.java
index 62b24ea..6d77493 100644
--- a/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/attachment/external/EndpointReferenceDomainExpressionBuilder.java
+++ b/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/attachment/external/EndpointReferenceDomainExpressionBuilder.java
@@ -26,13 +26,13 @@ import java.util.ResourceBundle;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
-import javax.xml.bind.Unmarshaller;
 import javax.xml.namespace.QName;
 
 import org.w3c.dom.Element;
 
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.i18n.Message;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.ws.addressing.EndpointReferenceType;
 import org.apache.cxf.ws.policy.PolicyException;
@@ -48,7 +48,7 @@ public class EndpointReferenceDomainExpressionBuilder implements DomainExpressio
     private static final Collection<QName> SUPPORTED_TYPES = Collections.singletonList(
         new QName("http://www.w3.org/2005/08/addressing", "EndpointReference"));
     
-    private Unmarshaller unmarshaller;
+    private JAXBContext context;
     
     public EndpointReferenceDomainExpressionBuilder() {
 
@@ -61,7 +61,7 @@ public class EndpointReferenceDomainExpressionBuilder implements DomainExpressio
     public DomainExpression build(Element e) {
         Object obj = null;
         try {
-            obj = getUnmarshaller().unmarshal(e);
+            obj = JAXBUtils.unmarshall(createJAXBContext(), e);
         } catch (JAXBException ex) {
             throw new PolicyException(new Message("EPR_DOMAIN_EXPRESSION_BUILD_EXC", BUNDLE,

                                                   (Object[])null), ex);
@@ -75,28 +75,18 @@ public class EndpointReferenceDomainExpressionBuilder implements DomainExpressio
         eprde.setEndpointReference((EndpointReferenceType)obj);
         return eprde;
     }
-
-    protected Unmarshaller getUnmarshaller() {
-        if (unmarshaller == null) {
-            createUnmarshaller();
-        }
-        
-        return unmarshaller;
-    }
     
-    protected synchronized void createUnmarshaller() {
-        if (unmarshaller != null) {
-            return;
-        }
-        
-        try {
-            Class<?> clz = EndpointReferenceType.class;
-            String pkg = PackageUtils.getPackageName(clz);
-            JAXBContext context = JAXBContext.newInstance(pkg, clz.getClassLoader());
-            unmarshaller = context.createUnmarshaller();
-        } catch (JAXBException ex) {
-            throw new PolicyException(new Message("EPR_DOMAIN_EXPRESSION_BUILDER_INIT_EXC",
BUNDLE, 
-                                                  (Object[])null), ex);
+    protected synchronized JAXBContext createJAXBContext() {
+        if (context == null) {
+            try {
+                Class<?> clz = EndpointReferenceType.class;
+                String pkg = PackageUtils.getPackageName(clz);
+                context = JAXBContext.newInstance(pkg, clz.getClassLoader());
+            } catch (JAXBException ex) {
+                throw new PolicyException(new Message("EPR_DOMAIN_EXPRESSION_BUILDER_INIT_EXC",
BUNDLE, 
+                                                      (Object[])null), ex);
+            }
         }
+        return context;
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
----------------------------------------------------------------------
diff --git a/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
b/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
index 22c9bd5..17966b4 100644
--- a/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
+++ b/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/builder/jaxb/JaxbAssertionBuilder.java
@@ -138,10 +138,13 @@ public class JaxbAssertionBuilder<T> implements AssertionBuilder<Element>
{
     @SuppressWarnings("unchecked")
     protected T getData(Element element) {
         Object obj = null;
+        Unmarshaller um = getUnmarshaller();
         try {
-            obj = getUnmarshaller().unmarshal(element);
+            obj = um.unmarshal(element);
         } catch (JAXBException ex) {
             LogUtils.log(LOG, Level.SEVERE, "UNMARSHAL_ELEMENT_EXC", ex);
+        } finally {
+            JAXBUtils.closeUnmarshaller(um);
         }
         if (obj instanceof JAXBElement<?>) {
             JAXBElement<?> el = (JAXBElement<?>)obj;

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/rt/wsdl/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java
----------------------------------------------------------------------
diff --git a/rt/wsdl/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java b/rt/wsdl/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java
index abe0bcc..f5bae66 100644
--- a/rt/wsdl/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java
+++ b/rt/wsdl/src/main/java/org/apache/cxf/wsdl/JAXBExtensionHelper.java
@@ -346,8 +346,9 @@ public class JAXBExtensionHelper implements ExtensionSerializer, ExtensionDeseri
                                            QName qname, Element element, Definition wsdl,
                                            ExtensionRegistry registry) throws WSDLException
{
         XMLStreamReader reader = null;
+        Unmarshaller u = null;
         try {
-            Unmarshaller u = createUnmarshaller();
+            u = createUnmarshaller();
         
             Object o = null;
             if (namespace == null) {
@@ -376,6 +377,7 @@ public class JAXBExtensionHelper implements ExtensionSerializer, ExtensionDeseri
             } catch (XMLStreamException ex) {
                 throw new WSDLException(WSDLException.PARSER_ERROR, ex.getMessage(), ex);
             }
+            JAXBUtils.closeUnmarshaller(u);
         }
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/174bd11d/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/internal/WSDiscoveryServiceImpl.java
----------------------------------------------------------------------
diff --git a/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/internal/WSDiscoveryServiceImpl.java
b/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/internal/WSDiscoveryServiceImpl.java
index 713034b..9b9d0e8 100644
--- a/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/internal/WSDiscoveryServiceImpl.java
+++ b/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/internal/WSDiscoveryServiceImpl.java
@@ -56,6 +56,7 @@ import org.w3c.dom.Document;
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.common.jaxb.JAXBContextCache;
+import org.apache.cxf.common.jaxb.JAXBUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.endpoint.Server;
@@ -498,7 +499,7 @@ public class WSDiscoveryServiceImpl implements WSDiscoveryService {
                     return null;
                 }
                 
-                Object obj = context.createUnmarshaller().unmarshal(doc.getDocumentElement());
+                Object obj = JAXBUtils.unmarshall(context, doc.getDocumentElement());
                 if (obj instanceof JAXBElement) {
                     obj = ((JAXBElement)obj).getValue();
                 }


Mime
View raw message