[jbossws-commits] JBossWS SVN: r3735 - trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws.

jbossws-commits at lists.jboss.org jbossws-commits at lists.jboss.org
Tue Jun 26 18:03:45 EDT 2007


Author: heiko.braun at jboss.com
Date: 2007-06-26 18:03:45 -0400 (Tue, 26 Jun 2007)
New Revision: 3735

Added:
   trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/DefaultJAXBContextFactory.java
   trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextFactory.java
Modified:
   trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextCache.java
   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
Log:
Breakup JAXBContextCache and JAXBContextFactory

Added: trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/DefaultJAXBContextFactory.java
===================================================================
--- trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/DefaultJAXBContextFactory.java	                        (rev 0)
+++ trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/DefaultJAXBContextFactory.java	2007-06-26 22:03:45 UTC (rev 3735)
@@ -0,0 +1,47 @@
+/*
+ * 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.WSException;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+/**
+ * Doesn't do anything fancy. It just delegates to <code>JAXBContext.newInstance(clazzes)</code>
+ * 
+ * @author Heiko.Braun at jboss.com
+ *         Created: Jun 26, 2007
+ */
+public class DefaultJAXBContextFactory extends JAXBContextFactory
+{
+   public JAXBContext createContext(Class[] clazzes) throws WSException
+   {
+      try
+      {
+         return JAXBContext.newInstance(clazzes);
+      }
+      catch (JAXBException e) {
+         throw new WSException("Failed to create JAXBContext", e);
+      }
+   }
+}


Property changes on: trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/DefaultJAXBContextFactory.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: 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	2007-06-26 21:46:51 UTC (rev 3734)
+++ trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextCache.java	2007-06-26 22:03:45 UTC (rev 3735)
@@ -42,76 +42,67 @@
  */
 public class JAXBContextCache
 {
-   private Map<Integer, JAXBContext> cache = new ConcurrentHashMap<Integer, JAXBContext>();
+    private Map<Integer, JAXBContext> cache = new ConcurrentHashMap<Integer, JAXBContext>();
 
-   private JAXBContext get(Integer id)
-   {
-      return cache.get(id);
-   }
+    public JAXBContext get(Class[] clazzes)
+    {
+        Integer id = buildId(clazzes);
+        return get(id);
+    }
 
-   private void add(Integer id, JAXBContext context)
-   {
-      cache.put(id, context);
-   }
+    public void add(Class[] clazzes, JAXBContext context)
+    {
+        Integer id = buildId(clazzes);
+        add(id, context);
+    }
 
-   /**
-    * Retrieve a cached JAXBContext instance.
-    * If no instance is cached a new one will be created and registered.
-    */
-   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);
-      }
+    private JAXBContext get(Integer id)
+    {
+        return cache.get(id);
+    }
 
-      return ctx;
-   }
+    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.
-    */
-   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);
-      }
+    /**
+     * Retrieve a cached JAXBContext instance.
+     * If no instance is cached a new one will be created and registered.
+     */
+    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;
-   }
+        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 = HashCodeUtil.SEED;
+        for (Class cls : classes)
+        {
+            sum = HashCodeUtil.hash(sum, cls.getName());
+        }
+        return sum;
+    }
 
-   private static Integer buildId(Class[] classes)
-   {
-      int sum = HashCodeUtil.SEED;
-      for (Class cls : classes)
-      {
-         sum = HashCodeUtil.hash(sum, cls.getName());
-      }      
-      return new Integer(sum);
-   }
-
-   private static Integer buildId(Class clazz)
-   {
-      return buildId(new Class[] { clazz });
-   }
+    /**
+     * 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();
+    }
 }
+

Added: trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextFactory.java
===================================================================
--- trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextFactory.java	                        (rev 0)
+++ trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextFactory.java	2007-06-26 22:03:45 UTC (rev 3735)
@@ -0,0 +1,50 @@
+/*
+ * 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.WSException;
+import org.jboss.wsf.spi.utils.ServiceLoader;
+
+import javax.xml.bind.JAXBContext;
+
+/**
+ * Creates JAXBContext's.<p>
+ * The factory uses the WSF {@link ServiceLoader} to find
+ * the actual factory that's going to be used.
+ *
+ * @author Heiko.Braun at jboss.com
+ *         Created: Jun 26, 2007
+ */
+public abstract class JAXBContextFactory {
+
+   public final static String DEFAULT_JAXB_CONTEXT_FACTORY = "org.jboss.ws.core.jaxws.DefaultJAXBContextFactory";
+
+   public abstract JAXBContext createContext(Class[] clazzes) throws WSException;
+
+   public static JAXBContextFactory newInstance()
+   {
+      return (JAXBContextFactory)ServiceLoader.loadService(
+          JAXBContextFactory.class.getName(),
+          DEFAULT_JAXB_CONTEXT_FACTORY
+      );
+   }
+}


Property changes on: trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBContextFactory.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

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-06-26 21:46:51 UTC (rev 3734)
+++ trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBDeserializer.java	2007-06-26 22:03:45 UTC (rev 3735)
@@ -23,21 +23,20 @@
 
 // $Id$
 
