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: 7.0.0.Alpha4
Show newest version
package org.jboss.resteasy.plugins.providers.jaxb;

import java.lang.annotation.Annotation;
import java.util.concurrent.ConcurrentHashMap;

import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.ContextResolver;
import jakarta.ws.rs.ext.Provider;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;

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

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
@Provider
@Produces({ "text/xml", "text/*+xml", "application/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();

    @Override
    public JAXBContext findCachedContext(Class type, MediaType mediaType, Annotation[] parameterAnnotations)
            throws JAXBException {
        JAXBContext jaxb = findProvidedJAXBContext(type, mediaType);
        if (jaxb != null) {
            return jaxb;
        }
        jaxb = type != null ? cache.get(type) : null;
        if (jaxb == null) {
            jaxb = createContext(parameterAnnotations, type);
            if (jaxb != null && type != null) {
                cache.putIfAbsent(type, jaxb);
            }
        }
        return jaxb;
    }

    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 - 2024 Weber Informatics LLC | Privacy Policy