Return-Path: Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: (qmail 61579 invoked from network); 10 Dec 2010 11:57:41 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 10 Dec 2010 11:57:41 -0000 Received: (qmail 83351 invoked by uid 500); 10 Dec 2010 11:57:41 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 83249 invoked by uid 500); 10 Dec 2010 11:57:41 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 83242 invoked by uid 99); 10 Dec 2010 11:57:40 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Dec 2010 11:57:40 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Dec 2010 11:57:36 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 63ECE23889B3; Fri, 10 Dec 2010 11:57:15 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1044314 - in /cxf/branches/2.2.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Date: Fri, 10 Dec 2010 11:57:15 -0000 To: commits@cxf.apache.org From: ningjiang@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101210115715.63ECE23889B3@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ningjiang Date: Fri Dec 10 11:57:14 2010 New Revision: 1044314 URL: http://svn.apache.org/viewvc?rev=1044314&view=rev Log: Merged revisions 1044309 via svnmerge from https://svn.apache.org/repos/asf/cxf/branches/2.3.x-fixes ................ r1044309 | ningjiang | 2010-12-10 19:46:00 +0800 (Fri, 10 Dec 2010) | 13 lines Merged revisions 1044238,1044305 via svnmerge from https://svn.apache.org/repos/asf/cxf/trunk ........ r1044238 | ningjiang | 2010-12-10 16:04:00 +0800 (Fri, 10 Dec 2010) | 1 line CXF-3180 try to fix the Jaxrs/Jaxb unmarshaller code regularly causing performance problems & thread lock-up ........ r1044305 | ningjiang | 2010-12-10 19:40:30 +0800 (Fri, 10 Dec 2010) | 1 line CXF-3180 removed the synchronized part of JAXBMarshallerUnmarshallerCache as it is used per thread ........ ................ Modified: cxf/branches/2.2.x-fixes/ (props changed) cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Propchange: cxf/branches/2.2.x-fixes/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Fri Dec 10 11:57:14 2010 @@ -1,2 +1,2 @@ -/cxf/branches/2.3.x-fixes:1041185 -/cxf/trunk:1041183 +/cxf/branches/2.3.x-fixes:1041185,1044309 +/cxf/trunk:1041183,1044238-1044305 Propchange: cxf/branches/2.2.x-fixes/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1044314&r1=1044313&r2=1044314&view=diff ============================================================================== --- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java (original) +++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java Fri Dec 10 11:57:14 2010 @@ -24,6 +24,7 @@ import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.annotation.Annotation; +import java.lang.ref.SoftReference; import java.lang.reflect.Array; import java.lang.reflect.Type; import java.util.ArrayList; @@ -91,7 +92,6 @@ public abstract class AbstractJAXBProvid private static Map packageContexts = new HashMap(); private static Map, JAXBContext> classContexts = new HashMap, JAXBContext>(); - protected Set> collectionContextClasses = new HashSet>(); protected JAXBContext collectionContext; @@ -106,6 +106,8 @@ public abstract class AbstractJAXBProvid protected List inDropElements; protected Map inElementsMap; protected Map inAppendMap; + protected Map> threadMarshallerCaches + = new HashMap>(); private boolean attributesToElements; private MessageContext mc; @@ -349,6 +351,7 @@ public abstract class AbstractJAXBProvid } } + public JAXBContext getPackageContext(Class type) { if (type == null || type == JAXBElement.class) { return null; @@ -372,6 +375,24 @@ public abstract class AbstractJAXBProvid } } + public JAXBMarshallerUnmarshallerCache getThreadMarshallerCache(Thread thread) { + JAXBMarshallerUnmarshallerCache marshallerUnMarshallerCache = null; + synchronized (threadMarshallerCaches) { + SoftReference marshallerUnMarshallerCacheRef = + threadMarshallerCaches.get(thread.getId()); + if (marshallerUnMarshallerCacheRef != null) { + marshallerUnMarshallerCache = marshallerUnMarshallerCacheRef.get(); + } + if (marshallerUnMarshallerCache == null) { + marshallerUnMarshallerCache = new JAXBMarshallerUnmarshallerCache(); + threadMarshallerCaches + .put(thread.getId(), + new SoftReference(marshallerUnMarshallerCache)); + } + return marshallerUnMarshallerCache; + } + } + protected boolean isSupported(Class type, Type genericType, Annotation[] anns) { if (jaxbElementClassMap != null && jaxbElementClassMap.containsKey(type.getName()) || isSkipJaxbChecks()) { @@ -408,7 +429,10 @@ public abstract class AbstractJAXBProvid throws JAXBException { JAXBContext context = isCollection ? getCollectionContext(cls) : getJAXBContext(cls, genericType); - Unmarshaller unmarshaller = context.createUnmarshaller(); + + JAXBMarshallerUnmarshallerCache marshallerUnmarshallerCache = + getThreadMarshallerCache(Thread.currentThread()); + Unmarshaller unmarshaller = marshallerUnmarshallerCache.getUnmarshall(context); if (schema != null) { unmarshaller.setSchema(schema); } @@ -427,9 +451,16 @@ public abstract class AbstractJAXBProvid ? ((JAXBElement)obj).getDeclaredType() : cls; JAXBContext context = getJAXBContext(objClazz, genericType); - Marshaller marshaller = context.createMarshaller(); + JAXBMarshallerUnmarshallerCache marshallerUnmarshallerCache = + getThreadMarshallerCache(Thread.currentThread()); + Marshaller marshaller = marshallerUnmarshallerCache.getMarshall(context); + // need to set this value to make JAXRSClientServerBookTest passed + marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE); if (enc != null) { marshaller.setProperty(Marshaller.JAXB_ENCODING, enc); + } else { + // set the default the value to the marshaller + marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); } return marshaller; } @@ -675,6 +706,40 @@ public abstract class AbstractJAXBProvid } + protected static class JAXBMarshallerUnmarshallerCache { + private Map marshallers = new HashMap(); + private Map unmarshallers = new HashMap(); + + public Marshaller getMarshall(JAXBContext jaxbContext) throws JAXBException { + if (jaxbContext == null) { + return null; + } + // don't need the synchronized statement, as this ojbect is used per thread + Marshaller marshaller = marshallers.get(jaxbContext); + if (marshaller == null) { + marshaller = jaxbContext.createMarshaller(); + marshallers.put(jaxbContext, marshaller); + } + return marshaller; + + } + + public Unmarshaller getUnmarshall(JAXBContext jaxbContext) throws JAXBException { + if (jaxbContext == null) { + return null; + } + // don't need the synchronized statement, as this ojbect is used per thread + Unmarshaller unmarshaller = unmarshallers.get(jaxbContext); + if (unmarshaller == null) { + unmarshaller = jaxbContext.createUnmarshaller(); + unmarshallers.put(jaxbContext, unmarshaller); + } + return unmarshaller; + + } + + } + protected static class OutTransformWriter extends DelegatingXMLStreamWriter { private QNamesMap elementsMap; private Map appendMap = new HashMap(5); Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1044314&r1=1044313&r2=1044314&view=diff ============================================================================== --- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java (original) +++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Fri Dec 10 11:57:14 2010 @@ -51,7 +51,6 @@ import javax.xml.stream.XMLOutputFactory import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; -import javax.xml.transform.stream.StreamSource; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.jaxb.NamespaceMapper; @@ -162,7 +161,7 @@ public class JAXBElementProvider extends if (JAXBElement.class.isAssignableFrom(type) || unmarshalAsJaxbElement || jaxbElementClassMap != null && jaxbElementClassMap.containsKey(theType.getName())) { - response = unmarshaller.unmarshal(new StreamSource(is), theType); + response = unmarshaller.unmarshal(StaxUtils.createXMLStreamReader(is), theType); } else { response = doUnmarshal(unmarshaller, type, is, mt); } @@ -222,7 +221,8 @@ public class JAXBElementProvider extends protected Object unmarshalFromInputStream(Unmarshaller unmarshaller, InputStream is, MediaType mt) throws JAXBException { - return unmarshaller.unmarshal(is); + // Try to create the read before unmarshalling the stream + return unmarshaller.unmarshal(StaxUtils.createXMLStreamReader(is)); } protected Object unmarshalFromReader(Unmarshaller unmarshaller, XMLStreamReader reader, MediaType mt)