All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.plugins.providers.jaxb.XmlJAXBContextFinder Maven / Gradle / Ivy

There is a newer version: 4.0.0.Beta6
Show newest version
package org.jboss.resteasy.plugins.providers.jaxb;

import org.jboss.resteasy.annotations.providers.jaxb.JAXBConfig;
import org.jboss.resteasy.util.FindAnnotation;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.lang.annotation.Annotation;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
@Provider
@Produces({"text/*+xml", "application/*+xml"})
public class XmlJAXBContextFinder extends AbstractJAXBContextFinder implements ContextResolver
{
   private ConcurrentHashMap, JAXBContext> cache = new ConcurrentHashMap, JAXBContext>();
   private ConcurrentHashMap collectionCache = new ConcurrentHashMap();
   private ConcurrentHashMap xmlTypeCollectionCache = new ConcurrentHashMap();


   public JAXBContext findCachedContext(Class type, MediaType mediaType, Annotation[] parameterAnnotations) throws JAXBException
   {
      JAXBContext result;

      JAXBContext jaxb = null;
      if (type != null)
      {
         jaxb = cache.get((Class) type);
         if (jaxb != null)
         {
            return jaxb;
         }
      }
      jaxb = findProvidedJAXBContext((Class) type, mediaType);
      if (jaxb != null)
      {
         cache.putIfAbsent((Class) type, jaxb);
         return jaxb;
      }
      jaxb = createContext(parameterAnnotations, type);
      if (jaxb != null && type != null) cache.putIfAbsent((Class) type, jaxb);
      result = jaxb;
      return result;
   }

   protected JAXBContext createContextObject(Annotation[] parameterAnnotations, Class... classes) throws JAXBException
   {
      JAXBConfig config = FindAnnotation.findAnnotation(parameterAnnotations, JAXBConfig.class);
      return new JAXBContextWrapper(config, classes);
   }

   @Override
   protected JAXBContext createContextObject(Annotation[] parameterAnnotations, String contextPath) throws JAXBException
   {
      JAXBConfig config = FindAnnotation.findAnnotation(parameterAnnotations, JAXBConfig.class);
      return new JAXBContextWrapper(contextPath, config);
   }

   public JAXBContext findCacheContext(MediaType mediaType, Annotation[] paraAnnotations, Class... classes) throws JAXBException
   {
      CacheKey key = new CacheKey(classes);
      JAXBContext ctx = collectionCache.get(key);
      if (ctx != null) return ctx;

      ctx = createContext(paraAnnotations, classes);
      collectionCache.put(key, ctx);

      return ctx;
   }

   @Override
   public JAXBContext findCacheXmlTypeContext(MediaType mediaType, Annotation[] paraAnnotations, Class... classes) throws JAXBException
   {
      CacheKey key = new CacheKey(classes);
      JAXBContext ctx = xmlTypeCollectionCache.get(key);
      if (ctx != null) return ctx;

      ctx = createXmlTypeContext(paraAnnotations, classes);
      xmlTypeCollectionCache.put(key, ctx);

      return ctx;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy