Author: heiko.braun(a)jboss.com
Date: 2007-01-26 11:41:43 -0500 (Fri, 26 Jan 2007)
New Revision: 2142
Added:
trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextCache.java
Modified:
trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBDeserializer.java
trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java
trunk/jbossws-core/src/main/java/org/jboss/ws/metadata/umdm/EndpointMetaData.java
Log:
Cache JAXB contexts per endpoint
Added: trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextCache.java
===================================================================
--- trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextCache.java
(rev 0)
+++
trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextCache.java 2007-01-26
16:41:43 UTC (rev 2142)
@@ -0,0 +1,118 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.ws.core.jaxws;
+
+import org.jboss.ws.core.CommonMessageContext;
+import org.jboss.ws.core.soap.MessageContextAssociation;
+import org.jboss.ws.metadata.umdm.EndpointMetaData;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Cache JAXBContext's.
+ *
+ * @author Heiko.Braun(a)jboss.org
+ * @version $Id:$
+ * @since 26.01.2007
+ */
+public class JAXBContextCache {
+
+ private Map<Integer, JAXBContext> cache = new ConcurrentHashMap<Integer,
JAXBContext>();
+
+ private JAXBContext get(Integer id) {
+ return cache.get(id);
+ }
+
+ private void add(Integer id, JAXBContext context) {
+ cache.put(id, context);
+ }
+
+ /**
+ * Retrieve a cached JAXBContext instance.
+ * If no instance is cached a new one will be created and registered.
+ * @param classes
+ * @return
+ * @throws JAXBException
+ */
+ public JAXBContext getInstance(Class[] classes) throws JAXBException
+ {
+ Integer id = buildId(classes);
+ JAXBContext ctx = get(id);
+ if(null==ctx)
+ {
+ ctx = JAXBContext.newInstance(classes);
+ add(id, ctx);
+ }
+
+ return ctx;
+ }
+
+ /**
+ * Retrieve a cached JAXBContext instance.
+ * If no instance is cached a new one will be created and registered.
+ * @param clazz
+ * @return
+ * @throws JAXBException
+ */
+ public JAXBContext getInstance(Class clazz) throws JAXBException
+ {
+ Integer id = buildId(clazz);
+ JAXBContext ctx = get(id);
+ if(null==ctx)
+ {
+ ctx = JAXBContext.newInstance(clazz);
+ add(id, ctx);
+ }
+
+ return ctx;
+ }
+
+ /**
+ * Access the JAXBContext cache through the message context.
+ * The actual instance is assiciated with the EndpointMetaData.
+ * @return JAXBContextCache
+ */
+ public static JAXBContextCache getContextCache()
+ {
+ CommonMessageContext msgContext = MessageContextAssociation.peekMessageContext();
+ EndpointMetaData epMetaData = msgContext.getEndpointMetaData();
+ return epMetaData.getJaxbCache();
+ }
+
+ private static Integer buildId(Class[] classes)
+ {
+ int sum = 0;
+ for(Class cls : classes)
+ {
+ sum += cls.hashCode();
+ }
+ return new Integer(sum);
+ }
+
+ private static Integer buildId(Class clazz)
+ {
+ return buildId(new Class[] {clazz});
+ }
+}
Modified: trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBDeserializer.java
===================================================================
---
trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBDeserializer.java 2007-01-26
16:06:02 UTC (rev 2141)
+++
trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBDeserializer.java 2007-01-26
16:41:43 UTC (rev 2142)
@@ -29,6 +29,7 @@
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.WebServiceException;
@@ -68,7 +69,10 @@
TypeMappingImpl typeMapping = serContext.getTypeMapping();
Class javaType = typeMapping.getJavaType(xmlType);
- JAXBContext jaxbContext = JAXBContext.newInstance(types);
+
+ JAXBContextCache contextCache = JAXBContextCache.getContextCache();
+ JAXBContext jaxbContext = contextCache.getInstance(types);
+
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setAttachmentUnmarshaller( new AttachmentUnmarshallerImpl());
@@ -86,6 +90,11 @@
}
+ private JAXBContext getJAXBContext(Class[] types) throws JAXBException {
+ JAXBContext jaxbContext = JAXBContext.newInstance(types);
+ return jaxbContext;
+ }
+
// 4.21 Conformance (Marshalling failure): If an error occurs when using the supplied
JAXBContext to marshall
// a request or unmarshall a response, an implementation MUST throw a
WebServiceException whose
// cause is set to the original JAXBException.
Modified: trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java
===================================================================
---
trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java 2007-01-26
16:06:02 UTC (rev 2141)
+++
trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java 2007-01-26
16:41:43 UTC (rev 2142)
@@ -88,7 +88,9 @@
if(null == javaType)
throw new Exception("Unable to resolve target java type");
- JAXBContext jaxbContext = JAXBContext.newInstance(javaType);
+ JAXBContextCache contextCache = JAXBContextCache.getContextCache();
+ JAXBContext jaxbContext = contextCache.getInstance(javaType);
+
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
Modified:
trunk/jbossws-core/src/main/java/org/jboss/ws/metadata/umdm/EndpointMetaData.java
===================================================================
---
trunk/jbossws-core/src/main/java/org/jboss/ws/metadata/umdm/EndpointMetaData.java 2007-01-26
16:06:02 UTC (rev 2141)
+++
trunk/jbossws-core/src/main/java/org/jboss/ws/metadata/umdm/EndpointMetaData.java 2007-01-26
16:41:43 UTC (rev 2142)
@@ -52,6 +52,7 @@
import org.jboss.ws.core.jaxrpc.binding.SOAPArraySerializerFactory;
import org.jboss.ws.core.jaxws.JAXBDeserializerFactory;
import org.jboss.ws.core.jaxws.JAXBSerializerFactory;
+import org.jboss.ws.core.jaxws.JAXBContextCache;
import org.jboss.ws.core.server.UnifiedVirtualFile;
import org.jboss.ws.core.utils.JavaUtils;
import org.jboss.ws.metadata.config.CommonConfig;
@@ -126,6 +127,8 @@
private ConfigObservable configObservable = new ConfigObservable();
+ private JAXBContextCache jaxbCache;
+
public EndpointMetaData(ServiceMetaData service, QName portName, QName portTypeName,
Type type)
{
this.serviceMetaData = service;
@@ -575,6 +578,12 @@
}
}
+ public JAXBContextCache getJaxbCache() {
+ if(null == jaxbCache)
+ jaxbCache = new JAXBContextCache();
+ return jaxbCache;
+ }
+
// ---------------------------------------------------------------
// Configuration provider impl