+import org.jboss.logging.Logger;
+import org.jboss.ws.core.jaxrpc.TypeMappingImpl;
+import org.jboss.ws.core.jaxrpc.binding.BindingException;
+import org.jboss.ws.core.jaxrpc.binding.ComplexTypeDeserializer;
+import org.jboss.ws.core.jaxrpc.binding.SerializationContext;
+import org.jboss.ws.extensions.xop.jaxws.AttachmentUnmarshallerImpl;
+
 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.transform.Source;
 import javax.xml.ws.WebServiceException;
 
-import org.jboss.logging.Logger;
-import org.jboss.ws.core.jaxrpc.TypeMappingImpl;
-import org.jboss.ws.core.jaxrpc.binding.BindingException;
-import org.jboss.ws.core.jaxrpc.binding.ComplexTypeDeserializer;
-import org.jboss.ws.core.jaxrpc.binding.SerializationContext;
-import org.jboss.ws.extensions.xop.jaxws.AttachmentUnmarshallerImpl;
-
 /**
  * A Deserializer that can handle complex types by delegating to JAXB.
  *
@@ -65,12 +64,11 @@
          TypeMappingImpl typeMapping = serContext.getTypeMapping();
          Class javaType = typeMapping.getJavaType(xmlType);
 
-         JAXBContextCache contextCache = JAXBContextCache.getContextCache();
-         JAXBContext jaxbContext = contextCache.getInstance(javaTypes);
-         
+         JAXBContext jaxbContext = getJAXBContext(javaTypes);
+
          Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
          unmarshaller.setAttachmentUnmarshaller( new AttachmentUnmarshallerImpl());
-         
+
          JAXBElement jbe = unmarshaller.unmarshal(xmlFragment, javaType);
          value = jbe.getValue();
 
@@ -84,9 +82,20 @@
 
    }
 
-   private JAXBContext getJAXBContext(Class[] types) throws JAXBException {
-      JAXBContext jaxbContext = JAXBContext.newInstance(types);
-      return jaxbContext;
+   /**
+    * Retrieve JAXBContext from cache or create new one and cache it.
+    * @param types
+    * @return JAXBContext
+    */
+   private JAXBContext getJAXBContext(Class[] types){
+      JAXBContextCache cache = JAXBContextCache.getContextCache();
+      JAXBContext context = cache.get(types);
+      if(null==context)
+      {
+         context = JAXBContextFactory.newInstance().createContext(types);
+         cache.add(types, context);
+      }
+      return context;
    }
 
    // 4.21 Conformance (Marshalling failure): If an error occurs when using the supplied JAXBContext to marshall

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-06-26 21:46:51 UTC (rev 3734)
+++ trunk/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java	2007-06-26 22:03:45 UTC (rev 3735)
@@ -23,6 +23,14 @@
 
 // $Id$
 
+import org.jboss.logging.Logger;
+import org.jboss.ws.core.jaxrpc.binding.BindingException;
+import org.jboss.ws.core.jaxrpc.binding.BufferedStreamResult;
+import org.jboss.ws.core.jaxrpc.binding.ComplexTypeSerializer;
+import org.jboss.ws.core.jaxrpc.binding.SerializationContext;
+import org.jboss.ws.extensions.xop.jaxws.AttachmentMarshallerImpl;
+import org.w3c.dom.NamedNodeMap;
+
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.Marshaller;
@@ -31,15 +39,6 @@
 import javax.xml.transform.Result;
 import javax.xml.ws.WebServiceException;
 
-import org.jboss.logging.Logger;
-import org.jboss.ws.core.jaxrpc.binding.BindingException;
-import org.jboss.ws.core.jaxrpc.binding.BufferedStreamResult;
-import org.jboss.ws.core.jaxrpc.binding.ComplexTypeSerializer;
-import org.jboss.ws.core.jaxrpc.binding.SerializationContext;
-import org.jboss.ws.extensions.xop.jaxws.AttachmentMarshallerImpl;
-import org.jboss.ws.metadata.umdm.ParameterMetaData;
-import org.w3c.dom.NamedNodeMap;
-
 /**
  * A Serializer that can handle complex types by delegating to JAXB.
  *
@@ -67,11 +66,10 @@
          // This should be more efficient and accurate than searching the type mapping
          Class expectedType = serContext.getJavaType();
          Class actualType = value.getClass();
-
-         JAXBContextCache contextCache = JAXBContextCache.getContextCache();
          Class[] types = shouldFilter(actualType) ? new Class[]{expectedType} : new Class[]{expectedType, actualType};
-         JAXBContext jaxbContext = contextCache.getInstance(types);
          
+         JAXBContext jaxbContext = getJAXBContext(types);
+         
          Marshaller marshaller = jaxbContext.createMarshaller();
 
          marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
@@ -92,6 +90,22 @@
       return result;
    }
 
+   /**
+    * Retrieve JAXBContext from cache or create new one and cache it.
+    * @param types
+    * @return JAXBContext
+    */
+   private JAXBContext getJAXBContext(Class[] types){
+      JAXBContextCache cache = JAXBContextCache.getContextCache();
+      JAXBContext context = cache.get(types);
+      if(null==context)
+      {
+         context = JAXBContextFactory.newInstance().createContext(types);
+         cache.add(types, context);
+      }
+      return context;
+   }
+
    // Remove this when we add a XMLGregorianCalendar Serializer
    private boolean shouldFilter(Class<?> actualType)
    {




More information about the jbossws-commits mailing list